Esempio n. 1
0
        public bool StopSniffing()
        {
            try
            {
                aborting = true;

                if (sniffingThread != null)
                {
                    sniffingThread.Join();
                }

                if (device != null && device.Opened)
                {
                    device.Close();
                }

                if (decodingThread != null)
                {
                    decodingThread.Join();
                }

                aborting = false;
            }
            catch (Exception e)
            {
                Log(e.ToString());
                return(false);
            }

            Log("Sniffing stopped");
            return(true);
        }
Esempio n. 2
0
 private void 停止ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     device.DumpFlush();
     device.DumpClose();
     device.StopCapture();
     device.Close();
     this.lblStatus.Text = "捕获已经停止";
     this.开始ToolStripMenuItem.Enabled = true;
     this.停止ToolStripMenuItem.Enabled = false;
 }
Esempio n. 3
0
        /// <summary>
        /// pcap close
        /// </summary>
        private void PcapClose()
        {
            // キャプチャ停止中にキャプチャが走るのを防ぐため
            device.OnPacketArrival -= OnPacketArrival;

            //await Task.Run(async () => {
            //    await Task.Delay(500);
            //            device.StopCapture();
            //            device.Close();
            //});

            //await Task.Delay(500);
            // キャプチャ停止
            System.Threading.Thread.Sleep(200);
            device.StopCapture();
            device.Close();
        }
Esempio n. 4
0
 private void btnStop_Click(object sender, EventArgs e)
 {
     device.StopCapture();
     device.Close();
 }
Esempio n. 5
0
        private void StartFilter()
        {
            m_Device = null;

            while (!m_bStop)
            {
                try
                {
                    LivePcapDeviceList devices = null;
                    devices = LivePcapDeviceList.Instance;

                    int i = 0;
                    /* Scan the list printing every entry */
                    foreach (LivePcapDevice dev in devices)
                    {
                        if (dev.Description.ToString() == m_strNIC)
                        {
                            m_Device = devices[i];
                            break;
                        }
                        else
                        {
                            i++;
                        }
                    }

                    if (m_Device == null)
                    {
                        m_IStatusUpdate.UpdateStatus("Failed to get handle to NIC");
                    }
                    else
                    {
                        //Open the device for capturing
                        int readTimeoutMilliseconds = 1000;
                        m_Device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);

                        //Register our handler function to the 'packet arrival' event
                        m_Device.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);

                        // udpdump filter to capture only UDP/IP packets
                        string filter = "udp";
                        m_Device.SetFilter(filter);

                        if (m_dtBound != DateTime.MaxValue)
                        {
                            m_IStatusUpdate.UpdateStatus("Next update at " + (m_dtBound + m_spanLease).ToString());
                        }
                        else
                        {
                            m_IStatusUpdate.UpdateStatus("Started DHCP Client...");
                        }
                        // Start capture packets
                        m_Device.Capture();
                        // NO stop request...
                        if (!m_bStop)
                        {
                            if (m_Device != null)
                            {
                                m_Device.Close();
                                m_Device = null;
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    m_IStatusUpdate.UpdateStatus("Exception: " + exc.Message);
                    try
                    {
                        m_Device.Close();
                    }
                    catch (Exception)
                    { }
                    m_Device = null;
                }
                Thread.Sleep(1000);
            }
        }
Esempio n. 6
0
        //发送ARP广播,返回192.168局域网中其他计算机的ARP相应数据包
        public static ArrayList ARPBroadcast(LivePcapDevice device)
        {
            ArrayList tmpArrayList = new ArrayList();
            PhysicalAddress localMAC = device.Interface.MacAddress;
            //这是我们伪造的一个IP
            IPAddress srcIP = IPAddress.Parse("192.168.3.3");
            String arpFilter = "arp and ether dst " + localMAC.ToString();

            //open the device with 20ms timeout
            device.Open(DeviceMode.Normal, 20);
            device.Filter = arpFilter;
            IPAddress destIP;
            SharpPcap.ARP tmpArp=new ARP();
            //发送65535个数据包耗时30秒,这30秒内到达的数据包由网卡缓存
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    destIP = IPAddress.Parse("192.168." + i.ToString() + "." + j.ToString());
                    //request是Packet类型
                    var request = tmpArp.BuildRequest(destIP, localMAC, srcIP);
                    //发送数据包到网络中
                    device.SendPacket(request);
                }
            }
            DateTime StartTime = DateTime.Now;
            DateTime endTime = StartTime.AddSeconds(5);
            PacketDotNet.ARPPacket arpPacket = null;
            //接收5秒钟数据包,然后闪人
            while (DateTime.Now <= endTime)
            {
                var reply = device.GetNextPacket();
                if (reply == null)
                    continue;
                var packet = PacketDotNet.Packet.ParsePacket(reply);
                arpPacket = PacketDotNet.ARPPacket.GetEncapsulated(packet);
                if (arpPacket == null)
                {
                    continue;
                }
                else
                {
                    //exists判断是否ARP回应包存在重复
                    bool exists = false;
                    foreach (Object obj in tmpArrayList)
                    {
                        ARPPacket tmp=(ARPPacket)obj;
                        if (arpPacket.SenderHardwareAddress==tmp.SenderHardwareAddress)
                        {
                            exists = true;
                            break;
                        }
                    }
                    if (exists == false)
                    {
                        tmpArrayList.Add(arpPacket);
                    }
                }
            }
            device.Close();
            return tmpArrayList;
        }
Esempio n. 7
0
        //发送ARP广播,返回192.168局域网中其他计算机的ARP相应数据包
        public static ArrayList ARPBroadcast(LivePcapDevice device)
        {
            ArrayList       tmpArrayList = new ArrayList();
            PhysicalAddress localMAC     = device.Interface.MacAddress;
            //这是我们伪造的一个IP
            IPAddress srcIP     = IPAddress.Parse("192.168.3.3");
            String    arpFilter = "arp and ether dst " + localMAC.ToString();

            //open the device with 20ms timeout
            device.Open(DeviceMode.Normal, 20);
            device.Filter = arpFilter;
            IPAddress destIP;

            SharpPcap.ARP tmpArp = new ARP();
            //发送65535个数据包耗时30秒,这30秒内到达的数据包由网卡缓存
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    destIP = IPAddress.Parse("192.168." + i.ToString() + "." + j.ToString());
                    //request是Packet类型
                    var request = tmpArp.BuildRequest(destIP, localMAC, srcIP);
                    //发送数据包到网络中
                    device.SendPacket(request);
                }
            }
            DateTime StartTime = DateTime.Now;
            DateTime endTime   = StartTime.AddSeconds(5);

            PacketDotNet.ARPPacket arpPacket = null;
            //接收5秒钟数据包,然后闪人
            while (DateTime.Now <= endTime)
            {
                var reply = device.GetNextPacket();
                if (reply == null)
                {
                    continue;
                }
                var packet = PacketDotNet.Packet.ParsePacket(reply);
                arpPacket = PacketDotNet.ARPPacket.GetEncapsulated(packet);
                if (arpPacket == null)
                {
                    continue;
                }
                else
                {
                    //exists判断是否ARP回应包存在重复
                    bool exists = false;
                    foreach (Object obj in tmpArrayList)
                    {
                        ARPPacket tmp = (ARPPacket)obj;
                        if (arpPacket.SenderHardwareAddress == tmp.SenderHardwareAddress)
                        {
                            exists = true;
                            break;
                        }
                    }
                    if (exists == false)
                    {
                        tmpArrayList.Add(arpPacket);
                    }
                }
            }
            device.Close();
            return(tmpArrayList);
        }