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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include "DllMain.h"
LPFN_NTQUERYSYSTEMINFORMATIONORIGINALDEF __WhiteListAddrss;
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpvReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: {
HMODULE ModuleHandle = GetModuleHandle(_T("ntdll.dll")); LPVOID OriginalAddress = GetProcAddress(ModuleHandle, "NtQuerySystemInformation"); TCHAR BufferData[0x1000] = { 0 }; _stprintf(BufferData, _T("0x%p"), OriginalAddress);
MessageBox(NULL, BufferData, _T("DLL_PROCESS_ATTACH"), NULL); #if defined(ENVIRONMENT_32) InstallInlineHook_x86(); #endif #if defined(ENVIRONMENT_64) InstallInlineHook_x64(); #endif break; } case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: {
} break;
}
return TRUE; }
void InstallInlineHook_x86() { BYTE v1[10] = { 0, 0, 0, 0, 0, 0xE9, 0, 0, 0, 0 }; BYTE v2[5] = { 0xE9, 0, 0, 0, 0 }; HMODULE ModuleHandle = GetModuleHandle(_T("ntdll.dll")); LPVOID OriginalAddress = GetProcAddress(ModuleHandle, "NtQuerySystemInformation");
LPVOID TrampolineAddress = VirtualAlloc(NULL, 10, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
memcpy(v1, OriginalAddress, 5);
*(DWORD*)(v1 + 6) = (DWORD)OriginalAddress - (DWORD)TrampolineAddress - 5;
memcpy(TrampolineAddress, v1, 10);
__WhiteListAddrss = (LPFN_NTQUERYSYSTEMINFORMATIONORIGINALDEF)TrampolineAddress;
*(DWORD*)&v2[1] = (DWORD)FakeNtQuerySystemInformation - (DWORD)OriginalAddress - 5;
BOOL IsOk = WriteProcessMemory(GetCurrentProcess(), OriginalAddress, v2, sizeof(v2), NULL); } void InstallInlineHook_x64() { BYTE v1[14] = { 0xFF, 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; BYTE v2[13] = { 0, 0, 0, 0, 0, 0, 0, 0, 0xE9, 0, 0, 0, 0 };
BYTE v3[5] = { 0xE9, 0, 0, 0, 0 };
HMODULE ModuleHandle = GetModuleHandle(_T("ntdll.dll")); LPVOID OriginalAddress = GetProcAddress(ModuleHandle, "NtQuerySystemInformation");
LPVOID TrampolineAddress = Allocate2GBRange((ULONG_PTR)OriginalAddress, 27);
*(__int64*)&v1[6] = (__int64)FakeNtQuerySystemInformation; memcpy(TrampolineAddress, v1, 14); memcpy(v2, OriginalAddress, 5);
*(DWORD*)(v2 + 9) = (DWORD)OriginalAddress - 15 - (DWORD)TrampolineAddress - 5; memcpy((LPVOID)((__int64)TrampolineAddress + 15), v2, 13);
__WhiteListAddrss = (LPFN_NTQUERYSYSTEMINFORMATIONORIGINALDEF) ((__int64)TrampolineAddress + (__int64)15); *(DWORD*)&v3[1] = (DWORD)TrampolineAddress - (DWORD)OriginalAddress - 5; BOOL IsOk = WriteProcessMemory(GetCurrentProcess(), OriginalAddress, v3, sizeof(v3), NULL); } PVOID Allocate2GBRange(UINT_PTR VirtualAddress, SIZE_T ViewSize) { static ULONG AllocationGranularity; if (!AllocationGranularity) { SYSTEM_INFO SystemInfo; GetSystemInfo(&SystemInfo); AllocationGranularity = SystemInfo.dwAllocationGranularity; }
UINT_PTR Min, Max, v1, v2 = AllocationGranularity - 1, Mask = ~v2;
Min = VirtualAddress >= 0x80000000 ? (VirtualAddress - 0x80000000 + v2) & Mask : 0; Max = VirtualAddress < (UINTPTR_MAX - 0x80000000) ? (VirtualAddress + 0x80000000) & Mask : UINTPTR_MAX;
MEMORY_BASIC_INFORMATION MemoryBasicInfo; do { if (!VirtualQuery((void*)Min, &MemoryBasicInfo, sizeof(MemoryBasicInfo))) return NULL;
Min = (UINT_PTR)MemoryBasicInfo.BaseAddress + MemoryBasicInfo.RegionSize;
if (MemoryBasicInfo.State == MEM_FREE) { v1 = ((UINT_PTR)MemoryBasicInfo.BaseAddress + v2) & Mask;
if (v1 < Min && ViewSize <= (Min - v1)) { if (v1 = (UINT_PTR)VirtualAlloc((PVOID)v1, ViewSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE)) return (PVOID)v1; } } } while (Min < Max);
return NULL;
}
NTSTATUS _stdcall FakeNtQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, SIZE_T SystemInformationLength, PSIZE_T ReturnLength) { NTSTATUS Status; PSYSTEM_PROCESS_INFORMATION v1 = NULL; PSYSTEM_PROCESS_INFORMATION v2 = NULL;
Status = __WhiteListAddrss(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
if (NT_SUCCESS(Status) && SystemInformationClass == SystemProcessInformation) { v1 = (PSYSTEM_PROCESS_INFORMATION)SystemInformation; v2 = (PSYSTEM_PROCESS_INFORMATION)((PBYTE)v1 + v1->NextEntryOffset);
while (v2->NextEntryOffset != 0) { if (lstrcmpW((&v2->ImageName)->Buffer, L"explorer.exe") == 0) { v1->NextEntryOffset += v2->NextEntryOffset; } v1 = v2; v2 = (PSYSTEM_PROCESS_INFORMATION)((PBYTE)v1 + v1->NextEntryOffset); } }
return Status; }
|