vsantiago113 / ReadWriteMemory

This module can read and write to the memory of any process on Windows.
MIT License
148 stars 36 forks source link

AttributeError: 'Process' object has no attribute 'get_modules' #24

Open MeblIkea opened 2 years ago

MeblIkea commented 2 years ago

Hi, I have the same problem of others people with the get_pointer function who can't find the address because it needs the base address. So I tried

base_address = process.get_modules()[0]
process.get_pointer(process.get_modules()[0] + address, offsets=offsets)

as said @MillhioreBT in issue #12, but I have this error AttributeError: 'Process' object has no attribute 'get_modules'.

I'm it with Python 3.10, and the module version is 0.1.5. Thx!

MillhioreBT commented 2 years ago
from ReadWriteMemory import ReadWriteMemory

rwm = ReadWriteMemory()

process = rwm.get_process_by_name('ac_client.exe')
process.open()
base_address = process.get_modules()[0]
MeblIkea commented 2 years ago

Hi, sorry for late answer. my python says that get_modules doesn't exist. Here is my code:

from ReadWriteMemory import ReadWriteMemory

rwm = ReadWriteMemory()

process = rwm.get_process_by_name('LandlordsSuper.exe')
process.open()

energy_pointer = process.get_pointer(process.get_modules()[0]+0x019E4E08, offsets=[0x10, 0x38, 0x4E8, 0x28, 0x110, 0xB0, 0x18])

energy = process.read(energy_pointer)

print({'Energy': energy})

Here is the error:

Traceback (most recent call last):
  File "C:\Users\MeblIkea\dev\python\MemoryReadingTest\main.py", line 8, in <module>
    money_pointer = process.get_pointer(process.get_modules()[0]+0x019E4E08, offsets=[0x10, 0x38, 0x4E8, 0x28, 0x110, 0xB0, 0x18])
AttributeError: 'Process' object has no attribute 'get_modules'

Here is my modules on PyCharm: Capture

MeblIkea commented 2 years ago

Hi, so I fixed my problem, with copy-paste init.py from this repo to the init.py in my modules folders.

Now I have another problem, I can't figure out why I cant read a value from my game. Can you help me please?, Thanks. Capture

MillhioreBT commented 2 years ago

You are trying to read a float, the read method only returns integers and only if they are not very long. I have not tried but you can try to test these two methods:

read_float

    def read_float(self, lp_base_address: int) -> Any:
        """
        Read data from the process's memory.

        :param lp_base_address: The process's pointer

        :return: The data from the process's memory if succeed if not raises an exception.
        """
        try:
            read_buffer = ctypes.c_float()
            lp_buffer = ctypes.byref(read_buffer)
            n_size = ctypes.sizeof(read_buffer)
            lp_number_of_bytes_read = ctypes.c_float(0)
            ctypes.windll.kernel32.ReadProcessMemory(self.handle, ctypes.c_void_p(lp_base_address), lp_buffer,
                                                     n_size, lp_number_of_bytes_read)
            return read_buffer.value
        except (BufferError, ValueError, TypeError) as error:
            if self.handle:
                self.close()
            self.error_code = self.get_last_error()
            error = {'msg': str(error), 'Handle': self.handle, 'PID': self.pid,
                     'Name': self.name, 'ErrorCode': self.error_code}
            ReadWriteMemoryError(error)

write_float

    def write_float(self, lp_base_address: int, value: float) -> bool:
        """
        Write data to the process's memory.

        :param lp_base_address: The process' pointer.
        :param value: The data to be written to the process's memory

        :return: It returns True if succeed if not it raises an exception.
        """
        try:
            write_buffer = ctypes.c_float(value)
            lp_buffer = ctypes.byref(write_buffer)
            n_size = ctypes.sizeof(write_buffer)
            lp_number_of_bytes_written = ctypes.c_float(0)
            ctypes.windll.kernel32.WriteProcessMemory(self.handle, ctypes.c_void_p(lp_base_address), lp_buffer,
                                                      n_size, lp_number_of_bytes_written)
            return True
        except (BufferError, ValueError, TypeError) as error:
            if self.handle:
                self.close()
            self.error_code = self.get_last_error()
            error = {'msg': str(error), 'Handle': self.handle, 'PID': self.pid,
                     'Name': self.name, 'ErrorCode': self.error_code}
            ReadWriteMemoryError(error)

for example I have a program that needs to read a memory address that contains a uint64_t and I have to use a custom method that is not by default in this repository for now.

    def read_long(self, lp_base_address: int) -> Any:
        """
        Read data from the process's memory.

        :param lp_base_address: The process's pointer

        :return: The data from the process's memory if succeed if not raises an exception.
        """
        try:
            read_buffer = ctypes.c_ulonglong()
            lp_buffer = ctypes.byref(read_buffer)
            n_size = ctypes.sizeof(read_buffer)
            lp_number_of_bytes_read = ctypes.c_ulonglong(0)
            ctypes.windll.kernel32.ReadProcessMemory(self.handle, ctypes.c_void_p(lp_base_address), lp_buffer,
                                                     n_size, lp_number_of_bytes_read)
            return read_buffer.value
        except (BufferError, ValueError, TypeError) as error:
            if self.handle:
                self.close()
            self.error_code = self.get_last_error()
            error = {'msg': str(error), 'Handle': self.handle, 'PID': self.pid,
                     'Name': self.name, 'ErrorCode': self.error_code}
            ReadWriteMemoryError(error)