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.
Asked
Active
Viewed 1,806 times
-2
-
@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 Answers
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.

CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP
- 977
- 2
- 17
-
This is close to what I'm looking for, I need that output to be b'\x48\x65\x6c\x6c\x6f'. Thats what im stuck on, if i use .encode() it outputs b'\\x48\\x65\\x6c\\x6c\\x6f' – Jack Smith Dec 29 '20 at 11:08
-
`print('b\'' + ''.join(['\\x'+hex(c)[2:] for c in "Hello".encode()]) + '\'')` lol. Output will be like you want, but it is just an imitation. – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:09
-
-
1You both realize that the preceding `b` has a special syntactic meaning and is not simply the letter `'b'`? – Tomerikoo Dec 29 '20 at 11:13
-
@tomerikoo to do what? He wants an output. So he gets it. Should he redefine the default output of bytes? If you send `b'\x48\x65\x6c\x6c\x6f'` to stdout with standard methods, he will get `b'Hello'`. – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:13
-
-
@tomerikoo I know the meaning lol, but how you want to output it in other way? – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:14
-
To do any of that... Simply adding `'b'` to the start of a string doesn't make it a bytes object... It's a literal syntax... See the links I provided under the question to understand the differences... – Tomerikoo Dec 29 '20 at 11:15
-
I didn't propose to make a byte object. It is simply created like this: `"Hello".encode()`. I proposed how to output it... – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:17
-
-
2Man, I am speaking about it. b'Hello' and b'\x48\x65\x6c\x6c\x6f' are the same. If you want to output it in hex here is the answer... – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:19
-
Exactly, @JackSmith you already have that, under the hood. It's just displayed as "Hello". The `b` in the start suggests it's actually a bytes object... – Tomerikoo Dec 29 '20 at 11:20
-
I dont have that. b ='"' + ''.join(['\\x'+hex(c)[2:] for c in "Hello".encode()]) + '"' Litterally is "\\x48\\x65\\x6c\\x6c\\x6f", If i send this with shellcode, the program will not understand that. – Jack Smith Dec 29 '20 at 11:23
-
Great, just send b'Hello' in the shellcode. It will work :D – CPPCPPCPPCPPCPPCPPCPPCPPCPPCPP Dec 29 '20 at 11:24
-
if it is hardcoded like: buf = b"\x48\x65\x6c\x6c\x6f", that works, but i need to be able to create that "buf" in the code – Jack Smith Dec 29 '20 at 11:24
-
AGAIN, it is created as `b"Hello"` which can be achieved by `"Hello".encode()`... `b"\x48\x65\x6c\x6c\x6f" == b"Hello"` – Tomerikoo Dec 29 '20 at 11:25
-
I'm sorry, i see what you are saying. Youre right. I didnt assume that they were seen as the same by the computer. – Jack Smith Dec 29 '20 at 11:26
-
1The problem was i thought i had tried that earlier and it didnt work, it mustve been something else off. Apologies – Jack Smith Dec 29 '20 at 11:27
-
1@JackSmith No worries, we are here to help. Just try to isolate the problem and find out what is the REAL error and edit the question accordingly – Tomerikoo Dec 29 '20 at 11:28
-
1