天天看點

網絡安全開發系列--筆記(1)

網絡安全技術

1.資料包捕獲技術作業系統提供的資料包捕獲技術主要存在三種:

SOCK_PACKET類型套接口:利用作業系統提供的程式設計接口來實作

資料鍊路提供者接口DLPI:

伯克利資料包過濾(BPF):高效資料包捕獲技術,工作作業系統核心層

2.網絡協定分析三個内容

         捕獲資料包:使用專業的捕獲資料包的開發包。例如Libpcap和Winpcap

         過濾資料包:核心層進行資料包過濾;應用層進行資料包過濾。使用Libpcap因為使用了BPF過濾機制在核心層進行過濾效率表現的更高。

         協定分析:逐層進行分析。

3.資料包生成技術:代表Libnet,專業的網絡資料包生成開發包

4.網絡安全掃描技術:掃描、漏洞掃描、特殊掃描

5.防火牆技術

6.入侵檢測技術

網絡安全開發包種類

  1. 資料包捕獲開發包:Libpcap
  2. Windows平台專業資料包捕獲開發包WinPcap
  3. 網絡資料包構造和發送開發包Libnet
  4. 網絡入侵檢測開發包Libnids
  5. 通用網絡安全開發包libdnet

一、網絡資料包捕獲開發包Libpcap

         Libpcap官網:http://www.tcpdump.org/

linux下安裝Libpcap:apt-get install libpcap-dev

       下載下傳目前最新版本源代碼用于分析:wget http://www.tcpdump.org/release/libpcap-1.9.0.tar.gz(目前最新版本1.9.0)

         1.Libpcap的作用主要有三個方面:(1)捕獲各種資料包;(2)過濾網絡資料包;(3)分析網絡資料包;(4)存儲網絡資料包

         2.libpcap的構成:

                   (1)BPF捕獲機制:轉發和過濾兩部分構成

                   (2)過濾規則:兩類資料構成辨別和辨別的修飾詞(類型、方向、協定)

                   (3)網卡的設定:libpcap需要将網卡設定為混雜模式

         3.檔案

                   (1)pcap安裝之後頭檔案在:/usr/include/pcap目錄下在/usr/include下有個pcap.h檔案但是該檔案内容為#include<pcap/pcap.h>。

                            /usr/include/pcap下檔案結構如下:

網絡安全開發系列--筆記(1)

4.相關函數:在pcap.h中定義了libpcap的相關使用函數。其中包括接口函數、規則函數、資料包捕獲函數、檔案相關函數、錯誤處理函數、以及一些其他輔助函數。

Libpcap使用

  1. 簡單擷取接口資訊

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<pcap.h>

typedef u_int32_t in_addr_t;

int main(int argc,char **argv)

{

        char error_content[PCAP_ERRBUF_SIZE];

        struct in_addr net_ip_address;

        struct in_addr net_mask_address;

        char *net_interface;

        char *net_ip_string;

        char *net_mask_string;

        u_int32_t net_ip;

        u_int32_t net_mask;

        net_interNetwork Interface is:%s\n",net_interface);

        net_ip_address.s_addr=net_ip;

        net_ip_string=inet_ntoa(net_ip_address);

        printf("Network IP Address is:%s\n",net_ip_string);

        net_mask_address.s_addr=net_mask;

        net_mask_string=inet_ntoa(net_mask_address);

        printf("Network Mask Address is:%s\n",net_mask_string);

        return 0;

}

編譯和執行:
網絡安全開發系列--筆記(1)
網絡安全開發系列--筆記(1)

說明:在源代碼進行分析之前介紹一個插件工具cscope這個工具本來用于前期分析核心源代碼使用的。

cscope安裝:apt-getinstall cscope

在/home/Soft/libpcap-1.9.0目錄下使用如下指令:

cscope –Rbq

打開vim指令行輸入:cscope add cscope.out

然後查詢需要找的函數例如:cs find pcap_lookupdev就可以查詢相關函數的定義了。

本程式幾點需要注意:

(1)inet_ntoa()該函數需要三個頭檔案:<sys/socket.h>;<netinet/in.h>;<arpa/inet.h>

(2)編譯時-lpcap一定不能丢了。

值得注意的是,pcap_lookupdev傳回的是eth0的結果。這是因為該函數查找要捕獲的預設裝置,如果傳回結果不為空則傳回預設清單的第一個裝置。目前該函數已經被廢棄由pcap_findalldes函數代替。

pcap_lookupnet則用于擷取網絡接口資訊。

為了能擷取所有接口資訊,需要對該代碼進行改寫:

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<pcap.h>

typedef u_int32_t in_addr_t;

int main(int argc,char **argv)

{

        char error_content[PCAP_ERRBUF_SIZE];

        struct in_addr net_ip_address;

        struct in_addr net_mask_address;

        char *net_interface;

        char *net_ip_string;

        char *net_mask_string;

        u_int32_t net_ip;

        u_int32_t net_mask;

        pcap_if_t *alldevs;

        int ret;

        //net_intererror:%s\n",error_content);

                return -1;

        }

        while(alldevs)

        {

        printf("interface:%s\n",alldevs->name);

        alldevs=alldevs->next;

        }

        pcap_freealldevs(alldevs);

        return 0;

}

編譯和執行:
網絡安全開發系列--筆記(1)
加入共色部分代碼
網絡安全開發系列--筆記(1)

說明:

         本部分用pcap_findalldevs函數代替了pcap_lookupdev能夠輸出所有端口資訊(可以進一步擴充)。值得注意的是這裡使用了pcap_if_t.。

typedef struct pcap_if pcap_if_t;       
struct pcap_if {      
struct pcap_if *next;       
char *name;           
char *description;        
struct pcap_addr *addresses;       
u_int flags;              
};      
struct pcap_addr {      
struct pcap_addr *next;       
struct sockaddr *addr;            
struct sockaddr *netmask;         
struct sockaddr *broadaddr;       
struct sockaddr *dstaddr;         
};      

另外一點是使用了pcap_findalldevs函數後需要使用pcap_freealldevs函數進行釋放,否則可能出現段錯誤。

繼續閱讀