private void DoWork(object sender, DoWorkEventArgs e) { using (PacketCommunicator communicator = _device.Open()) { //add static arp-entry for gateway StaticARP.AddEntry(_gateway.IP, _gateway.PMAC, _device.GetNetworkInterface().Name); while (!_bgWorker.CancellationPending) { foreach (var target in _targets) { communicator.SendPacket(BuildPoisonArpPacketReply(target, _gateway)); //Packet which gets sent to the victim communicator.SendPacket(BuildPoisonArpPacketReply(_gateway, target)); //Packet which gets sent to the gateway } Thread.Sleep(1000); } //antidote foreach (var target in _targets) { communicator.SendPacket(BuildAntidoteArpPacketReply(target, _gateway)); communicator.SendPacket(BuildAntidoteArpPacketReply(_gateway, target)); } //remove static arp-entry for gateway StaticARP.RemoveEntry(_gateway.IP, _gateway.PMAC, _device.GetNetworkInterface().Name); } }
/* * initiate attack by setting static mac addresses and make a starter attack */ private void BeforeAllAttack(LivePacketDevice livePacketDevice, PacketCommunicator communicator) { var networkInterface = livePacketDevice.GetNetworkInterface(); Task.Run(() => FightBackAnnoyingBroadcasts(livePacketDevice)); InsertAllStaticMacAddresses(networkInterface); SpoofAllFakeAddresses(networkInterface, communicator); }
/// <summary> /// Returns the <see cref="MacAddress"/> of the network interface of the given device. /// If no interface matches the given packet device, an exception is thrown. /// We first look for the device using <see cref="NetworkInterface.GetAllNetworkInterfaces"/> and if that fails we look for them using WMI. /// </summary> /// <param name="livePacketDevice">The packet device to look for the matching interface.</param> /// <returns>The <see cref="MacAddress"/> of the given device's matching interface.</returns> public static MacAddress GetMacAddress(this LivePacketDevice livePacketDevice) { // First, look for a NetworkInterface NetworkInterface networkInterface = livePacketDevice.GetNetworkInterface(); if (networkInterface != null) { byte[] addressBytes = networkInterface.GetPhysicalAddress().GetAddressBytes(); return(new MacAddress(addressBytes.ReadUInt48(0, Endianity.Big))); } return(livePacketDevice.GetMacAddressWmi()); }
//methods public void ScanAddresses() { _ownMacAddrByte = _device.GetNetworkInterface().GetPhysicalAddress().GetAddressBytes(); _ownMacAddr = _device.GetMacAddress(); _ownIpAddrByte = _device.getIpAddressBytes(); if (!_bgWorker.IsBusy) { _bgWorker.RunWorkerAsync(); } else { throw new Exception("Scanning is still ongoing, cannot start another scan!"); } }
/* * public static Dictionary<string,string> GetMacAddress(HashSet<string> ipAddresses) * { * Ping ping = new Ping(); * Task[] pingTasks = new Task[ipAddresses.Count]; * int i = 0; * foreach (var ip in ipAddresses) * pingTasks[i++] = ping.SendPingAsync(ip); * Task.WaitAll(pingTasks); * //TODO: test out the format of the ouput and capture its output to get mac addresses * Process pProcess = new Process * { * StartInfo = * { * FileName = "arp", * Arguments = "-a " + ipAddress, * UseShellExecute = false, * RedirectStandardOutput = true, * CreateNoWindow = true * } * }; * pProcess.Start(); * var strOutput = pProcess.StandardOutput.ReadToEnd(); * var substrings = strOutput.Split('-'); * if (substrings.Length < 8) return "not found"; * var macAddress = string.Format("{0}:{1}:{2}:{3}:{4}:{5}", substrings[3].Substring(Math.Max(0, substrings[3].Length - 2)), substrings[4], substrings[5], substrings[6], substrings[7], substrings[8].Substring(0, 2)); * return macAddress; * } */ public static string GetGatewayIp(LivePacketDevice nic) { try { var gatewayIpAddressInformation = nic.GetNetworkInterface().GetIPProperties().GatewayAddresses.FirstOrDefault(); var result = gatewayIpAddressInformation?.Address.MapToIPv4().ToString() ?? ""; ConsoleLogger.GetInstance().Debug(Tag, result); return(result); } catch (Exception e) { _fileLogger.Error(Tag, e.ToString()); return(""); } }
private void UpdateAddresses(LivePacketDevice iface, DeviceAddress address) { var interfaceAddress = ((IpV4SocketAddress)address.Address).Address; lblInterfaceAddress.Text = interfaceAddress.ToString(); if (address.Netmask?.Family == SocketAddressFamily.Internet) { tbNetmask.Text = ((IpV4SocketAddress)address.Netmask).Address.ToString(); } var gateways = iface.GetNetworkInterface().GetIPProperties().GatewayAddresses; foreach (var gateway in gateways) { if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) { tbGatewayIp.Text = gateway.Address.ToString(); ShowStatus("resolving MAC address..."); cbInterfaces.Enabled = false; Task.Run(() => { var macAddress = NetworkUtilities.GetMacByIpAddress(iface, _packetCommunicator, interfaceAddress, new IpV4Address(gateway.Address.ToString()), 3000); Invoke((MethodInvoker) delegate { if (macAddress != null) { tbGatewayMac.Text = macAddress.ToString(); } HideStatus(); cbInterfaces.Enabled = true; }); }); } } }
public PacketInject(LivePacketDevice selectedDevice) { InitializeComponent(); this.selectedDevice = selectedDevice; this.sourceMac = selectedDevice.GetMacAddress(); this.gatewayAddy = selectedDevice.GetNetworkInterface().GetIPProperties().GatewayAddresses[0].Address; this.destMac = new MacAddress(GetMacAddress(gatewayAddy).ToString()); this.count = 0; this.timer = new Timer(); if (selectedDevice == null) { Visible = false; MessageBox.Show("Must select a network interface first!"); Close(); } else { Visible = true; } radPSH.Checked = true; }
static IpV4Address GetGatewayIpV4Address(LivePacketDevice livePacketDevice) { var gatewayIpAddresses = livePacketDevice.GetNetworkInterface().GetIPProperties().GatewayAddresses; if (gatewayIpAddresses.Count() > 1) { throw new NotSupportedException($"'{livePacketDevice.Name}' has multiple gateways."); } var gatewayIpAddress = gatewayIpAddresses.SingleOrDefault(); if (gatewayIpAddress == null) { throw new ArgumentException($"'{livePacketDevice.Name}' does not have a gateway."); } if (gatewayIpAddress.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) { throw new NotSupportedException($"'{livePacketDevice.Name}' gateway does not have an IpV4 address."); } return(new IpV4Address(gatewayIpAddress.Address.ToString())); }
public static MacAddress?GetMacByIpAddress(LivePacketDevice iface, PacketCommunicator communicator, IpV4Address sourceAddress, IpV4Address targetAddress, int timeout = 2000) { var sourcePhysicalAddress = iface.GetNetworkInterface().GetPhysicalAddress(); MacAddress?result = null; var resultLock = new object(); var tokenSource = new CancellationTokenSource(); var cancellationToken = tokenSource.Token; var packet = PacketBuilder.Build( DateTime.Now, new EthernetLayer() { EtherType = EthernetType.None, Source = PhysicalAddressToMacAddress(sourcePhysicalAddress), Destination = BroadcastMac, }, new ArpLayer() { ProtocolType = EthernetType.IpV4, Operation = ArpOperation.Request, SenderProtocolAddress = sourceAddress.ToBytes(), SenderHardwareAddress = sourcePhysicalAddress.GetAddressBytes().AsReadOnly(), TargetProtocolAddress = targetAddress.ToBytes(), TargetHardwareAddress = MacAddress.Zero.ToBytes(), } ); communicator.SendPacket(packet); Task.Run(() => { while (true) { if (cancellationToken.IsCancellationRequested) { break; } communicator.ReceivePacket(out Packet p); if (p != null && p.IsValid && p.Ethernet.IsValid && p.Ethernet.EtherType == EthernetType.Arp && p.Ethernet.Arp.IsValid && p.Ethernet.Arp.Operation == ArpOperation.Reply && p.Ethernet.Arp.SenderProtocolIpV4Address == targetAddress && p.Ethernet.Arp.TargetProtocolIpV4Address == sourceAddress) { lock (resultLock) result = new MacAddress(BitConverter.ToString(p.Ethernet.Arp.SenderHardwareAddress.ToArray()).Replace('-', ':')); break; } } }); var stopwatch = new Stopwatch(); stopwatch.Start(); while (true) { lock (resultLock) { if (result != null) { return(result); } } if (stopwatch.ElapsedMilliseconds >= timeout) { tokenSource.Cancel(); communicator.Break(); break; } else { Thread.Sleep(100); } } return(result); }
public static GatewayIPAddressInformation getIpV4Gateway(this LivePacketDevice device) { return(device.GetNetworkInterface().GetIPProperties().GatewayAddresses.First((x) => x.Address.GetAddressBytes().Length == 4)); }
public static byte[] getIpAddressBytes(this LivePacketDevice device) { return(device.GetNetworkInterface().GetIPProperties().UnicastAddresses.First((x) => x.Address.GetAddressBytes().Length == 4).Address.GetAddressBytes()); }