我在同一个局域网中连接了 3 台笔记本电脑。
第一圈:192.168.1.2
第二圈:192.168.1.3
第三圈:192.168.1.4
我将 lap-1 作为服务器并监听 9333 端口。 lap-2 作为客户端。我使用 netcat 将数据从 lap2 发送到 lap1。我能够在 lap1 中使用 pcap 捕获数据包。我已经使用 sudo ifconfig eth0 promisc
打开了混杂模式。同样在 pcap_live_open
方法中,我设置了混杂模式标志。
然后我关闭了混杂模式,也在 pcap_live_open
函数中。我仍然能够捕获数据包。
我在谷歌上搜索了混杂模式,我可以推断,如果设备在混杂模式下打开一个接口(interface),它将能够捕获连接到该网络的所有数据包。
所以考虑到这一点,我将 lap-3 作为服务器,lap-2 仍然作为客户端。我遵循了与上述相同的程序。我在 lap-1 中运行 pcap 可执行文件,希望我能够捕获在 lap-3 和 lap-2 之间传输的数据包,但是在 lap-1 中运行的 pcap 在混杂模式打开时无法做到这一点。所有 3 圈都连接到同一网络。
谁能教我在简单场景下使用混杂模式?
这是我的 pcap 代码: 29988 是 9333 的反向(交换),我正在寻找它。
#include <pcap/pcap.h>
#include <stdint.h>
const u_char *packet;
int main()
{
char *dev = "eth0";
pcap_t *handle;
int j=0;
char errbuf[PCAP_ERRBUF_SIZE];
struct bpf_program fp;
bpf_u_int32 mask;
bpf_u_int32 net;
struct pcap_pkthdr header;
uint8_t *ip_header_len;
uint16_t ip_header_len_val;
uint16_t *port;
/* Find the properties for the device */
while (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
printf("Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
printf("lookedup pcap device: %s\n", dev);
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ,1,0, errbuf);
if (handle == NULL) {
printf("Couldn't open device %s: %s\n", dev, errbuf);
}
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
printf("Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
pcap_close(handle);
}
/* if (pcap_setfilter(handle, &fp) == -1) {
printf("Couldn't install filter %s: %s", filter_exp, pcap_geterr(handle));
return(-1);
}
*/
/* Grab a packet */
while ((packet = pcap_next(handle, &header)) != NULL)
{
uint16_t *data_size;
uint16_t size,total_len_val,tcp_header_len_val;
char tdata[128];
uint8_t *data,*tcp_header_len;
uint16_t *total_len;
//ip_proto = (uint8_t *)&packet[9];
ip_header_len = (uint8_t *)&packet[14];
ip_header_len_val = (*ip_header_len) & 0x0F;
ip_header_len_val = ip_header_len_val*4;
// printf("IP header len val:%d\n",ip_header_len_val);
port = (uint16_t *)&packet[14+ip_header_len_val+2];
//printf("port:%d\n",*port);
total_len = (uint16_t *)&packet[14+2];
total_len_val = ((*total_len) >> 8) & 0x00FF;
total_len_val = total_len_val + (((*total_len) << 8) & 0xFF00);
//total_len_val=*total_len;
// printf("tot len val:%d\n",total_len_val);
tcp_header_len = (uint8_t *)&packet[14+ip_header_len_val+12];
tcp_header_len_val = (*tcp_header_len) & 0xF0;
tcp_header_len_val = tcp_header_len_val>>4;
tcp_header_len_val = tcp_header_len_val * 4;
// printf("tcp header len val:%d\n",tcp_header_len_val);
size = (total_len_val- ip_header_len_val) - tcp_header_len_val;
data = (uint8_t *)&packet[14+ip_header_len_val+tcp_header_len_val];
memset(tdata,0,128);
mempcpy(tdata,data,size);
tdata[size]='\0';
if((*port)==29988)
{
printf("Data Packet:%s\n",tdata);
}
}
}
最佳答案
我希望当您说它们都在同一个网络上时,您的意思是它们连接到同一个以太网交换机。该交换机只会将数据发送到 laptop1,该数据发往 laptop1。在过去普遍使用以太网集线器的时代,所有流量都流向所有连接的设备,但现在交换机非常便宜,集线器不再普遍。如果您能找到一个集线器,那么您可以尝试一下,否则您将只能看到发往您设备的流量。
关于c - 使用混杂模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244083/