#include "pcap.h"
#include <stdio.h>
/* 6字节的MAC地址 */
typedef struct mac_address{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
u_char byte5;
u_char byte6;
}mac_address;
/* MAC 首部 */
typedef struct mac_header{
mac_address daddr; // 目的地址(Destination address)
mac_address saddr; // 源地址(Source address)
u_short tos; // 服务类型(Type of service)
}mac_header;
/* 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;
/* ARP 首部 */
typedef struct arp_header{
u_short hard_type; // 硬件类型(Hardware type)
u_short proto_type; // 协议类型(Protocol type)
u_char hard_add_len; // 硬件地址长度(Hardware address length)
u_char proto_add_len; // 协议类型长度(Protocol address length)
u_short oper; // 操作码(Operation)
mac_address shadd; // 发送端MAC地址(Sender hardware address)
ip_address spadd; // 发送端IP地址(Sender protocol address)
mac_address thadd; // 目的端MAC地址(Target hardware address)
ip_address tpadd; // 目的端IP地址(Target protocol address)
}arp_header;
/* ICMP 首部 */
typedef struct icmp_header{
u_char type; // 类型(Type)
u_char code; // 代码(Code)
}icmp_header;
/* TCP 首部 */
typedef struct tcp_header{
u_short sport; // 源端口(Source port)
u_short dport; // 目的端口(Destination port)
u_int seqnum; // 序号(Sequence number)
u_int acknum; // 确认号(Acknowledgment number)
u_short do_res_flags; // 数据偏移(Data offset) (4 bits) +
// 保留位(Reserved) (6 bits) +
// 控制位(Flags) (6 bits)
u_short wins; // 窗口大小(Window size)
u_short crc; // 检验和(Checksum)
u_short urgp; // 紧急指针(Urgent pointer)
u_int op_pad; // 选项与填充(Option + Padding)
}tcp_header;
/* 回调函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
FILE *fout; // 将结果输出至文件output.txt
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum,jnum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[48] = "";
struct bpf_program fcode;
fout = fopen("output.txt", "w"); //打开文件output.txt
/* 获得设备列表 */
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);
printf("Please enter the rules to capture and analyze: ");
fflush(stdin) ;
scanf("%[^\n]", &packet_filter);
printf("Please enter the number you want to capture: ");
scanf("%d", &jnum);
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, jnum, packet_handler, NULL);
fclose(fout); //关闭文件output.txt
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];
mac_header *mh;
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short mac_tos;
u_short sport,dport;
time_t local_tv_sec;
u_char ip_ver;
char ip_ds[16];
u_short ip_tlen;
u_short ip_ident;
u_short ip_flags_fo;
u_char ip_flags;
u_short ip_fo;
char ip_crc[24];
u_short udp_len;
char udp_crc[24];
u_char *udp_data;
int udp_o;
tcp_header *th;
u_short tcp_sport, tcp_dport;
u_int tcp_seqnum;
u_int tcp_acknum;
u_short tcp_do_res_flags;
u_short tcp_wins;
char tcp_crc[24];
u_short tcp_urgp;
u_int tcp_len;
u_short tcp_urg;
u_short tcp_ack;
u_short tcp_psh;
u_short tcp_rst;
u_short tcp_syn;
u_short tcp_fin;
char tcp_flags[16];
u_int tcp_dlen;
u_char *tcp_data;
int tcp_o;
icmp_header *ich;
arp_header *ah;
u_short arp_htype;
u_short arp_ptype;
u_short arp_oper;
/* 将时间戳转换成可识别的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
/* 打印数据包的时间戳和长度 */
fprintf(fout, "***************************\n");
fprintf(fout, "Packet:\n");
fprintf(fout, "Time Stamp: %s.%.6d\n", timestr, header->ts.tv_usec);
- 1
- 2
前往页