How does an injected python interpreter become aware and has access to the process it was injected into?

Viewed 353

I've been testing pymem module. If the following code could be called testing:

from pymem import Pymem
import os
import subprocess

# launching notepad as subprocess
notepad_process = subprocess.Popen(['notepad.exe'])

# attaching pymem to the launched notepad process
pm = Pymem('notepad.exe')

# injecting python interpreter
pm.inject_python_interpreter()

# defining path to the textfile
filepath = os.path.join(os.path.abspath('.'), 'pymem_injection.txt')
print(filepath)
# fixing path string
filepath = filepath.replace("\\", "\\\\")
print(filepath)

# payload
# notepad.exe will have an injected interpreter that will execute the following code
shellcode = """
f = open("{}", "w+")
f.write("pymem injection using the pymem module")
f.close()
""".format(filepath)

# executing the payload
pm.inject_python_shellcode(shellcode)

# closing the notepad process
notepad_process.kill()

I do not understand what has to be done next, since the interpreter and notepad are pretty much unaware of each other's existence. For example how can I write text to notepad using the injected interpreter?

0 Answers
Related