当我运行此代码时:
#!/usr/bin/env python
import scapy.all as scapy
from scapy_http import http
def sniff(interface):
scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)
def process_sniffed_packet(packet):
if packet.haslayer(http.HTTPRequest):
print(packet)
sniff("eth0")
我得到了:
Traceback (most recent call last): File "packet_sniffer.py", line 16, in <module>
sniff("eth0") File "packet_sniffer.py", line 8, in sniff
scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet) File "/usr/local/lib/python3.7/dist-packages/scapy/sendrecv.py", line 886, in sniff
r = prn(p) File "packet_sniffer.py", line 13, in process_sniffed_packet
print(packet) File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 438, in
__str__
return str(self.build()) File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 556, in build
p = self.do_build() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
pay = self.do_build_payload() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
return self.payload.do_build() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
pay = self.do_build_payload() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
return self.payload.do_build() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
pay = self.do_build_payload() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
return self.payload.do_build() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
pay = self.do_build_payload() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
return self.payload.do_build() File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 538, in do_build
pkt = self.self_build() File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 179, in self_build
return _self_build(self, field_pos_list) File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 101, in _self_build
val = _get_field_value(obj, f.name) File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 74, in _get_field_value
headers = _parse_headers(val) File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 18, in _parse_headers
headers = s.split("\r\n") TypeError: a bytes-like object is required, not 'str'
我该怎么办?
答案 0 :(得分:1)
您可以使用python2
而不是python3
来解决此问题。如果您不想更改python版本,则需要对scapy-http
的库代码进行一些更改。从您的追溯中,我可以看到该文件位于:/usr/local/lib/python3.7/dist-packages/scapy_http/http.py
。现在,使用任何文本编辑器打开该文件。
从以下位置更改第18行:
headers = s.split("\r\n")
收件人:
try:
headers = s.split("\r\n")
except TypeError as err:
headers = s.split(b"\r\n")
然后从以下位置更改第109行:
p = f.addfield(obj, p, val + separator)
收件人:
try:
p = f.addfield(obj, p, val + separator)
except TypeError as err:
p = f.addfield(obj, p, str(val) + str(separator))
答案 1 :(得分:0)
除scapy_http
外,您还使用scapy
。
scapy_http
已有一段时间没有更新,并且与Python 3+的兼容性较差
随时查看https://github.com/secdev/scapy/pull/1925:它尚未在Scapy中合并(我想有时可能很快就会合并),但是它是scapy_http
的更新和改进的端口,是为最新的{{ 1}}版本