gpt4 book ai didi

python - 用 scapy 写入 pcap

转载 作者:行者123 更新时间:2023-12-01 00:46:52 28 4
gpt4 key购买 nike

过滤掉所有 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

关于python - 用 scapy 写入 pcap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740583/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com