Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert a hexstring to shellcode format?

I have a bytes key define as:

KEY = b’\xb5\x89\xd5\x03\x03\x96`\x9dq\xa7\x81\xed\xb2gYR’

I want this to be formatted like shellcode, i.e two hexa characters like: \x41\x42\x43…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So I tried to do it like this:

KEY = b'\xb5\x89\xd5\x03\x03\x96`\x9dq\xa7\x81\xed\xb2gYR'
hexkey = KEY.hex()
l = []

for i in range(0, len(hexkey) - 2, 2):
    b = '\\x' + hexkey[i] + hexkey[i+1]
    l.append(b)

but this isn’t escaping the backslash for some reason, I get this output:

[‘\\xb5’, ‘\\x89’, ‘\\xd5’, ‘\\x03’, ‘\\x03’, ‘\\x96’, ‘\\x60’,
‘\\x9d’, ‘\\x71’, ‘\\xa7’, ‘\\x81’, ‘\\xed’, ‘\\xb2’, ‘\\x67’,
‘\\x59’]

what am i doing wrong? Is there a better way to do this?

>Solution :

.join() and print them. You can iterate over the bytes directly as well:

KEY = b'\xb5\x89\xd5\x03\x03\x96`\x9dq\xa7\x81\xed\xb2gYR'
print(''.join(f'\\x{b:02x}' for b in KEY))

Output:

\xb5\x89\xd5\x03\x03\x96\x60\x9d\x71\xa7\x81\xed\xb2\x67\x59\x52
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading