最近項目中需要對網絡數據包進行分析。這篇文章是在這裏轉的。
pcap文件格式
瞭解pcap格式可以加深對原始數據包的瞭解,自己也可以手工構造任意的數據包進行測試。
文件頭 24字節
數據包頭 + 數據包 數據包頭爲16字節,後面緊跟數據包
數據包頭 + 數據包 ......
struct pcap_file_header {
bpf_u_int32 magic;
u_short version_major;
u_short version_minor;
bpf_int32 thiszone; /* gmt to local correction */
bpf_u_int32 sigfigs; /* accuracy of timestamps */
bpf_u_int32 snaplen; /* max length saved portion of each pkt */
bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
};
magic: 4字節 pcap文件標識 目前爲“d4 c3 b2 a1”
major: 2字節 主版本號 #define PCAP_VERSION_MAJOR 2
minor: 2字節 次版本號 #define PCAP_VERSION_MINOR 4
thiszone:4字節 時區修正 並未使用,目前全爲0
sigfigs: 4字節 精確時間戳 並未使用,目前全爲0
snaplen: 4字節 抓包最大長度 如果要抓全,設爲0x0000ffff(65535),
tcpdump -s 0就是設置這個參數,缺省爲68字節
linktype:4字節 鏈路類型 一般都是1:ethernet
| d4 c3 b2 a1 | 02 00 | 04 00 | 00 00 00 00 | 00 00 00 00 | ff ff 00 00 | 01 00 00 00 |
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
struct timeval {
long tv_sec; /* seconds (XXX should be time_t) */
suseconds_t tv_usec; /* and microseconds */
};
ts: 8字節 抓包時間 4字節表示秒數,4字節表示微秒數
caplen:4字節 保存下來的包長度(最多是snaplen,比如68字節)
len: 4字節 數據包的真實長度,如果文件中保存的不是完整數據包,可能比caplen大