0

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 + " )")
  • 2
    Possible duplicate of [Convert bytes to a string](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string) – Ken Y-N Oct 11 '19 at 07:30
  • Note also that `elif proto_sniff in ["arp", "bootp", ...]:` is more idomatic. – Ken Y-N Oct 11 '19 at 07:31
  • i had tried it but it didn't work. This is what i did ``` – Adhika Darmesta Oct 11 '19 at 07:43
  • [This might also help](https://stackoverflow.com/q/34791583/1270789)? – Ken Y-N Oct 11 '19 at 07:48
  • Rolling out the welcome mat - Welcome to Stack Overflow! Please read the [help pages](https://stackoverflow.com/help), take the [SO tour](https://stackoverflow.com/tour), read about [how to ask good questions](https://stackoverflow.com/help/how-to-ask), as well as this [question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please learn how to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Ross Jacobs Oct 11 '19 at 08:37

0 Answers0