前面对网络分析抓包的一些知识进行了学习,现在对这一阶段的最后成果进行展示,下面是Linux中,利用libpcap实现抓包操作,以及分析:
#include "pcap.h"
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void ip_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void tcp_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void udp_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
unsigned int persist_ssl_hashKeyConvert(char *pUserInput, unsigned char *pKeyArray);
//以太网协议头
struct ether_header
{
u_int8_t ether_dhost[6]; //目的Mac地址
u_int8_t ether_shost[6]; //源Mac地址
u_int16_t ether_type; //协议类型
};
//IPv4协议头
struct ip_header
{
#if defined(WORDS_BIENDIAN)
u_int8_t ip_version:4,ip_header_length:4;
#else
u_int8_t ip_header_length:4,ip_version:4;
#endif
u_int8_t ip_tos;
u_int16_t ip_length;
u_int16_t ip_id;
u_int16_t ip_off;
u_int8_t ip_ttl;
u_int8_t ip_protocol;
u_int16_t ip_checksum;
struct in_addr ip_souce_address;
struct in_addr ip_destination_address;
};
//UDP协议头
struct udphdr
{
u_int16_t source_port; /*源地址端口*/
u_int16_t dest_port; /*目的地址端口*/
u_int16_t len; /*UDP长度*/
u_int16_t check; /*UDP校验和*/
};
//TCP协议头
#define __LITTLE_ENDIAN_BITFIELD
struct tcphdr
{
u_int16_t source_port; /*源地址端口*/
u_int16_t dest_port; /*目的地址端口*/
u_int32_t seq; /*序列号*/
u_int32_t ack_seq; /*确认序列号*/
#if defined(__LITTLE_ENDIAN_BITFIELD)
u_int16_t res1:4, /*保留*/
doff:4, /*偏移*/
fin:1, /*关闭连接标志*/
syn:1, /*请求连接标志*/
rst:1, /*重置连接标志*/
psh:1, /*接收方尽快将数据放到应用层标志*/
ack:1, /*确认序号标志*/
urg:1, /*紧急指针标志*/
ece:1, /*拥塞标志位*/
cwr:1; /*拥塞标志位*/
#elif defined(__BIG_ENDIAN_BITFIELD)
u_int16_t doff:4, /*偏移*/
res1:4, /*保留*/
cwr:1, /*拥塞标志位*/
ece:1, /*拥塞标志位*/
urg:1, /*紧急指针标志*/
ack:1, /*确认序号标志*/
psh:1,