設置網卡爲混雜模式

    有時候爲嗅探到網絡上的數據,需要將網卡設置到混雜模式。進入該模式將網絡上的數據一併抓獲,爲此在設置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. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章