@@ -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,79 @@ 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+ LPBYTE LdrpHandleTlsDataBlock = *(LPDWORD)(SearchContext.Result - 8 ) + LPBYTE (DllBase);
122+ LPBYTE LdrpHandleTlsDataBlockBackup = LdrpHandleTlsDataBlock;
123+
124+ // Search back for LdrpHandleTls
125+ // Search up for 0xCC, and make sure it's not false positive by checking alignment (0x4)
126+ while (*LdrpHandleTlsDataBlock != 0xcc || (((LONGLONG)LdrpHandleTlsDataBlock + 1 ) % 0x4 ) != 0 ) {
127+ // Normally ~0x140 bytes
128+ if (LdrpHandleTlsDataBlockBackup - LdrpHandleTlsDataBlock > 0x400 ) return STATUS_NOT_SUPPORTED;
129+ LdrpHandleTlsDataBlock--;
130+ }
131+ LdrpHandleTlsDataBlock++;
132+ LdrpHandleTlsData = LdrpHandleTlsDataBlock;
133+ return STATUS_SUCCESS;
134+ #else
135+ return STATUS_NOT_SUPPORTED;
136+ #endif
137+ }
138+
139+ static NTSTATUS NTAPI RtlFindLdrpHandleTlsData () {
140+ if (MmpGlobalDataPtr->NtVersions .MajorVersion >= 10 ) {
141+ return RtlFindLdrpHandleTlsData10 ();
142+ }
143+ else {
144+ return RtlFindLdrpHandleTlsDataOld ();
145+ }
146+ }
147+
97148static NTSTATUS NTAPI RtlFindLdrpReleaseTlsEntry () {
98149 NTSTATUS status = STATUS_SUCCESS;
99150 LPCVOID Feature = nullptr ;
@@ -104,23 +155,12 @@ static NTSTATUS NTAPI RtlFindLdrpReleaseTlsEntry() {
104155 case 10 : {
105156 if (MmpGlobalDataPtr->NtVersions .MinorVersion ) return STATUS_NOT_SUPPORTED;
106157
107- if (MmpGlobalDataPtr->NtVersions .BuildNumber >= 22621 ) {
108158#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 ;
159+ Feature = " \x48\x89\x5c\x24\x08\x57\x48\x83\xec\x20\x48\x8b\xfa\x48\x8b\xd9\x48\x85\xd2\x75\x0c " ;
160+ Size = 21 ;
112161#else
113- return STATUS_NOT_SUPPORTED;
162+ return STATUS_NOT_SUPPORTED;
114163#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-
124164 break ;
125165 }
126166 default :
0 commit comments