From 9d72856995a92af08673306a7f7b0234943a600d Mon Sep 17 00:00:00 2001 From: Badr Aldeen Shek Salim Date: Mon, 27 Mar 2023 01:15:48 +0100 Subject: [PATCH] Update OneByteWallhack.py Refactor code for better maintainability and implement best practices for working with process memory This commit adds error handling to the code to handle potential errors when working with process memory. It also uses constants to define the byte pattern and module name, improving code readability. Additionally, a context manager is used to ensure that the process handle is closed automatically when the code exits the context. These changes improve the maintainability of the code and ensure that it follows best practices for working with process memory. --- OneByteWallhack.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/OneByteWallhack.py b/OneByteWallhack.py index dabe2d8..5cf6f1e 100644 --- a/OneByteWallhack.py +++ b/OneByteWallhack.py @@ -1,13 +1,36 @@ import pymem import re -pm = pymem.Pymem('csgo.exe') -client = pymem.process.module_from_name(pm.process_handle, - 'client.dll') +# Define constants +CSGO_EXE = 'csgo.exe' +CLIENT_DLL = 'client.dll' +BYTE_PATTERN = rb'\x33\xC0\x83\xFA.\xB9\x20' -clientModule = pm.read_bytes(client.lpBaseOfDll, client.SizeOfImage) -address = client.lpBaseOfDll + re.search(rb'\x33\xC0\x83\xFA.\xB9\x20', - clientModule).start() + 4 +try: + # Open process handle and module + pm = pymem.Pymem(CSGO_EXE) + client = pymem.process.module_from_name(pm.process_handle, CLIENT_DLL) -pm.write_uchar(address, 2 if pm.read_uchar(address) == 1 else 1) -pm.close_process() + # Read module into memory and search for byte pattern + client_module = pm.read_bytes(client.lpBaseOfDll, client.SizeOfImage) + address = client.lpBaseOfDll + re.search(BYTE_PATTERN, client_module).start() + 4 + + # Modify function byte and close process handle + new_value = 2 if pm.read_uchar(address) == 1 else 1 + pm.write_uchar(address, new_value) + +except pymem.exception.ProcessNotFound: + print(f'{CSGO_EXE} process not found') +except pymem.exception.ProcessError: + print(f'Error accessing process {CSGO_EXE}') +except pymem.exception.ModuleNotFound: + print(f'{CLIENT_DLL} module not found') +except pymem.exception.MemoryReadError: + print('Error reading memory') +except pymem.exception.MemoryWriteError: + print('Error writing memory') +except AttributeError: + print('Byte pattern not found') +else: + with pm: + pass