Example #1
0
        private void SnifferLoop()
        {
            RawPacket rawPacket = null;

            try
            {
                while (true)
                {
                    if (aborting)
                    {
                        // Signal the DecoderLoop to iterate one last time
                        packetAvailiable.Set();
                        return;
                    }

                    rawPacket = device.GetNextPacket();
                    if (rawPacket == null)
                    {
                        //    // Need to empty the buffer before updating filter because buffer gets reset when changing filter !
                        //    // This must happen as fast as possible to minimise risks of losing packets.
                        //    if (this.filterChanged)
                        //    {
                        //        lock (this.filterLock)
                        //        {
                        //            this.device.PcapSetFilter();
                        //            this.filterChanged = false;
                        //        }
                        //        this.DebugLog("filter updated");
                        //    }
                        continue;
                    }

                    lock (packetLock)
                    {
                        packetQueue.Enqueue(rawPacket);
                    }
                    packetAvailiable.Set();
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
        }
Example #2
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;
        }
Example #3
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);
        }