网络数据包抓取以及流量分析
- 格式:docx
- 大小:25.13 KB
- 文档页数:13
#include
#include
typedef struct macaddress{
u_char mac1;
u_char mac2;
u_char mac3;
u_char mac4;
u_char mac5;
u_char mac6;
};
typedef struct macheader{
macaddress dest;
macaddress src;
u_short type;
};
//IP地址32位,这里用4个字节来表示。
typedef struct ipaddress{
u_char by1;
u_char by2;
u_char by3;
u_char by4;
};
//IP报文格式
typedef struct ipbaowen{
u_char ver_ihl; //首部长度和版本号
u_char tos; //服务类型
u_short tlen; // 报文总长度
u_short ident; // 标识
u_short flags_fo; // 标志和片偏移
u_char ttl; // 生存时间
u_char proto; //协议类型
#define IP_ICMP 1
#define IP_IGMP 2
#define IP_TCP 6
#define IP_UDP 17
#define IP_IGRP 88
#define IP_OSPF 89
u_short crc;
ipaddress saddr;
ipaddress daddr; };
typedef struct tcpheader{
u_short sport; // 源端口
u_short dport; // 目的端口
u_int th_seq; // 序列号
u_int th_ack; // 确认号
u_char th_lenand; // 报文长度
u_char th_flags; //标志
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
u_short th_win; //窗口
u_short th_sum; //校验和
u_short th_urp; //紧急
};
// UDP格式
typedef struct udpheader{
u_short sport; // Source port 源端口
u_short dport; // Destination port 目的端口
u_short uh_len; // Datagram length 用户数据包长度
u_short uh_sum; // Checksum 校验和
};
typedef struct udpnode{
ipaddress saddr;
ipaddress daddr;
u_short sport;
u_short dport;
u_short length;
u_int upnum;
u_int downnum;
struct udpnode * next;
struct udpnode * pre;
};
typedef struct tcpnode{
ipaddress saddr;
ipaddress daddr;
u_short sport;
u_short dport; u_short length;
u_int upnum;
u_int downnum;
struct tcpnode * next;
struct tcpnode * pre;
};
#define tcphashtablelength 10
#define udphashtablelength 10
udpnode udphashtable[udphashtablelength];
tcpnode tcphashtable[tcphashtablelength];
void initudp()
{
for (int i = 0; i < udphashtablelength; i++)
{
udphashtable[i].pre = udphashtable + i;
udphashtable[i].next = NULL;
udphashtable[i].length = 0;
}
}
void inittcp()
{
for (int i = 0; i < tcphashtablelength; i++)
{
tcphashtable[i].pre = tcphashtable + i;
tcphashtable[i].next = NULL;
tcphashtable[i].length = 0;
}
}
int hash(int a, int b, int c, int d)
{
return (a % 2 + b % 3 + c % 4 + d % 5);
}
void packet_handler(u_char *param, const struct pcap_pkthdr * header, const u_char *pkt_data);
void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
void showudphashtable();
void showtcphashtable();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
FILE * PP; 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";
struct bpf_program fcode;
#define LINE_LEN 10
pcap_t *fp;
char errbuf2[PCAP_ERRBUF_SIZE];
char source[PCAP_BUF_SIZE];
initudp();//////////////////////////////////
inittcp();//////////////////////////////////
//获取设备列表;
if (pcap_findalldevs(&alldevs, errbuf) == -1)//
{
fprintf(stderr, "Errorinpcap_findalldevs:%s\n", errbuf);
exit(1);
}
//显示设备名及其描述
for (d = alldevs; d; d = d->next)
{
printf("%d. %s\n", ++i, d->name);//设备名
printf(" (%s)\n", d->description);//设备描述
}
printf("适配器总共有%d个\n", i);
if (i == 0)
{
printf("\nNointerfacesfound!MakesureWinPcapisinstalled.\n");
return -1;
}
//输入某个适配器;
printf("Enter the device number(1-%d):", i);
scanf_s("%d", &inum);
if (inum <= 0 || inum>i)
{
printf("\ndevice number out of range.\n");
pcap_freealldevs(alldevs);
return -1; }
//使d指向输入的那个;
for (d = alldevs, i = 0; i< inum - 1; d = d->next, i++);
//打开指定的适配器;
if ((adhandle = pcap_open_live(d->name, 65536, 1, 1000, errbuf)) == NULL)
{
fprintf(stderr, "\nUnabletoopentheadapter.%sisnotsupportedbyWinPcap\n");
pcap_freealldevs(alldevs); /*Freethedevicelist*/
return -1;
}
//检查链路层
if (pcap_datalink(adhandle) != DLT_EN10MB)
{
fprintf(stderr, "\nThisprogramworksonlyonEthernetnetworks.\n");
/*Freethedevicelist*/
pcap_freealldevs(alldevs);
return -1;
}
/* 获得接口第一个地址的掩码 */
if (d->addresses != NULL)//
netmask = ((struct sockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
netmask = 0xffffff;
//编译过滤器
if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask)<0){
fprintf(stderr, "\nUnable to compile the packet filter. Check the syntax.\n");
/*Freethedevicelist*/
pcap_freealldevs(alldevs);
return -1;
}
//设置过滤器
if (pcap_setfilter(adhandle, &fcode)<0){
fprintf(stderr, "\nErrorsettingthefilter.\n");
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on : %s...\n", d->description);
pcap_freealldevs(alldevs);
pcap_dumper_t * dumpfp;
dumpfp = pcap_dump_open(adhandle, "data");
char filename[] = "data";