public void CheckPacket(Packet packet, MacAddress mac)
            {
                IP.SocketID key = new IP.SocketID();
                if (packet.Ethernet.EtherType == EthernetType.IpV4)
                {
                    // egress key
                    if (packet.Ethernet.Source == mac)
                    {
                        key.LocalEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Source.ToString()), packet.Ethernet.IpV4.Udp.SourcePort);
                        key.RemoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Destination.ToString()), packet.Ethernet.IpV4.Udp.DestinationPort);

                    }
                    // ingress key
                    else //if (packet.Ethernet.Destination == mac)
                    {
                        key.LocalEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Destination.ToString()), packet.Ethernet.IpV4.Udp.DestinationPort);
                        key.RemoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Source.ToString()), packet.Ethernet.IpV4.Udp.SourcePort);
                    }
                    key.Protocol = (IP.ProtocolFamily)packet.Ethernet.IpV4.Protocol;
                }
                else //if (packet.Ethernet.EtherType == EthernetType.IpV6)
                {
                    // egress key
                    if (packet.Ethernet.Source == mac)
                    {
                        key.LocalEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.Source.ToString()), packet.Ethernet.IpV6.Udp.SourcePort);
                        key.RemoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.CurrentDestination.ToString()), packet.Ethernet.IpV6.Udp.DestinationPort);
                    }
                    // ingress key
                    else //if (packet.Ethernet.Destination == mac)
                    {
                        key.LocalEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.CurrentDestination.ToString()), packet.Ethernet.IpV6.Udp.DestinationPort);
                        key.RemoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.Source.ToString()), packet.Ethernet.IpV6.Udp.SourcePort);
                    }
                    key.Protocol = (IP.ProtocolFamily)packet.Ethernet.IpV6.NextHeader;
                }
                // egress update
                if (packet.Ethernet.Source == mac)
                    byteTable.AddOrUpdate(key, new Bytes(0, packet.Ethernet.PayloadLength), (k, v) => v.Update(0, packet.Ethernet.PayloadLength));
                // ingress update
                else //if (packet.Ethernet.Destination == mac)
                    byteTable.AddOrUpdate(key, new Bytes(packet.Ethernet.PayloadLength, 0), (k, v) => v.Update(packet.Ethernet.PayloadLength, 0));

                if (NewBytes != null)
                    NewBytes(null, new NewBytesEventArgs(key));
                //if (Global.Config.Gadget.Debug)
                //    Global.WriteLog("New bytes: " +
                //        key.LocalEP.Address.ToString() + ":" + key.LocalEP.Port + (packet.Ethernet.Source == mac ? " -> " : " <- ") +
                //        key.RemoteEP.Address.ToString() + ":" + key.RemoteEP.Port + " " + key.Protocol);
            }
 public TapWorker(Guid guid, string name, string mac, string ip, string gateway)
 {
     Guid = guid;
     Name = name;
     ifHardwareAddressString = mac;
     ifProtocolAddressString = ip;
     ownProtocolAddressString = gateway;
     ifHardwareAddress = new MacAddress(ifHardwareAddressString);
     ifProtocolAddress = new IpV4Address(ifProtocolAddressString);
     ownProtocolAddress = new IpV4Address(ownProtocolAddressString);
     ifHardwareAddressByte = ifHardwareAddressString.Split(':').Select(s => Convert.ToByte(s, 16)).ToArray();
     ifProtocolAddressByte = ifProtocolAddressString.Split('.').Select(byte.Parse).ToArray();
     ownProtocolAddressByte = ownProtocolAddressString.Split('.').Select(byte.Parse).ToArray();
 }
 public InterfaceWorker(string guid)
 {
     Guid = guid;
     NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
     // hopefully all captured device types have a MAC address
     foreach (NetworkInterface adapter in nics)
         if (adapter.Id == Guid)
         {
             ifHardwareAddressByte = adapter.GetPhysicalAddress().GetAddressBytes();
             ifHardwareAddressString = ifHardwareAddressByte.BytesSequenceToHexadecimalString(":");
             ifHardwareAddress = new MacAddress(ifHardwareAddressString);
             Name = adapter.Name;
             break;
         }
 }
Exemple #4
0
 public void CheckPacket(Packet packet, MacAddress mac)
 {
     IPEndPoint localEP;
     IPEndPoint remoteEP;
     if (packet.Ethernet.EtherType == EthernetType.IpV4)
     {
         // egress
         if (packet.Ethernet.Source == mac)
         {
             localEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Source.ToString()), packet.Ethernet.IpV4.Udp.SourcePort);
             remoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Destination.ToString()), packet.Ethernet.IpV4.Udp.DestinationPort);
         }
         // ingress
         else //if (packet.Ethernet.Destination == mac)
         {
             localEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Destination.ToString()), packet.Ethernet.IpV4.Udp.DestinationPort);
             remoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV4.Source.ToString()), packet.Ethernet.IpV4.Udp.SourcePort);
         }
     }
     else //if (packet.Ethernet.EtherType == EthernetType.IpV6)
     {
         // egress
         if (packet.Ethernet.Source == mac)
         {
             localEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.Source.ToString()), packet.Ethernet.IpV6.Udp.SourcePort);
             remoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.CurrentDestination.ToString()), packet.Ethernet.IpV6.Udp.DestinationPort);
         }
         // ingress
         else //if (packet.Ethernet.Destination == mac)
         {
             localEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.CurrentDestination.ToString()), packet.Ethernet.IpV6.Udp.DestinationPort);
             remoteEP = new IPEndPoint(IPAddress.Parse(packet.Ethernet.IpV6.Source.ToString()), packet.Ethernet.IpV6.Udp.SourcePort);
         }
     }
     udpTable.AddOrUpdate(localEP, remoteEP, (k,v) => remoteEP);
     if (NewEndPoint != null)
         NewEndPoint(null, new NewEndPointEventArgs(localEP));
     if (Global.Config.Gadget.Debug)
         Global.WriteLog("New remote UDP end point: " +
             localEP.Address.ToString() + ":" + localEP.Port + (packet.Ethernet.Source == mac ? " -> " : " <- ") +
             remoteEP.Address.ToString() + ":" + remoteEP.Port);
     
 }
 /// <summary>
 /// Writes the given value to the buffer using the given endianity and increments the offset by the number of bytes written.
 /// </summary>
 /// <param name="buffer">The buffer to write the value to.</param>
 /// <param name="offset">The offset in the buffer to start writing.</param>
 /// <param name="value">The value to write.</param>
 /// <param name="endianity">The endianity to use when converting the value to bytes.</param>
 public static void Write(this byte[] buffer, ref int offset, MacAddress value, Endianity endianity)
 {
     buffer.Write(ref offset, value.ToValue(), endianity);
 }
Exemple #6
0
        private static void preparePcap()
        {
            var source = new MacAddress(getClientMAC());
            var dest = new MacAddress(getRouterMAC());

            device = LivePacketDevice.AllLocalMachine[0];
            communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000);

            EthernetLayer ethLayer = new EthernetLayer { Source = source, Destination = dest };
            ipLayer = new IpV4Layer { Source = new IpV4Address(LocalIPAddress()), Ttl = 128 };
            icmpLayer = new IcmpEchoLayer();
            icmpBuilder = new PacketBuilder(ethLayer, ipLayer, icmpLayer);

            TCPLayer = new TcpLayer();
            TCPBuilder = new PacketBuilder(ethLayer, ipLayer, TCPLayer);

            if (!TCPMode)
            {
                communicator.SetFilter("icmp[0] = 0");
            }
            else
            {
                //tcp[13] = 18
                communicator.SetFilter("tcp[13] = 18 and port " + TCPPort);
            }
        }
Exemple #7
0
 public static Task SendAsync(IPEndPoint target, MacAddress macAddress)
 {
     if (macAddress == null)
         throw new ArgumentNullException("macAddress");
     var p = GetWolPacket(macAddress.Address);
     return SendPacketAsync(target, p);
 }
 public static void SendWol(this IPEndPoint target, MacAddress macAddress, SecureOnPassword password)
 {
     Net.SendWol.Send(target, macAddress, password);
 }
Exemple #9
0
        private static void DiscoverNetworkBroadcast(PacketCommunicator communicator, MyDevice device)
        {
            // Supposing to be on ethernet, set mac source
            MacAddress source = new MacAddress(device.MacAddressWithDots());

            // set mac destination to broadcast
            MacAddress destination = new MacAddress("FF:FF:FF:FF:FF:FF");

            // Create the packets layers

            // Ethernet Layer
            EthernetLayer ethernetLayer = new EthernetLayer
            {
                Source = source,
                Destination = destination
            };

            // IPv4 Layer
            IpV4Layer ipV4Layer = new IpV4Layer
            {
                Source = new IpV4Address(device.IPAddress),
                Ttl = 128,
                // The rest of the important parameters will be set for each packet
            };

            // ICMP Layer
            IcmpEchoLayer icmpLayer = new IcmpEchoLayer();

            // Create the builder that will build our packets
            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer);

            string ipBeg = device.IpWithoutEnd();
            //Send 100 Pings to different destination with different parameters
            for (int i = 0; i < 256; i++)
            {
                // Set IPv4 parameters
                ipV4Layer.CurrentDestination = new IpV4Address(ipBeg + i);
                ipV4Layer.Identification = (ushort)i;

                // Set ICMP parameters
                icmpLayer.SequenceNumber = (ushort)i;
                icmpLayer.Identifier = (ushort)i;

                // Build the packet
                Packet packet = builder.Build(DateTime.Now);

                // Send down the packet
                communicator.SendPacket(packet);
                //Console.WriteLine("172.16.1." + i);
            }
        }
Exemple #10
0
 public static ReadOnlyCollection<byte> MacAddressToBytes(MacAddress macAddress)
 {
     return Array.AsReadOnly(BitConverter.GetBytes(macAddress.ToValue()).Reverse().Skip(2).ToArray());
 }
        /// <summary>
        /// Bestimmt, ob eine Zeichenfolge eine gültige MAC-Adresse ist.
        /// </summary>
        /// <param name="macString">Die zu validierende Zeichenfolge.</param>
        /// <param name="numberStyle">Der Nummernstil der zu validierenden Zeichenfolge.</param>
        /// <param name="delimieter">Das Delimiterzeichen der einzelnen Adress-Bytes.</param>
        /// <param name="macAddress">Die System.Net.MacAddress-Version der Zeichenfolge.</param>
        /// <returns>true, wenn macString eine gültige MAC-Adresse ist, andernfalls false.</returns>
        public static bool TryParse(string macString, char delimieter, System.Globalization.NumberStyles numberStyle, out MacAddress macAddress)
        {
            macAddress = null;

            if (string.IsNullOrEmpty(macString) || macString.Trim() == string.Empty)
                return false;

            macString = macString.Trim();

            //00-00-00-00-00-00
            //6*2+5 =>17
            if (macString.Length != 17)
                return false;

            string[] split;
            if (!macString.Contains(delimieter))
                return false;

            split = macString.Split(delimieter);

            if (split.Length != 6)
                return false;

            byte[] mac = new byte[6];
            for (int i = 0; i < split.Length; i++)
                if (!byte.TryParse(split[i], numberStyle, System.Globalization.CultureInfo.InvariantCulture, out mac[i]))
                    return false;
            macAddress = new MacAddress(mac);
            return true;
        }
 public static void SendWol(this IPEndPoint target, MacAddress macAddress)
 {
     Net.SendWol.Send(target, macAddress);
 }
 public static Task SendWolAsync(this IPEndPoint target, MacAddress macAddress, SecureOnPassword password)
 {
     return Net.SendWol.SendAsync(target, macAddress, password);
 }
 public static Task SendWolAsync(this IPEndPoint target, MacAddress macAddress)
 {
     return Net.SendWol.SendAsync(target, macAddress);
 }
Exemple #15
0
        private static Packet BuildIcmpPacket(MacAddress SourceMac, MacAddress DestinationMac, IpV4Address SourceIp, IpV4Address CurrentDestination, string packetType)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
                {
                    Source = SourceMac,
                    Destination = DestinationMac,
                    EtherType = EthernetType.None, // Will be filled automatically.
                };

            IpV4Layer ipV4Layer =
                new IpV4Layer
                {
                    //Source = new IpV4Address("1.2.3.4"),
                    //CurrentDestination = new IpV4Address("11.22.33.44"),
                    Source = SourceIp,
                    CurrentDestination = CurrentDestination,
                    Fragmentation = IpV4Fragmentation.None,
                    HeaderChecksum = null, // Will be filled automatically.
                    Identification = 123,
                    Options = IpV4Options.None,
                    Protocol = null, // Will be filled automatically.
                    Ttl = 100,
                    TypeOfService = 0,
                };

            IcmpEchoLayer icmpLayer = null;
            IcmpEchoReplyLayer icmpRLayer = null;

            PacketBuilder builder = null;

            if (packetType.Equals("REQUEST"))
            {
                Console.WriteLine("Request");
                icmpLayer =
                    new IcmpEchoLayer
                    {
                        Checksum = null, // Will be filled automatically.
                        Identifier = 456,
                        SequenceNumber = 800,
                    };
                builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpLayer);
            }
            else
            {
                Console.WriteLine("Reply");
                icmpRLayer =
                    new IcmpEchoReplyLayer
                    {
                        Checksum = null, // Will be filled automatically.
                        Identifier = 456,
                        SequenceNumber = 800,
                    };
                builder = new PacketBuilder(ethernetLayer, ipV4Layer, icmpRLayer);
            }

            return builder.Build(DateTime.Now);
        }
Exemple #16
0
        /// <summary>
        /// This function build an TCP over IPv4 over Ethernet with payload packet.
        /// </summary>
        private static Packet BuildTcpPacket(MacAddress SourceMac, MacAddress DestinationMac, IpV4Layer ipV4Layer, TcpLayer tcpLayer, PayloadLayer payloadLayer)
        {
            EthernetLayer ethernetLayer =
                new EthernetLayer
                {
                    Source = SourceMac,
                    Destination = DestinationMac,
                    EtherType = EthernetType.None, // Will be filled automatically.
                };

            //IpV4Layer ipV4Layer =
            //    new IpV4Layer
            //    {
            //        Source = SourceIp,
            //        CurrentDestination = DestinatinIp,
            //        Fragmentation = IpV4Fragmentation.None,
            //        HeaderChecksum = null, // Will be filled automatically.
            //        Identification = 123,
            //        Options = IpV4Options.None,
            //        Protocol = null, // Will be filled automatically.
            //        Ttl = 100,
            //        TypeOfService = 0,
            //    };

            //TcpLayer tcpLayer =
            //    new TcpLayer
            //    {
            //        SourcePort = 4050,
            //        DestinationPort = 25,
            //        Checksum = null, // Will be filled automatically.
            //        SequenceNumber = 100,
            //        AcknowledgmentNumber = 50,
            //        ControlBits = TcpControlBits.Acknowledgment,
            //        Window = 100,
            //        UrgentPointer = 0,
            //        Options = TcpOptions.None,
            //    };

            //PayloadLayer payloadLayer =
            //    new PayloadLayer
            //    {
            //        Data = new Datagram(Encoding.ASCII.GetBytes("hello world")),
            //    };

            PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, tcpLayer, payloadLayer);

            return builder.Build(DateTime.Now);
        }
Exemple #17
0
        public static void Send(IPEndPoint target, MacAddress macAddress)
        {
            if (macAddress == null)
                throw new ArgumentNullException("macAddress");

            byte[] packet = GetWolPacket(macAddress.Address);
            SendPacket(target, packet);
        }
Exemple #18
0
        public static Task SendAsync(IPEndPoint target, MacAddress macAddress, SecureOnPassword password)
        {
            if (macAddress == null)
                throw new ArgumentNullException("macAddress");

            if (password == null)
                throw new ArgumentNullException("password");

            byte[] packet = GetWolPacket(macAddress.Address, password.Password);
            return SendPacketAsync(target, packet);
        }
 public PhysicalWorker(Guid guid, string name, string ifMac, string ifIp, string ifMask, string gatewayMac, string gatewayIp)
 {
     Guid = guid;
     Name = name;
     ifHardwareAddressString = ifMac;
     ifProtocolAddressString = ifIp;
     ifProtocolMaskString = ifMask;
     gatewayHardwareAddressString = gatewayMac;
     gatewayProtocolAddressString = gatewayIp;
     ifHardwareAddress = new MacAddress(ifHardwareAddressString);
     ifProtocolAddress = new IpV4Address(ifProtocolAddressString);
     ifProtocolMask = new IpV4Address(ifProtocolMaskString);
     gatewayHardwareAddress = new MacAddress(gatewayHardwareAddressString);
     gatewayProtocolAddress = new IpV4Address(gatewayProtocolAddressString);
     ifHardwareAddressByte = ifHardwareAddressString.Split(':').Select(s => Convert.ToByte(s, 16)).ToArray();
     ifProtocolAddressByte = ifProtocolAddressString.Split('.').Select(byte.Parse).ToArray();
     ifProtocolMaskByte = ifProtocolMaskString.Split('.').Select(byte.Parse).ToArray();
     gatewayHardwareAddressByte = gatewayHardwareAddressString.Split(':').Select(s => Convert.ToByte(s, 16)).ToArray();
     gatewayProtocolAddressByte = gatewayProtocolAddressString.Split('.').Select(byte.Parse).ToArray();
 }