winPcap学习笔记7_处理脱机文件(转)

在本讲中,我们将学习如何处理捕获到文件中的数据包。 WinPcap提供了很多函数来将网络数据流保存到文件并读取它们 -- 本讲将教你如何使用这些函数。我们还将看到如何使用WinPcap内核堆特性来获取一个高性能的堆。(请注意:此时,由于一些有关新内核缓冲的问题,这些特性将无法使用)

堆文件的格式是libpcap的一种。这种格式中,包含了被捕捉到的包的二进制数据,并且,这种格式是许多网络工具所使用的一种标准,这些工具包括WinDump,Etheral和Snort。

保存数据包到堆文件

首先,让我们看一下如何将一个数据包写成libpcap的格式。

接下来的例子讲从一个选定的接口捕获数据包,并且将它们保存到用户指定的文件中。

 

代码
1 #include "pcap.h"
2  #define FILENAME "rh"
3
4  /* 回调函数原型 */
5  void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
6
7 main(int argc, char **argv)
8 {
9 pcap_if_t *alldevs;
10 pcap_if_t *d;
11  int inum;
12  int i=0;
13 pcap_t *adhandle;
14  char errbuf[PCAP_ERRBUF_SIZE];
15 pcap_dumper_t *dumpfile;
16
17
18 /* 获取本机设备列表 */
19 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
20 {
21 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
22 exit(1);
23 }
24
25 /* 打印列表 */
26 for(d=alldevs; d; d=d->next)
27 {
28 printf("%d. %s", ++i, d->name);
29 if (d->description)
30 printf(" (%s)\n", d->description);
31 else
32 printf(" (No description available)\n");
33 }
34
35 if(i==0)
36 {
37 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
38 return -1;
39 }
40
41 printf("Enter the interface number (1-%d):",i);
42 scanf("%d", &inum);
43
44 if(inum < 1 || inum > i)
45 {
46 printf("\nInterface number out of range.\n");
47 /* 释放列表 */
48 pcap_freealldevs(alldevs);
49 return -1;
50 }
51
52 /* 跳转到选中的适配器 */
53 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
54
55
56 /* 打开适配器 */
57 if ( (adhandle= pcap_open(d->name, // 设备名
58 65536, // 要捕捉的数据包的部分
59 // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
60 PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
61 1000, // 读取超时时间
62 NULL, // 远程机器验证
63 errbuf // 错误缓冲池
64 ) ) == NULL)
65 {
66 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
67 /* 释放设备列表 */
68 pcap_freealldevs(alldevs);
69 return -1;
70 }
71
72 /* 打开堆文件 */
73 dumpfile = pcap_dump_open(adhandle, FILENAME);
74
75 if(dumpfile==NULL)
76 {
77 fprintf(stderr,"\nError opening output file\n");
78 return -1;
79 }
80
81 printf("\nlistening on %s... Press Ctrl+C to stop...\n", d->description);
82
83 /* 释放设备列表 */
84 pcap_freealldevs(alldevs);
85
86 /* 开始捕获 */
87 pcap_loop(adhandle, 0, packet_handler, (unsigned char *)dumpfile);
88
89 return 0;
90 }
91
92 /* 回调函数,用来处理数据包 */
93 void packet_handler(u_char *dumpfile, const struct pcap_pkthdr *header, const u_char *pkt_data)
94 {
95 /* 保存数据包到堆文件 */
96 pcap_dump(dumpfile, header, pkt_data);
97 }
98
99

你可以看到,这个程序的结构和前面几讲的程序非常相似,它们的区别有:

  • 只有当接口打开时,调用 pcap_dump_open() 才是有效的。 这个调用将打开一个堆文件,并将它关联到特定的接口上。
  • 数据包将会通过 pcap_dump() 函数写入堆文件中,这个函数是packet_handler()的回调函数。 pcap_dump() 的参数和 pcap_handler() 函数中的参数是一一对应的。

从堆文件中读取数据包

既然我们有了可用的堆文件,那我们就能读取它的内容了。 以下代码将打开一个WinPcap/libpcap的堆文件,并显示文件中每一个包的信息。文件通过 pcap_open_offline() 打开,然后,我们通常使用 pcap_loop() 来有序获取数据包。你可以看到,从脱机文件中读取数据包和从物理接口中接收它们是很相似的。

这个例子还会介绍另一个函数:pcap_createsrcsrc()。这个函数用于创建一个源字符串,这个源字符串以一个标志开头,这个标志会告诉WinPcap这个源的类型。比如,使用"rpcap://"标志来打开一个适配器,使用"file://"来打开一个文件。如果 pcap_findalldevs_ex() 已经被使用,那么这部是不需要的,因为其返回值已经包含了这些字符串。然而,在这个例子中,我们需要它。因为文件的名字来自于用户的输入。

 

代码


#include
<stdio.h>
#include
<pcap.h>

#define FILENAME "rh"
#define LINE_LEN 16

void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);

main(
int argc, char **argv)
{
pcap_t
*fp;
char errbuf[PCAP_ERRBUF_SIZE];
char source[PCAP_BUF_SIZE];

if(argc != 2){

printf(
"usage: %s filename", argv[0]);
return -1;

}

/* 根据新WinPcap语法创建一个源字符串 */
if ( pcap_createsrcstr( source, // 源字符串
PCAP_SRC_FILE, // 我们要打开的文件
NULL, // 远程主机
NULL, // 远程主机端口
FILENAME, // 我们要打开的文件名
errbuf // 错误缓冲区
) != 0)
{
fprintf(stderr,
"\nError creating a source string\n");
return -1;
}

/* 打开捕获文件 */
if ( (fp= pcap_open(source, // 设备名
65536, // 要捕捉的数据包的部分
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,
"\nUnable to open the file %s.\n", source);
return -1;
}

// 读取并解析数据包,直到EOF为真
pcap_loop(fp, 0, dispatcher_handler, NULL);

return 0;
}



void dispatcher_handler(u_char *temp1,
const struct pcap_pkthdr *header, const u_char *pkt_data)
{
u_int i
=0;

/* 打印pkt时间戳和pkt长度 */
printf(
"%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);

/* 打印数据包 */
for (i=1; (i < header->caplen + 1 ) ; i++)
{
printf(
"%.2x ", pkt_data[i-1]);
if ( (i % LINE_LEN) == 0) printf("\n");
}

printf(
"\n\n");

}

下面的程序同样实现了如上功能,只是,我们使用了 pcap_next_ex() 函数来代替需要进行回调的 pcap_loop()

 

代码
1
2 #include <stdio.h>
3 #include <pcap.h>
4
5 #define FILENAME "rh"
6 #define LINE_LEN 16
7
8 main(int argc, char **argv)
9 {
10 pcap_t *fp;
11 char errbuf[PCAP_ERRBUF_SIZE];
12 char source[PCAP_BUF_SIZE];
13 struct pcap_pkthdr *header;
14 const u_char *pkt_data;
15 u_int i=0;
16 int res;
17
18 if(argc != 2)
19 {
20 printf("usage: %s filename", argv[0]);
21 return -1;
22 }
23
24 /* 根据新WinPcap语法创建一个源字符串 */
25 if ( pcap_createsrcstr( source, // 源字符串
26 PCAP_SRC_FILE, // 我们要打开的文件
27 NULL, // 远程主机
28 NULL, // 远程主机端口
29 argv[1], // 我们要打开的文件名
30 errbuf // 错误缓冲区
31 ) != 0)
32 {
33 fprintf(stderr,"\nError creating a source string\n");
34 return -1;
35 }
36
37 /* 打开捕获文件 */
38 if ( (fp= pcap_open(source, // 设备名
39 65536, // 要捕捉的数据包的部分
40 // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
41 PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
42 1000, // 读取超时时间
43 NULL, // 远程机器验证
44 errbuf // 错误缓冲池
45 ) ) == NULL)
46 {
47 fprintf(stderr,"\nUnable to open the file %s.\n", source);
48 return -1;
49 }
50
51 /* 从文件获取数据包 */
52 while((res = pcap_next_ex( fp, &header, &pkt_data)) >= 0)
53 {
54 /* 打印pkt时间戳和pkt长度 */
55 printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
56
57 /* 打印数据包 */
58 for (i=1; (i < header->caplen + 1 ) ; i++)
59 {
60 printf("%.2x ", pkt_data[i-1]);
61 if ( (i % LINE_LEN) == 0) printf("\n");
62 }
63
64 printf("\n\n");
65 }
66
67
68 if (res == -1)
69 {
70 printf("Error reading the packets: %s\n", pcap_geterr(fp));
71 }
72
73 return 0;
74 }
75
76
77

 

使用pcap_live_dump将包写入堆文件

注意: 此时,由于新内核缓冲的一些问题,这个特性可能不可用。

WinPcap的最近几个版本提供了一个更好的途径,来讲数据流保存到磁盘,那就是 pcap_live_dump() 函数。 pcap_live_dump() 函数有3个参数:文件名,文件最大的大小(字节为单位),和文件可以允许存储的数据包的最大数量 。 0表示没有限制。 注意,在调用 pcap_live_dump() 将数据流保存下来之前,程序可以设置过滤器(使用 pcap_setfilter(),详情请参见教程的 过滤数据包这部分) ,这样,我们就可以定义要保存的那部分数据流了。

pcap_live_dump() 不会被阻塞, 因此,它开始堆处理后会立即返回。 堆处理以异步的方式进行,直到文件达到最大大小或者存储的数据包达到最大数量。

应用程序可以使用 pcap_live_dump_ended()来检查数据是否存储完毕。 特别注意: sync 参数必须是非零的, 如果它们是0,那么程序将永远被阻塞。

 

代码
#include <stdlib.h>
#include
<stdio.h>

#include
<pcap.h>

#error At the moment the kernel dump feature is not supported in the driver

main(
int argc, char **argv) {

pcap_if_t
*alldevs, *d;
pcap_t
*fp;
u_int inum, i
=0;
char errbuf[PCAP_ERRBUF_SIZE];

printf(
"kdump: saves the network traffic to file using WinPcap kernel-level dump faeature.\n");
printf(
"\t Usage: %s [adapter] | dump_file_name max_size max_packs\n", argv[0]);
printf(
"\t Where: max_size is the maximum size that the dump file will reach (0 means no limit)\n");
printf(
"\t Where: max_packs is the maximum number of packets that will be saved (0 means no limit)\n\n");


if(argc < 5){

/* 用户没有提供数据源。获取设备列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,
"Error in pcap_findalldevs: %s\n", errbuf);
exit(
1);
}

/* 打印列表 */
for(d=alldevs; d; d=d->next)
{
printf(
"%d. %s", ++i, d->name);
if (d->description)
printf(
" (%s)\n", d->description);
else
printf(
" (No description available)\n");
}

if(i==0)
{
printf(
"\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}

printf(
"Enter the interface number (1-%d):",i);
scanf(
"%d", &inum);

if(inum < 1 || inum > i)
{
printf(
"\nInterface number out of range.\n");
/* 释放列表 */
return -1;
}

/* 跳转到所选的设备 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* 打开设备 */
if ( (fp = pcap_open_live(d->name, 100, 1, 20, errbuf) ) == NULL)
{
fprintf(stderr,
"\nError opening adapter\n");
return -1;
}

/* 释放设备列表 */
pcap_freealldevs(alldevs);

/* 开始堆过程 */
if(pcap_live_dump(fp, argv[1], atoi(argv[2]), atoi(argv[3]))==-1){
printf(
"Unable to start the dump, %s\n", pcap_geterr(fp));
return -1;
}
}
else{

/* 打开设备 */
if ( (fp= pcap_open_live(argv[1], 100, 1, 20, errbuf) ) == NULL)
{
fprintf(stderr,
"\nError opening adapter\n");
return -1;
}

/* 开始堆过程 */
if(pcap_live_dump(fp, argv[0], atoi(argv[1]), atoi(argv[2]))==-1){
printf(
"Unable to start the dump, %s\n", pcap_geterr(fp));
return -1;
}
}

/* 等待,知道堆过程完成,也就是当数据到达max_size或max_packs的时候 */
pcap_live_dump_ended(fp, TRUE);

/* 关闭适配器,这样,就可以将数据立刻输出到文件了 */
pcap_close(fp);

return 0;
}

由于我的电脑不支持这个特性,程序不能正常运行,产生如下错误:

 

posted @ 2010-07-18 23:56  董雨  阅读(488)  评论(0编辑  收藏  举报