srounet / Pymem

A python library for windows, providing the needed functions to start working on your own with memory editing.
MIT License
303 stars 45 forks source link

Optimize write_bytes method #100

Closed iconFehu closed 1 year ago

iconFehu commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

Your Environment

  1. python version 3.11
  2. os version 64
  3. pymem version 1.10.0

Expected behavior A clear and concise description of what you expected to happen. An exception is thrown when executing ctypes.windll.kernel32.WriteProcessMemory ctypes.windll.kernel32.WriteProcessMemory(handle, dst, data, length, 0) need ctypes.c_ulong

Traceback

Traceback (most recent call last):
  File "PycharmProjects\demo\test\test10.py", line 47, in <module>
    pm.write_bytes(address + len(AOB), bytes(encoding), len(encoding))
  File "PycharmProjects\demo\venv\Lib\site-packages\pymem\__init__.py", line 925, in write_bytes
    pymem.memory.write_bytes(self.process_handle, address, value, length)
  File "PycharmProjects\demo\venv\Lib\site-packages\pymem\memory.py", line 625, in write_bytes
    res = ctypes.windll.kernel32.WriteProcessMemory(handle, dst, data, length, 0x0)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ctypes.ArgumentError: argument 5: TypeError: expected LP_c_ulong instance instead of int

Additional context Add any other context about the problem here.

OpsecGuy commented 1 year ago

Main post has been edited so I remove my answer

iconFehu commented 1 year ago

According to the error message, it can be seen that it is a type error. Specifically, this error is because the WriteProcessMemory function expects the fifth parameter type to be LP_c_ulong, and is now passing an int type. LP_c_ulong is a ctypes type that represents a pointer to an unsigned long integer, and the int type is not such a type. To fix this, code modified like this is required

res = ctypes.windll.kernel32.WriteProcessMemory(handle, dst, data, length, ctypes.c_ulong(0x0))
iconFehu commented 1 year ago

101