(C)libnet-发送arp/tcp/icmp数据包

一、发送arp数据包

#include <stdio.h>   
#include <libnet.h>   
int main()   
{   
    int res;   
    /*********init paras*****************/  
    libnet_t *l;/*****libnet handler*/  
    libnet_ptag_t p_tag;   
    char *device="eth0";   
    char err_buff[LIBNET_ERRBUF_SIZE];   
    char *src_ip_str="192.168.85.129";   
    char *dest_ip_str="192.168.85.132";   
    u_char src_mac[6]={0x00,0x1c,0xf9,0x6a,0x4c,0x00};   
    u_char dest_mac[6]={0xff,0xff,0xff,0xff,0xff,0xff};   
    u_long src_ip;   
    u_long dest_ip;   
    src_ip=libnet_name2addr4(l,src_ip_str,LIBNET_RESOLVE);   
    dest_ip=libnet_name2addr4(l,dest_ip_str,LIBNET_RESOLVE);   
    /**********init libnet*****************/  
    l=libnet_init(   
        LIBNET_LINK_ADV,   
        device,   
        err_buff   
    );   //开启系统的网络功能
    if(l==NULL)   
    {   
        printf("libnet_init err!\n");   
        fprintf(stderr,"%s",err_buff);   
        exit(0);   
    }   
    /**********build arp packet************/  
    p_tag=libnet_build_arp(   
        ARPHRD_ETHER,/*hardware type ethernet*/  
        ETHERTYPE_IP,/*protocol type*/  
        6,/*length of mac*/  
        4,/*length of IP*/  
        ARPOP_REPLY,/*ARP operation type*/  
        src_mac,   
        (u_int8_t*) &src_ip,   
        dest_mac,   
        (u_int8_t*) &dest_ip,   
        NULL,/*payload*/  
        0,/*payload size*/  
        l,/*libnet handler*/  
        0/*'0' stands out building a new packet*/  
    );   
    if(p_tag==-1)   
    {   
        printf("libnet_build_arp err!\n");   
        exit(0);   
    }   
    /***********build ethernet packet header*************/  
    p_tag=libnet_build_ethernet(   
        dest_mac,   
        src_mac,   
        ETHERTYPE_ARP,   
        NULL,   
        0,   
        l,   
        0   
    );   
    if(p_tag==-1)   
    {   
        printf("libnet_build_ethernet err!\n");   
        exit(0);   
    }   
    /*********send packets*******************************/
    int j;  
    int num=5;
    for(j=0;j<num;j++)   
    {   
        if((res=libnet_write(l))==-1)   
        {   
            printf("libnet_write err!\n");   
            exit(0);   
        }   
        printf("arp packet has been sent\n");   
        sleep(1);   
    }   
    /*********over and destroy**************************/  
    libnet_destroy(l);   //关闭使用的网络通讯设备
    return 0;   
} 

二、发送tcp数据包

#include <libnet.h>

int main() {
    libnet_t *handle; /* Libnet句柄 */
    int packet_size; /* 构造的数据包大小 */
    char *device = "eth0"; /* 设备名字,也支持点十进制的IP地址,会自己找到匹配的设备 */
    char *src_ip_str = "192.168.85.129"; /* 源IP地址字符串 */
    char *dst_ip_str = "192.168.85.132"; /* 目的IP地址字符串 */
    u_char src_mac[6] = {0x00, 0x0c, 0x29, 0xc0, 0xd0, 0xcd}; /* 源MAC */
    u_char dst_mac[6] = {0x00, 0x0c, 0x29, 0x24, 0x6b, 0x77}; /* 目的MAC */
    u_long dst_ip, src_ip; /* 网路序的目的IP和源IP */
    char error[LIBNET_ERRBUF_SIZE]; /* 出错信息 */
    libnet_ptag_t eth_tag, ip_tag, tcp_tag, tcp_op_tag; /* 各层build函数返回值 */
    u_short proto = IPPROTO_TCP; /* 传输层协议 */
    u_char payload[255] = {0}; /* 承载数据的数组,初值为空 */
    u_long payload_s = 0; /* 承载数据的长度,初值为0 */

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

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

    strncpy(payload, "test", sizeof(payload)-1); /* 构造负载的内容 */
    payload_s = strlen(payload); /* 计算负载内容的长度 */

#if 0
    /* 构建TCP的选项,通常在第一个TCP通信报文中设置MSS */
    tcp_op_tag = libnet_build_tcp_options(
                payload,
                payload_s,
                handle,
                0
    );
    if (tcp_op_tag == -1) {
        printf("build_tcp_options failure\n");
        return (-2);
    };
#endif

    tcp_tag = libnet_build_tcp(
                30330,                    /* 源端口 */
                30331,                    /* 目的端口 */
                8888,                    /* 序列号 */
                8889,                    /* 确认号 */
                TH_PUSH | TH_ACK,        /* Control flags */
                14600,                    /* 窗口尺寸 */
                0,                        /* 校验和,0为自动计算 */
                0,                        /* 紧急指针 */
                LIBNET_TCP_H + payload_s, /* 长度 */
                payload,                    /* 负载内容 */
                payload_s,                /* 负载内容长度 */
                handle,                    /* libnet句柄 */
                0                        /* 新建包 */
    );
    if (tcp_tag == -1) {
        printf("libnet_build_tcp failure\n");
        return (-3);
    };

    /* 构造IP协议块,返回值是新生成的IP协议快的一个标记 */
    ip_tag = libnet_build_ipv4(
        LIBNET_IPV4_H + LIBNET_TCP_H + payload_s, /* IP协议块的总长,*/
        0, /* tos */
        (u_short) libnet_get_prand(LIBNET_PRu16), /* id,随机产生0~65535 */
        0, /* frag 片偏移 */
        (u_int8_t)libnet_get_prand(LIBNET_PR8), /* ttl,随机产生0~255 */
        proto, /* 上层协议 */
        0, /* 校验和,此时为0,表示由Libnet自动计算 */
        src_ip, /* 源IP地址,网络序 */
        dst_ip, /* 目标IP地址,网络序 */
        NULL, /* 负载内容或为NULL */
        0, /* 负载内容的大小*/
        handle, /* Libnet句柄 */
        0 /* 协议块标记可修改或创建,0表示构造一个新的*/
    );
    if (ip_tag == -1) {
        printf("libnet_build_ipv4 failure\n");
        return (-4);
    };

    /* 构造一个以太网协议块,只能用于LIBNET_LINK */
    eth_tag = libnet_build_ethernet(
        dst_mac, /* 以太网目的地址 */
        src_mac, /* 以太网源地址 */
        ETHERTYPE_IP, /* 以太网上层协议类型,此时为IP类型 */
        NULL, /* 负载,这里为空 */ 
        0, /* 负载大小 */
        handle, /* Libnet句柄 */
        0 /* 协议块标记,0表示构造一个新的 */ 
    );
    if (eth_tag == -1) {
        printf("libnet_build_ethernet failure\n");
        return (-5);
    };

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

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

    return (0);
}

三、发送icmp数据包

#include <libnet.h>   
#define NUM 10 /* the num of ICMP packets*/
void send_packets(){
    int i;
    libnet_t *l = NULL;/* libnet句柄 */   
    libnet_ptag_t protocol_tag;/* 协议标记 */   
    char *payload_liu_wen_tao = "12435"; /* 负载 */   
    u_short payload_length = 1000; /* 负载长度 */   
    char *device = "ech0";/*网络设备接口*/   
    char *destination_ip_str = "192.168.111.129";/* 目的IP地址字符串 */   
    char *source_ip_str = "192.168.111.129"; /* 源IP地址字符串 */   
    u_long source_ip = 0; /* 源IP地址 */   
    u_long destination_ip = 0; /* 目的IP地址 */   
    char errbuf[LIBNET_ERRBUF_SIZE]; /* 错误信息 */   
    int packet_length; /* 发送的数据包的长度 */   
    l = libnet_init( /* 初始化libnet */
    LIBNET_RAW4, /* libnet类型,为原始套接字IPv4类型 */
    device,  /* 网络设备接口 */errbuf /* 错误信息 */   
    );   
    source_ip = libnet_name2addr4(l, source_ip_str, LIBNET_RESOLVE);   
    /* 把源IP地址字符串形式转化为网络字节顺序的数据 */   
    destination_ip = libnet_name2addr4(l, destination_ip_str, LIBNET_RESOLVE);   
    /* 把目的IP地址字符串形式转化为网络字节顺序的数据 */   
    protocol_tag = libnet_build_icmpv4_echo( /* 构造ICMP回显数据包 */    
    ICMP_ECHO, /* 类型,此时为回显请求 */   
    0,/* 代码,应该为0 */   
    0, /* 校验和,为0,表示由libnet句柄自动计算 */   
    123,  /* 标识符,赋值为123,自己任意填写数值 */   
    456, /* 序列号,赋值为245,自己任意填写数值 */   
    NULL,  /* 负载,赋值为空 */   
    0, /* 负载的长度,赋值为0 */   
    l, /* libnet句柄,应该是由libnet_init()函数得到的 */   
    0  /* 协议块标记,赋值为0,表示构造一个新的协议块 */   
    );   
    protocol_tag = libnet_build_ipv4(/* 构造IP协议块 */   
    LIBNET_IPV4_H + LIBNET_ICMPV4_ECHO_H + payload_length,/* IP协议块的长度 */   
    0, /* 服务质量,这里赋值为0 */   
    10,  /* 标识符,这里赋值为10 */   
    0, /* 偏移,这里赋值为0 */   
    20,/* 生存时间,这里赋值为20 */   
    IPPROTO_ICMP,/* 上层协议类型,这里是ICMP协议 */   
    0, /* 校验和,这里为0表示由libnet计算校验和 */   
    source_ip, /* 源IP地址 */   
    destination_ip,/* 目的IP地址 */   
    payload_liu_wen_tao, /* 负载 */   
    payload_length, /* 负载的长度 */   
    l,/* libnet句柄 */   
    0 /* 协议块标记,为0表示构造一个新的IP协议块 */   
    );   
    while(1){
        packet_length = libnet_write(l); /* 发送由libnet句柄l表示的数据包 */   
        printf("the length of the ICMP packet is %d\n", packet_length);   
        /* 输出发送的数据包信息 */   
        }
    libnet_destroy(l); /* 销毁libnet */
}

void main()   
{   
   send_packets();
}   

 

posted @ 2014-12-15 15:51  云裳诉  阅读(1360)  评论(0编辑  收藏  举报