From 6df8862dbc12746f7d46c7169d511e34e1704ee0 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 3 May 2023 22:15:14 +0200 Subject: [PATCH] Add linux support and code improvments --- ARP-Scanner/ARP-Scanner.csproj | 3 ++- ARP-Scanner/ArpUtilities.cs | 39 ---------------------------------- ARP-Scanner/Program.cs | 29 ++++++++++++++++--------- 3 files changed, 21 insertions(+), 50 deletions(-) delete mode 100644 ARP-Scanner/ArpUtilities.cs diff --git a/ARP-Scanner/ARP-Scanner.csproj b/ARP-Scanner/ARP-Scanner.csproj index 90f73be..a9b7949 100644 --- a/ARP-Scanner/ARP-Scanner.csproj +++ b/ARP-Scanner/ARP-Scanner.csproj @@ -9,7 +9,8 @@ - + + diff --git a/ARP-Scanner/ArpUtilities.cs b/ARP-Scanner/ArpUtilities.cs deleted file mode 100644 index 2d40d5c..0000000 --- a/ARP-Scanner/ArpUtilities.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Stone_Red_Utilities.ConsoleExtentions; - -using System.Net; -using System.Runtime.InteropServices; - -namespace ARP_Scanner; - -internal class ArpUtilities -{ - [DllImport("iphlpapi.dll", ExactSpelling = true)] - private static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen); - - private uint macAddrLen = (uint)new byte[6].Length; - - public string? SendArpRequest(IPAddress ipAddress) - { - byte[] macAddr = new byte[6]; - - try - { - _ = SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen); - if (MacAddresstoString(macAddr) != "00-00-00-00-00-00") - { - return MacAddresstoString(macAddr); - } - } - catch (Exception e) - { - ConsoleExt.WriteLine(e.Message, ConsoleColor.Red); - } - return null; - } - - public static string MacAddresstoString(byte[] macAdrr) - { - string macString = BitConverter.ToString(macAdrr); - return macString.ToUpper(); - } -} \ No newline at end of file diff --git a/ARP-Scanner/Program.cs b/ARP-Scanner/Program.cs index 9249cca..043d903 100644 --- a/ARP-Scanner/Program.cs +++ b/ARP-Scanner/Program.cs @@ -1,4 +1,4 @@ -using ARP_Scanner; +using ArpLookup; using NetTools; @@ -7,15 +7,24 @@ using Stone_Red_Utilities.ConsoleExtentions; using System.Collections.Concurrent; using System.Net; +using System.Net.NetworkInformation; -internal class Program +namespace ARP_Scanner; + +internal static class Program { - private static void Main(string[] args) + private static async Task Main(string[] args) { bool success = false; IPAddress[]? ipAddresses = null; MacVendorLookup macVendorLookup = new MacVendorLookup("mac-vendors.csv"); + if (!Arp.IsSupported) + { + ConsoleExt.WriteLine("ARP is not supported on this platform!", ConsoleColor.Red); + return; + } + if (args.Length >= 1) { success = IPAddressRange.TryParse(string.Join("", args), out IPAddressRange iPAddressRange); @@ -38,21 +47,21 @@ internal class Program ConsoleExt.WriteLine("Starting scan...", ConsoleColor.DarkYellow); - Parallel.ForEach(ipAddresses, ipAddress => + await Parallel.ForEachAsync(ipAddresses, async (ipAddress, _) => { - string? mac = new ArpUtilities().SendArpRequest(ipAddress); + PhysicalAddress? mac = await Arp.LookupAsync(ipAddress); - Interlocked.Increment(ref processedIpAddressesCount); + int localProcessedIpAddressesCount = Interlocked.Increment(ref processedIpAddressesCount); if (mac is not null) { - List info = new List { ipAddress.ToString(), mac }; - info.AddRange(macVendorLookup.GetInformation(mac)); - ConsoleExt.WriteLine($"Progress: {processedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * processedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green); + List info = new List { ipAddress.ToString(), mac.ToString() }; + info.AddRange(macVendorLookup.GetInformation(mac.ToString())); + ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green); activeHosts.Add(info.ToArray()); } else { - ConsoleExt.WriteLine($"Progress: {processedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * processedIpAddressesCount:0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red); + ConsoleExt.WriteLine($"Progress: {localProcessedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * localProcessedIpAddressesCount:0.00}%] | Inactive: {ipAddress}", ConsoleColor.Red); } });