设置网卡为混杂模式

    有时候为嗅探到网络上的数据,需要将网卡设置到混杂模式。进入该模式将网络上的数据一并抓获,为此在设置nic的混杂模式的时候有诸多方法?通过shell命令来实现:
ifconfig eth1 promisc  设置混杂模式
ifconfig eth1 -promisc 取消混杂模式
执行结果如下
[root@localhost tftpboot]# ifconfig
eth6      Link encap:Ethernet  HWaddr 08:00:27:70:1D:79  
          inet6 addr: fe80::a00:27ff:fe70:1d79/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:100124 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12986638 (12.3 MiB)  TX bytes:6452270 (6.1 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1303 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1303 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:303973 (296.8 KiB)  TX bytes:303973 (296.8 KiB)

[root@localhost tftpboot]# ifconfig eth6 promisc
[root@localhost tftpboot]# ifconfig
eth6      Link encap:Ethernet  HWaddr 08:00:27:70:1D:79  
          inet6 addr: fe80::a00:27ff:fe70:1d79/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:100154 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8795 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13007885 (12.4 MiB)  TX bytes:6452270 (6.1 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1303 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1303 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:303973 (296.8 KiB)  TX bytes:303973 (296.8 KiB)

[root@localhost tftpboot]# 

也可以通过C语言方式编程来实现,

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <errno.h>
  7. #include <linux/if_ether.h>
  8. #include <net/if.h>
  9. #include <sys/ioctl.h>
  10. #include <string.h>

  11. #define ETH_NAME    "eth1"

  12. int do_promisc(void) {
  13.     int f, s;
  14.     struct ifreq ifr;
  15.     if ( (f=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))<0){ 
  16.         return -1;
  17.     }
  18.     strcpy(ifr.ifr_name, ETH_NAME);
  19.     
  20.     if ((= ioctl(f, SIOCGIFFLAGS, &ifr))<0){
  21.       close(f);
  22.       return-1;
  23.     }
  24.     
  25.     if(ifr.ifr_flags & IFF_RUNNING){
  26.         printf("eth link up\n");
  27.     }else{
  28.         printf("eth link down\n");
  29.     }
  30.   
  31.     ifr.ifr_flags |= IFF_PROMISC;
  32.     if ((= ioctl(f, SIOCSIFFLAGS, &ifr)) < 0){
  33.       return -1;
  34.     }
  35.     printf("Setting interface ::: %s ::: to promisc\n\n", ifr.ifr_name);
  36.     return 0;
  37. }

  38. int check_nic(void)
  39. {
  40.     struct ifreq ifr;
  41.     int skfd = socket(AF_INET, SOCK_DGRAM, 0);

  42.     strcpy(ifr.ifr_name, ETH_NAME);
  43.     if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
  44.     {
  45.         close(skfd);
  46.         return -1;
  47.     }
  48.     if(ifr.ifr_flags & IFF_RUNNING){
  49.         printf("link up\n");
  50.         close(skfd);
  51.         return 0; // 网卡已插上网线
  52.     }else {
  53.         printf("link down\n");
  54.         close(skfd);
  55.         return -1;
  56.     }
  57. }



  58. int main(void) {
  59.     do_promisc();
  60.     return 0;
  61. }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值