-2

I am trying to make a program that generates shellcode, in Python3, I need to to convert a string to the "\x00\x00" format, for example: "Hello" -> "\x68\x65\x6c\x6c\x6f". I cannot figure this out, if I try to just add "\\x" to "68", it does not work. I have tried to google everything I can, I thought this would be simple, but I'm stuck on it.

  • @Tomerikoo No, I don't know if i explained what I'm looking for well enough: b"\x68" works, if it is hard coded it works fine, I'm looking for a way to take a 68, and turn that into the equivalent of b"\x68" – Jack Smith Dec 29 '20 at 10:46
  • Does this answer your question? [Python convert strings of bytes to byte array](https://stackoverflow.com/questions/51754731/python-convert-strings-of-bytes-to-byte-array) – Tomerikoo Dec 29 '20 at 11:10

1 Answers1

0

Emmm... Actually "Hello" is the same as "\x68\x65\x6c\x6c\x6f"

If you want just to print that or whatever, you can do like that:

>>> print('"' + ''.join(['\\x'+hex(c)[2:] for c in "Hello".encode()]) + '"')
"\x48\x65\x6c\x6c\x6f"

If you want to output bytes (you asked in comments):

>>> print('b\'' + ''.join(['\\x'+hex(c)[2:] for c in "Hello".encode()]) + '\'')
b'\x48\x65\x6c\x6c\x6f'

If you want to send bytes like b"Hello" somewhere, just send them. Yay.