adhandle= pcap_open_live(d->name, // 网卡名字
65536, // 65536表示对所有数据捕获
0, //网卡为混杂模式
1, // 抓包时间间隔
errbuf // error buffer
)
设置过滤规则 packet_filter[] = "ip||udp"; 连接并设置过滤器:
pcap_compile(adhandle, &fcode, packet_filter, 1, netmask);
pcap_setfilter(adhandle, &fcode);
最后使用回调函数捕获并解析数据包:
pcap_loop(adhandle, 0, packet_handler, NULL);
解码数据包模块实现
Winpcap捕获到数据包后,根据对packet_handler()函数的操作可以对IP、TCP、UDP协议各字段的信息进行分析。数据结构如下:
/* IPv4 头 */
typedef struct ip_header
{ u_char ver_ihl; // 版本 (4 bits) + 首部长度 (4 bits)
u_char tos; // 服务类型
u_short tlen; // 16位数据报的长度 (字节)
u_short identification; // 16位标识
u_short flags_fo; // 标志 (3 bits) + 片偏移 (13 bits)
u_char ttl; // 8位生存时间
u_char proto; // 协议
u_short crc; // 16位首部检验和
ip_address saddr; // 32位源IP地址
ip_address daddr; // 32位目的IP地址
u_int op_pad; // 可选择使用
}ip_header;
/* UDP 头*/
typedef struct udp_header
{ u_short sport; // 16位源端口号
u_short dport; // 16位目的端口号
u_short len; // 16位UDP长度
u_short crc; // 16位UDP检验和
}udp_header;
/*TCP 头*/
typedef struct tcphdr {
USHORT th_sport; // 16位源端口号
USHORT th_dport; // 16位目的端口号
ULONG th_seq; //32位序列号
ULONG th_ack; //32位确认号
UCHAR th_x2:4;
UCHAR th_off:4; //保留
UCHAR th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#defi
首页 上一页 3 4 5 6 7 8 9 下一页 尾页 6/10/10
免费个人入侵检测系统的实现(六)由毕业论文网(www.huoyuandh.com)会员上传。