@@ -158,22 +158,53 @@ PVOID FindLdrpInvertedFunctionTable64() {
158158#define FindLdrpInvertedFunctionTable FindLdrpInvertedFunctionTable64
159159#endif
160160
161+ BOOL IsValidLdrpHashTable (PLIST_ENTRY LdrpHashTable) {
162+
163+ //
164+ // Additional checks are performed to ensure that the LdrpHashTable is valid.
165+ //
166+
167+ __try {
168+
169+ for (ULONG i = 0 ; i < LDR_HASH_TABLE_ENTRIES; ++i) {
170+ PLIST_ENTRY head = &LdrpHashTable[i], entry = head->Flink ;
171+
172+ while (head != entry) {
173+ PLDR_DATA_TABLE_ENTRY current = CONTAINING_RECORD (entry, LDR_DATA_TABLE_ENTRY, LDR_DATA_TABLE_ENTRY::HashLinks);
174+
175+ if (LdrHashEntry (current->BaseDllName ) != i) {
176+ return FALSE ;
177+ }
178+
179+ entry = entry->Flink ;
180+ }
181+ }
182+
183+ return TRUE ;
184+ }
185+ __except (EXCEPTION_EXECUTE_HANDLER) {
186+ return FALSE ;
187+ }
188+
189+ }
190+
161191PLIST_ENTRY FindLdrpHashTable () {
162- PLIST_ENTRY list = nullptr ;
163192 PLIST_ENTRY head = &NtCurrentPeb ()->Ldr ->InInitializationOrderModuleList , entry = head->Flink ;
164- PLDR_DATA_TABLE_ENTRY CurEntry = nullptr ;
193+
165194 while (head != entry) {
166- CurEntry = CONTAINING_RECORD (entry, LDR_DATA_TABLE_ENTRY, LDR_DATA_TABLE_ENTRY::InInitializationOrderLinks);
167- entry = entry-> Flink ;
168- if (CurEntry-> HashLinks . Flink == &CurEntry-> HashLinks ) continue ;
169- list = CurEntry-> HashLinks . Flink ;
170- if (list-> Flink == &CurEntry-> HashLinks ) {
171- list = ( decltype (list))(( size_t )CurEntry-> HashLinks . Flink - LdrHashEntry (CurEntry-> BaseDllName ) * sizeof (_LIST_ENTRY));
172- break ;
195+ PLDR_DATA_TABLE_ENTRY current = CONTAINING_RECORD (entry, LDR_DATA_TABLE_ENTRY, LDR_DATA_TABLE_ENTRY::InInitializationOrderLinks);
196+ PLIST_ENTRY hashEntry = ¤t-> HashLinks ;
197+
198+ if (hashEntry-> Flink != hashEntry && hashEntry-> Flink -> Flink == hashEntry) {
199+ PLIST_ENTRY table = &hashEntry-> Flink [-(LONG) LdrHashEntry (current-> BaseDllName )];
200+
201+ return IsValidLdrpHashTable (table) ? table : nullptr ;
173202 }
174- list = nullptr ;
203+
204+ entry = entry->Flink ;
175205 }
176- return list;
206+
207+ return nullptr ;
177208}
178209
179210VOID InitializeWindowsVersion () {
0 commit comments