Yonka2019

Untitled

Sep 22nd, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using PcapDotNet.Core;
  2. using PcapDotNet.Packets;
  3. using PcapDotNet.Packets.Ethernet;
  4. using PcapDotNet.Packets.IpV4;
  5. using PcapDotNet.Packets.Transport;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9.  
  10. namespace SendingASinglePacketWithSendPacket
  11. {
  12.     internal class Program
  13.     {
  14.         private static void Main(string[] args)
  15.         {
  16.             // Retrieve the device list from the local machine
  17.             IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
  18.  
  19.             if (allDevices.Count == 0)
  20.             {
  21.                 Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
  22.                 return;
  23.             }
  24.  
  25.             // Print the list
  26.             for (int i = 0; i != allDevices.Count; ++i)
  27.             {
  28.                 LivePacketDevice device = allDevices[i];
  29.                 Console.Write(i + 1 + ". " + device.Name);
  30.                 if (device.Description != null)
  31.                     Console.WriteLine(" (" + device.Description + ")");
  32.                 else
  33.                     Console.WriteLine(" (No description available)");
  34.             }
  35.  
  36.             int deviceIndex = 0;
  37.             do
  38.             {
  39.                 Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
  40.                 string deviceIndexString = Console.ReadLine();
  41.                 if (!int.TryParse(deviceIndexString, out deviceIndex) ||
  42.                     deviceIndex < 1 || deviceIndex > allDevices.Count)
  43.                 {
  44.                     deviceIndex = 0;
  45.                 }
  46.             } while (deviceIndex == 0);
  47.  
  48.             // Take the selected adapter
  49.             PacketDevice selectedDevice = allDevices[deviceIndex - 1];
  50.  
  51.             // Open the output device
  52.             using (PacketCommunicator communicator = selectedDevice.Open(100, // name of the device
  53.                                                                          PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
  54.                                                                          1000)) // read timeout
  55.             {
  56.                 communicator.SendPacket(BuildUdpPacket());
  57.             }
  58.         }
  59.         private static Packet BuildUdpPacket()
  60.         {
  61.             EthernetLayer ethernetLayer =
  62.                 new EthernetLayer
  63.                 {
  64.                     Source = new MacAddress("01:01:01:01:01:01"),
  65.                     Destination = new MacAddress("02:02:02:02:02:02"),
  66.                     EtherType = EthernetType.None, // Will be filled automatically.
  67.                 };
  68.  
  69.             IpV4Layer ipV4Layer =
  70.                 new IpV4Layer
  71.                 {
  72.                     Source = new IpV4Address("1.2.3.4"),
  73.                     CurrentDestination = new IpV4Address("11.22.33.44"),
  74.                     Fragmentation = IpV4Fragmentation.None,
  75.                     HeaderChecksum = null, // Will be filled automatically.
  76.                     Identification = 123,
  77.                     Options = IpV4Options.None,
  78.                     Protocol = null, // Will be filled automatically.
  79.                     Ttl = 100,
  80.                     TypeOfService = 0,
  81.                 };
  82.  
  83.             UdpLayer udpLayer =
  84.                 new UdpLayer
  85.                 {
  86.                     SourcePort = 4050,
  87.                     DestinationPort = 25,
  88.                     Checksum = null, // Will be filled automatically.
  89.                     CalculateChecksumValue = true,
  90.                 };
  91.  
  92.             PayloadLayer payloadLayer =
  93.                 new PayloadLayer
  94.                 {
  95.                     Data = new Datagram(Encoding.ASCII.GetBytes("mi she kore et ze homo")),
  96.                 };
  97.  
  98.             PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, payloadLayer);
  99.  
  100.             return builder.Build(DateTime.Now);
  101.         }
  102.     }
  103. }
  104.  
Add Comment
Please, Sign In to add comment