Getting error In python3 scapy Module .... Packet Sniffer

Viewed 566

I'm building a sniffing tool using python-3.7 and I got that error when I was trying to use the scapy_http module.

It works fine in python-2.x. The object type is packet[scapy.Raw].load

Code:

#!usr/bin/env python
import scapy.all as scapy
from scapy.layers import http

def sniff(interface):
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)
    #store=Flase(does not store the output in memory)
    #prn= call another  function after one packet or any data is sniffed is sniffed

def process_sniffed_packet(packet):
    if packet.haslayer(http.HTTPRequest):
        if packet.haslayer(scapy.Raw):
            load = packet[scapy.Raw].load
            if "username" in load:
                print(load)
sniff("eth0")

ERROR:

 File "packet_sniffer.py", line 16, in <module>
    sniff("eth0")
  File "packet_sniffer.py", line 6, in sniff
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)
  File "/usr/local/lib/python3.8/site-packages/scapy/sendrecv.py", line 1036, in sniff
    sniffer._run(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/scapy/sendrecv.py", line 989, in _run
    session.on_packet_received(p)
  File "/usr/local/lib/python3.8/site-packages/scapy/sessions.py", line 82, in on_packet_received
    result = self.prn(pkt)
  File "packet_sniffer.py", line 14, in process_sniffed_packet
    if "username" in load:
****TypeError: a bytes-like object is required, not 'str'****
1 Answers

packet[scapy.Raw].load uses load from the scapy.packet.Raw class. You are asking python to determine if a string is inside of a bytes object, which is a type error as it says. It's easy to replicate this error:

>>> if "word" in b"word":
...     pass
...
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

Try using if "username" in str(load): or if b"username" in load: on line 14 instead so there isn't a type mismatch.


Note: Class hierarchies are confused because of your use of import scapy.all as scapy, so scapy.Raw is the same as scapy.Packet.Raw in your snippet.

Related