|
| 1 | +#include "stdafx.h" |
| 2 | + |
| 3 | +#if (!MMPP_USE_TLS) |
| 4 | + |
| 5 | +static bool stdcall; |
| 6 | +static PVOID LdrpHandleTlsData; |
| 7 | +static PVOID LdrpReleaseTlsEntry; |
| 8 | + |
| 9 | +static NTSTATUS NTAPI RtlFindLdrpHandleTlsData() { |
| 10 | + NTSTATUS status = STATUS_SUCCESS; |
| 11 | + LPCVOID Feature = nullptr; |
| 12 | + BYTE Size = 0; |
| 13 | + WORD OffsetOfFunctionBegin = 0; |
| 14 | + |
| 15 | + switch (MmpGlobalDataPtr->NtVersions.MajorVersion) { |
| 16 | + case 10: { |
| 17 | + if (MmpGlobalDataPtr->NtVersions.MinorVersion)return STATUS_NOT_SUPPORTED; |
| 18 | + |
| 19 | + if (MmpGlobalDataPtr->NtVersions.BuildNumber >= 22621) { |
| 20 | +#ifdef _WIN64 |
| 21 | + Feature = "\x39\x1D\x23\xFC\x17\x00\x74\x37\x44\x8D\x43\x09\x44\x39\x81\x0C\x01\x00\x00\x74\x2A"; |
| 22 | + Size = 22; |
| 23 | + OffsetOfFunctionBegin = 0x43; |
| 24 | +#else |
| 25 | + return STATUS_NOT_SUPPORTED; |
| 26 | +#endif |
| 27 | + } |
| 28 | + // |
| 29 | + // Add more conditions here. |
| 30 | + // |
| 31 | + // else if (MmpGlobalDataPtr->NtVersions.BuildNumber >= XXXXXXXXX) |
| 32 | + else { |
| 33 | + return STATUS_NOT_SUPPORTED; |
| 34 | + } |
| 35 | + |
| 36 | + break; |
| 37 | + } |
| 38 | + case 6: { |
| 39 | + switch (MmpGlobalDataPtr->NtVersions.MinorVersion) { |
| 40 | + //8.1 |
| 41 | + case 3: { |
| 42 | +#ifdef _WIN64 |
| 43 | + Size = 10; |
| 44 | + OffsetOfFunctionBegin = 0x43; |
| 45 | + Feature = "\x44\x8d\x43\x09\x4c\x8d\x4c\x24\x38"; |
| 46 | +#else |
| 47 | + Size = 8; |
| 48 | + OffsetOfFunctionBegin = 0x1B; |
| 49 | + Feature = "\x50\x6a\x09\x6a\x01\x8b\xc1"; |
| 50 | +#endif |
| 51 | + break; |
| 52 | + } |
| 53 | + //8 |
| 54 | + case 2: { |
| 55 | +#ifdef _WIN64 |
| 56 | + Size = 9; |
| 57 | + OffsetOfFunctionBegin = 0x49; |
| 58 | + Feature = "\x48\x8b\x79\x30\x45\x8d\x66\x01"; |
| 59 | +#else |
| 60 | + Size = 7; |
| 61 | + OffsetOfFunctionBegin = 0xC; |
| 62 | + Feature = "\x8b\x45\x08\x89\x45\xa0"; |
| 63 | +#endif |
| 64 | + break; |
| 65 | + } |
| 66 | + //7 |
| 67 | + case 1: { |
| 68 | +#ifdef _WIN64 |
| 69 | + Size = 12; |
| 70 | + OffsetOfFunctionBegin = 0x27; |
| 71 | + Feature = "\x41\xb8\x09\x00\x00\x00\x48\x8d\x44\x24\x38"; |
| 72 | +#else |
| 73 | + Size = 9; |
| 74 | + OffsetOfFunctionBegin = 0x14; |
| 75 | + Feature = "\x74\x20\x8d\x45\xd4\x50\x6a\x09"; |
| 76 | +#endif |
| 77 | + break; |
| 78 | + } |
| 79 | + default:return STATUS_NOT_SUPPORTED; |
| 80 | + } |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + default: { |
| 85 | + return STATUS_NOT_SUPPORTED; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + SEARCH_CONTEXT SearchContext{ SearchContext.SearchPattern = LPBYTE(Feature),SearchContext.PatternSize = Size - 1 }; |
| 90 | + if (!NT_SUCCESS(RtlFindMemoryBlockFromModuleSection(HMODULE(MmpGlobalDataPtr->MmpBaseAddressIndex->NtdllLdrEntry->DllBase), ".text", &SearchContext))) |
| 91 | + return STATUS_NOT_SUPPORTED; |
| 92 | + |
| 93 | + LdrpHandleTlsData = SearchContext.Result - OffsetOfFunctionBegin; |
| 94 | + return status; |
| 95 | +} |
| 96 | + |
| 97 | +static NTSTATUS NTAPI RtlFindLdrpReleaseTlsEntry() { |
| 98 | + NTSTATUS status = STATUS_SUCCESS; |
| 99 | + LPCVOID Feature = nullptr; |
| 100 | + BYTE Size = 0; |
| 101 | + WORD OffsetOfFunctionBegin = 0; |
| 102 | + |
| 103 | + switch (MmpGlobalDataPtr->NtVersions.MajorVersion) { |
| 104 | + case 10: { |
| 105 | + if (MmpGlobalDataPtr->NtVersions.MinorVersion) return STATUS_NOT_SUPPORTED; |
| 106 | + |
| 107 | + if (MmpGlobalDataPtr->NtVersions.BuildNumber >= 22621) { |
| 108 | +#ifdef _WIN64 |
| 109 | + Feature = "\x74\x34\x48\x8B\x08\x48\x39\x41\x08\x75\x65\x48\x8B\x40\x08\x48\x39\x18\x75\x5C\x48\x89\x08"; |
| 110 | + Size = 24; |
| 111 | + OffsetOfFunctionBegin = 0x2F; |
| 112 | +#else |
| 113 | + return STATUS_NOT_SUPPORTED; |
| 114 | +#endif |
| 115 | + } |
| 116 | + // |
| 117 | + // Add more conditions here. |
| 118 | + // |
| 119 | + // else if (MmpGlobalDataPtr->NtVersions.BuildNumber >= XXXXXXXXX) |
| 120 | + else { |
| 121 | + return STATUS_NOT_SUPPORTED; |
| 122 | + } |
| 123 | + |
| 124 | + break; |
| 125 | + } |
| 126 | + default: |
| 127 | + return STATUS_NOT_SUPPORTED; |
| 128 | + } |
| 129 | + |
| 130 | + SEARCH_CONTEXT SearchContext{ SearchContext.SearchPattern = LPBYTE(Feature),SearchContext.PatternSize = Size - 1 }; |
| 131 | + if (!NT_SUCCESS(RtlFindMemoryBlockFromModuleSection(HMODULE(MmpGlobalDataPtr->MmpBaseAddressIndex->NtdllLdrEntry->DllBase), ".text", &SearchContext))) |
| 132 | + return STATUS_NOT_SUPPORTED; |
| 133 | + |
| 134 | + LdrpReleaseTlsEntry = SearchContext.Result - OffsetOfFunctionBegin; |
| 135 | + return status; |
| 136 | +} |
| 137 | + |
| 138 | +BOOL NTAPI MmpTlsInitialize() { |
| 139 | + if (!NT_SUCCESS(RtlFindLdrpHandleTlsData()) || |
| 140 | + !NT_SUCCESS(RtlFindLdrpReleaseTlsEntry())) { |
| 141 | + MmpGlobalDataPtr->MmpFeatures &= ~MEMORY_FEATURE_LDRP_HANDLE_TLS_DATA; |
| 142 | + return FALSE; |
| 143 | + } |
| 144 | + |
| 145 | + stdcall = !RtlIsWindowsVersionOrGreater(6, 3, 0); |
| 146 | + return TRUE; |
| 147 | +} |
| 148 | + |
| 149 | +NTSTATUS NTAPI MmpReleaseTlsEntry(_In_ PLDR_DATA_TABLE_ENTRY lpModuleEntry) { |
| 150 | + typedef NTSTATUS(__stdcall* STDCALL)(PLDR_DATA_TABLE_ENTRY); |
| 151 | + typedef NTSTATUS(__stdcall* THISCALL)(PLDR_DATA_TABLE_ENTRY); |
| 152 | + |
| 153 | + union { |
| 154 | + STDCALL stdcall; |
| 155 | + THISCALL thiscall; |
| 156 | + |
| 157 | + PVOID ptr; |
| 158 | + }fp; |
| 159 | + fp.ptr = LdrpReleaseTlsEntry; |
| 160 | + |
| 161 | + if (fp.ptr) { |
| 162 | + return stdcall ? fp.stdcall(lpModuleEntry): fp.thiscall(lpModuleEntry); |
| 163 | + } |
| 164 | + else { |
| 165 | + return STATUS_NOT_SUPPORTED; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +NTSTATUS NTAPI MmpHandleTlsData(_In_ PLDR_DATA_TABLE_ENTRY lpModuleEntry) { |
| 170 | + typedef NTSTATUS(__stdcall* STDCALL)(PLDR_DATA_TABLE_ENTRY); |
| 171 | + typedef NTSTATUS(__stdcall* THISCALL)(PLDR_DATA_TABLE_ENTRY); |
| 172 | + |
| 173 | + union { |
| 174 | + STDCALL stdcall; |
| 175 | + THISCALL thiscall; |
| 176 | + |
| 177 | + PVOID ptr; |
| 178 | + }fp; |
| 179 | + fp.ptr = LdrpHandleTlsData; |
| 180 | + |
| 181 | + if (fp.ptr) { |
| 182 | + return stdcall ? fp.stdcall(lpModuleEntry) : fp.thiscall(lpModuleEntry); |
| 183 | + } |
| 184 | + else { |
| 185 | + return STATUS_NOT_SUPPORTED; |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +#endif |
0 commit comments