UnicodeDecodeError: 'utf-8' codec can't decode byte 0xda in position 24: invalid continuation byte

75 Views Asked by At

I’m learning ethical hacking using python from a tutorial (the tutorial is old and python2 is used in the tutorial in most cases while I’m using python3). I’m using Pycharm for coding in Kali Linux virtual machine. Following the tutorial I ran the script in zsh and visited a website in Firefox http://testphp.vulnweb.com/login.php to try capturing the username and password that I fill in the login page (the same script is used in the tutorial):

import scapy.all as scapy
from scapy.layers 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):
        if packet.haslayer(scapy.Raw):
            load = packet[scapy.Raw].load
            print(load)

sniff("eth0")

The output in the terminal is:

b'uname=MyName&pass=MyPassword'

Then I tried with this (the same script is used in the tutorial):

import scapy.all as scapy
from scapy.layers 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):
        if packet.haslayer(scapy.Raw):
            load = packet[scapy.Raw].load
            if "uname" in load:
                print(load)

sniff("eth0")

Then there was an error in the output:

Traceback (most recent call last):
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 19, in <module>
    sniff("eth0")
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 8, in sniff
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 1311, in sniff
    sniffer._run(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 1254, in _run
    session.on_packet_received(p)
  File "/usr/lib/python3/dist-packages/scapy/sessions.py", line 109, in on_packet_received
    result = self.prn(pkt)
             ^^^^^^^^^^^^^
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 15, in process_sniffed_packet
    if "uname" in load:
       ^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'

To solve it, I tried with this by my own, (it was not in the tutorial because in the tutorial there was no error like I just faced):

import scapy.all as scapy
from scapy.layers 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):
        if packet.haslayer(scapy.Raw):
            load = packet[scapy.Raw].load.decode("utf-8")
            if "uname" in load:
                print(load)

sniff("eth0")

The output was as expected this time:

uname=MyName&pass=MyPassword But after sometime, an error showed up in the terminal:

uname=MyName&pass=MyPassword
Traceback (most recent call last):
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 19, in <module>
    sniff("eth0")
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 8, in sniff
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 1311, in sniff
    sniffer._run(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 1254, in _run
    session.on_packet_received(p)
  File "/usr/lib/python3/dist-packages/scapy/sessions.py", line 109, in on_packet_received
    result = self.prn(pkt)
             ^^^^^^^^^^^^^
  File "/home/kali/PycharmProjects/packet_sniff/packet_sniff.py", line 14, in process_sniffed_packet
    load = packet[scapy.Raw].load.decode("utf-8")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xda in position 24: invalid continuation byte

As you can see above the first line of the output was expected, but the rest was not. Why is it happening and what’s the solution?

0

There are 0 best solutions below