Пример #1
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();
        }
Пример #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);
        }