网络数据截获方法 (2)
2008-04-02 10:57:16来源:互联网 阅读 ()
/* Read all packets on the device. Continue until cnt packets read */
pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
{
register int n;
for { //for循环
if (p->sf.rfile != NULL) {
n = pcap_offline_read(p, cnt, callback, user);
} else {
// XXX keep reading until we get something
do {
n = p->read_op(p, cnt, callback, user);
} while (n == 0);
}
if (n <= 0)
return (n); //遇错误,返回
if (cnt > 0) {
cnt -= n;
if (cnt <= 0)
return (0); //达到制定数量,返回
}
}
}
pcap_loop()有几个重要参数:
参数是pv.pkt_cnt,表示总共要捕捉的包的数量。在main函数初始化时,缺省设置为-1,成为永真循环,一直捕捉直到程序退出:
/* initialize the packet counter to loop forever */
pv.pkt_cnt = -1;
或者在命令行中设置要捕捉的包的数量。ParseCmdLine函数的调用中,遇到参数n,重新设定pv.pkt_cnt的值。ParseCmdLine中相关语句如下:
case 'n': /* grab x packets and exit */
pv.pkt_cnt = atoi(optarg);
Snort.c主程序中,Pcap_loop()函数有两种提取数据模式:
打开网卡和打开文件。
/* get the device file descriptor,打开网卡接口 */
pd = pcap_open_live(pv.interface, snaplen,
pv.promisc_flag ? PROMISC : 0, READ_TIMEOUT, errorbuf);
或者
/* open the file,打开文件 */
pd = pcap_open_offline(intf, errorbuf);
只有以上两种返回情况。
Snort把真实的数据包存储在内存中指针指向的数据结构中。在decode.h中,定义了所有Snort要使用到的数据结构,包括Tcp,Ip,以太桢,vlan桢等。Snort的这些结构将指针指向截获的包上来代表相应的协议。如指向以太桢用数据指针使用_EthrHdr头。
数据结构:
Typededf struct _etherhdr
{
u_int8_t ether_dst[6]; //目的地址;
u_int8_t ether_src[6]; //源地址;
u_int16_t ether_type; //协议类型;
}
typedef struct _Tcphdr
{
u_int16_t th_sport; //源端口;
u_int16_t th_dport; //目端口;
u_int32_t th_seq; //sequence number;
u_int32_t th_ack; //acknowledgement number;
u_int8_t th_offx2; //offset and reserved;
u_int8_t th_flags; //flags;
u_int16_t th_win; //window;
u_int16_t th_sum; //checksum;
u_int16_t th_urp; //urgent pointer;
}
小结
网络数据的截取是入侵检测系统的基础。本节从网络数据截取机制开始讲,讲述了常见的两种机制。重点讲述了Libpcap库的网络数据包截取机制。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:预测现在-安全策略新观点
下一篇:关于日志记录系统设计思想
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
