Skip to content

Commit

Permalink
modifications after PR to make custom versions of screeninfo and pyme…
Browse files Browse the repository at this point in the history
…m work (syntax errors and some cyclical import statements that needed fixing)
  • Loading branch information
oskros committed Dec 12, 2021
1 parent e126236 commit ad00403
Show file tree
Hide file tree
Showing 25 changed files with 310 additions and 313 deletions.
316 changes: 158 additions & 158 deletions libs/pymem/__init__.py

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions libs/pymem/memory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ctypes
import struct

from libs import pymem.exception
from libs import pymem.ressources.kernel32
from libs import pymem.ressources.structure
import libs.pymem.exception
import libs.pymem.ressources.kernel32
import libs.pymem.ressources.structure


def allocate_memory(handle, size, allocation_type=None, protection_type=None):
Expand All @@ -19,9 +19,9 @@ def allocate_memory(handle, size, allocation_type=None, protection_type=None):
The handle must have the PROCESS_VM_OPERATION access right.
size: int
The size of the region of memory to allocate, in bytes.
allocation_type: pymem.ressources.structure.MEMORY_STATE
allocation_type: libs.pymem.ressources.structure.MEMORY_STATE
The type of memory allocation.
protection_type: pymem.ressources.structure.MEMORY_PROTECTION
protection_type: libs.pymem.ressources.structure.MEMORY_PROTECTION
The memory protection for the region of pages to be allocated.
Returns
Expand All @@ -30,11 +30,11 @@ def allocate_memory(handle, size, allocation_type=None, protection_type=None):
The address of the allocated region of pages.
"""
if not allocation_type:
allocation_type = pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT.value
allocation_type = libs.pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT.value
if not protection_type:
protection_type = pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READWRITE.value
protection_type = libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READWRITE.value
ctypes.windll.kernel32.SetLastError(0)
address = pymem.ressources.kernel32.VirtualAllocEx(handle, None, size, allocation_type, protection_type)
address = libs.pymem.ressources.kernel32.VirtualAllocEx(handle, None, size, allocation_type, protection_type)
return address


Expand All @@ -50,7 +50,7 @@ def free_memory(handle, address, free_type=None):
The handle must have the PROCESS_VM_OPERATION access right.
address: int
An address of the region of memory to be freed.
free_type: pymem.ressources.structure.MEMORY_PROTECTION
free_type: libs.pymem.ressources.structure.MEMORY_PROTECTION
The type of free operation.
Returns
Expand All @@ -59,9 +59,9 @@ def free_memory(handle, address, free_type=None):
A boolean indicating if the call was a success.
"""
if not free_type:
free_type = pymem.ressources.structure.MEMORY_STATE.MEM_RELEASE
free_type = libs.pymem.ressources.structure.MEMORY_STATE.MEM_RELEASE
ctypes.windll.kernel32.SetLastError(0)
ret = pymem.ressources.kernel32.VirtualFreeEx(handle, address, 0, free_type)
ret = libs.pymem.ressources.kernel32.VirtualFreeEx(handle, address, 0, free_type)
return ret


Expand Down Expand Up @@ -98,11 +98,11 @@ def read_bytes(handle, address, byte):
buff = ctypes.create_string_buffer(byte)
bytes_read = ctypes.c_size_t()
ctypes.windll.kernel32.SetLastError(0)
pymem.ressources.kernel32.ReadProcessMemory(handle, ctypes.c_void_p(address), ctypes.byref(buff), byte, ctypes.byref(bytes_read))
libs.pymem.ressources.kernel32.ReadProcessMemory(handle, ctypes.c_void_p(address), ctypes.byref(buff), byte, ctypes.byref(bytes_read))
error_code = ctypes.windll.kernel32.GetLastError()
if error_code:
ctypes.windll.kernel32.SetLastError(0)
raise pymem.exception.WinAPIError(error_code)
raise libs.pymem.exception.WinAPIError(error_code)
raw = buff.raw
return raw

Expand Down Expand Up @@ -585,7 +585,7 @@ def write_bytes(handle, address, data, length):
error_code = ctypes.windll.kernel32.GetLastError()
if error_code:
ctypes.windll.kernel32.SetLastError(0)
raise pymem.exception.WinAPIError(error_code)
raise libs.pymem.exception.WinAPIError(error_code)
return res


Expand Down Expand Up @@ -1051,8 +1051,8 @@ def virtual_query(handle, address):
MEMORY_BASIC_INFORMATION
A memory basic information object
"""
mbi = pymem.ressources.structure.MEMORY_BASIC_INFORMATION()
mbi = libs.pymem.ressources.structure.MEMORY_BASIC_INFORMATION()
mbi_pointer = ctypes.byref(mbi)
size = ctypes.sizeof(mbi)
pymem.ressources.kernel32.VirtualQueryEx(handle, address, mbi_pointer, size)
libs.pymem.ressources.kernel32.VirtualQueryEx(handle, address, mbi_pointer, size)
return mbi
48 changes: 24 additions & 24 deletions libs/pymem/pattern.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import struct

from libs import pymem.memory
from libs import pymem.ressources.kernel32
from libs import pymem.ressources.structure
import libs.pymem.memory
import libs.pymem.ressources.kernel32
import libs.pymem.ressources.structure


def scan_pattern_page(handle, address, pattern, mask):
Expand All @@ -27,19 +27,19 @@ def scan_pattern_page(handle, address, pattern, mask):
tuple
next_region, found address
"""
mbi = pymem.memory.virtual_query(handle, address)
mbi = libs.pymem.memory.virtual_query(handle, address)
next_region = mbi.BaseAddress + mbi.RegionSize
allowed_protections = [
pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READ,
pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READWRITE,
pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READWRITE,
pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READONLY,
libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READ,
libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READWRITE,
libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READWRITE,
libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READONLY,
]
if (mbi.state != pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT or
if (mbi.state != libs.pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT or
mbi.protect not in allowed_protections
):
return next_region, []
page_bytes = pymem.memory.read_bytes(handle, address, mbi.RegionSize)
page_bytes = libs.pymem.memory.read_bytes(handle, address, mbi.RegionSize)

found = None
for offset in range(0, (mbi.RegionSize - len(pattern)), 1):
Expand Down Expand Up @@ -76,14 +76,14 @@ def scan_string_page(handle, address, search):
tuple
next_region, found address
"""
mbi = pymem.memory.virtual_query(handle, address)
mbi = libs.pymem.memory.virtual_query(handle, address)
next_region = mbi.BaseAddress + mbi.RegionSize
if (mbi.state != pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT or
mbi.protect != pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READONLY and
mbi.type != pymem.ressources.structure.MEMORY_TYPES.MEM_IMAGE
if (mbi.state != libs.pymem.ressources.structure.MEMORY_STATE.MEM_COMMIT or
mbi.protect != libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_READONLY and
mbi.type != libs.pymem.ressources.structure.MEMORY_TYPES.MEM_IMAGE
):
return next_region, []
page_bytes = pymem.memory.read_bytes(handle, address, mbi.RegionSize)
page_bytes = libs.pymem.memory.read_bytes(handle, address, mbi.RegionSize)

found = None
for offset in range(0, (mbi.RegionSize - len(search)), 1):
Expand All @@ -101,12 +101,12 @@ def scan_string_page(handle, address, search):
def search_real_address(handle, address, found_address):
"""
"""
mbi = pymem.memory.virtual_query(handle, address)
mbi = libs.pymem.memory.virtual_query(handle, address)
next_region = mbi.BaseAddress + mbi.RegionSize
# Search within code sections
if (mbi.protect != pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READ):
if (mbi.protect != libs.pymem.ressources.structure.MEMORY_PROTECTION.PAGE_EXECUTE_READ):
return next_region, None
page_bytes = pymem.memory.read_bytes(handle, address, mbi.RegionSize)
page_bytes = libs.pymem.memory.read_bytes(handle, address, mbi.RegionSize)

found = None
for i in range(len(page_bytes)):
Expand Down Expand Up @@ -153,10 +153,10 @@ def string_scan_module(handle, module, search):
Examples
--------
>>> p = pymem.Pymem()
>>> p = libs.pymem.Pymem()
>>> p.open_process_from_name("Gw2-64.exe")
>>> module = pymem.process.module_from_name(p.process_handle, "Gw2-64.exe")
>>> GetContext_address = pymem.pattern.pattern_scan_module(p.process_handle, module, GetContext_pattern, GetContext_mask)
>>> module = libs.pymem.process.module_from_name(p.process_handle, "Gw2-64.exe")
>>> GetContext_address = libs.pymem.pattern.pattern_scan_module(p.process_handle, module, GetContext_pattern, GetContext_mask)
"""
base_address = module.lpBaseOfDll
max_address = module.lpBaseOfDll + module.SizeOfImage
Expand Down Expand Up @@ -206,12 +206,12 @@ def pattern_scan_module(handle, module, pattern, mask):
Examples
--------
>>> p = pymem.Pymem()
>>> p = libs.pymem.Pymem()
>>> p.open_process_from_name("Gw2-64.exe")
>>> GetContext_pattern = b"\\x65\\x48\\x8B\\x04\\x25\\x58\\x00\\x00\\x00\\xBA\\x08\\x00\\x00\\x00"
>>> GetContext_mask = "x" * 14
>>> module = pymem.process.module_from_name(p.process_handle, "Gw2-64.exe")
>>> GetContext_address = pymem.pattern.pattern_scan_module(p.process_handle, module, GetContext_pattern, GetContext_mask )
>>> module = libs.pymem.process.module_from_name(p.process_handle, "Gw2-64.exe")
>>> GetContext_address = libs.pymem.pattern.pattern_scan_module(p.process_handle, module, GetContext_pattern, GetContext_mask )
"""
base_address = module.lpBaseOfDll
max_address = module.lpBaseOfDll + module.SizeOfImage
Expand Down
Loading

0 comments on commit ad00403

Please sign in to comment.