Skip to content

Instantly share code, notes, and snippets.

/Program.cs Secret

Created February 17, 2015 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4695e310f03f935e377f to your computer and use it in GitHub Desktop.
Save anonymous/4695e310f03f935e377f to your computer and use it in GitHub Desktop.
Unhandled Exception:
System.InvalidOperationException: Unable to open the adapter. Adapter name: rpcap://\Device\NPF_{20F
ECDAD-4919-4679-804B-8E59E9D1FF4C}. Error: Error opening adapter: The handle is invalid. (6)
at PcapDotNet.Core.LivePacketCommunicator.PcapOpen(SByte* source, Int32 snapshotLength, PacketDev
iceOpenAttributes attributes, Int32 readTimeout, pcap_rmtauth* auth) in c:\tfs\tfs.codeplex.com\tfs0
6\pcapdotnet\pcapdotnet\src\pcapdotnet.core\livepacketcommunicator.cpp:line 50
at PcapDotNet.Core.LivePacketCommunicator..ctor(SByte* source, Int32 snapshotLength, PacketDevice
OpenAttributes attributes, Int32 readTimeout, pcap_rmtauth* auth, SocketAddress netmask) in c:\tfs\t
fs.codeplex.com\tfs06\pcapdotnet\pcapdotnet\src\pcapdotnet.core\livepacketcommunicator.cpp:line 31
at PcapDotNet.Core.LivePacketDevice.Open(Int32 snapshotLength, PacketDeviceOpenAttributes attribu
tes, Int32 readTimeout) in c:\tfs\tfs.codeplex.com\tfs06\pcapdotnet\pcapdotnet\src\pcapdotnet.core\l
ivepacketdevice.cpp:line 75
at buster.Program.Listen(Object liveDev) in Program.cs:line 76
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallba
ck callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callb
ack, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callb
ack, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
using System;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using PcapDotNet.Core;
using PcapDotNet.Core.Extensions;
namespace buster
{
class Program
{
static void Main(string[] args)
{
foreach (LivePacketDevice device in LivePacketDevice.AllLocalMachine)
{
Console.WriteLine("{1}\n{2}\n{0}\n\n", device.Description, device.GetMacAddress(), device.Name);
}
var allInt = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface networkInterface =
allInt.FirstOrDefault(
ni =>
!ni.IsReceiveOnly &&
ni.OperationalStatus == OperationalStatus.Up);
if (networkInterface == null)
{
Console.WriteLine("No interface selected");
Environment.Exit(0);
}
new Task(() =>
{
var s = new Sender(networkInterface.GetLivePacketDevice()
.Open(PacketDevice.DefaultSnapshotLength, PacketDeviceOpenAttributes.Promiscuous, 0),
"127.0.0.1", "00:11:22:33:44:55");
while (true)
{
Console.WriteLine(DateTime.Now + " sending...");
s.SendSynTcpPacket(80, "127.0.0.1", "11:22:33:44:55:66");
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}).Start();
Console.WriteLine("SPAWNING THREADS");
int numThreads = args.Length == 1 ? Convert.ToInt32(args[0]) : 32;
var threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++)
{
threads[i] = new Thread(Listen);
threads[i].Start(networkInterface.GetLivePacketDevice()); //sometimes it returns null
Thread.Sleep(TimeSpan.FromSeconds(5));
}
Console.WriteLine("JOINING");
foreach (var thread in threads)
{
thread.Join();
Console.WriteLine("{0} Finished!", thread.ManagedThreadId);
}
Console.WriteLine("Finished");
Console.ReadKey();
}
private static void Listen(object liveDev)
{
var dev = (LivePacketDevice)liveDev;
using (var comm = dev.Open(PacketDevice.DefaultSnapshotLength, PacketDeviceOpenAttributes.Promiscuous, 0))
{
comm.ReceivePackets(10, p => Console.WriteLine(p.ToString()));
}
}
}
}
using System;
using System.Collections.Generic;
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
namespace buster
{
public class Sender
{
public Sender(PacketCommunicator communicator, string source, string sourceMac)
{
SourceMac = sourceMac;
Source = source;
Communicator = communicator;
}
public string SourceMac { get; set; }
public string Source { get; set; }
public PacketCommunicator Communicator { get; set; }
public void SendSynTcpPacket(ushort dstPort, string destination, string destinationMac)
{
EthernetLayer ethernetLayer = new EthernetLayer();
ethernetLayer.Source = new MacAddress(SourceMac);
ethernetLayer.Destination = new MacAddress(destinationMac);
IpV4Layer ipLayer = new IpV4Layer();
ipLayer.CurrentDestination = new IpV4Address(destination);
ipLayer.Source = new IpV4Address(Source);
ipLayer.Ttl = 128;
ipLayer.Identification = (ushort)(new Random().Next(1, 65535));
TcpLayer tcpLayer = new TcpLayer();
tcpLayer.DestinationPort = dstPort;
tcpLayer.ControlBits = TcpControlBits.Synchronize;
tcpLayer.Window = 8192;
List<TcpOption> optionsList = new List<TcpOption>();
TcpOptionMaximumSegmentSize option = new TcpOptionMaximumSegmentSize(1460);
optionsList.Add(option);
TcpOptions options = new TcpOptions(optionsList);
tcpLayer.Options = options;
tcpLayer.SequenceNumber = (uint)(new Random().Next(65535, 16 * 65535));
tcpLayer.SourcePort = (ushort)(new Random().Next(2048, 65535));
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipLayer, tcpLayer);
Packet packet = builder.Build(DateTime.Now);
Communicator.SendPacket(packet);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment