def enumerate_handles(): size_needed = ULONG() size = 0x1000 buffer = ctypes.c_buffer(size) try: winproxy.NtQuerySystemInformation( 16, buffer, size, ReturnLength=ctypes.byref(size_needed)) except WindowsError as e: pass size = size_needed.value + 0x1000 buffer = ctypes.c_buffer(size) winproxy.NtQuerySystemInformation(16, buffer, size, ReturnLength=ctypes.byref(size_needed)) x = SYSTEM_HANDLE_INFORMATION.from_buffer(buffer) class _GENERATED_SYSTEM_HANDLE_INFORMATION(ctypes.Structure): _fields_ = [ ("HandleCount", ULONG), ("Handles", Handle * x.HandleCount), ] return list( _GENERATED_SYSTEM_HANDLE_INFORMATION.from_buffer_copy( buffer[:size_needed.value]).Handles)
def enumerate_handles(): if windows.current_process.is_wow_64: return enumerate_handles_syswow64() size_needed = gdef.ULONG() # Should at least be sizeof(gdef.SYSTEM_HANDLE_INFORMATION) tmp_buffer = windows.utils.BUFFER(gdef.SYSTEM_HANDLE_INFORMATION)() try: winproxy.NtQuerySystemInformation(gdef.SystemHandleInformation, tmp_buffer, tmp_buffer.real_size, ReturnLength=ctypes.byref(size_needed)) except WindowsError as e: pass size = size_needed.value + 0x1000 # In case we have some more handle created buf = windows.utils.BUFFER(gdef.SYSTEM_HANDLE_INFORMATION)(size=size) size_needed.value = 0 winproxy.NtQuerySystemInformation(gdef.SystemHandleInformation, buf, buf.real_size, ReturnLength=ctypes.byref(size_needed)) handle_array = windows.utils.resized_array(buf[0].Handles, buf[0].HandleCount, Handle) return list(handle_array)
def enumerate_kernel_modules(): if windows.current_process.is_wow_64: return enumerate_kernel_modules_syswow64() cbsize = gdef.DWORD() winproxy.NtQuerySystemInformation(gdef.SystemModuleInformation, None, 0, ctypes.byref(cbsize)) raw_buffer = (cbsize.value * gdef.BYTE)() buffer = gdef.SYSTEM_MODULE_INFORMATION.from_address( ctypes.addressof(raw_buffer)) winproxy.NtQuerySystemInformation(gdef.SystemModuleInformation, ctypes.byref(raw_buffer), ctypes.sizeof(raw_buffer), ctypes.byref(cbsize)) modules = (SystemModule * buffer.ModulesCount).from_buffer( raw_buffer, gdef.SYSTEM_MODULE_INFORMATION.Modules.offset) return list(modules)