【问题标题】:How do I update specific Netflow v10/ IPFIX flow data fields in Python3 Scapy?如何在 Python3 Scapy 中更新特定的 Netflow v10/IPFIX 流数据字段?
【发布时间】:2022-10-23 17:01:37
【问题描述】:

设想
我有一个包含 Netflow v10/IPFIX 数据模板和数据流的 PCAP,并且想重放 PCAP。在发送数据包之前,我想更新其中一个流数据字段(即 startTime 与当前时间)。


当前代码
我当前的代码能够读取 PCAP,抓取最后一层(Netflowv10),创建一个套接字并通过接口发送数据包。套接字负责以太网 -> IP -> UDP 层,Scapy 中的“getlayer”功能保留了来自施主 PCAP 的 Netflow v10 模板和数据流层。


问题
我需要帮助的问题是,如何更新 startTime 流数据字段?


代码
我只提供了与此问题范围内的问题相关的 Python3 代码,与 Scapy 相关

packets = sniff(session=NetflowSession, offline=open(pcap_file, "rb"))

for packet in packets:

    if packet.haslayer(NetflowDataflowsetV9):
        # This return the NetflowDateflowset Records key/value pairs
        flowset = netflowv9_defragment(packet[NetflowDataflowsetV9].records)

变量“flowset”返回一个列表并且不是可调用对象。浏览 Scapy 的文档,并没有提供很多帮助。

任何指针或建议将不胜感激。先感谢您 :)

【问题讨论】:

    标签: python python-3.x scapy netflow


    【解决方案1】:

    首先,netflowv9_defragment 不应该在这里使用:它与session=NetflowSession 具有相同的效果,并且应该给出一个数据包列表。

    流集的主要帮助页面是https://scapy.readthedocs.io/en/latest/layers/netflow.html,但https://github.com/secdev/scapy/blob/master/test/scapy/layers/netflow.uts 中也有一些测试用例提供了有关模块如何工作的信息。

    你可以做类似的事情

    for packet in packets:
        if packet.haslayer(NetflowDataflowsetV9):
            for rec in packet.records:
                if 'startTime' in rec:
                    rec.startTime = 12345
    

    在我看来,构建示例是理解 netflow 数据包如何构建的好方法:

    header = Ether()/IP()/UDP()
    netflow_header = NetflowHeader()/NetflowHeaderV9()
    
    # Let's first build the template. Those need an ID > 255.
    # The (full) list of possible fieldType is available in the
    # NetflowV910TemplateFieldTypes list. You can also use the int value.
    flowset = NetflowFlowsetV9(
        templates=[NetflowTemplateV9(
            template_fields=[
                NetflowTemplateFieldV9(fieldType="IN_BYTES", fieldLength=1),
                NetflowTemplateFieldV9(fieldType="IN_PKTS", fieldLength=4),
                NetflowTemplateFieldV9(fieldType="PROTOCOL"),
                NetflowTemplateFieldV9(fieldType="IPV4_SRC_ADDR"),
                NetflowTemplateFieldV9(fieldType="IPV4_DST_ADDR"),
            ],
            templateID=256,
            fieldCount=5)
        ],
        flowSetID=0
    )
    # Let's generate the record class. This will be a Packet class
    # In case you provided several templates in ghe flowset, you will need
    # to pass the template ID as second parameter
    recordClass = GetNetflowRecordV9(flowset)
    # Now lets build the data records
    dataFS = NetflowDataflowsetV9(
        templateID=256,
        records=[ # Some random data.
            recordClass(
                IN_BYTES=b"",
                IN_PKTS=b"
    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多