Skip to content

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

@yangzdu

Description

@yangzdu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions