原始通訊端簡介(原始通訊端系列一)

來源:互聯網
上載者:User

標籤:des   style   class   code   http   tar   

大多數程式員所接觸到的通訊端(Socket)為兩類:

  (1)流式通訊端(SOCK_STREAM):一種連線導向的Socket,針對於連線導向的TCP服務應用;

  (2)資料報式通訊端(SOCK_DGRAM):一種不需連線的Socket,對應於不需連線的UDP服務應用。

  從使用者的角度來看,SOCK_STREAM、SOCK_DGRAM這兩類通訊端似乎的確涵蓋了TCP/IP應用的全部,因為基於TCP/IP的應用,從協議棧的層次上講,在傳輸層的確只可能建立於TCP或UDP協議之上(圖1),而SOCK_STREAM、SOCK_DGRAM又分別對應於TCP和UDP,所以幾乎所有的應用都可以用這兩類通訊端實現。


圖1 TCP/IP協議棧


  但是,當我們面對如下問題時,SOCK_STREAM、SOCK_DGRAM將顯得這樣無助:

  (1) 怎樣發送一個自訂的IP包?

  (2) 怎樣發送一個ICMP協議包?

  (3) 怎樣使本機進入雜糅模式,從而能夠進行網路sniffer?

  (4) 怎樣分析所有經過網路的包,而不管這樣包是否是發給自己的?

  (5) 怎樣偽裝本地的IP地址?

  這使得我們必須面對另外一個深刻的主題――原始通訊端(Raw Socket)。Raw Socket廣泛應用於進階網路編程,也是一種廣泛的駭客手段。著名的網路sniffer、拒絕服務的攻擊(DOS)、IP欺騙等都可以以Raw Socket實現。

  Raw Socket與標準通訊端(SOCK_STREAM、SOCK_DGRAM)的區別在於前者直接置"根"於作業系統網路核心(Network Core),而SOCK_STREAM、SOCK_DGRAM則"懸浮"於TCP和UDP協議的外圍,2所示:


圖2 Raw Socket與標準Socket


  當我們使用Raw Socket的時候,可以完全自訂IP包,一切形式的包都可以"製造"出來。因此,本文事先必須對TCP/IP所涉及IP包結構進行必要的交待。

  目前,IPv4的前序結構為:

版本號碼(4) 包頭長(4) 服務類型(8) 資料包長度(16)
標識(16) 位移量(16)
存留時間(8) 傳輸協議(8) 校正和(16)
源地址(32)  
目的地址(32)  
選項(8) ......... 填充


  對其進行資料結構封裝:

typedef struct _iphdr //定義IP前序 

 unsigned char h_lenver; //4位首部長度+4位IP版本號碼 
 unsigned char tos; //8位服務類型TOS 
 unsigned short total_len; //16位總長度(位元組) 
 unsigned short ident; //16位標識 
 unsigned short frag_and_flags; //3位標誌位 
 unsigned char ttl; //8位存留時間 TTL 
 unsigned char proto; //8位協議 (TCP, UDP 或其他) 
 unsigned short checksum; //16位IP首部校正和 
 unsigned int sourceIP; //32位源IP地址 
 unsigned int destIP; //32位目的IP地址 
} IP_HEADER;


  或者將上述定義中的第一位元組按位拆分:

typedef struct _iphdr //定義IP前序 

 unsigned char h_len : 4; //4位首部長度
 unsigned char ver : 4; //4位IP版本號碼 
 unsigned char tos; 
 unsigned short total_len; 
 unsigned short ident; 
 unsigned short frag_and_flags; 
 unsigned char ttl; 
 unsigned char proto; 
 unsigned short checksum; 
 unsigned int sourceIP; 
 unsigned int destIP; 
} IP_HEADER;


  更加嚴格地講,上述定義中h_len、ver欄位的記憶體存放順序還與具體CPU的Endian有關,因此,更加嚴格的IP_HEADER可定義為:

typedef struct _iphdr //定義IP前序 

 #if defined(__LITTLE_ENDIAN_BITFIELD)
  unsigned char h_len : 4; //4位首部長度
  unsigned char ver : 4; //4位IP版本號碼 
 #elif defined (__BIG_ENDIAN_BITFIELD)
  unsigned char ver : 4; //4位IP版本號碼 
  unsigned char h_len : 4; //4位首部長度
 #endif
 unsigned char tos; 
 unsigned short total_len; 
 unsigned short ident; 
 unsigned short frag_and_flags; 
 unsigned char ttl; 
 unsigned char proto; 
 unsigned short checksum; 
 unsigned int sourceIP; 
 unsigned int destIP; 
} IP_HEADER;


  TCP前序結構為:

源連接埠(16) 目的連接埠(16)
序號(32)
確認號(32)
TCP位移量(4) 保留(6) 標誌(6) 視窗(16)
校正和(16) 緊急(16)
選項(0或32)
資料(可變)


  對應資料結構:

typedef struct psd_hdr //定義TCP偽前序 

 unsigned long saddr; //源地址 
 unsigned long daddr; //目的地址 
 char mbz; 
 char ptcl; //協議類型 
 unsigned short tcpl; //TCP長度 
}PSD_HEADER; 
typedef struct _tcphdr //定義TCP前序 

 unsigned short th_sport; //16位源連接埠 
 unsigned short th_dport; //16位目的連接埠 
 unsigned int th_seq; //32位序號 
 unsigned int th_ack; //32位確認號 
 unsigned char th_lenres; //4位首部長度/4位保留字 
 unsigned char th_flag; //6位標誌位 
 unsigned short th_win; //16位視窗大小 
 unsigned short th_sum; //16位校正和 
 unsigned short th_urp; //16位緊急資料位移量 
} TCP_HEADER;


  同樣地,TCP頭的定義也可以將位域拆分:

typedef struct _tcphdr
{
 unsigned short th_sport; 
 unsigned short th_dport; 
 unsigned int th_seq; 
 unsigned int th_ack; 
 /*little-endian*/
 unsigned short tcp_res1: 4, tcp_hlen: 4, tcp_fin: 1, tcp_syn: 1, tcp_rst: 1, tcp_psh: 1, tcp_ack: 1, tcp_urg: 1, tcp_res2: 2;
 unsigned short th_win; 
 unsigned short th_sum; 
 unsigned short th_urp; 
} TCP_HEADER;


  UDP前序為:

源連接埠(16) 目的連接埠(16)
報文長(16) 校正和(16)


  對應的資料結構為:

typedef struct _udphdr //定義UDP前序 
{
 unsigned short uh_sport;//16位源連接埠
 unsigned short uh_dport;//16位目的連接埠
 unsigned short uh_len;//16位長度
 unsigned short uh_sum;//16位校正和
} UDP_HEADER;


  ICMP協議是網路層中一個非常重要的協議,其全稱為Internet Control Message Protocol(網際網路控制報文協議),ICMP協議彌補了IP的缺限,它使用IP協議進行資訊傳遞,向資料包中的源端節點提供發生在網路層的錯誤資訊反饋。ICMP前序為:

類型(8) 代碼(8) 校正和(16)
訊息內容


  常用的回送與或回送響應ICMP訊息對應資料結構為:

typedef struct _icmphdr //定義ICMP前序(回送與或回送響應)

 unsigned char i_type;//8位類型
 unsigned char i_code; //8位代碼 
 unsigned short i_cksum; //16位校正和 
 unsigned short i_id; //識別號(一般用進程號作為識別號) 
 unsigned short i_seq; //報文序號 
 unsigned int timestamp;//時間戳記 
} ICMP_HEADER;


  常用的ICMP報文包括ECHO-REQUEST(響應請求訊息)、ECHO-REPLY(響應應答訊息)、Destination Unreachable(目標不可到達訊息)、Time Exceeded(逾時訊息)、Parameter Problems(參數錯誤訊息)、Source Quenchs(源抑制訊息)、Redirects(重新導向訊息)、Timestamps(時間戳記訊息)、Timestamp Replies(時間戳記響應訊息)、Address Masks(位址遮罩請求訊息)、Address Mask Replies(位址遮罩響應訊息)等,是Internet上十分重要的訊息。後面章節中所涉及到的ping命令、ICMP拒絕服務的攻擊、路由欺騙都與ICMP協議息息相關。

  另外,本系列文章中的部分原始碼參考了一些優秀程式員的開源項目,由於篇幅的關係我們不能一一列舉,在此一併表示感謝。

  So, let‘s go.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.