|
| 1 | +import ctypes |
| 2 | +from ctypes import wintypes |
| 3 | +from consts import * |
| 4 | + |
| 5 | +kernel32 = ctypes.windll.kernel32 |
| 6 | + |
| 7 | +def GetProcId(processName): |
| 8 | + procId = None |
| 9 | + hSnap = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) |
| 10 | + |
| 11 | + if (hSnap != INVALID_HANDLE_VALUE): |
| 12 | + procEntry = PROCESSENTRY32() |
| 13 | + procEntry.dwSize = ctypes.sizeof(PROCESSENTRY32) |
| 14 | + |
| 15 | + if (kernel32.Process32First(hSnap, ctypes.byref(procEntry))): |
| 16 | + def processCmp(procEntry): |
| 17 | + if (procEntry.szExeFile.decode('utf-8') == processName): |
| 18 | + nonlocal procId #Means that the variable is not local to the function, and we use the procId from the outer scope (above) |
| 19 | + procId = int(procEntry.th32ProcessID) |
| 20 | + |
| 21 | + processCmp(procEntry) #Check the first process (the one we already have) before we start the loop (so we don't have to check it twice) |
| 22 | + while (kernel32.Process32Next(hSnap, ctypes.byref(procEntry))): |
| 23 | + processCmp(procEntry) |
| 24 | + |
| 25 | + kernel32.CloseHandle(hSnap) |
| 26 | + return procId |
| 27 | + |
| 28 | +def GetModuleBaseAddress(pid, moduleName): |
| 29 | + baseAddress = None |
| 30 | + hSnap = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) |
| 31 | + if (hSnap != INVALID_HANDLE_VALUE): |
| 32 | + modEntry = MODULEENTRY32() |
| 33 | + modEntry.dwSize = ctypes.sizeof(MODULEENTRY32) |
| 34 | + |
| 35 | + if (kernel32.Module32First(hSnap, ctypes.byref(modEntry))): |
| 36 | + def moduleCmp(modEntry): |
| 37 | + if (modEntry.szModule.decode('utf-8') == moduleName): |
| 38 | + nonlocal baseAddress |
| 39 | + baseAddress = int(hex(ctypes.addressof(modEntry.modBaseAddr.contents)), 16) |
| 40 | + |
| 41 | + moduleCmp(modEntry) |
| 42 | + while (kernel32.Module32Next(hSnap, ctypes.byref(modEntry))): |
| 43 | + moduleCmp(modEntry) |
| 44 | + |
| 45 | + kernel32.closeHandle(hSnap) |
| 46 | + return baseAddress |
| 47 | + |
| 48 | +def FindDMAAddy(hProc, base, offsets, arch=64): |
| 49 | + size = 8 |
| 50 | + if (arch == 32): size = 4 |
| 51 | + address = ctypes.c_uint64(base) |
| 52 | + |
| 53 | + for offset in offsets: |
| 54 | + kernel32.ReadProcessMemory(hProc, address, ctypes.byref(address), size, 0) |
| 55 | + address = ctypes.c_uint64(address.value + offset) |
| 56 | + |
| 57 | + return (address.value) |
| 58 | + |
| 59 | +def patchBytes(handle, src, destination, size): |
| 60 | + src = bytes.fromhex(src) |
| 61 | + size = ctypes.c_size_t(size) |
| 62 | + destination = ctypes.c_ulonglong(destination) |
| 63 | + oldProtect = ctypes.wintypes.DWORD() |
| 64 | + |
| 65 | + kernel32.VirtualProtectEx(handle,destination,size, PAGE_EXECUTE_READWRITE, ctypes.byref(oldProtect)) |
| 66 | + kernel32.WriteProcessMemory(handle, destination, src, size, None) |
| 67 | + kernel32.VirtualProtectEx(handle,destination,size, oldProtect, ctypes.byref(oldProtect)) |
| 68 | + |
| 69 | +def nopBytes(handle,destination,size): |
| 70 | + hexString = "" |
| 71 | + for i in range(size): |
| 72 | + hexString += "90" |
| 73 | + patchBytes(handle, hexString, destination, size) |
| 74 | + |
0 commit comments