feat scan_and_protect_handles
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
#pragma once
|
||||
#ifndef PROCESS_HANDLE_H
|
||||
#define PROCESS_HANDLE_H
|
||||
#include "Base.h"
|
||||
|
||||
#pragma warning(disable : 4201)
|
||||
struct _EX_PUSH_LOCK
|
||||
{
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
ULONGLONG Locked : 1; //0x0
|
||||
ULONGLONG Waiting : 1; //0x0
|
||||
ULONGLONG Waking : 1; //0x0
|
||||
ULONGLONG MultipleShared : 1; //0x0
|
||||
ULONGLONG Shared : 60; //0x0
|
||||
};
|
||||
ULONGLONG Value; //0x0
|
||||
VOID* Ptr; //0x0
|
||||
};
|
||||
};
|
||||
|
||||
struct _HANDLE_TABLE_FREE_LIST
|
||||
{
|
||||
struct _EX_PUSH_LOCK FreeListLock; //0x0
|
||||
union _HANDLE_TABLE_ENTRY* FirstFreeHandleEntry; //0x8
|
||||
union _HANDLE_TABLE_ENTRY* LastFreeHandleEntry; //0x10
|
||||
LONG HandleCount; //0x18
|
||||
ULONG HighWaterMark; //0x1c
|
||||
};
|
||||
|
||||
|
||||
struct _HANDLE_TABLE_w10
|
||||
{
|
||||
ULONG NextHandleNeedingPool; //0x0
|
||||
LONG ExtraInfoPages; //0x4
|
||||
volatile ULONGLONG TableCode; //0x8
|
||||
struct _EPROCESS* QuotaProcess; //0x10
|
||||
struct _LIST_ENTRY HandleTableList; //0x18
|
||||
ULONG UniqueProcessId; //0x28
|
||||
union
|
||||
{
|
||||
ULONG Flags; //0x2c
|
||||
struct
|
||||
{
|
||||
UCHAR StrictFIFO : 1; //0x2c
|
||||
UCHAR EnableHandleExceptions : 1; //0x2c
|
||||
UCHAR Rundown : 1; //0x2c
|
||||
UCHAR Duplicated : 1; //0x2c
|
||||
UCHAR RaiseUMExceptionOnInvalidHandleClose : 1; //0x2c
|
||||
};
|
||||
};
|
||||
struct _EX_PUSH_LOCK HandleContentionEvent; //0x30
|
||||
struct _EX_PUSH_LOCK HandleTableLock; //0x38
|
||||
union
|
||||
{
|
||||
struct _HANDLE_TABLE_FREE_LIST FreeLists[1]; //0x40
|
||||
struct
|
||||
{
|
||||
UCHAR ActualEntry[32]; //0x40
|
||||
struct _HANDLE_TRACE_DEBUG_INFO* DebugInfo; //0x60
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
struct _HANDLE_TABLE_W7
|
||||
{
|
||||
ULONGLONG TableCode; //0x0
|
||||
struct _EPROCESS* QuotaProcess; //0x8
|
||||
VOID* UniqueProcessId; //0x10
|
||||
struct _EX_PUSH_LOCK HandleLock; //0x18
|
||||
struct _LIST_ENTRY HandleTableList; //0x20
|
||||
struct _EX_PUSH_LOCK HandleContentionEvent; //0x30
|
||||
struct _HANDLE_TRACE_DEBUG_INFO* DebugInfo; //0x38
|
||||
LONG ExtraInfoPages; //0x40
|
||||
union
|
||||
{
|
||||
ULONG Flags; //0x44
|
||||
UCHAR StrictFIFO : 1; //0x44
|
||||
};
|
||||
ULONG FirstFreeHandle; //0x48
|
||||
struct _HANDLE_TABLE_ENTRY_EX* LastFreeHandleEntry; //0x50
|
||||
ULONG HandleCount; //0x58
|
||||
ULONG NextHandleNeedingPool; //0x5c
|
||||
ULONG HandleCountHighWatermark; //0x60
|
||||
};
|
||||
|
||||
|
||||
struct _HANDLE_TABLE_ENTRY_INFO
|
||||
{
|
||||
ULONG AuditMask; //0x0
|
||||
ULONG MaxRelativeAccessMask; //0x4
|
||||
};
|
||||
|
||||
struct _EXHANDLE
|
||||
{
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
ULONG TagBits : 2; //0x0
|
||||
ULONG Index : 30; //0x0
|
||||
};
|
||||
VOID* GenericHandleOverlay; //0x0
|
||||
ULONGLONG Value; //0x0
|
||||
};
|
||||
};
|
||||
|
||||
// Windows x64 stores one handle entry in 16 bytes. The encoded object pointer
|
||||
// is in the first qword and the granted-access mask is in the second.
|
||||
typedef struct _HANDLE_TABLE_ENTRY_EX
|
||||
{
|
||||
union
|
||||
{
|
||||
volatile LONG64 VolatileLowValue;
|
||||
LONG64 LowValue;
|
||||
struct
|
||||
{
|
||||
ULONGLONG Unlocked : 1;
|
||||
ULONGLONG RefCnt : 16;
|
||||
ULONGLONG Attributes : 3;
|
||||
ULONGLONG ObjectPointerBits : 44;
|
||||
};
|
||||
};
|
||||
|
||||
union
|
||||
{
|
||||
LONG64 HighValue;
|
||||
struct
|
||||
{
|
||||
ULONG GrantedAccessBits : 25;
|
||||
ULONG NoRightsUpgrade : 1;
|
||||
ULONG Spare1 : 6;
|
||||
};
|
||||
};
|
||||
} HANDLE_TABLE_ENTRY, * PHANDLE_TABLE_ENTRY;
|
||||
|
||||
|
||||
namespace process_handle
|
||||
{
|
||||
auto scan_and_protect_handles(PEPROCESS process)->BOOL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif // !PROCESS_HANDLE_H
|
||||
Reference in New Issue
Block a user