如何使用 pcap 从多个接口捕获流量
为了使用 pcap 嗅探多个接口,我会执行以下操作(以伪代码):
foreach interface:
open a file descriptor using pcap_open_live()
set the file descriptor to non-blocking
while true:
check for a ready file descriptor using select() or an equivalent I/O multiplexer
read data from every ready file descriptor using pcap_dispatch()
handle EndOfStream or Errors and break out of loop if needed
这是否足够或者是否需要考虑一些特定的警告?
In order to sniff from multiple interfaces using pcap, I would do the following (in pseudocode):
foreach interface:
open a file descriptor using pcap_open_live()
set the file descriptor to non-blocking
while true:
check for a ready file descriptor using select() or an equivalent I/O multiplexer
read data from every ready file descriptor using pcap_dispatch()
handle EndOfStream or Errors and break out of loop if needed
Is this enough or are there some particular caveats to take into account ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个小的 C 程序片段,用于使用 PCAP 库以混合(隐形)模式在网络中捕获(嗅探)网络数据包。
gcc file.c -lpcap
./file eth0 eth1 wlan0
Here is small C-Program snippet to use PCAP library to capture (sniff) network packets in promiscus (stealth) mode in the network.
gcc file.c -lpcap
./file eth0 eth1 wlan0
我在尝试使用 pcap 从特定接口捕获时遇到了一些问题,并在此询问。似乎很少有人熟悉pcap。我的问题以及我最终得到的答案指出了非常有用的细节,可以在下面的链接中找到,您可能会发现它有用:
对 libcap (pcap) 和无线感到困惑
I ran into some issues trying to capture from particular interfaces with pcap and asked about it here. It seemed few were familiar with pcap. My issues, and an answer I finally got pointing out very helpful details, can be found here in the below link which you might find useful:
Confused by libcap (pcap) and wireless
pcap_open_live(3) 的手册页显示:
答案在最后一行。
因此,在
pcap_open_live()
调用的device
参数中使用"any"
或NULL
来从所有接口捕获。Man page of pcap_open_live(3) says:
The answer is in the last line.
So, use
"any"
orNULL
indevice
parameter ofpcap_open_live()
call to capture from all interfaces.要从多个网络接口捕获数据包,您需要调用
fork()
新子进程,然后执行pcap_lookupnet()
和pcap_open_live()
。注意:您不能使用线程来代替为每个网络接口创建子进程。
To capture packets from multiple network interfaces you need to call
fork()
new child process and then dopcap_lookupnet()
and thenpcap_open_live()
.Note: You cannot use thread instead of create child process for each network interface.