日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

利用原始套接字抓取數(shù)據(jù)

 leon0821 2013-11-23

項目需求,需要從鏈路層抓包,分析實現(xiàn)網(wǎng)絡(luò)登錄認證功能,現(xiàn)在網(wǎng)上找到兩個不錯的抓包程序,參考此文章,順利完成任務(wù),現(xiàn)將此文章收藏與此,便參考,同時感謝文章版主,謝謝!

 

一:抓包分析:http://blog.csdn.net/aaa6695798/archive/2009/03/20/4008322.aspx

 

二:原始套接字抓包分析

 

原始套接字的創(chuàng)建

方法1: socket(PF_INET,SOCK_RAW,IPPROTO_TCP|IPPROTO_UDP)
采用這樣的方法創(chuàng)建的套接字,是在IP層接收的數(shù)據(jù)
數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)格式為:

第三個參數(shù)協(xié)議若是指定,那么該套接字只能接受符合指定協(xié)議的數(shù)據(jù)包:IPPROTO_TCP接收采用tcp傳輸?shù)臄?shù)據(jù)包,IPPROTO_UDP接受采用udp傳輸?shù)臄?shù)據(jù)包,這一項決定了接受到的數(shù)據(jù)包中的IP數(shù)據(jù)包頭struct iphdr中的protocol值,為IPPROTO_TCP或者IPPROTO_UDP.

struct iphdr+struct tcphdr+數(shù)據(jù)//若struct iphdr中的protocol值為IPPROTO_TCP
struct iphdr+struct udphdr+數(shù)據(jù)//若struct iphdr中的protocol值為IPPROTO_UDP
采用這樣的方法接收到的數(shù)據(jù)都是發(fā)往本機的數(shù)據(jù),不能接受從本機發(fā)出去的數(shù)據(jù),要抓取發(fā)送出去的數(shù)據(jù),需要采用以下的方法

而且,我們抓取的數(shù)據(jù)只是數(shù)據(jù)的拷貝,不會說阻礙數(shù)據(jù)的傳送,把數(shù)據(jù)給攔截了

方法2:socket(PF_PACKET,SOCK_RAW, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ARP|ETH|RARP))
      socket(PF_PACKET,SOCK_DGRAM, htons(ETH_P_IP|ETH_P_ARP|ETH_P_ARP|ETH|RARP))
這種方法創(chuàng)建的套接字是在數(shù)據(jù)鏈路層直接抓取以太網(wǎng)幀,其中第二個參數(shù)有2種,若為SOCK_RAW,那么抓取到的數(shù)據(jù)沒有經(jīng)過系統(tǒng)處理,完整的保留有以太網(wǎng)幀頭,若為SOCK_DGRAM那么以太網(wǎng)幀頭會被自動處理掉。
第三個參數(shù)的協(xié)議類型有:
ETH_P_IP 0x0800 只接收發(fā)往本機mac的ip類型的數(shù)據(jù)幀
ETH_P_ARP 0x0806 只接受發(fā)往本機mac的arp類型的數(shù)據(jù)幀
ETH_P_RARP 0x8035 只接受發(fā)往本機mac的rarp類型的數(shù)據(jù)幀
ETH_P_ALL 0x0003 接收發(fā)往本機mac的所有類型ip arp rarp的數(shù)據(jù)幀, 接收從本機發(fā)出的所有類型的數(shù)據(jù)幀.(混雜模式打開的情況下,會接收到非發(fā)往本地mac的數(shù)據(jù)幀)
如果我們要利用這個套接字發(fā)送數(shù)據(jù),那么發(fā)送的時候需要自己組織整個以太網(wǎng)數(shù)據(jù)幀.所有相關(guān)的地址使用struct sockaddr_ll 而不是struct sockaddr_in(因為協(xié)議簇是PF_PACKET不是AF_INET了),比如發(fā)送給某個機器,對方的地址需要使用struct sockaddr_ll.
接收到的完整的以太網(wǎng)幀數(shù)據(jù)格式為:
ETH_P_IP:struct ether_header +struct iphdr+....后面為udphdr或者tcphdr,由iphdr中的protocol決定
ETH_P_ARP:struct ether_header+ARP數(shù)據(jù)包
等等
其中struct ether_header為以太網(wǎng)幀頭
關(guān)于上述的數(shù)據(jù)結(jié)構(gòu),都有在標準的頭文件給于定義,不需要自己去寫,因為太長的緣故,需要的自己去搜下,很容易找到。

關(guān)于網(wǎng)卡的混亂模式
正常的網(wǎng)卡只會接受mac地址為本機的以太網(wǎng),當我們設(shè)置它為混亂模式的話,那么它就會接受所有經(jīng)過它的以太網(wǎng)數(shù)據(jù)幀,具體的設(shè)置方法可以用函數(shù)ioctl函數(shù)實現(xiàn)
,可以參考下面的代碼.

當然有原始套接字的創(chuàng)建的程序執(zhí)行必須有root權(quán)限或者在程序中setuid(0)
自己的寫的一個抓包程序:(在本例子中是從IP層抓取,依據(jù)注釋的代碼稍作修改就可以從數(shù)據(jù)鏈路層抓取以太網(wǎng)幀,同時只要修改對應(yīng)的注釋的處理代碼,就可以處理這個數(shù)據(jù)了,在本實現(xiàn)中僅統(tǒng)計顯示接受到的數(shù)據(jù)包的傳輸協(xié)議,來源IP,端口,目標IP,端口等一些簡單信息)
執(zhí)行方式(ubuntu下):
gcc -o sniffer sniffer.c
sudo ./sniffer eth0  
要指定網(wǎng)口eth0

#include <stdio.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <string.h>
#include <net/if.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
int analyData(char *data);
int rawSocket();
int setPromisc(char *,int *);
int count=0;
int main(int argc,char **argv)
{
    if(argc!=2)
    {
        perror("please enter the ecterface");
        exit(1);
    }
    int sock;
    int msgsock;
    struct sockaddr_in rcvaddr;
    char buf[9216];
    struct ifreq ifr;
    int len;
    int rval;
    sock=rawSocket();
    setPromisc(argv[1],&sock);
    len=sizeof(struct sockaddr);
    memset(buf,0,sizeof(buf));
    while(1)
    {
        rval=recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&len);
        if(rval>0)
        {
            printf("Get %d bytes/n",rval);
            analyData(buf);

                }

    }
    return 0;


}
int analyData(char *data)
{
    struct iphdr *ip;
    struct tcphdr *tcp;
     struct ether_header *ether;
//    ether=(struct ether_header*)data;//若數(shù)據(jù)是從數(shù)據(jù)鏈路曾抓取的,那么就有這個以太網(wǎng)幀頭

//    printf("shu ju bao lei xing xie yi:%d/n",ether->ether_type);

//    ip=(struct iphdr*)(data+sizeof(struct ether_header));

    ip=(struct iphdr*)data;
    count++;
    printf("Protocol::%d/n",ip->protocol);
    printf("Source IP::%s/n",inet_ntoa(*((struct in_addr*)&ip->saddr)));
    printf("Dest IP::%s/n",inet_ntoa(*((struct in_addr*)&ip->daddr)));
    tcp=(struct tcphdr*)(data+sizeof(*ip));
    printf("Source Port::%d/n",ntohs(tcp->source));
    printf("Dest Port::%d/n",ntohs(tcp->dest));
    printf("Already get %d package/n",count);
    printf("/n");
    return 1;
}

int rawSocket()//創(chuàng)建原始套接字
{
    int sock;
    sock=socket(PF_INET,SOCK_RAW,IPPROTO_TCP);//IP層抓取

   //soket=socket(PF_PACKET,SOCK_RAW,ETH_P_IP)//數(shù)據(jù)鏈路層抓取
    if(sock<0)
    {
        printf("create raw socket failed::%s/n",strerror(errno));
        exit(1);
    }
    
    printf("raw socket ::%d created successful/n",sock);
    return sock;
}
int setPromisc(char *enterface,int *sock)//設(shè)置eth0的混亂模式
{
    struct ifreq ifr;
    strcpy(ifr.ifr_name,"eth0");
    ifr.ifr_flags=IFF_UP|IFF_PROMISC|IFF_BROADCAST|IFF_RUNNING;
    if(ioctl(*sock,SIOCSIFFLAGS,&ifr)==-1)//設(shè)置混亂模式
    {
        perror("set 'eth0' to promisc model failed/n");
        exit(1);
    }
    printf("set '%s' to promisc successed/n",enterface);
    return 1;

}


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多