我试图将所有的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替换为input。