Created
September 21, 2025 14:26
-
-
Save mnixry/db67203482d616c0dc98da9c603d9796 to your computer and use it in GitHub Desktop.
Replace space in string to underscore w/ x86_64 shellcode in Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import mmap | |
def replace_space_to_underscore(input_: str): | |
assert input_.isascii() | |
buf = ctypes.create_string_buffer(input_.encode()) | |
shellcode = ( | |
b"\x48\xbf" | |
+ ctypes.addressof(buf).to_bytes(8, "little") | |
+ b"\x48\xbe" | |
+ len(input_).to_bytes(8, "little") | |
+ b"\x48\x89\xf1\x48\x85\xc9\x74\x10\x80\x3f\x20\x75\x03\xc6\x07\x5f\x48\xff\xc7\x48\xff\xc9\x75\xf0\xc3" | |
) | |
mem = mmap.mmap( | |
-1, | |
mmap.PAGESIZE, | |
mmap.MAP_SHARED, | |
mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC, | |
) | |
mem.write(shellcode) | |
addr = int.from_bytes(ctypes.string_at(id(mem) + 16, 8), "little") | |
functype = ctypes.CFUNCTYPE(ctypes.c_void_p) | |
fn = functype(addr) | |
fn() | |
return buf.value.decode() | |
print(replace_space_to_underscore("Back to python!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment