Advertisement
Yonka2019

pcap net support

Sep 29th, 2022
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using PcapDotNet.Core;
  2. using PcapDotNet.Packets;
  3. using PcapDotNet.Packets.Transport;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12.  
  13. namespace ClientForm
  14. {
  15.     public partial class MainView : Form
  16.     {
  17.         private static ushort current_packet_id = 0;
  18.         private static ushort last_packet_id = 1;
  19.         private static readonly bool firstImage = true;
  20.  
  21.         private static readonly List<byte> data = new List<byte>();
  22.         private static PacketCommunicator communicator;
  23.         private Thread packetRecvThread;
  24.  
  25.  
  26.         private const string DEFAULT_INTERFACE_SUBSTRING = "Intel"; // default interface must contain this substring to be automatically chosen
  27.  
  28.         public MainView()
  29.         {
  30.             InitializeComponent();
  31.             // Retrieve the device list from the local machine
  32.             IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
  33.             int deviceIndex = -1;
  34.  
  35.             if (allDevices.Count == 0)
  36.             {
  37.                 Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
  38.                 return;
  39.             }
  40.  
  41.             // Print the list
  42.             for (int i = 0; i != allDevices.Count; ++i)
  43.             {
  44.                 LivePacketDevice device = allDevices[i];
  45.                 Console.Write(i + 1 + ". " + device.Name);
  46.                 if (device.Description != null)
  47.                     if (device.Description.Contains(DEFAULT_INTERFACE_SUBSTRING))
  48.                     {
  49.                         deviceIndex = i + 1;
  50.                         Console.WriteLine("\n\n[!] Interface selected automatically: " + allDevices[deviceIndex - 1].Description);
  51.                         Console.WriteLine("Press any button to continue..");
  52.                         Console.ReadKey();
  53.                         break;
  54.                     }
  55.                     else
  56.                         Console.WriteLine(" (" + device.Description + ")");
  57.                 else
  58.                     Console.WriteLine(" (No description available)");
  59.             }
  60.             if (deviceIndex == -1)
  61.             {
  62.                 do
  63.                 {
  64.                     Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
  65.                     string deviceIndexString = Console.ReadLine();
  66.                     if (!int.TryParse(deviceIndexString, out deviceIndex) ||
  67.                         deviceIndex < 1 || deviceIndex > allDevices.Count)
  68.                     {
  69.                         deviceIndex = 0;
  70.                     }
  71.                 } while (deviceIndex == 0);
  72.             }
  73.  
  74.             BackgroundWorker bw = new BackgroundWorker();
  75.             bw.DoWork += new DoWorkEventHandler(
  76.             delegate (object o, DoWorkEventArgs args)
  77.             {
  78.                 BackgroundWorker b = o as BackgroundWorker;
  79.                 recvP();
  80.             });
  81.  
  82.             // Take the selected adapter
  83.             PacketDevice selectedDevice = allDevices[deviceIndex - 1];
  84.  
  85.             // Open the device
  86.             using (communicator =
  87.                 selectedDevice.Open(65536,                                  // portion of the packet to capture
  88.                                                                             // 65536 guarantees that the whole packet will be captured on all the link layers
  89.                                     PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
  90.                                     1000))                                  // read timeout
  91.             {
  92.                 Console.WriteLine("Listening on " + selectedDevice.Description + "...");
  93.  
  94.                 // start the capture
  95.                 bw.RunWorkerAsync();
  96.  
  97.             }
  98.             Console.ReadKey();
  99.         }
  100.  
  101.         private void recvP()
  102.         {
  103.             communicator.ReceivePackets(0, PacketHandler);
  104.         }
  105.  
  106.         // Callback function invoked by Pcap.Net for every incoming packet
  107.         private void PacketHandler(Packet packet)
  108.         {
  109.             UdpDatagram datagram = packet.Ethernet.IpV4.Udp;
  110.             if (datagram != null && datagram.SourcePort == 6969)
  111.             {
  112.                 MemoryStream stream = datagram.Payload.ToMemoryStream();
  113.                 byte[] byteStream = stream.ToArray();
  114.  
  115.                 current_packet_id = BitConverter.ToUInt16(byteStream, 0);
  116.                 Console.WriteLine("Got chunk number: " + current_packet_id);
  117.                 if (current_packet_id < last_packet_id) // new image (first chunk of the image)
  118.                 {
  119.                     if (!firstImage)
  120.                         ShowImage(); // show image if all his chunks arrived
  121.  
  122.                     data.Clear(); // clear all data from past images
  123.                     data.AddRange(byteStream.Skip(2).Take(byteStream.Length - 2).ToList());
  124.                 }
  125.                 else // next packets (same chunk continues)
  126.                     data.AddRange(byteStream.Skip(2).Take(byteStream.Length - 2).ToList());
  127.  
  128.                 last_packet_id = current_packet_id;
  129.             }
  130.         }
  131.  
  132.         private void ShowImage()
  133.         {
  134.             using (MemoryStream ms = new MemoryStream(data.ToArray()))
  135.             {
  136.                 pictureBox1.Image = new Bitmap(Image.FromStream(ms));
  137.             }
  138.         }
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement