簡體   English   中英

用 scapy 寫入 pcap

[英]Writing to a pcap with scapy

過濾掉所有 NBNS 流量后,我正在嘗試寫入 pcap 文件。 這給了我一個語法錯誤。

from scapy.all import *

Capture = raw_input("Enter file path of pcap file: " )
pcap = rdpcap(Capture)

ports=137

filtered = (pkt for pkt in Capture if
    (UDP in pkt and 
    (pkt[UDP].sport in str(ports)))

wrpcap("filtered.pcap",filtered)

我發現語法錯誤的答案只是在...str(ports))))末尾缺少一個括號,但現在我有一個不同的錯誤。

  File "receiver2.py", line 18, in <module>
    wrpcap("filtered.pcap",filtered)
  File "/usr/lib/python2.7/dist-packages/scapy/utils.py", 
    line 470, in wrpcap
  PcapWriter(filename, *args, **kargs).write(pkt)
  File "/usr/lib/python2.7/dist-packages/scapy/utils.py", line 652, in write
    for p in pkt:
  File "receiver2.py", line 13, in <genexpr>
    (UDP in pkt and 
  TypeError: 'in <string>' requires string as left operand, not Packet_metaclass

我正在嘗試您的腳本,但無法按照編寫的方式進行。 我對它進行了一些更改,我認為它可以滿足您的需求。 希望這可以幫助。

from scapy.all import *

capture = raw_input("Enter file path of pcap file: " )
pcap = rdpcap(capture)

ports=137

def write(pkt):
    wrpcap('filtered.pcap', pkt, append=True)  #appends packet to output file

for pkt in pcap:
    if pkt.haslayer(UDP) and pkt.getlayer(UDP).sport == ports:  #checks for UDP layer and sport 137
        write(pkt)  #sends the packet to be written if it meets criteria
    else:
        pass

pkt[UDP].sport通常應該是整數而不是字符串。 str(ports)應僅替換為ports

我正在使用 scapy v3.x。 如果您仍然遇到問題,請嘗試使用 scapy 3.x(pip install scapy-python3),我將能夠與您一起完成。 我在此代碼示例中看到的從 python2 到 python3 的唯一必要更改是用輸入替換 raw_input。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM