sniffer 程序网卡混杂模式

2017-05-13 19:31:34 +08:00
 firebroo

用这种方式可以设置网卡为混杂模式, 代码如下

void 
set_promisc (int sockfd, char *interface)
{
    int     s;  
    struct  ifreq ifr;

    strcpy(ifr.ifr_name, interface);  
  
    s = ioctl(sockfd, SIOCGIFFLAGS, &ifr);  
  
    if (s < 0) {  
        close(sockfd);  
        perror("can not get flags");  
        return;
    }  
  
    ifr.ifr_flags |= IFF_PROMISC;  
  
    s = ioctl(sockfd, SIOCSIFFLAGS, &ifr);  
    if (s < 0) {  
        close(sockfd);  
        perror("can not set flags");  
        return;
    }  
}

socketfd 是 socket 函数返回的 fd,interface 是网卡名字, 使用 ifconfig 可以看到 PROMISC

wlp4s0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500
        inet 192.168.212.237  netmask 255.255.252.0  broadcast 192.168.215.255
        inet6 fe80::4298:b56b:882c:b27c  prefixlen 64  scopeid 0x20<link>
        ether e4:a4:71:e4:be:11  txqueuelen 1000  (Ethernet)
        RX packets 13617741  bytes 12026115621 (11.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8344313  bytes 1515273567 (1.4 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

然后我看了 libpcap 的接口 pcap_open_live,第三个参数为 true 的时候是设置为混杂模式,我设置了 true,使用 ifconfig 没有看到 PROMISC 阿。。。

混杂模式是不是必须在网卡看到 PROMISC 才算开启了?

3445 次点击
所在节点    Linux
6 条回复
aip
2017-05-13 19:35:00 +08:00
dmesg | tail

进入混杂模式的话,dmesg 应该会有消息的。
mengyaoss77
2017-05-13 19:42:58 +08:00
貌似不是所有的网卡都能混杂模式吧?
我最近也想用 wireshark 混杂模式抓个包的,结果发现并不能开启混杂,不知道为啥
linhua
2017-05-13 20:55:38 +08:00
Halry
2017-05-13 21:29:09 +08:00
@mengyaoss77 网卡一般都支持,看驱动了
firebroo
2017-05-17 10:24:33 +08:00
@aip 正解~~
hilyjiang
2017-05-20 12:51:32 +08:00
当然得网卡开了混杂模式才行。

开:
ifconfig eth0 promisc

关:
ifconfig eth0 -promisc

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/361127

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX