Пример #1
0
        /// <summary>
        /// Returns all pcap network devices available on this machine.
        /// </summary>
        public static PcapDeviceList GetAllDevices()
        {
            IntPtr         ptrDevs = IntPtr.Zero;               // pointer to a PCAP_IF struct
            IntPtr         next    = IntPtr.Zero;               // pointer to a PCAP_IF struct
            PCAP_IF        pcap_if;
            StringBuilder  errbuf     = new StringBuilder(256); //will hold errors
            PcapDeviceList deviceList = new PcapDeviceList();

            /* Retrieve the device list */
            int res = pcap_findalldevs(ref ptrDevs, errbuf);

            if (res == -1)
            {
                string err = "Error in WinPcap.GetAllDevices(): " + errbuf;
                throw new Exception(err);
            }
            else
            {                   // Go through device structs and add to list
                next = ptrDevs;
                while (next != IntPtr.Zero)
                {
                    pcap_if = (PCAP_IF)Marshal.PtrToStructure(next, typeof(PCAP_IF));                     //Marshal memory pointer into a struct
                    if (NetworkDevice.IsNetworkDevice(pcap_if.Name))
                    {
                        try
                        {
                            deviceList.Add(new NetworkDevice(pcap_if));
                        }
                        catch
                        {
                            deviceList.Add(new PcapDevice(pcap_if));
                        }
                    }
                    else
                    {
                        deviceList.Add(new PcapDevice(pcap_if));
                    }

                    next = pcap_if.Next;
                }
            }
            pcap_freealldevs(ptrDevs);                // free buffers
            return(deviceList);
        }
Пример #2
0
        /// <summary>
        /// Resolves the MAC address of the specified IP address
        /// </summary>
        /// <param name="destIP">The IP address to resolve</param>
        /// <param name="deviceName">The local network device name on which to send the ARP request</param>
        /// <returns>The MAC address that matches to the given IP address</returns>
        public string Resolve(String destIP, string deviceName)
        {
            string localMAC = LocalMAC;
            string localIP = LocalIP;
            NetworkDevice device = new NetworkDevice(DeviceName);

            //if no local IP and MAC addresses specified, use the ones
            //configured on the local device
            if(localIP==null)
                localIP	= device.IpAddress;
            if(LocalMAC==null)
                localMAC = device.MacAddress;

            //Build a new ARP request packet
            ARPPacket request = BuildRequest(destIP, localMAC, localIP);

            //create a "tcpdump" filter for allowing only arp replies to be read
            String arpFilter = "arp and ether dst " + IPUtil.MacFormat(localMAC);

            //open the device with 20ms timeout
            device.PcapOpen(true, 20);
            //set the filter
            device.PcapSetFilter(arpFilter);
            //inject the packet to the wire
            device.PcapSendPacket(request);

            ARPPacket reply;

            while(true)
            {
                //read the next packet from the network
                reply = (ARPPacket)device.PcapGetNextPacket();
                if(reply==null)continue;

                //if this is the reply we're looking for, stop
                if(reply.ARPSenderProtoAddress.Equals(destIP))
                {
                    break;
                }
            }
            //free the device
            device.PcapClose();
            //return the resolved MAC address
            return reply.ARPSenderHwAddress;
        }
 public bool Contains(NetworkDevice value)
 {
     // If value is not of type NetworkDevice, this will return false.
     return(List.Contains(value));
 }
 public void Remove(NetworkDevice value)
 {
     List.Remove(value);
 }
 public void Insert(int index, NetworkDevice value)
 {
     List.Insert(index, value);
 }
 public int IndexOf(NetworkDevice value)
 {
     return(List.IndexOf(value));
 }
 public int Add(NetworkDevice value)
 {
     return(List.Add(value));
 }
Пример #8
0
 public void Remove( NetworkDevice value )
 {
     List.Remove( value );
 }
Пример #9
0
 public void Insert( int index, NetworkDevice value )
 {
     List.Insert( index, value );
 }
Пример #10
0
 public int IndexOf( NetworkDevice value )
 {
     return( List.IndexOf( value ) );
 }
Пример #11
0
 public bool Contains( NetworkDevice value )
 {
     // If value is not of type NetworkDevice, this will return false.
     return( List.Contains( value ) );
 }
Пример #12
0
 public int Add( NetworkDevice value )
 {
     return( List.Add( value ) );
 }
Пример #13
0
        public static void SendTcpSyn(NetworkDevice dev)
        {
            byte[] bytes = new byte[54];

            TCPPacket tcp = new TCPPacket(lLen, bytes, true);

            //Ethernet fields
            tcp.SourceHwAddress = dev.MacAddress;			//Set the source mac of the local device
            tcp.DestinationHwAddress = destMAC;		//Set the dest MAC of the gateway
            tcp.EthernetProtocol = EthernetProtocols_Fields.IP;

            //IP fields
            tcp.DestinationAddress = destIP;			//The IP of the destination host
            tcp.SourceAddress = dev.IpAddress;			//The IP of the local device
            tcp.IPProtocol = IPProtocols_Fields.TCP;
            tcp.TimeToLive = 20;
            tcp.Id = 100;
            tcp.Version = 4;
            tcp.IPTotalLength = bytes.Length-lLen;			//Set the correct IP length
            tcp.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;

            //TCP fields
            tcp.SourcePort = sourcePort;				//The TCP source port
            tcp.DestinationPort = destPort;				//The TCP dest port
            tcp.Syn = true;						//Set the SYN flag on
            tcp.WindowSize = 555;
            tcp.AcknowledgementNumber = 1000;
            tcp.SequenceNumber = 1000;
            tcp.TCPHeaderLength = TCPFields_Fields.TCP_HEADER_LEN;			//Set the correct TCP header length

            //tcp.SetData( System.Text.Encoding.ASCII.GetBytes("HELLO") );

            //Calculate checksums
            tcp.ComputeIPChecksum();
            tcp.ComputeTCPChecksum();

            dev.PcapOpen(true, 20);

            //Set a filter to capture only replies
            dev.PcapSetFilter("ip src "+destIP+" and ip dst "+
                dev.IpAddress+" and tcp src port "+destPort+" and tcp dst port "+sourcePort);

            //Send the packet
            Console.Write("Sending packet: "+tcp+"...");
            dev.PcapSendPacket(tcp);
            Console.WriteLine("Packet sent.");

            //Holds the reply
            Packet reply;
            //Wait till you get a reply
            while((reply=dev.PcapGetNextPacket())==null);
            //print the reply
            Console.WriteLine("Reply received: "+reply);

            dev.PcapClose();
        }