利用scapy实现无线数据包捕获

2024-05-14 22:40:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过的代码如下:

from scapy.all import *

def PacketHandler(pkt) :

       if pkt.haslayer == 2 and pkt.subtype==0:

          if pkt.haslayer(IP) :

             ip=pkt.getlayer(IP)
             print ip.dst

          if pkt.haslayer(UDP):
               udp=pkt.getlayer(UDP)
               print udp.dport
          if pkt.haslayer(TCP) :
               tcp=pkt.getlayer(TCP)
               print tcp.port

sniff(iface="mon0", prn=PacketHandler) 

使用这个,我想捕获所有的无线数据包,但我只得到多播(IP/UDP)包。那么我怎样才能在我的无线网络中得到所有的数据包呢?我已经为此(暂时)禁用了访问点上的加密,以便可以访问数据包中的数据。


Tags: 代码fromipif数据包tcpudpprint
1条回答
网友
1楼 · 发布于 2024-05-14 22:40:39

如果只处理Data帧,而不处理ManagementControl帧,则可以执行以下操作:

from scapy.all import *

def packet_handler(pkt) :
    # if packet has 802.11 layer, and type of packet is Data frame
    if pkt.haslayer(Dot11) and pkt.type == 2:
            # do your stuff here
            print(pkt.show())


sniff(iface="mon0", prn=packet_handler)

此外,还可以使用sniff函数的filter选项仅筛选Data帧以转到packet_handler函数:

from scapy.all import *

def packet_handler(pkt) :
    # if packet has 802.11 layer
    if pkt.haslayer(Dot11):
        # do your stuff here
        print(pkt.show())

sniff(iface="mon0", prn=packet_handler, filter="type Data")

Here,是帧的typesubtype值的良好列表。

相关问题 更多 >

    热门问题