乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       leon0821 2013-11-23

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

       

      一:抓包分析: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)格式為:

      第三個(gè)參數(shù)協(xié)議若是指定,那么該套接字只能接受符合指定協(xié)議的數(shù)據(jù)包:IPPROTO_TCP接收采用tcp傳輸?shù)臄?shù)據(jù)包,IPPROTO_UDP接受采用udp傳輸?shù)臄?shù)據(jù)包,這一項(xiàng)決定了接受到的數(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ā)往本機(jī)的數(shù)據(jù),不能接受從本機(jī)發(fā)出去的數(shù)據(jù),要抓取發(fā)送出去的數(shù)據(jù),需要采用以下的方法

      而且,我們抓取的數(shù)據(jù)只是數(shù)據(jù)的拷貝,不會(huì)說(shuō)阻礙數(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)幀,其中第二個(gè)參數(shù)有2種,若為SOCK_RAW,那么抓取到的數(shù)據(jù)沒(méi)有經(jīng)過(guò)系統(tǒng)處理,完整的保留有以太網(wǎng)幀頭,若為SOCK_DGRAM那么以太網(wǎng)幀頭會(huì)被自動(dòng)處理掉。
      第三個(gè)參數(shù)的協(xié)議類(lèi)型有:
      ETH_P_IP 0x0800 只接收發(fā)往本機(jī)mac的ip類(lèi)型的數(shù)據(jù)幀
      ETH_P_ARP 0x0806 只接受發(fā)往本機(jī)mac的arp類(lèi)型的數(shù)據(jù)幀
      ETH_P_RARP 0x8035 只接受發(fā)往本機(jī)mac的rarp類(lèi)型的數(shù)據(jù)幀
      ETH_P_ALL 0x0003 接收發(fā)往本機(jī)mac的所有類(lèi)型ip arp rarp的數(shù)據(jù)幀, 接收從本機(jī)發(fā)出的所有類(lèi)型的數(shù)據(jù)幀.(混雜模式打開(kāi)的情況下,會(huì)接收到非發(fā)往本地mac的數(shù)據(jù)幀)
      如果我們要利用這個(gè)套接字發(fā)送數(shù)據(jù),那么發(fā)送的時(shí)候需要自己組織整個(gè)以太網(wǎng)數(shù)據(jù)幀.所有相關(guān)的地址使用struct sockaddr_ll 而不是struct sockaddr_in(因?yàn)閰f(xié)議簇是PF_PACKET不是AF_INET了),比如發(fā)送給某個(gè)機(jī)器,對(duì)方的地址需要使用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),都有在標(biāo)準(zhǔn)的頭文件給于定義,不需要自己去寫(xiě),因?yàn)樘L(zhǎng)的緣故,需要的自己去搜下,很容易找到。

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

      當(dāng)然有原始套接字的創(chuàng)建的程序執(zhí)行必須有root權(quán)限或者在程序中setuid(0)
      自己的寫(xiě)的一個(gè)抓包程序:(在本例子中是從IP層抓取,依據(jù)注釋的代碼稍作修改就可以從數(shù)據(jù)鏈路層抓取以太網(wǎng)幀,同時(shí)只要修改對(duì)應(yīng)的注釋的處理代碼,就可以處理這個(gè)數(shù)據(jù)了,在本實(shí)現(xiàn)中僅統(tǒng)計(jì)顯示接受到的數(shù)據(jù)包的傳輸協(xié)議,來(lái)源IP,端口,目標(biāo)IP,端口等一些簡(jiǎn)單信息)
      執(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ù)鏈路曾抓取的,那么就有這個(gè)以太網(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;

      }


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

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多