+ -
当前位置:首页 → 问答吧 → winpcap 抓包问题, 为什么抓不到局域网内其他人的QQ包

winpcap 抓包问题, 为什么抓不到局域网内其他人的QQ包

时间:2010-06-25

来源:互联网

代码是从winpcap手册上COPY下来的, 网卡也设置成混杂模式了, 但是收包的结果显示 只能捕获到本机器的包 以及 其他机器发送的广播包,  按道理说应该能收到其他机器发送的UDP包啊,比如QQ, 下面是代码, 希望高手能解答下 万分感谢

#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <Winbase.h>

using namespace std;
#pragma comment(lib,"wpcap.lib")
#pragma comment(lib, "Ws2_32.lib")



#define HAVE_REMOTE

#include "pcap.h"
#include "remote-ext.h"

/* 4字节的IP地址 */
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
}ip_address;

/* IPv4 首部 */
typedef struct ip_header{
    u_char  ver_ihl;        // 版本 (4 bits) + 首部长度 (4 bits)
    u_char  tos;            // 服务类型(Type of service)
    u_short tlen;           // 总长(Total length)
    u_short identification; // 标识(Identification)
    u_short flags_fo;       // 标志位(Flags) (3 bits) + 段偏移量(Fragment offset) (13 bits)
    u_char  ttl;            // 存活时间(Time to live)
    u_char  proto;          // 协议(Protocol)
    u_short crc;            // 首部校验和(Header checksum)
    ip_address  saddr;      // 源地址(Source address)
    ip_address  daddr;      // 目的地址(Destination address)
    u_int   op_pad;         // 选项与填充(Option + Padding)
}ip_header;

/* UDP 首部*/
typedef struct udp_header{
    u_short sport;          // 源端口(Source port)
    u_short dport;          // 目的端口(Destination port)
    u_short len;            // UDP数据包长度(Datagram length)
    u_short crc;            // 校验和(Checksum)
}udp_header;

/* 回调函数原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);


main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;

    /* 获得设备列表 */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
   
    /* 打印列表 */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
   
    printf("Enter the interface number (1-%d):",i);
    scanf("%d", &inum);
   
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* 跳转到已选设备 */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
   
    /* 打开适配器 */
    if ( (adhandle= pcap_open(d->name,  // 设备名
                             65536,     // 要捕捉的数据包的部分
                                        // 65535保证能捕获到不同数据链路层上的每个数据包的全部内容
                             PCAP_OPENFLAG_PROMISCUOUS,         // 混杂模式
                             1000,      // 读取超时时间
                             NULL,      // 远程机器验证
                             errbuf     // 错误缓冲池
                             ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
   
    /* 检查数据链路层,为了简单,我们只考虑以太网 */
    if(pcap_datalink(adhandle) != DLT_EN10MB)
    {
        fprintf(stderr,"\nThis program works only on Ethernet networks.\n");
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
   
    if(d->addresses != NULL)
        /* 获得接口第一个地址的掩码 */
        netmask=((struct sockaddr_in *)(d->addresses->netmask))->sin_addr.S_un.S_addr;
    else
        /* 如果接口没有地址,那么我们假设一个C类的掩码 */
        netmask=0xffffff;


    //编译过滤器
    if (pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) <0 )
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
   
    //设置过滤器
    if (pcap_setfilter(adhandle, &fcode)<0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        /* 释放设备列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
   
    printf("\nlistening on %s...\n", d->description);
   
    /* 释放设备列表 */
    pcap_freealldevs(alldevs);
   
    /* 开始捕捉 */
    pcap_loop(adhandle, 0, packet_handler, NULL);
   
    return 0;
}

/* 回调函数,当收到每一个数据包时会被libpcap所调用 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    struct tm *ltime;
    char timestr[16];
    ip_header *ih;
    udp_header *uh;
    u_int ip_len;
    u_short sport,dport;
    time_t local_tv_sec;

    /* 将时间戳转换成可识别的格式 */
    local_tv_sec = header->ts.tv_sec;
    ltime=localtime(&local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

    /* 打印数据包的时间戳和长度 */
    printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);

    /* 获得IP数据包头部的位置 */
    ih = (ip_header *) (pkt_data +
        14); //以太网头部长度

    /* 获得UDP首部的位置 */
    ip_len = (ih->ver_ihl & 0xf) * 4;
    uh = (udp_header *) ((u_char*)ih + ip_len);

    /* 将网络字节序列转换成主机字节序列 */
    sport = ntohs( uh->sport );
    dport = ntohs( uh->dport );

    /* 打印IP地址和UDP端口 */
    printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
        ih->saddr.byte1,
        ih->saddr.byte2,
        ih->saddr.byte3,
        ih->saddr.byte4,
        sport,
        ih->daddr.byte1,
        ih->daddr.byte2,
        ih->daddr.byte3,
        ih->daddr.byte4,
        dport);
}

作者: qianhulou   发布时间: 2010-06-25

因为你接的是路由器。如果都接在HUB上就能抓到了

作者: hellioncu   发布时间: 2010-06-25

去网关上抓。

作者: 没本   发布时间: 2010-06-25

抓也没用,QQ消息已经加密

作者: qishking   发布时间: 2010-06-25

2楼的 能讲讲原理吗 路由器为什么就抓不到了啊 ,  为什么用P2P 就可以查局域网的流量啊, P2P为什么能监空到 而我不能?

作者: qianhulou   发布时间: 2010-06-25

也不是没用吧,起码知道谁在用QQ,如果公司制度不允许,正好扣奖金。

作者: 没本   发布时间: 2010-06-25

把你的网卡设置成“混合模式”

作者: c/unix   发布时间: 2010-06-25

恩现在懂了, 可P2P软件是如何做到监控其他主机的流量的呢?

作者: qianhulou   发布时间: 2010-06-25

建议楼主先去学习一下交换机原理.

作者: samlumengjun   发布时间: 2010-06-25

别搞什么先学习学习, 来这里就是学习的, 我就是问 P2P是如何做到监控局域网的流量的 楼上的 你说了等于没说

作者: qianhulou   发布时间: 2010-06-25

用HUB

作者: yikaikai   发布时间: 2010-06-25

p2p使用arp诈骗,就是把自己伪装成网关

作者: nelab   发布时间: 2010-06-25

啥年代了,还有人用HUB给你抓啊...

作者: gtkmm   发布时间: 2010-06-25

感谢各位的回复 我现在学习ARP 欺骗

作者: qianhulou   发布时间: 2010-06-25