C - ARP Puffer abfragen

Hi

Ich habe ein Programm geschrieben, welches den lokalen ARP-Puffer auf Man in the Middle ARP Poisoning Angriffe überprüft (es schaut nach, ob für verschiedene IPs die gleiche MAC eingetragen ist). zZ macht dieses Programm über system("arp -a>>file.txt") eine Datei, die anschließend eingelesen und dann ausgewertet wird. Geht das nicht eleganter?

cu, Heinzi
 
Code:
#include <cstdlib>
#include <iostream>
#include <winsock2.h>
#include <Iprtrmib.h>
#include <Iphlpapi.h>

using namespace std;

void initWinsock()
{
  WSADATA wsaData;
  int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  if (iResult != NO_ERROR)
    printf("Error at WSAStartup()\n");
}

PMIB_IPNETTABLE readArpCache()
{
  PMIB_IPNETTABLE arpCache = NULL;
  ULONG ulSize  = 0; 
  
  GetIpNetTable(arpCache, &ulSize, TRUE);   // groesse des buffers bestimmen
  arpCache = new MIB_IPNETTABLE[ulSize];
  if (NULL != arpCache)
  {
      GetIpNetTable(arpCache, &ulSize, TRUE); // buffer fuellen
      return arpCache;
  } else {
      cout << "kaputt" << endl;
      return NULL;
  } 
}

void printArpCache(PMIB_IPNETTABLE arpCache)
{
  if (!arpCache)
    return;
  PMIB_IPNETROW arpCacheRow = NULL;
  in_addr ipAddress;
  for (int i = 0; i < arpCache->dwNumEntries; i++) {
    arpCacheRow = &(arpCache->table[i]);
    ipAddress.S_un.S_addr = arpCacheRow->dwAddr;
    if (!arpCache)
      continue;
    printf("IP Address: %s", inet_ntoa(ipAddress));
    printf("    MAC Address: %02x-%02x-%02x-%02x-%02x-%02x\r\n", 
            arpCacheRow->bPhysAddr[0],
            arpCacheRow->bPhysAddr[1],
            arpCacheRow->bPhysAddr[2],
            arpCacheRow->bPhysAddr[3],
            arpCacheRow->bPhysAddr[4],
            arpCacheRow->bPhysAddr[5]);
  }
  delete [] arpCache;
}

int main(int argc, char *argv[])
{
  initWinsock();
  printArpCache(readArpCache());
  system("PAUSE");
  return EXIT_SUCCESS;
}

mehr infos:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/intprothelp.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getipnettable.asp
http://www.codeproject.com/internet/IPHelper.asp
 
Zurück
Oben