1、,信息安全产品开发实践数据包的构造,Gujianjun (ruth), 13880804340Dec 2007,Libnet介绍,Libnet介绍,Mike D. Schiffmanhttp:/ injection_type,char *device,char *err_buf)完成Injection_type:表示构造的类型Device:表示网络接口Err_buf:表示存放出错的信息,初始化-确定libnet类型,6种类型LIBNET_LINK:表示构造链路层开始的数据包,比IP层的更丰富;LIBNET_RAW4:表示构造IPV4原始套接字接口类型的数据包;LIBNET_RAW6:表示构造I
2、PV6原始套接字接口类型的数据包;,初始化确定网络接口,数据包发送的接口一种是自己指定,可以是网络接口的名字,如以太网接口”eth0”等,也可以是IP地址;一种是由libnet自动查询搜索,只要在函数libnet_init()的参数device赋值null即可,此时libnet会从系统中搜索可用的网络接口,初始化获取libnet的句柄,执行完后返回一个libnet句柄,在这个句柄里面包含了重要的信息不直接打交道struct libnet_context#if (_WIN32_) /* file descriptor of packet device */#endif,int injection
3、_type; /* raw (ipv4 or ipv6) or link */#define LIBNET_LINK 0x00 /* link-layer interface */#define LIBNET_RAW4 0x01 /* raw socket interface (ipv4) */#define LIBNET_RAW6 0x02 /* raw socket interface (ipv6) */* the following should actually set a flag in the flags variable above */#define LIBNET_LINK_A
4、DV 0x08 /* advanced mode link-layer */#define LIBNET_RAW4_ADV 0x09 /* advanced mode raw socket (ipv4) */#define LIBNET_RAW6_ADV 0x0a /* advanced mode raw socket (ipv6) */#define LIBNET_ADV_MASK 0x08 /* mask to determine adv mode */,libnet_pblock_t *protocol_blocks; /* protocol headers / data */ libn
5、et_pblock_t *pblock_end; /* last node in list */ u_int32_t n_pblocks; /* number of pblocks */ int link_type; /* link-layer type */ int link_offset; /* link-layer header size */ int aligner; /* used to align packets */ char *device; /* device name */,struct libnet_stats stats; /* statistics */ libnet
6、_ptag_t ptag_state; /* state holder for pblock tag */ char labelLIBNET_LABEL_SIZE; /* textual label for cq interface */ char err_bufLIBNET_ERRBUF_SIZE; /* error buffer */ u_int32_t total_size; /* total size */;typedef struct libnet_context libnet_t;,构造数据包,构造一个数据包就是构造一系列协议块一个数据包是由一个协议块构成的例如:构造一个tcp数据
7、包,、构造一个tcp协议块,然后插入协议块链表;2、在构造一个ip协议块,插入到tcp协议块后面;、最后构造一个以太网协议块,插入到ip协议块后面,数据包检测,数据包的检测最主要的是计算校验和,如果不想自己计算校验和,只需要将校验和的参数赋值为即可,libnet就会自动计算校验和,数据包发送,非常简单,只需要由函数libnet_write()函数完成的,构造arp数据包的流程,过程说明,1、利用函数libnet_init()进行初始化操作,因为arp协议在网络层,所以必须先构造arp协议块,也就是利用函数libnet_build_arp()来构造arp协议块;、利用函数libnet_autob
8、uild_ethernet()或者libnet_build_ethernet()来构造以太网协议块;、利用函数libnet_write()发送数据包;,初始化libnet_init(),构造ARP协议块Libnet_build_arp()Libnet_autobuild_arp(),构造以太网协议块Libnet_build_ethernet()Libnet_autobuild_ethernet(),发送数据包libnet_write(),销毁libnet_destroy(),数据包的构造及设计,过程简单说明,1、对内存进行初试化;2、对网络进行初始化;3、构造各种网络数据包;4、对数据包进行合
9、法性检查5、发送网络数据包,初始化函数,由libnet_init(int injection_type,char *device,char *err_buf)完成函数返回值:如果成功就返回一个libnet句柄,如果失败,则返回null参数描述:Injection_type:表示构造的类型,Device:表示网络接口,Err_buf:表示存放出错的信息,地址函数,由char * libnet_addr2name4(u_init32_t,u_int8_t usr_name)完成函数返回值:返回IPV4地址的文字面熟字符串格式参数描述:参数in表示网络字节顺序的ipv4地址,参数use_name表示
10、是否需要进行名字,构造函数,由u_int32_t libnet_name2addr4(libnet_t *l,char * host_name,u_int8_t use_name)完成函数返回值:返回网络字节顺序IPV4地址参数描述:参数l表示libnet句柄,参数host_name表示主机名字,参数use_name表示是否要进行域名解析destination_ip=libnet_name2addr4(1,destination_ip_str,LIBNET_RESOLVE),构造函数,libnet_build_arp()函数返回值:返回一个协议标志,失败返回,构造函数,libnet_autob
11、uild_ethernet(u_int8_t *dst, u_int16_t type, libnet_t *l)函数返回值:返回一个协议标志,失败返回;参数描述:参数dst表示目的以太网地址,参数type表示协议类型,参数l表示libnet句柄libnet_autobuild_ethernet(hardware_destination,ETHERTYPE_ARP,l),数据包构造,ARP数据包构造IP数据包构造Tcp数据包构造Synflood数据包构造Ping of death数据包构造gcc -o xxx arp.c -lnet,数据包构造,tar -zxvf libnetxxxx.tar.gzcd libnet./configuremake make installgcc -o xxx arp.c -lnet,Chapter over,This is the end of this chapter ThanksAny question ?,