mirror of
https://github.com/Stone-Red-Code/ARP-Scanner.git
synced 2026-09-04 00:46:04 +02:00
Add linux support and code improvments
This commit is contained in:
@@ -9,7 +9,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="IPAddressRange" Version="4.2.0" />
|
||||
<PackageReference Include="ArpLookup" Version="2.0.3" />
|
||||
<PackageReference Include="IPAddressRange" Version="5.0.0" />
|
||||
<PackageReference Include="Stone_Red-C-Sharp-Utilities" Version="1.0.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
+19
-10
@@ -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<string> info = new List<string> { ipAddress.ToString(), mac };
|
||||
info.AddRange(macVendorLookup.GetInformation(mac));
|
||||
ConsoleExt.WriteLine($"Progress: {processedIpAddressesCount}/{ipAddressesCount} [{100d / ipAddressesCount * processedIpAddressesCount:0.00}%] | Active: {ipAddress}", ConsoleColor.Green);
|
||||
List<string> info = new List<string> { 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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user