來源:IsBlog.cn 以太網上數據幀的監聽剖析
以太網上的數據幀主要涉及Tcp/ip協議,針對以下幾個協議的分析:IP,ARP,RARP,IPX,其中重點在于ip和 arp協議,這兩個協議是多數網絡協議的基礎,因此把他們研究徹底,就對大多數的協議的原理和特性比較清楚了。 由于各種協議的數據幀個不相同,所以涉及很多的數據幀頭格式分析,接下來將一一描述。
在linux 下監聽網絡,應先設置網卡狀態,使其處于雜混模式以便監聽網絡上的所有數據幀。然后選擇用Linux socket 來截取數據幀,通過設置socket() 函數參數值,可以使socket截取未處理的網絡數據幀,關鍵是函數的參數設置,下面就是有關的程序部分:
if ( ( fd=socket (AF_INET, SOCK_PACKET,htons(0x0003)))<0)
{perror (“can get SOCK_PACKET socket
”);
exit(0);
}
??AF_INET=2 表示 internet ip protocol
??SOCK_PACKET=10 表示 截取數據幀的層次在物理層,既不作處理。
??Htons(0x0003)表示 截取的數據幀的類型為不確定,既接受所有的包。
總的設定就是網卡上截取所有的數據幀。這樣就可以截取底層數據幀,因為返回的將是一個指向數據的指針,為了分析方便,我設置了一個基本的數據幀頭結構。
Struct etherpacket
{struct ethhdr eth;
struct iphdr ip;
struct tcphdr tcp;
char buff[8192];
} ep;
將返回的指針賦值給指向數據幀頭結構的指針,然后對其進行分析。以下是有關協議的報頭:ethhdr 這是以太網數據幀的mac報頭:
--------------------------------------------------------
|48bit 目的物理地址 | 48bit 源物理地址 | 16bit 協議地址|
--------------------------------------------------------
相應的數據結構如下
struct ethhdr
{
unsigned char h_dest[ETH_ALEN];
unsigned char h_source[ETH_ALEN];
unsigned short h_proto;
}
其中h_dest[6]是48位的目標地址的網卡物理地址,h_source [6] 是48位的源地址的物理網卡地址。H_proto是16位的以太網協議,其中主要有0x0800 ip,0x8035.X25,0x8137 ipx,0x8863-0x8864 pppoe(這是Linux的 ppp),0x0600 ether _loop_back ,0x0200-0x0201 pup等。Iphdr 這是ip協議的報頭:
由此可以定義其結構如下:
struct iphdr
{
#elif defined (_LITTLE_ENDIAN_BITFIELD)
_u8 version :4,
#elif defined (_BIG_ENDIAN_BITFIELD)
_u8 version:4,
ihl:4;
#else
#error "Please fix"
#endif
_u8 tos;
_16 tot_len;
_u16 id;
_u16 frag_off;
_u8 ttl;
_u8 protocol;
_u16 check;
_u32 saddr;
_u32 daddr;
};
這是Linux 的ip協議報頭,針對版本的不同它可以有不同的定義,我們國內一般用BIG的定義,其中version 是ip的版本,protocol是ip的協議分類主要有0x06 tcp.0x11 udp,0x01 icmp,0x02 igmp等,saddr是32位的源ip地址,daddr是32位的目標ip地址。
相應的數據結構:
struct arphdr
{
unsigned short int ar_hrd;
unsigned short int ar_pro;
unsigned char ar_hln;
unsigned char ar_pln;
unsigned short int ar_op;
#if 0
unsigned char _ar_sha[ETH_ALEN];
unsigned char _ar_sip[4];
unsigned char _ar_tha[ETH_ALEN];
unsigned char _ar_tip[4];
#end if
};
這是linux 的arp 協議報頭,其中ar_hrd 是硬件地址的格式,ar_pro協議地址的格式,ar_hln是硬件地址的長度,ar_pln時協議地址的長度,ar_op是arp協議的分類0x001是arp echo 0x0002 是 arp reply.接下來的分別是源地址的物理地址,源ip地址,目標地址的物理地址,目標ip地址。
Tcphdr ip協議的tcp協議報頭
以下是相應數據結構:
struct tcphdr
{
u_int16_t source;
u_int16_t dest;
u_int32_t seq;
u_int32_t ack_seq;
# if _BYTE_ORDER == _LITTLE _ENDIAN
u_int16_t resl:4;
u_int16_t doff:4;
u_int16_t fin:1;
u_int16_t syn:1;
u_int16_t rst:1;
u_int16_t psh:1;
u_int16_t ack:1;
u_int16_t urg:1;
u_int16_t res2:2;
#elif _BYTE _ORDER == _BIG _ENDIAN
u_int16_t doff:4;
u_int16_t res1:4;
u_int16_t res2:2;
u_int16_t urg:1;
u_int16_t ack:1;
u_int16_t psh:1;
u_int16_t rst:1;
u_int16_t syn:1;
u_int16_t fin:1;
#else
#error "Adjust your defines"
#endif
u_int16_t window;
u_int16_t check;
u_int16_t urg_ptr;
};
這是Linux 下tcp協議的一部分與ip協議相同取BIG,其中source是源端口,dest 是目的端口,seq是s序,ack_seq是a序號,其余的是tcp的連接標志其中包括6個標志:syn表示連接請求,urg 表示緊急信息,fin表示連接結束,ack表示連接應答,psh表示推棧標志,rst表示中斷連接。window是表示接受數據窗口大小,check是校驗碼,urg ptr是緊急指針。
Udphdr 這是udp協議報頭
struct udphdr {
u_int16_t source;
u_int16_t dest;
u_int16_t len;
u_int16_t check;
}
這是Linux下ip協議中udp協議的一部分,結構很明顯 source 源端口,dest目的端口,len udp 長度,check 是校驗碼。
Icmphdr 這是ip協議的icmp協議的報頭
struct icmphdr
{
u_int8_t type;
u_int8_t code;
u_int16_t checksum;
union
{
struct
{
u_int16_t id;
u_int16_t sequence;
} echo;
u_int32_t gateway;
struct
{
u_int16_t_unused;
u_int16_t mtu;
} frag;
} un;
};