使用python从pcap文件读取802.11数据包

时间:2019-07-14 19:32:10

标签: python-3.x pcap packet-capture 802.11

我发现了类似的问题,但不能解决我的问题

我有一个pcap文件(其截图在下面的Wireshark中打开),其中包含一些802.11帧数据 enter image description here

我尝试使用从this问题中找到的以下代码来阅读它。但是它只打印了以下内容:

代码:

from scapy.all import Dot11
from scapy.all import sniff

def parse(frame):
    if frame.haslayer(Dot11):
        print("ToDS:", frame.FCfield & 0b1 != 0)
        print("MF:", frame.FCfield & 0b10 != 0)
        print("WEP:", frame.FCfield & 0b01000000 != 0)
        print("src MAC:", frame.addr2)
        print("dest MAC:", frame.addr1)
        print("BSSID:", frame.addr3)
        print("Duration ID:", frame.ID)
        print("Sequence Control:", frame.SC)
        print(feature(frame))
        print("\n")

    else:
        print("Not dot11")


sniff(offline="./testData/test.pcap", prn=parse)

结果:

D:\Apps\Python3\python.exe F:/tes/pcapReader/main.py
Not dot11
Not dot11
Not dot11
Not dot11
...

据此,我的理解是跟踪中没有dot11数据包,这令人困惑,因为根据Wireshark的输出,大多数数据包的协议都是802.11。

我也尝试使用dpkt 802.11 package,但没有得到任何结果

我在这里想念什么?

(如果有帮助,我正在使用Python3)

1 个答案:

答案 0 :(得分:0)

我发现了这个名为pyshark的图书馆。 它将pcap文件转换为xml,以使其易于阅读,而与数据包的类型无关

我的代码:

import pyshark

cap = pyshark.FileCapture('./test.pcap')
try:
    print(cap[0]['WLAN'])
except:
    pass

结果:

Layer WLAN:
Frame check sequence: 0x4761f1b6 [correct]
0... .... = Order flag: Not strictly ordered
Receiver address: 01:0b:85:00:00:00
.... ..11 = DS status: WDS (AP to AP) or Mesh (MP to MP) Frame (To DS: 1 From DS: 1) (0x3)
Frame Control Field: 0x0803
Type/Subtype: Data (0x0020)
Transmitter address: f0:25:72:70:a3:a0
0000 .... = Subtype: 0
.... .0.. = More Fragments: This is the last fragment
1000 0111 1111 .... = Sequence number: 2175
.... 10.. = Type: Data frame (2)
FCS Status: Good
...0 .... = PWR MGT: STA will stay up
.... ..00 = Version: 0
.... .... .... 0000 = Fragment number: 0
Destination address: 01:0b:85:00:00:00
Source address: f0:25:72:70:a3:a0
.000 0000 0000 0000 = Duration: 0 microseconds
Flags: 0x03
.... 0... = Retry: Frame is not being retransmitted
..0. .... = More Data: No data buffered
.0.. .... = Protected flag: Data is not protected