免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

項目需求,需要從鏈路層抓包,分析實現(xiàn)網絡登錄認證功能,現(xiàn)在網上找到兩個不錯的抓包程序,參考此文章,順利完成任務,現(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ù)結構格式為:

第三個參數(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ù)鏈路層直接抓取以太網幀,其中第二個參數(shù)有2種,若為SOCK_RAW,那么抓取到的數(shù)據(jù)沒有經過系統(tǒng)處理,完整的保留有以太網幀頭,若為SOCK_DGRAM那么以太網幀頭會被自動處理掉。
第三個參數(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ā)送的時候需要自己組織整個以太網數(shù)據(jù)幀.所有相關的地址使用struct sockaddr_ll 而不是struct sockaddr_in(因為協(xié)議簇是PF_PACKET不是AF_INET了),比如發(fā)送給某個機器,對方的地址需要使用struct sockaddr_ll.
接收到的完整的以太網幀數(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為以太網幀頭
關于上述的數(shù)據(jù)結構,都有在標準的頭文件給于定義,不需要自己去寫,因為太長的緣故,需要的自己去搜下,很容易找到。

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

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

//    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)//設置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)//設置混亂模式
    {
        perror("set 'eth0' to promisc model failed/n");
        exit(1);
    }
    printf("set '%s' to promisc successed/n",enterface);
    return 1;

}


本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
自己動手寫網絡抓包工具
linux sock_raw原始套接字編程
Linux下getsockopt/setsockopt 函數(shù)說明 - xioahw的專欄 ...
linux sock
IP數(shù)據(jù)包長度問題總結 (MTU MSS)
【轉】使用dev_queue_xmit發(fā)送定制報文
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服