srounet / Pymem

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

`pymem.pattern.scan_pattern_page` throw windows api error, code 299 #111

Closed SakuraHentai closed 3 months ago

SakuraHentai commented 11 months ago

Describe the bug When scan with address which higher than mbi.BaseAddress, mbi.RegionSize will over read the memory. Ref: https://github.com/srounet/Pymem/blob/4d8380c2e095461783f3f5f214843c40d2ea7684/pymem/pattern.py#L66C1-L66C1

Your Environment

  1. Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
  2. Win10 x64 19045.3570
  3. Pymem 1.13.0

Expected behavior Read correct size

Traceback

start_address = pymem.pattern.pattern_scan_all(pid, aob)
# will throw error next line
end_address = pymem.pattern.scan_pattern_page(pid, start_address, end_aob)

Memory: |-------------------------- page ----------------------------------| mbi.BaseAddress --- address


May read_bytes size be mbi.RegionSize - (address - mbi.BaseAddress)?

jmctune commented 11 months ago

Can concur that this also happens to me. I ended up just catching it and ignoring it, then scanning again.

SnowMeteors commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

SakuraHentai commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

Check your code params, 87 is incorrect params error.

SnowMeteors commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

Check your code params, 87 is incorrect params error.

keyBytes = b'-----BEGIN PUBLIC KEY-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True)

this is my error code,I don't understand why an error was reported。

I have tried running the above code many times, and sometimes I will report an error, although the probability is very low

SakuraHentai commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

Check your code params, 87 is incorrect params error.

keyBytes = b'-----BEGIN PUBLIC KEY-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True)

this is my error code,I don't understand why an error was reported。

I have tried running the above code many times, and sometimes I will report an error, although the probability is very low

Sorry I can't help you. Is the target process still exist when the error throw?

SnowMeteors commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

Check your code params, 87 is incorrect params error.

keyBytes = b'-----BEGIN PUBLIC KEY-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True) this is my error code,I don't understand why an error was reported。 I have tried running the above code many times, and sometimes I will report an error, although the probability is very low

Sorry I can't help you. Is the target process still exist when the error throw?

The target program is running normally

SnowMeteors commented 11 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87

Check your code params, 87 is incorrect params error.

keyBytes = b'-----BEGIN PUBLIC KEY-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True) this is my error code,I don't understand why an error was reported。 I have tried running the above code many times, and sometimes I will report an error, although the probability is very low

Sorry I can't help you. Is the target process still exist when the error throw?

I find the pymem bugs,the pymem.memory.virtual_query(handle, address) fail , throw error。so in pattern_scan_all The repaired code is as follows

def pattern_scan_all(handle, pattern, *, return_multiple=False):
    next_region = 0

    found = []
    user_space_limit = 0x7FFFFFFF0000 if sys.maxsize > 2 ** 32 else 0x7fff0000
    while next_region < user_space_limit:
        try:
            next_region, page_found = pymem.pattern.scan_pattern_page(handle, next_region, pattern,
                                                                      return_multiple=return_multiple)
        except:
            return found

        if not return_multiple and page_found:
            return page_found

        if page_found:
            found += page_found

    if not return_multiple:
        return None

    return found
xaoyaoo commented 10 months ago

like same error File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 166, in pattern_scan_all return_multiple=return_multiple File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py", line 54, in scan_pattern_page mbi = pymem.memory.virtual_query(handle, address) File "C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py", line 1119, in virtual_query raise pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api error, error_code: 87像同样的错误 文件“C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py”,第 166 行,在 pattern_scan_all return_multiple=return_multiple 文件“C:\ProgramData\python3.7.3\lib\site-packages\pymem\pattern.py”,第 54 行,在 scan_pattern_page mbi = pymem.memory.virtual_query(句柄,地址) 文件“C:\ProgramData\python3.7.3\lib\site-packages\pymem\memory.py”,第 1119 行,在virtual_query引发 pymem.exception.WinAPIError(error_code) pymem.exception.WinAPIError: Windows api 错误,error_code:87

Check your code params, 87 is incorrect params error.检查您的代码参数, 87 是否不正确的参数错误。

keyBytes = b'-----BEGIN PUBLIC KEY-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True)keyBytes = b'-----开始公钥-----' publicKeyList = pymem.pattern.pattern_scan_all(self._pm.process_handle, keyBytes, return_multiple=True) this is my error code,I don't understand why an error was reported。这是我的错误代码,我不明白为什么报错了。 I have tried running the above code many times, and sometimes I will report an error, although the probability is very low我尝试过多次运行上面的代码,有时我会报错,虽然概率很低

Sorry I can't help you. Is the target process still exist when the error throw?对不起,我帮不了你。抛出错误时目标进程是否仍然存在?

I find the pymem bugs,the pymem.memory.virtual_query(handle, address) fail , throw error。so in pattern_scan_all The repaired code is as follows我发现pymem错误,pymem.memory.virtual_query(句柄,地址)失败,抛出错误。所以在pattern_scan_all修复后的代码如下

def pattern_scan_all(handle, pattern, *, return_multiple=False):
    next_region = 0

    found = []
    user_space_limit = 0x7FFFFFFF0000 if sys.maxsize > 2 ** 32 else 0x7fff0000
    while next_region < user_space_limit:
        try:
            next_region, page_found = pymem.pattern.scan_pattern_page(handle, next_region, pattern,
                                                                      return_multiple=return_multiple)
        except:
            return found

        if not return_multiple and page_found:
            return page_found

        if page_found:
            found += page_found

    if not return_multiple:
        return None

    return found

Pymem/issues/117 I also encountered the same problem.