public void stop() { if (_statsOn) { // Stop the capturing process _device.StopCapture(); // Print out the device statistics Console.WriteLine(_device.Statistics.ToString()); _device.Close(); _statsOn = false; } }
//checks device/interface to see if there is traffic on it, same logic as Sniffer private static void IsDeviceActive(NpcapDevice device) { device.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(DeviceConnectivityHandler); device.Open(DeviceMode.Promiscuous, 1000); device.Filter = "ip and tcp"; device.StartCapture(); Thread.Sleep(1000); device.StopCapture(); device.Close(); }
/* * get list of all device interfaces on machine, iterate through list and check for wifi/ethernet connectivity * collect packets for 00:01:30, print to console. */ private static void Sniffer() { NpcapDeviceList devices = NpcapDeviceList.Instance; NpcapDevice activeDevice = null; foreach (NpcapDevice currentDevice in devices) { IsDeviceActive(currentDevice); if ((isDeviceActive && ((currentDevice.ToString().Contains("FriendlyName: Wi-Fi")) || (currentDevice.ToString().Contains("FriendlyName: Ethernet"))))) { activeDevice = currentDevice; break; } } foreach (PcapAddress ipaddress in activeDevice.Addresses) { if (ipaddress.Addr != null && ipaddress.Addr.ipAddress != null) { if (ipaddress.Addr.ipAddress.ToString().Contains("192.168")) { currentDeviceLocalIp = ipaddress.Addr.ipAddress; break; } } } activeDevice.OnPacketArrival += new SharpPcap.PacketArrivalEventHandler(PacketHandler); Console.WriteLine(string.Format("{0}\n", activeDevice)); Console.WriteLine("Initiating Packet Capture..."); activeDevice.Open(DeviceMode.Promiscuous, 1000); activeDevice.Filter = "ip and tcp"; activeDevice.StartCapture(); Thread.Sleep(90000); activeDevice.StopCapture(); activeDevice.Close(); Console.WriteLine("...Packet Capture complete."); Console.WriteLine("==============================================\n\n\n"); }