【问题标题】:Save UDP packets from x amount of seconds in a pcap file在 pcap 文件中保存 x 秒的 UDP 数据包
【发布时间】:2022-07-08 03:37:37
【问题描述】:

我正在尝试将通过以太网连接从传感器接收到的 UDP 数据包保存到我的 PC。我想以 pcap 文件的形式保存 UDP 数据。

到目前为止,我已经编写了以下代码来保存 1 个数据包。我运行了我的代码,然后使用wireshark打开了保存的pcap文件,并将其与原始wireshark数据的输出进行了比较。数据包 1 中的 0000 - 0010 行相同,但 0020 行不同。 test.pcap 仅捕获了 42 个字节,而从 Wireshark 数据包 1 中捕获了 1236 个字节。

如果我错了,请纠正我,但我认为这是因为我的代码没有足够的时间从数据包中收集所有数据。 我希望在修改下面的代码时获得帮助,以便它按时间间隔保存 pcap 文件。例如,每 5 秒保存一个 pcap 文件。

from scapy.all import wrpcap, Ether, IP, UDP

pkts = [Ether(src=" ", dst=" ") / IP(src=" ", dst=" ") / UDP(src=" ", dst=" ")]

wrpcap('test.pcap', [pkts])
      

【问题讨论】:

    标签: python sockets networking udp scapy


    【解决方案1】:

    试试这个代码,如果你仍然收到更少的数据包,请告诉我:

    #!/usr/bin/env python3
    import sys
    import struct
    import os
    import argparse
    
    from scapy.all import sniff, sendp, hexdump, linehexdump, get_if_list, get_if_hwaddr
    from scapy.all import Packet, IPOption
    from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
    from scapy.all import IP, TCP, UDP, Raw
    from scapy.layers.inet import _IPOption_HDR
    from scapy.all import raw
    from scapy.all import bytes_hex
    import hashlib
    import pcapng.blocks as blocks
    from pcapng import FileWriter
    
    
    counter = 1
    
    def get_if():
        ifs=get_if_list()
        iface=None
        for i in get_if_list():
            if "enp1s0f1" in i:
                iface=i
                break;
        if not iface:
            print("Cannot find eth0 interface")
            exit(1)
        return iface
    
    
    def main():
        global counter
        ifaces = [i for i in os.listdir('/sys/class/net/') ]
        iface = get_if()
        print(("sniffing on %s" % iface))
        sys.stdout.flush()
        writer = FileWriter(args.outfile, shb)
    
        orig_packets = sniff(filter='tcp and port 5201',iface = iface)
        for packet in orig_packets:
            spb = shb.new_member(blocks.SimplePacket)
            spb.packet_data = bytes(packet)
            writer.write_block(spb)
            print("C=",counter)
            counter=counter+1
    
    if __name__ == '__main__':
        parser = argparse.ArgumentParser()
        parser.add_argument("outfile", type=argparse.FileType("wb"))
        args = parser.parse_args()
    
        shb = blocks.SectionHeader(
        options={
            "shb_hardware": "artificial",
            "shb_os": "python",
            "shb_userappl": "python-pcapng",
        })
        idb = shb.new_member(
        blocks.InterfaceDescription,
        link_type=1,
        options={
            "if_description": "Hand-rolled",
            "if_os": "Python",
            "if_filter": [(0, b"tcp port 5201 and host 192.168.1.3")],
        },)
    
        main()
    
    

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多