linux网卡混杂模式
1. 怎样网卡设置混杂模式
有时候为嗅探到网络上的数据,需要将网卡设置到混杂袜辩模式。进入该模式将网络上的数据一并抓获,为此在告烂缺历者设置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语言方式编程来实现
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <string.h>
#define ETH_NAME "eth1"
int do_promisc(void) {
int f, s;
struct ifreq ifr;
if ( (f=socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))<0){
return -1;
}
strcpy(ifr.ifr_name, ETH_NAME);
if ((s = ioctl(f, SIOCGIFFLAGS, &ifr))<0){
close(f);
return-1;
}
if(ifr.ifr_flags & IFF_RUNNING){
printf("eth link up\n");
}else{
printf("eth link down\n");
}
ifr.ifr_flags |= IFF_PROMISC;
if ((s = ioctl(f, SIOCSIFFLAGS, &ifr)) < 0){
return -1;
}
printf("Setting interface ::: %s ::: to promisc\n\n", ifr.ifr_name);
return 0;
}
int check_nic(void)
{
struct ifreq ifr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, ETH_NAME);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
{
close(skfd);
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING){
printf("link up\n");
close(skfd);
return 0; // 网卡已插上网线
}else {
printf("link down\n");
close(skfd);
return -1;
}
}
int main(void) {
do_promisc();
return 0;
}
2. Windows/Linux下,某个C++网络监听程序,设置了网卡为混杂模式。这会不会影响其他程序。
一般无影响,混杂模式开启,结果是不属于你的数据包也流入网卡,其他非监听程序根本不会去接收也无法接收这些数据,所以无影响。
当然混杂模式会增加网卡压力和CPU负荷,可能在这方面造成对其他程序的一点影响。
3. linux 查看 网卡接口是否为混杂模式
ifconfig 可以看到网卡是否混杂模式,如:
ifconfig eth0 看是否有“PROMISC”标志
ifconfig eth0 promisc #设置混杂模式
ifconfig eth0 -promisc #取消混杂模式
4. linux 下怎样查看网卡是否支持混杂模式
使用ifconfig命令可查看当前网卡是否设置为混杂模式[zhaihf@localhost~]$ ifconfig
eth0 Linkencap:Ethernet HWaddr 00:16:31:FF:A6:2E
inet addr:192.168.9.1 Bcast:192.168.9.255 Mask:255.255.255.0
UPBROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Memory:98420000-98440000
eth1 Link encap:Ethernet HWaddr 00:16:31:FF:A6:2F
inet addr:192.168.8.48 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::216:31ff:feff:a61f/64 Scope:Link
UPBROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:28011022 errors:0 dropped:0 overruns:0 frame:0
TX packets:18408510 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:31733322503 (29.5 GiB) TX bytes:21092798216 (19.6GiB)
Memory:98400000-98420000
5. linux下网卡混杂模式如何设置
那就每次开机登录时自动修改呗~~~
这个修改重启后会丢失,因此你要自己去修改~~修改mac地址也是重启就失效了
echo "ifconfig eth0 promisc " >>/root/.bash_profile
或者
echo "ifconfig eth0 promisc " >>/root/.bashrc
/root/.bashrc是进入root时自动会执行的一个脚本~~