Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Could not allocate memory for shellcode in inject_python_shellcode (Python 3.7.16) #140

Open
yangzdu opened this issue Jun 24, 2024 · 0 comments

Comments

@yangzdu
Copy link

yangzdu commented Jun 24, 2024

Description:

I am encountering an error while using the inject_python_shellcode function from the pymem package. The error message indicates that the memory allocation for the shellcode failed.

Environment:

Python version: 3.7.16
pymem version: 1.3
Operating System: Windows
Error Traceback:

arduino
复制代码
Traceback (most recent call last):
File "C:\Users\test.py", line 46, in
print(test_inject_python_shellcode())
File "C:\Users\test.py", line 38, in test_inject_python_shellcode
pm.inject_python_shellcode(shellcode)
File "C:\Users\yangzdu.conda\envs\VTMP\lib\site-packages\pymem_init_.py", line 175, in inject_python_shellcode
raise RuntimeError('Could not allocate memory for shellcode')
RuntimeError: Could not allocate memory for shellcode
Code to Reproduce:

python
复制代码
import pymem

def test_inject_python_shellcode():
notepad = subprocess.Popen(['notepad.exe'])

pm = pymem.Pymem('notepad.exe')
pm.inject_python_interpreter()

# test already injected
pm.inject_python_interpreter()

assert pm.py_run_simple_string

filepath = os.path.join(os.path.abspath('.'), 'pymem_injection.txt')
filepath = filepath.replace("\\", "\\\\")

shellcode = """

f = open("{}", "w+")
f.write("2131313")
f.close()
""".format(filepath)
pm.inject_python_shellcode(shellcode)

assert os.path.exists(filepath)

os.remove(filepath)
notepad.kill()

Create a new Python file with the code above.
Replace 'some_process.exe' with the actual process you are targeting.
Replace b'...' with the actual shellcode.
Run the script.
Expected Behavior:
The shellcode should be injected without any errors.

Actual Behavior:
The script raises a RuntimeError indicating that it could not allocate memory for the shellcode.

Additional Context:
I have tried different processes and shellcodes, but the error persists. Any help or suggestions would be greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant