Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save invictus-0x90/2e1ebcfefd8d7478e695131b88ce2926 to your computer and use it in GitHub Desktop.
Save invictus-0x90/2e1ebcfefd8d7478e695131b88ce2926 to your computer and use it in GitHub Desktop.
c# get arp cache
[StructLayout(LayoutKind.Sequential)]
struct MIB_IPNETROW
{
[MarshalAs(UnmanagedType.U4)]
public int dwIndex;
[MarshalAs(UnmanagedType.U4)]
public int dwPhysAddrLen;
[MarshalAs(UnmanagedType.U1)]
public byte mac0;
[MarshalAs(UnmanagedType.U1)]
public byte mac1;
[MarshalAs(UnmanagedType.U1)]
public byte mac2;
[MarshalAs(UnmanagedType.U1)]
public byte mac3;
[MarshalAs(UnmanagedType.U1)]
public byte mac4;
[MarshalAs(UnmanagedType.U1)]
public byte mac5;
[MarshalAs(UnmanagedType.U1)]
public byte mac6;
[MarshalAs(UnmanagedType.U1)]
public byte mac7;
[MarshalAs(UnmanagedType.U4)]
public int dwAddr;
[MarshalAs(UnmanagedType.U4)]
public int dwType;
}
[DllImport("IpHlpApi.dll")]
private static extern long GetIpNetTable(IntPtr pIpNetTable, ref int pdwSize, bool bOrder);
public static string ArpCache()
{
IntPtr buff;
int requiredLen = 0;
string ret = "--------------- ARP Table --------------\n";
long result = GetIpNetTable(IntPtr.Zero, ref requiredLen, false);
try
{
buff = Marshal.AllocCoTaskMem(requiredLen);
result = GetIpNetTable(buff, ref requiredLen, false);
int entries = Marshal.ReadInt32(buff);
IntPtr entryBuffer = new IntPtr(buff.ToInt64() + Marshal.SizeOf(typeof(int)));
MIB_IPNETROW[] arpTable = new MIB_IPNETROW[entries];
for(int i = 0; i < entries; i++)
{
int currentIndex = i * Marshal.SizeOf(typeof(MIB_IPNETROW));
IntPtr newStruct = new IntPtr(entryBuffer.ToInt64() + currentIndex);
arpTable[i] = (MIB_IPNETROW)Marshal.PtrToStructure(newStruct, typeof(MIB_IPNETROW));
}
for(int i = 0; i < entries; i++)
{
MIB_IPNETROW entry = arpTable[i];
IPAddress addr = new IPAddress(BitConverter.GetBytes(entry.dwAddr));
ret += String.Format("{0}: {1}-{2}-{3}-{4}-{5}-{6}\n", addr.ToString(), entry.mac0.ToString("X2"), entry.mac1.ToString("X2"), entry.mac2.ToString("X2"), entry.mac3.ToString("X2"), entry.mac4.ToString("X2"), entry.mac5.ToString("X2"));
}
}
catch(Exception e)
{
return "[x] Exception thrown: " + e.Message;
}
return ret;
}
@AfroThundr3007730
Copy link

Thanks for this. I made a thing with a tweaked version of this.

@Katadeus
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment