I'm curently making a packet sniffer program using python. Im using scapy module to sniff the packet. When i try to sniff DNS packet using filter "port 53" im getting this error.
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
This is my sniff variable.
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)
And this is where i process the filter when user give filter parameter port 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 + " )")
is "ip_src" and "ip_dst" are in byte? how to make it writeable in print() method?
I had tried the solution from this Convert bytes to a string but it still giving me this error
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'
This is what i had done
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 + " )")