6
0
mirror of https://github.com/JKornev/hidden synced 2024-06-28 09:52:05 +00:00
hidden/Hidden/PsTable.c

262 lines
6.6 KiB
C
Raw Normal View History

2016-07-21 23:02:31 +00:00
#include "PsTable.h"
#include "Helper.h"
#define PSTREE_ALLOC_TAG 'rTsP'
RTL_AVL_TABLE g_processTable;
2016-07-21 23:02:31 +00:00
RTL_AVL_TABLE g_hiddenProcessTable;
FAST_MUTEX g_hiddenProcessTableLock;
_Must_inspect_result_
_IRQL_requires_max_(APC_LEVEL)
NTKERNELAPI
NTSTATUS
PsLookupProcessByProcessId(
_In_ HANDLE ProcessId,
_Outptr_ PEPROCESS* Process
);
2017-02-02 22:55:19 +00:00
_Function_class_(RTL_AVL_COMPARE_ROUTINE)
2016-07-21 23:02:31 +00:00
RTL_GENERIC_COMPARE_RESULTS CompareProcessTableEntry(struct _RTL_AVL_TABLE *Table, PVOID FirstStruct, PVOID SecondStruct)
{
PProcessTableEntry first = (PProcessTableEntry)FirstStruct;
PProcessTableEntry second = (PProcessTableEntry)SecondStruct;
UNREFERENCED_PARAMETER(Table);
if (first->processId > second->processId)
return GenericGreaterThan;
if (first->processId < second->processId)
return GenericLessThan;
return GenericEqual;
}
2017-02-02 22:55:19 +00:00
_Function_class_(RTL_AVL_ALLOCATE_ROUTINE)
2016-07-21 23:02:31 +00:00
PVOID AllocateProcessTableEntry(struct _RTL_AVL_TABLE *Table, CLONG ByteSize)
{
UNREFERENCED_PARAMETER(Table);
return ExAllocatePoolWithTag(NonPagedPool, ByteSize, PSTREE_ALLOC_TAG);
}
2017-02-02 22:55:19 +00:00
_Function_class_(RTL_AVL_FREE_ROUTINE)
2016-07-21 23:02:31 +00:00
VOID FreeProcessTableEntry(struct _RTL_AVL_TABLE *Table, PVOID Buffer)
{
UNREFERENCED_PARAMETER(Table);
ExFreePoolWithTag(Buffer, PSTREE_ALLOC_TAG);
}
// API
BOOLEAN AddProcessToProcessTable(PProcessTableEntry entry)
{
BOOLEAN newone = FALSE;
ObReferenceObject(entry->reference);
if (!RtlInsertElementGenericTableAvl(&g_processTable, entry, sizeof(ProcessTableEntry), &newone))
{
ObDereferenceObject(entry->reference);
2016-07-21 23:02:31 +00:00
return FALSE;
}
2016-07-21 23:02:31 +00:00
if (!newone)
{
ObDereferenceObject(entry->reference);
return FALSE;
}
return TRUE;
2016-07-21 23:02:31 +00:00
}
BOOLEAN RemoveProcessFromProcessTable(PProcessTableEntry entry)
{
BOOLEAN result = FALSE;
if (GetProcessInProcessTable(entry))
result = RtlDeleteElementGenericTableAvl(&g_processTable, entry);
if (result)
ObDereferenceObject(entry->reference);
return result;
2016-07-21 23:02:31 +00:00
}
BOOLEAN GetProcessInProcessTable(PProcessTableEntry entry)
{
PProcessTableEntry entry2;
2016-08-28 16:52:50 +00:00
entry2 = (PProcessTableEntry)RtlLookupElementGenericTableAvl(&g_processTable, entry);
if (entry2)
RtlCopyMemory(entry, entry2, sizeof(ProcessTableEntry));
2016-07-21 23:02:31 +00:00
2016-08-28 16:52:50 +00:00
return (entry2 ? TRUE : FALSE);
2016-07-21 23:02:31 +00:00
}
BOOLEAN UpdateProcessInProcessTable(PProcessTableEntry entry)
{
PProcessTableEntry entry2;
entry2 = (PProcessTableEntry)RtlLookupElementGenericTableAvl(&g_processTable, entry);
if (entry2)
{
// We don't want allow modify reference cuz it can lead to resource leak
if (entry->reference != entry2->reference)
return FALSE;
2016-08-28 16:52:50 +00:00
RtlCopyMemory(entry2, entry, sizeof(ProcessTableEntry));
}
2016-07-21 23:02:31 +00:00
2016-08-28 16:52:50 +00:00
return (entry2 ? TRUE : FALSE);
2016-07-21 23:02:31 +00:00
}
// Initialization
NTSTATUS InitializeProcessTable(VOID(*InitProcessEntryCallback)(PProcessTableEntry, PCUNICODE_STRING, HANDLE))
{
PSYSTEM_PROCESS_INFORMATION processInfo = NULL, first;
NTSTATUS status;
2016-10-14 23:47:00 +00:00
SIZE_T size = 0, offset;
2016-07-21 23:02:31 +00:00
// Init process table
RtlInitializeGenericTableAvl(&g_processTable, CompareProcessTableEntry, AllocateProcessTableEntry, FreeProcessTableEntry, NULL);
// We should query processes information for creation process table for existing processes
status = QuerySystemInformation(SystemProcessInformation, &processInfo, &size);
if (!NT_SUCCESS(status))
{
2018-12-02 21:56:39 +00:00
LogError("Error, query system information(pslist) failed with code:%08x", status);
2016-07-21 23:02:31 +00:00
return status;
}
offset = 0;
first = processInfo;
do
{
ProcessTableEntry entry;
PUNICODE_STRING procName;
CLIENT_ID clientId;
OBJECT_ATTRIBUTES attribs;
HANDLE hProcess;
PEPROCESS process = 0;
2016-07-21 23:02:31 +00:00
// Get process path
processInfo = (PSYSTEM_PROCESS_INFORMATION)((SIZE_T)processInfo + offset);
if (processInfo->ProcessId == 0)
{
offset = processInfo->NextEntryOffset;
continue;
}
2016-07-21 23:02:31 +00:00
InitializeObjectAttributes(&attribs, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
clientId.UniqueProcess = processInfo->ProcessId;
clientId.UniqueThread = 0;
status = ZwOpenProcess(&hProcess, 0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, &attribs, &clientId);
2016-07-21 23:02:31 +00:00
if (!NT_SUCCESS(status))
{
2018-12-02 21:56:39 +00:00
LogWarning("Warning, can't open process (pid:%p) failed with code:%08x", processInfo->ProcessId, status);
2016-07-21 23:02:31 +00:00
offset = processInfo->NextEntryOffset;
continue;
}
status = QueryProcessInformation(ProcessImageFileName, hProcess, &procName, &size);
ZwClose(hProcess);
if (!NT_SUCCESS(status))
{
2018-12-02 21:56:39 +00:00
LogWarning("Warning, query process information(pid:%p) failed with code:%08x", processInfo->ProcessId, status);
2016-07-21 23:02:31 +00:00
offset = processInfo->NextEntryOffset;
continue;
}
// Add process in process table
RtlZeroMemory(&entry, sizeof(entry));
entry.processId = processInfo->ProcessId;
status = PsLookupProcessByProcessId(processInfo->ProcessId, &process);
if (!NT_SUCCESS(status))
{
LogWarning("Warning, lookup eprocess (pid:%p) failed with code:%08x", processInfo->ProcessId, status);
FreeInformation(procName);
offset = processInfo->NextEntryOffset;
continue;
}
entry.reference = process;
2018-12-02 21:56:39 +00:00
LogTrace("New process: %p, %wZ", processInfo->ProcessId, procName);
2016-07-21 23:02:31 +00:00
InitProcessEntryCallback(&entry, procName, processInfo->InheritedFromProcessId);
if (!AddProcessToProcessTable(&entry))
2018-12-02 21:56:39 +00:00
LogWarning("Warning, can't add process(pid:%p) to process table", processInfo->ProcessId);
2016-07-21 23:02:31 +00:00
ObDereferenceObject(process);
if (entry.excluded)
2018-12-02 21:56:39 +00:00
LogTrace(" excluded process:%p", entry.processId);
if (entry.protected)
2018-12-02 21:56:39 +00:00
LogTrace(" protected process:%p", entry.processId);
if (entry.subsystem)
2018-12-02 21:56:39 +00:00
LogTrace(" subsystem process:%p", entry.processId);
if (entry.hidden)
LogTrace(" hidden process:%p", entry.processId);
2016-07-21 23:02:31 +00:00
// Go to next
FreeInformation(procName);
offset = processInfo->NextEntryOffset;
}
while (offset);
FreeInformation(first);
2018-12-02 21:56:39 +00:00
LogTrace("Initialization is completed");
2016-07-21 23:02:31 +00:00
return status;
}
VOID ClearProcessTable(VOID(*CleanupCallback)(PProcessTableEntry))
2016-07-21 23:02:31 +00:00
{
PProcessTableEntry entry;
PVOID restartKey = NULL;
for (entry = RtlEnumerateGenericTableWithoutSplayingAvl(&g_processTable, &restartKey);
entry != NULL;
entry = RtlEnumerateGenericTableWithoutSplayingAvl(&g_processTable, &restartKey))
2016-07-21 23:02:31 +00:00
{
if (CleanupCallback)
CleanupCallback(entry);
ObDereferenceObject(entry->reference);
2016-07-21 23:02:31 +00:00
if (!RtlDeleteElementGenericTableAvl(&g_processTable, entry))
2018-12-02 21:56:39 +00:00
LogWarning("Warning, can't remove element from process table, looks like memory leak");
2016-07-21 23:02:31 +00:00
restartKey = NULL; // reset enum
}
2018-12-02 21:56:39 +00:00
LogTrace("Deinitialization is completed");
2016-07-21 23:02:31 +00:00
}
VOID EnumProcessTable(VOID(*EnumCallback)(PProcessTableEntry))
{
PProcessTableEntry entry;
PVOID restartKey = NULL;
for (entry = RtlEnumerateGenericTableWithoutSplayingAvl(&g_processTable, &restartKey);
entry != NULL;
entry = RtlEnumerateGenericTableWithoutSplayingAvl(&g_processTable, &restartKey))
{
EnumCallback(entry);
}
}