1

I'm trying to intercept HTTP requests in Python and to start to learn how to do this I'm following a guide that prompted me to create a socket and then use socket.recvfrom() function that returns the packets in the form of raw bytes and an address in a tuple.

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_TCP)
raw_data, addr = s.recvfrom(4096)

raw_data contains a byte string, similar to this one b'E\x00\x008(\x91@\x00p\x06\xc7`\xca\x89\x8e\x8f\xc0\xa8\x01\r\xd5z\x00\x16Q\x9dhV\xd4\xdc\x8f4P\x18\x00\xfd\xd2\xe1\x00\x00\x00\x00\x00\x0c\n\x15\xc79\x9c\xac,@\xc7\x94k\x9d'

I tried to decode the bytes using the bytes.decode() function using utf-8 decoder like this

raw_bytes.decode("utf-8")

but the result is always

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

so I concluded that this is not text.

I understand that those raw bytes are not utf-8 decodable so I was looking for a way to extract from those bytes, that I think are the fields of a packet, ithe values of each field.

I already tried the solution proposed in this thread but it does not work as I want, also this thread seemed to be useful but it's not the same issue and also it's relative to Java and not Python.

I tried using the scapy module but I don't understand how to use it properly. Can someone help me?

  • 1. Are you sure that the packet contains text in the first place? 2. If you know the packet contains text, don't you know the text's encoding? 3. What do you mean by _a nice formatted packet report_? – Armali Jun 24 '21 at 18:08
  • 1
    Sorry I didn't express myuself wrong. I edited the question to explain better what I need to do. – Riccardo Barbieri Jun 25 '21 at 07:26

0 Answers0