winPcap学习笔记6_分析数据包(转)

现在,我们可以捕捉并过滤网络流量了,那就让我们学以致用,来做一个简单使用的程序吧。

在本讲中,我们将会利用上一讲的一些代码,来建立一个更实用的程序。 本程序的主要目标是展示如何解析所捕获的数据包的协议首部。这个程序可以称为UDPdump,打印一些网络上传输的UDP数据的信息。

我们选择分析和现实UDP协议而不是TCP等其它协议,是因为它比其它的协议更简单,作为一个入门程序范例,是很不错的选择。让我们看看代码:

 

代码
/*
* Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
* Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Politecnico di Torino, CACE Technologies
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

#include
"pcap.h"

/* 4字节的IP地址 */
typedef
struct ip_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;

/* IPv4 首部 */
typedef
struct ip_header{
u_char ver_ihl;
// 版本 (4 bits) + 首部长度 (4 bits)
u_char tos; // 服务类型(Type of service)
u_short tlen; // 总长(Total length)
u_short identification; // 标识(Identification)
u_short flags_fo; // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
u_char ttl; // 存活时间(Time to live)
u_char proto; // 协议(Protocol)
u_short crc; // 首部校验和(Header checksum)
ip_address saddr; // 源地址(Source address)
ip_address daddr; // 目的地址(Destination address)
u_int op_pad; // 选项与填充(Option + Padding)
}ip_header;

/* UDP 首部*/
typedef
struct udp_header{
u_short sport;
// 源端口(Source port)
u_short dport; // 目的端口(Destination port)
u_short len; // UDP数据包长度(Datagram length)
u_short crc; // 校验和(Checksum)
}udp_header;

/* 回调函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);


main()
{
pcap_if_t
*alldevs;
pcap_if_t
*d;
int inum;
int i=0;
pcap_t
*adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;

/* 获得设备列表 */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &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");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}

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

/* 打开适配器 */
if ( (adhandle= pcap_open(d->name, // 设备名
65536, // 要捕捉的数据包的部分
// 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读取超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲池
) ) == NULL)
{
fprintf(stderr,
"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}

/* 检查数据链路层,为了简单,我们只考虑以太网 */
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr,
"\nThis program works only on Ethernet networks.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}

if(d->addresses != NULL)
/* 获得接口第一个地址的掩码 */
netmask
=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* 如果接口没有地址,那么我们假设一个C类的掩码 */
netmask
=0xffffff;


//编译过滤器
if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
{
fprintf(stderr,
"\nUnable to compile the packet filter. Check the syntax.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}

//设置过滤器
if (pcap_setfilter(adhandle, &fcode)<0)
{
fprintf(stderr,
"\nError setting the filter.\n");
/* 释放设备列表 */
pcap_freealldevs(alldevs);
return -1;
}

printf(
"\nlistening on %s...\n", d->description);

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

/* 开始捕捉 */
pcap_loop(adhandle,
0, packet_handler, NULL);

return 0;
}

/* 回调函数,当收到每一个数据包时会被libpcap所调用 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
ip_header
*ih;
udp_header
*uh;
u_int ip_len;
u_short sport,dport;
time_t local_tv_sec;

/* 将时间戳转换成可识别的格式 */
local_tv_sec
= header->ts.tv_sec;
ltime
=localtime(&local_tv_sec);
strftime( timestr,
sizeof timestr, "%H:%M:%S", ltime);

/* 打印数据包的时间戳和长度 */
printf(
"%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

/* 获得IP数据包头部的位置 */
ih
= (ip_header *) (pkt_data +
14); //以太网头部长度

/* 获得UDP首部的位置 */
ip_len
= (ih->ver_ihl & 0xf) * 4;
uh
= (udp_header *) ((u_char*)ih + ip_len);

/* 将网络字节序列转换成主机字节序列 */
sport
= ntohs( uh->sport );
dport
= ntohs( uh->dport );

/* 打印IP地址和UDP端口 */
printf(
"%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih
->saddr.byte1,
ih
->saddr.byte2,
ih
->saddr.byte3,
ih
->saddr.byte4,
sport,
ih
->daddr.byte1,
ih
->daddr.byte2,
ih
->daddr.byte3,
ih
->daddr.byte4,
dport);
}

首先,我们将过滤器设置成"ip and udp"。在这种方式下,我们确信packet_handler()只会收到基于IPv4的UDP数据包;这将简化解析过程,提高程序的效率。

我们还分别创建了用于描述IP首部和UDP首部的结构体。这些结构体中的各种数据会被packet_handler()合理地定位。

packet_handler(), 尽管只受限于单个协议的解析(比如基于IPv4的UDP),不过它展示了捕捉器(sniffers)是多么的复杂,就像TcpDump或WinDump对网络数据流进行解码那样。 因为我们对MAC首部不感兴趣,所以我们跳过它。 为了简洁,我们在开始捕捉前,使用了pcap_datalink() 对MAC层进行了检测,以确保我们是在处理一个以太网络。这样,我们就能确保MAC首部是14位的。

IP数据包的首部就位于MAC首部的后面。我们将从IP数据包的首部解析到源IP地址和目的IP地址。

处理UDP的首部有一些复杂,因为IP数据包的首部的长度并不是固定的。然而,我们可以通过IP数据包的length域来得到它的长度。一旦我们知道了UDP首部的位置,我们就能解析到源端口和目的端口。

被解析出来的值被打印在屏幕上,形式如下所示:

1. \Device\Packet_{A7FD048A-5D4B-478E-B3C1-34401AC3B72F} (Xircom t 10/100 Adapter)
Enter the interface number (1-2):1

listening on Xircom CardBus Ethernet 10/100 Adapter...
16:13:15.312784 len:87 130.192.31.67.2682 -> 130.192.3.21.53
16:13:15.314796 len:137 130.192.3.21.53 -> 130.192.31.67.2682
16:13:15.322101 len:78 130.192.31.67.2683 -> 130.192.3.21.53

最后3行中的每一行,分别代表了一个数据包。

posted @ 2010-07-18 14:28  董雨  阅读(481)  评论(0编辑  收藏  举报