Advertisement
Guest User

Untitled

a guest
May 17th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Collections;
  12. using PcapDotNet;
  13. using PcapDotNet.Core;
  14. using PcapDotNet.Packets;
  15. using PcapDotNet.Packets.IpV4;
  16. using PcapDotNet.Packets.Transport;
  17. using PcapDotNet.Packets.Ethernet;
  18. using System.Threading;
  19.  
  20. namespace DataFus
  21. {
  22.     public partial class Form1 : Form
  23.     {
  24.         public Hashtable protocolTypeId = new Hashtable();
  25.         public Hashtable protocolMessageId = new Hashtable();
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.         public string FindTextBetween(string text, string left, string right)
  31.         {
  32.             // TODO: Validate input arguments
  33.  
  34.             int beginIndex = text.IndexOf(left); // find occurence of left delimiter
  35.             if (beginIndex == -1)
  36.                 return string.Empty; // or throw exception?
  37.  
  38.             beginIndex += left.Length;
  39.  
  40.             int endIndex = text.IndexOf(right, beginIndex); // find occurence of right delimiter
  41.             if (endIndex == -1)
  42.                 return string.Empty; // or throw exception?
  43.  
  44.             return text.Substring(beginIndex, endIndex - beginIndex).Trim();
  45.         }    
  46.  
  47.         private void Form1_Load(object sender, EventArgs e)
  48.         {
  49.             int fileCount = 0;
  50.             string folderPath ="types";
  51.             string[] filePathsTypes = Directory.GetFiles(folderPath, "*.as", SearchOption.AllDirectories);
  52.             foreach (var item in filePathsTypes)
  53.                 fileCount++;
  54.             folderPath = "messages";
  55.             string[] filePathsMessages = Directory.GetFiles(folderPath, "*.as", SearchOption.AllDirectories);
  56.             foreach (var item in filePathsMessages)
  57.                 fileCount++;
  58.             label2.Text = fileCount.ToString();
  59.             fileCount = 0;
  60.             foreach (var item in filePathsTypes)
  61.             {
  62.                 string readText = File.ReadAllText(item);
  63.                 if (readText.Contains("public static const protocolId:uint ="))
  64.                 {
  65.                     fileCount++;
  66.                     string result = FindTextBetween(readText, "public static const protocolId:uint = ", ";");
  67.                     protocolTypeId.Add(result, item);
  68.                 }
  69.             }
  70.             foreach (var item in filePathsMessages)
  71.             {
  72.                 string readText = File.ReadAllText(item);
  73.                 if (readText.Contains("public static const protocolId:uint ="))
  74.                 {
  75.                     fileCount++;
  76.                     string result = FindTextBetween(readText, "public static const protocolId:uint = ", ";");
  77.                     protocolMessageId.Add(result, item);
  78.                 }
  79.             }
  80.             label4.Text = fileCount.ToString();
  81.         }
  82.         public delegate void UpdateUi(string text);
  83.         public delegate void UpdateUiDone();
  84.  
  85.         public void UiDoSome(string text)
  86.         {
  87.             label1.Text = text;
  88.         }
  89.         public void UiDoSomeDone()
  90.         {
  91.             label1.Text = "done";
  92.         }
  93.         public void  ThreadProc()
  94.         {
  95.             label1.BeginInvoke((MethodInvoker)delegate()
  96.             {
  97.                 label1.Text += "ee";
  98.             });
  99.             IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
  100.             if (allDevices.Count == 0)
  101.                 MessageBox.Show("No Device Found");
  102.             for (int i = 0; i != allDevices.Count; ++i)
  103.             {
  104.                 LivePacketDevice device = allDevices[i];
  105.                 //richTextBox1.AppendText((i + 1) + ". " + device.Name);
  106.                 if (device.Description != null)
  107.                     ;//richTextBox1.AppendText(" (" + device.Description + ")");
  108.                 else
  109.                     ;//richTextBox1.AppendText(" (No description available)");
  110.             }
  111.             int deviceIndex = 0;
  112.             do
  113.             {
  114.                 string deviceIndexString = "6";
  115.                 if (!int.TryParse(deviceIndexString, out deviceIndex) ||
  116.                     deviceIndex < 1 || deviceIndex > allDevices.Count)
  117.                 {
  118.                     deviceIndex = 0;
  119.                 }
  120.             } while (deviceIndex == 0);
  121.             PacketDevice selectedDevice = allDevices[deviceIndex - 1];
  122.             using (PacketCommunicator communicator =
  123.                 selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 10))
  124.             {
  125.                 if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
  126.                 {
  127.                     //richTextBox1.AppendText("This program works only on Ethernet networks.");
  128.                     return;
  129.                 }
  130.                 using (BerkeleyPacketFilter filter = communicator.CreateFilter("tcp and port " + 5555))
  131.                 {
  132.                     communicator.SetFilter(filter);
  133.                 }
  134.                 communicator.ReceivePackets(0, PacketHandler);
  135.             }
  136.         }
  137.         private void button1_Click(object sender, EventArgs e)
  138.         {
  139.             var t = new Thread(new ThreadStart(ThreadProc));
  140.             t.Start();
  141.             return;
  142.             IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
  143.             if (allDevices.Count == 0)
  144.                 MessageBox.Show("No Device Found");
  145.             for (int i = 0; i != allDevices.Count; ++i)
  146.             {
  147.                 LivePacketDevice device = allDevices[i];
  148.                 richTextBox1.AppendText((i + 1) + ". " + device.Name);
  149.                 if (device.Description != null)
  150.                     richTextBox1.AppendText(" (" + device.Description + ")");
  151.                 else
  152.                     richTextBox1.AppendText(" (No description available)");
  153.             }
  154.             int deviceIndex = 0;
  155.             do
  156.             {
  157.                 string deviceIndexString = "6";
  158.                 if (!int.TryParse(deviceIndexString, out deviceIndex) ||
  159.                     deviceIndex < 1 || deviceIndex > allDevices.Count)
  160.                 {
  161.                     deviceIndex = 0;
  162.                 }
  163.             } while (deviceIndex == 0);
  164.             PacketDevice selectedDevice = allDevices[deviceIndex - 1];
  165.             using (PacketCommunicator communicator =
  166.                 selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 10))
  167.             {
  168.                 if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
  169.                 {
  170.                     richTextBox1.AppendText("This program works only on Ethernet networks.");
  171.                     return;
  172.                 }
  173.                 using (BerkeleyPacketFilter filter = communicator.CreateFilter("tcp and port " + 5555))
  174.                 {
  175.                     communicator.SetFilter(filter);
  176.                 }
  177.                 communicator.ReceivePackets(0, PacketHandler);
  178.             }
  179.            
  180.         }
  181.  
  182.         public static string ByteArrayToString(byte[] ba)
  183.         {
  184.             StringBuilder hex = new StringBuilder(ba.Length * 2);
  185.             foreach (byte b in ba)
  186.             {
  187.                 hex.AppendFormat("{0:x2}", b);
  188.             }
  189.             return hex.ToString();
  190.         }
  191.         public  void PacketHandler(Packet packet)
  192.         {
  193.             IpV4Datagram datagram = packet.Ethernet.IpV4;
  194.             String ipSource = datagram.Source.ToString();
  195.             MemoryStream stream = datagram.Tcp.Payload.ToMemoryStream();
  196.             byte[] data = stream.ToArray();
  197.             if (data.Length > 0)
  198.             {
  199.                 string buff = ByteArrayToString(data);
  200.                 string hexid = buff.Substring(0, 4);
  201.                 int result = Convert.ToInt32(hexid, 16) >> 2;
  202.                 if (protocolMessageId.Contains(result.ToString()))
  203.                 {
  204.                     richTextBox1.BeginInvoke((MethodInvoker)delegate()
  205.                     {
  206.                         richTextBox1.AppendText("[" + result.ToString() + "]" + protocolMessageId[result.ToString()].ToString() + "\n");
  207.                     });
  208.                 }
  209.                 else if (protocolTypeId.Contains(result.ToString()))
  210.                 {
  211.                     richTextBox1.BeginInvoke((MethodInvoker)delegate()
  212.                     {
  213.                         richTextBox1.AppendText("{" + result.ToString() + "}" + protocolTypeId[result.ToString()].ToString() + "\n");
  214.                     });
  215.                 }
  216.             }
  217.             if (data.Length == 0)
  218.                 return;
  219.             var ethernetLayer = packet.Ethernet.ExtractLayer() as EthernetLayer;
  220.             var ipv4Layer = packet.Ethernet.IpV4.ExtractLayer() as IpV4Layer;
  221.             var tcpLayer = packet.Ethernet.IpV4.Tcp.ExtractLayer() as TcpLayer;
  222.             var payloadLayer = packet.Ethernet.IpV4.Tcp.Payload.ExtractLayer() as PayloadLayer;
  223.             richTextBox1.BeginInvoke((MethodInvoker)delegate()
  224.             {
  225.                 richTextBox1.ScrollToCaret();
  226.             });
  227.         }
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement