V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
firebroo
V2EX  ›  Linux

sniffer 程序网卡混杂模式

  •  
  •   firebroo ·
    firebroo · May 13, 2017 · 4183 views
    This topic created in 3281 days ago, the information mentioned may be changed or developed.

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

    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 才算开启了?

    6 replies    2017-05-20 12:51:32 +08:00
    aip
        1
    aip  
       May 13, 2017   ❤️ 1
    dmesg | tail

    进入混杂模式的话,dmesg 应该会有消息的。
    mengyaoss77
        2
    mengyaoss77  
       May 13, 2017
    貌似不是所有的网卡都能混杂模式吧?
    我最近也想用 wireshark 混杂模式抓个包的,结果发现并不能开启混杂,不知道为啥
    Halry
        4
    Halry  
       May 13, 2017 via iPhone
    @mengyaoss77 网卡一般都支持,看驱动了
    firebroo
        5
    firebroo  
    OP
       May 17, 2017
    @aip 正解~~
    hilyjiang
        6
    hilyjiang  
       May 20, 2017
    当然得网卡开了混杂模式才行。

    开:
    ifconfig eth0 promisc

    关:
    ifconfig eth0 -promisc
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3071 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 80ms · UTC 15:08 · PVG 23:08 · LAX 08:08 · JFK 11:08
    ♥ Do have faith in what you're doing.