feat scan_and_protect_handles

This commit is contained in:
2026-10-08 10:06:48 +08:00
parent 218ab6bc5a
commit 8d3ca6ed3e
2 changed files with 338 additions and 0 deletions
+191
View File
@@ -0,0 +1,191 @@
#include "process_handle.h"
#include "utils.h"
#include "protect_filter.h"
namespace process_handle
{
auto get_eprocess_objectTable() -> ULONG
{
auto os = utils::GetVersion();
if (os.dwBuildNumber == 7601 || os.dwBuildNumber == 7600)
return 0x200;
UNICODE_STRING unstr{ 0 };
RtlInitUnicodeString(&unstr, L"PsGetProcessWow64Process");
auto func = (unsigned char*)MmGetSystemRoutineAddress(&unstr);
auto offset = *(int*)(func + 3);
return (ULONG)(offset - 0x10);
}
PHANDLE_TABLE_ENTRY GetHandleTableEntry(ULONG64 TableCode, ULONG handle_value)
{
if (!TableCode)
return nullptr;
if (handle_value < 4 || (handle_value & 3))
return nullptr;
ULONG index = handle_value >> 2;
ULONG level = static_cast<ULONG>(TableCode & 3);
ULONG64 table = TableCode & ~3ULL;
if (!table)
return nullptr;
constexpr ULONG entries_per_page = PAGE_SIZE / sizeof(HANDLE_TABLE_ENTRY);
constexpr ULONG pointers_per_page = PAGE_SIZE / sizeof(ULONG64);
/*
* level 0
*/
if (level == 0)
{
if (index >= entries_per_page)
return nullptr;
auto entries = reinterpret_cast<PHANDLE_TABLE_ENTRY>(table);
return &entries[index];
}
/*
* level 1
*/
if (level == 1)
{
ULONG page_index = index / entries_per_page;
ULONG entry_index = index % entries_per_page;
if (page_index >= pointers_per_page)
return nullptr;
auto table_array = reinterpret_cast<PULONG64>(table);
ULONG64 entry_page = table_array[page_index];
if (!entry_page)
return nullptr;
return reinterpret_cast<PHANDLE_TABLE_ENTRY>(entry_page) + entry_index;
}
/*
* level 2
*/
if (level == 2)
{
constexpr ULONG entries_per_level2 = pointers_per_page * entries_per_page;
ULONG level1_index = index / entries_per_level2;
ULONG remainder = index % entries_per_level2;
ULONG level2_index = remainder / entries_per_page;
ULONG entry_index = remainder % entries_per_page;
if (level1_index >= pointers_per_page)
return nullptr;
auto table_array1 = reinterpret_cast<PULONG64>(table);
ULONG64 table2 = table_array1[level1_index];
if (!table2)
return nullptr;
auto table_array2 = reinterpret_cast<PULONG64>(table2);
if (level2_index >= pointers_per_page)
return nullptr;
ULONG64 entry_page = table_array2[level2_index];
if (!entry_page)
return nullptr;
return reinterpret_cast<PHANDLE_TABLE_ENTRY>(entry_page) + entry_index;
}
return nullptr;
}
auto scan_and_protect_handles(PEPROCESS process) -> BOOL
{
_HANDLE_TABLE_W7* handle_table_w7{ 0 };
_HANDLE_TABLE_w10* handle_table_w10{ 0 };
ULONG64 TableCode = NULL;
ULONG max_handle = 0;
auto os = utils::GetVersion();
if (os.dwBuildNumber == 7601 || os.dwBuildNumber == 7600)
{
handle_table_w7 = *(_HANDLE_TABLE_W7**)((ULONG64)process + get_eprocess_objectTable());
if (!handle_table_w7)
return FALSE;
TableCode = handle_table_w7->TableCode;
max_handle = handle_table_w7->NextHandleNeedingPool;
}
else
{
handle_table_w10 = *(_HANDLE_TABLE_w10**)((ULONG64)process + get_eprocess_objectTable());
if (!handle_table_w10)
return FALSE;
TableCode = handle_table_w10->TableCode;
max_handle = handle_table_w10->NextHandleNeedingPool;
}
if (!TableCode)
return FALSE;
for (ULONG value = 4; value < max_handle; value += 4)
{
HANDLE Handle = (HANDLE)(ULONG_PTR)value;
PEPROCESS Process = nullptr;
auto status = ObReferenceObjectByHandle(Handle, 0, *PsProcessType, UserMode, reinterpret_cast<PVOID*>(&Process), nullptr);
if (!NT_SUCCESS(status))
continue;
auto entry = GetHandleTableEntry(TableCode, value);
if (entry)
{
auto dwPid = PsGetProcessId(Process);
if (dwPid != NULL)
{
if (protect_filter::is_protect_pid(HandleToLong(dwPid)) &&
entry->GrantedAccessBits != 0)
{
if (PsGetProcessId(process) != dwPid)
{
entry->GrantedAccessBits = 0;
}
}
}
}
ObDereferenceObject(Process);
}
return TRUE;
}
}