Search code examples
python-3.xpyshark

How to get packet.tcp.payload and packet.http.data as string?


The return value for these attributes are in hex format seperated by ':' Eg : 70:79:f6:2e: something like this. When I am trying to decode it to plain string ( human readable ) it doesn't work. What encoding is being used? I tried various different methods like codecs.decode(), binascii.unhexlify(), bytes.fromhex() also different encodings ASCII and UTF-8. Nothing worked, any help is appreciated. I am using python 3.6


Solution

  • Thanks for your question! I believe you're wanting to read the payload in chunks of two hex places. The functions you tried are not able to parse the : delimiter out-of-the-box. Something like splitting the string by the : delimiter, converting their values to human-readable characters, and joining the "list" to a string should do the trick.

    hex_string = '70:79:f6:2e'
    
    hex_split = hex_string.split(':')
    hex_as_chars = map(lambda hex: chr(int(hex, 16)), hex_split)
    
    human_readable = ''.join(hex_as_chars)
    print(human_readable)
    

    Is this what you have in mind?