我目前正在使用python制作一个数据包嗅探器程序。我正在使用Scapy模块来嗅探数据包。当我尝试使用过滤器“端口53”嗅探DNS数据包时,出现此错误。
Traceback (most recent call last):
File "D:/Semester 7/Packet Sniffer TA/coba_scapy/packet_sniffing_python/sniffer.py", line 165, in <module>
sniff(iface=net_iface, filter=proto_sniff, count=int(pkt_to_sniff), timeout=int(time_to_sniff), prn=packet_log)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sendrecv.py", line 1020, in sniff
sniffer._run(*args, **kwargs)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sendrecv.py", line 973, in _run
session.on_packet_received(p)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sessions.py", line 82, in on_packet_received
result = self.prn(pkt)
File "D:/Semester 7/Packet Sniffer TA/coba_scapy/packet_sniffing_python/sniffer.py", line 138, in packet_log
print(str(ip_src)+" -> "+str(ip_dst)+ " : ( " + packet.getlayer(DNS).qd.qname+ " )")
TypeError: can only concatenate str (not "bytes") to str
这是我的嗅探变量。
elif (proto_sniff == "arp") or (proto_sniff == "bootp") or (proto_sniff == "icmp") or (proto_sniff == "port 53"):
sniff(iface=net_iface, filter=proto_sniff, count=int(pkt_to_sniff), timeout=int(time_to_sniff), prn=packet_log)
这是我在用户给过滤器参数端口53时处理过滤器的地方
def packet_log(packet):
....
elif proto_sniff == "port 53":
if IP in packet:
ip_src = packet[IP].src
ip_dst = packet[IP].dst
if packet.haslayer(DNS) and packet.getlayer(DNS).qr == 0:
print(str(ip_src) + " -> " + str(ip_dst) + " : ( " + packet.getlayer(DNS).qd.qname + " )")
“ ip_src”和“ ip_dst”是否以字节为单位?如何在print()方法中使其可写?
我已经尝试过此Convert bytes to a string的解决方案,但仍然给我这个错误
Traceback (most recent call last):
File "D:/Semester 7/Packet Sniffer TA/coba_scapy/packet_sniffing_python/sniffer.py", line 166, in <module>
sniff(iface=net_iface, filter=proto_sniff, count=int(pkt_to_sniff), timeout=int(time_to_sniff), prn=packet_log)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sendrecv.py", line 1020, in sniff
sniffer._run(*args, **kwargs)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sendrecv.py", line 973, in _run
session.on_packet_received(p)
File "C:\Users\adika\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scapy-2.4.3.dev61-py3.7.egg\scapy\sessions.py", line 82, in on_packet_received
result = self.prn(pkt)
File "D:/Semester 7/Packet Sniffer TA/coba_scapy/packet_sniffing_python/sniffer.py", line 133, in packet_log
ip_src = ip_src_byte.decode()
AttributeError: 'str' object has no attribute 'decode'
这就是我所做的
if IP in packet:
ip_src_byte = packet[IP].src
ip_dst_byte = packet[IP].dst
ip_src = ip_src_byte.decode()
ip_dst = ip_dst_byte.decode()
if packet.haslayer(DNS) and packet.getlayer(DNS).qr == 0:
print(str(ip_src) + " -> " + str(ip_dst) + " : ( " + packet.getlayer(DNS).qd.qname + " )")