Paul3
Paul3

Reputation: 31

Python scapy raw to hex

I'm using Scapy to get raw bytes from packets and I'm trying to parse the hex, however the raw bytes give me this type of output:

b"\x17\x03\x03\x00\xa3\x00\x00\x00\x00\x00\x00\x00\xd6tK\xf8\xc1itQ\xa0;

Some of the string prints as ascii making it difficult to parse, is there a function to get just hex numbers in Scapy or convert this to hex and not print in ascii?

also, chexdump() for some reason only prints

Upvotes: 0

Views: 6234

Answers (2)

f14284
f14284

Reputation: 141

Yes you have :

Use hexdump() to display one or more packets using classic hexdump format

Ref:

https://github.com/secdev/scapy/blob/master/doc/scapy/usage.rst

Scapy is Python lib, then using generator:

>>> [ "%02X"%(ord(x) & 0xff) for x in b'\x20\x56\x30\20' ]
>>> ' '.join([ "0x%02X"%(ord(x) & 0xff) for x in b'\x20\x56\x30\20' ])
>>> ' '.join([ "%02X"%(ord(x) & 0xff) for x in b'\x20\x56\x30\20' ])
>>> ''.join([ "%02X"%(ord(x) & 0xff) for x in b'\x20\x56\x30\20' ])

>>> rawbuffer = b'\x20\x56\x30\20'
>>> bufferArray = [ "%02X"%(ord(x) & 0xff) for x in rawbuffer ]
>>> strbuffer = ''.join(bufferArray)
>>> print(strbuffer)

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113930

import binascii

binascii.hexlify(b"\x17\x03\x03\x00\xa3\x00\x00\x00\x00\x00\x00\x00\xd6tK\xf8\xc1itQ\xa0;")

#outputs => b'17030300a300000000000000d6744bf8c1697451a03b'

is one way that you can get a hex string out of a bytestring

if you really want it grouped into 2's

b" ".join(re.findall(b'..',binascii.hexlify(b"\x17\x03\x03\x00\xa3\x00\x00\x00\x00\x00\x00\x00\xd6tK\xf8\xc1itQ\xa0;")))

should work

Upvotes: 0

Related Questions