本文的目的是通過隨機截取的一段網(wǎng)絡(luò)
數(shù)據(jù)包 ,然后根據(jù)協(xié)議類型來解析出這段內(nèi)存。學(xué)習(xí)本文需要掌握的基礎(chǔ)知識:
網(wǎng)絡(luò)協(xié)議 C語言 Linux操作 抓包工具的使用 通過抓包工具,隨機抓取一個tcp數(shù)據(jù)包科萊抓包工具解析出的數(shù)據(jù)包信息如下:數(shù)據(jù)包的內(nèi)存信息:數(shù)據(jù)信息可以直接拷貝出來:
二、用到的結(jié)構(gòu)體 下面,一口君就手把手教大家如何解析出這些數(shù)據(jù)包的信息。我們可以從Linux內(nèi)核中找到協(xié)議頭的定義
drivers\staging\rtl8188eu\include\if_ether.h?struct ?ethhdr ?{ ?unsigned ?char ?h_dest[ETH_ALEN];?/*?destination?eth?addr?*/ ?unsigned ?char ?h_source[ETH_ALEN];?/*?source?ether?addr?*/ ?unsigned ?short?h_proto;??/*?packet?type?ID?field?*/ };?include\uapi\linux\ip.h?struct ?iphdr ?{#if ?defined(__LITTLE_ENDIAN_BITFIELD)??//小端模式 ?__u8?ihl:4 , ??version:4 ;#elif ?defined(__BIG_ENDIAN_BITFIELD)????//大端模式 ?__u8?version:4 , ??ihl:4 ;#endif ?__u8?tos; ?__u16?tot_len; ?__u16?id; ?__u16?frag_off; ?__u8?ttl; ?__u8?protocol; ?__u16?check; ?__u32?saddr; ?__u32?daddr; ?/*The?options?start?here.?*/ };tcp頭
include\uapi\linux\tcp.hstruct ?tcphdr ?{ ?__be16?source; ?__be16?dest; ?__be32?seq; ?__be32?ack_seq;#if ?defined(__LITTLE_ENDIAN_BITFIELD) ?__u16?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) ?__u16?doff:4 , ??res1:4 , ??cwr:1 , ??ece:1 , ??urg:1 , ??ack:1 , ??psh:1 , ??rst:1 , ??syn:1 , ??fin:1 ;#else #error ?"Adjust?your??defines" #endif ? ?__be16?window; ?__sum16?check; ?__be16?urg_ptr; };因為協(xié)議頭長度都是按照標(biāo)準(zhǔn)協(xié)議來定義的,所以以太長度是14, IP頭長度是20, tcp頭長度是20,各個協(xié)議頭對應(yīng)的內(nèi)存空間如下:
三、解析以太頭 #define ?MAC_ARG(p)?p[0],p[1],p[2],p[3],p[4],p[5] ?struct ?ethhdr ?*ethh ; ?unsigned ?char ?*p?=?pkt; ? ?ethh?=?(struct?ethhdr?*)p; ?printf ("h_dest:x:x:x:x:x:x?\n" ,?MAC_ARG(ethh->h_dest)); ?printf ("h_source:x:x:x:x:x:x?\n" ,?MAC_ARG(ethh->h_source)); ?printf ("h_proto:x\n" ,ntohs(ethh->h_proto));注意,數(shù)據(jù)包 中的數(shù)據(jù)是網(wǎng)絡(luò)字節(jié)序,如果要提取數(shù)據(jù)一定要注意字節(jié)序問題 ethh->h_proto 是short類型,占2個字節(jié),所以存儲到本地需要使用函數(shù)ntohs 其中:n:network 網(wǎng)絡(luò)字節(jié)序 h:host ? ? ? 主機字節(jié)序 s:short ? ? 2個字節(jié) l:long ? ? ? 4個字節(jié) ntohl() ?:4字節(jié)網(wǎng)絡(luò)字節(jié)序數(shù)據(jù)轉(zhuǎn)換成主機字節(jié)序 htons() :2字節(jié)主機字節(jié)序數(shù)據(jù)轉(zhuǎn)換成網(wǎng)絡(luò)字節(jié)序 ntohs() :2字節(jié)網(wǎng)絡(luò)字節(jié)序數(shù)據(jù)轉(zhuǎn)換成主機字節(jié)序 htonl() :4字節(jié)主機字節(jié)序數(shù)據(jù)轉(zhuǎn)換成網(wǎng)絡(luò)字節(jié)序 當(dāng)執(zhí)行下面這條語句時,
ethh?=?(struct?ethhdr?*)p;結(jié)構(gòu)體指針變量
eth 的成員對應(yīng)關(guān)系如下:最終打印結(jié)果如下:
四、解析ip頭 解析ip頭思路很簡單,就是從pkt頭開始偏移過以太頭長度(14字節(jié))就可以找到IP頭,解析代碼如下:
#define ?IP_ARG(p)??p[0],p[1],p[2],p[3] ?/* ??解析IP頭 ?*/ ?if (ntohs(ethh->h_proto)?==?0x0800 ) ?{ ? ??iph?=?(struct?iphdr?*)(p? ?sizeof (struct?ethhdr)); ??q?=?(unsigned ?char ?*)