Как читать пакеты с уровнем VLAN - PullRequest
0 голосов
/ 30 апреля 2020

Я пишу программу на Python для чтения и декодирования GOOSE пакетов из файла .pcap. До сих пор я мог читать пакеты с помощью библиотеки Pypcapfile:

from pcapfile import savefile
file = input("Enter the name of the pcap file: ")
try:
    pcap = open(file, 'rb')
except IOError:
    print("No file with name \"{}\" was found.\n".format(file))
    return
capfile = savefile.load_savefile(pcap, verbose=True)
print(capfile)

Terminal:

Enter the name of the pcap file: goose2.pcap
[+] attempting to load goose2.pcap
[+] found valid header
[+] loaded 8023 packets
[+] finished loading savefile.
b'little'-endian capture file version 2.4
microsecond time resolution
snapshot length: 262144
linklayer type: LINKTYPE_ETHERNET
number of packets: 8023

Проблема заключается в том, что когда я тестирую свой код с пакетами, которые содержат заголовок VLAN (всего два байта), он говорит, что в файле pcap есть 0 пакетов:

Enter the name of the pcap file: vlan.pcap
[+] attempting to load vlan.pcap
[+] found valid header
[+] loaded 0 packets
[+] finished loading savefile.
b'big'-endian capture file version 2.4
nanosecond time resolution
snapshot length: 65535
linklayer type: LINKTYPE_ETHERNET
number of packets: 0

Я написал весь код вокруг библиотеки Pypcapfile, поэтому я хочу избежать начала с другой библиотеки такие как Scapy. Я уже пытался добавить аргумент "layer =" в load_savefile, но это не сработало. Есть ли способ обойти это?

1 Ответ

1 голос
/ 30 апреля 2020

Вот как я проверил вещи на моем конце. Я взял образец захвата vlan из wiki wireshark и распаковал его:

$ curl -o vlan.cap.gz 'https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=vlan.cap.gz'
$ gunzip vlan.cap.gz

Мы можем использовать, например, tshark, чтобы убедиться, что этот захват включает пакеты с тегами VLAN:

$ tshark -r vlan.cap -V
Frame 1: 1518 bytes on wire (12144 bits), 1518 bytes captured (12144 bits)
[...]
Ethernet II, Src: AniCommu_40:ef:24 (00:40:05:40:ef:24), Dst: 3com_9f:b1:f3 (00:60:08:9f:b1:f3)
[...]
    Type: 802.1Q Virtual LAN (0x8100)
802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 32
    000. .... .... .... = Priority: Best Effort (default) (0)
    ...0 .... .... .... = DEI: Ineligible
    .... 0000 0010 0000 = ID: 32
    Type: IPv4 (0x0800)
Internet Protocol Version 4, Src: 131.151.32.129, Dst: 131.151.32.21

I можно открыть, используя модуль pcapfile:

$ pip install --user Pypcapfile
$ python
>>> import pcapfile.savefile
>>> with open('vlan.cap', 'rb') as fd:
...   capfile = pcapfile.savefile.load_savefile(fd, layers=2)
...
>>> capfile
b'little'-endian capture file version 2.4
microsecond time resolution
snapshot length: 65535
linklayer type: LINKTYPE_ETHERNET
number of packets: 395
>>> capfile.packets[0]
ethernet from b'00:40:05:40:ef:24' to b'00:60:08:9f:b1:f3' type unknown

Но похоже, что pcapfile не имеет определенного декодера для кадров VLAN.


dpkt Модуль прекрасно работает:

>>> import dpkt
>>> fd = open('vlan.cap', 'rb')
>>> capfile = dpkt.pcap.Reader(fd)
>>> ts, buf = next(capfile)
>>> pkt = dpkt.ethernet.Ethernet(buf)
>>> pkt.vlan_tags
[VLANtag8021Q(pri=0, cfi=0, id=32)]

Как и scapy:

>>> import scapy.all
>>> capfile = scapy.all.rdpcap('vlan.cap')
>>> capfile[0].vlan
32
...