@@ -6,35 +6,13 @@ static bool stdcall;
66static PVOID LdrpHandleTlsData;
77static PVOID LdrpReleaseTlsEntry;
88
9- static NTSTATUS NTAPI RtlFindLdrpHandleTlsData () {
9+ static NTSTATUS NTAPI RtlFindLdrpHandleTlsDataOld () {
1010 NTSTATUS status = STATUS_SUCCESS;
1111 LPCVOID Feature = nullptr ;
1212 BYTE Size = 0 ;
1313 WORD OffsetOfFunctionBegin = 0 ;
1414
1515 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- }
3816 case 6 : {
3917 switch (MmpGlobalDataPtr->NtVersions .MinorVersion ) {
4018 // 8.1
@@ -94,6 +72,81 @@ static NTSTATUS NTAPI RtlFindLdrpHandleTlsData() {
9472 return status;
9573}
9674
75+ static NTSTATUS NTAPI RtlFindLdrpHandleTlsData10 () {
76+ LPVOID DllBase = MmpGlobalDataPtr->MmpBaseAddressIndex ->NtdllLdrEntry ->DllBase ;
77+ #ifdef _WIN64
78+ // search for LdrpHandleTls string literal
79+ SEARCH_CONTEXT SearchContext{ SearchContext.SearchPattern = LPBYTE (" LdrpHandleTlsData\x00 " ), SearchContext.PatternSize = 18 };
80+ if (!NT_SUCCESS (RtlFindMemoryBlockFromModuleSection (HMODULE (DllBase), " .rdata" , &SearchContext)))
81+ return STATUS_NOT_SUPPORTED;
82+ LPBYTE StringOffset = SearchContext.Result ;
83+
84+ SearchContext.Result = nullptr ;
85+ SearchContext.PatternSize = 3 ;
86+ SearchContext.SearchPattern = LPBYTE (" \x48\x8D\x15 " );
87+ LPBYTE ExceptionBlock = nullptr ;
88+
89+ // Search for lea rdx,[rip+0x????]
90+ // ???? is the relative offset from RIP to LdrpHandleTls string literal
91+ while (NT_SUCCESS (RtlFindMemoryBlockFromModuleSection (HMODULE (DllBase), " .text" , &SearchContext))) {
92+ DWORD InsOff = *(DWORD*)(SearchContext.Result + 3 );
93+ if (StringOffset == SearchContext.Result + InsOff + 7 ) {
94+ ExceptionBlock = SearchContext.Result ;
95+ break ;
96+ }
97+ }
98+ if (!ExceptionBlock) return STATUS_NOT_SUPPORTED;
99+
100+ // Search back for exception block function header
101+ while (*ExceptionBlock != 0xcc ) {
102+ // Normally ~13 bytes, but just in case...
103+ if (SearchContext.Result - ExceptionBlock > 0x50 ) return STATUS_NOT_SUPPORTED;
104+ ExceptionBlock--;
105+ }
106+ ExceptionBlock++;
107+
108+ // search for C_SCOPE_TABLE
109+ union Converter {
110+ BYTE Bytes[4 ];
111+ DWORD Dword;
112+ };
113+ Converter ExceptionBlockAddress{ .Dword = DWORD (ExceptionBlock - LPBYTE (DllBase)) };
114+ SearchContext.Result = nullptr ;
115+ SearchContext.PatternSize = 4 ;
116+ SearchContext.SearchPattern = ExceptionBlockAddress.Bytes ;
117+ if (!NT_SUCCESS (RtlFindMemoryBlockFromModuleSection (HMODULE (DllBase), " .rdata" , &SearchContext)))
118+ return STATUS_NOT_SUPPORTED;
119+
120+ // C_SCOPE_TABLE$$Begin
121+ LPDWORD LdrpHandleTlsBlock = LPDWORD (*(LPDWORD)(SearchContext.Result - 8 ) + LPBYTE (DllBase));
122+ // Pad to 0x04
123+ LdrpHandleTlsBlock = LPDWORD (LONGLONG (LdrpHandleTlsBlock) / 0x04 * 0x04 );
124+ LPDWORD LdrpHandleTlsBlockBackup = LdrpHandleTlsBlock;
125+
126+ // Search back for LdrpHandleTls
127+ // Search up for 4 consecutive 0xCC
128+ while (*LdrpHandleTlsBlock != 0xcccccccc ) {
129+ // Normally ~0x140 bytes
130+ if (LdrpHandleTlsBlockBackup - LdrpHandleTlsBlock > 0x400 ) return STATUS_NOT_SUPPORTED;
131+ LdrpHandleTlsBlock--;
132+ }
133+ LdrpHandleTlsBlock++;
134+ LdrpHandleTlsData = LdrpHandleTlsDataBlock;
135+ return STATUS_SUCCESS;
136+ #else
137+ return STATUS_NOT_SUPPORTED;
138+ #endif
139+ }
140+
141+ static NTSTATUS NTAPI RtlFindLdrpHandleTlsData () {
142+ if (MmpGlobalDataPtr->NtVersions .MajorVersion >= 10 ) {
143+ return RtlFindLdrpHandleTlsData10 ();
144+ }
145+ else {
146+ return RtlFindLdrpHandleTlsDataOld ();
147+ }
148+ }
149+
97150static NTSTATUS NTAPI RtlFindLdrpReleaseTlsEntry () {
98151 NTSTATUS status = STATUS_SUCCESS;
99152 LPCVOID Feature = nullptr ;
@@ -104,23 +157,12 @@ static NTSTATUS NTAPI RtlFindLdrpReleaseTlsEntry() {
104157 case 10 : {
105158 if (MmpGlobalDataPtr->NtVersions .MinorVersion ) return STATUS_NOT_SUPPORTED;
106159
107- if (MmpGlobalDataPtr->NtVersions .BuildNumber >= 22621 ) {
108160#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 ;
161+ Feature = " \x48\x89\x5c\x24\x08\x57\x48\x83\xec\x20\x48\x8b\xfa\x48\x8b\xd9\x48\x85\xd2\x75\x0c " ;
162+ Size = 21 ;
112163#else
113- return STATUS_NOT_SUPPORTED;
164+ return STATUS_NOT_SUPPORTED;
114165#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-
124166 break ;
125167 }
126168 default :
0 commit comments