Skip to content

Commit

Permalink
PE: Fix the import directory presence check.
Browse files Browse the repository at this point in the history
Fixes silent crashes when thcrap tries to apply detours to DLLs that themselves
don't import anything, like NTDLL.DLL for example.
  • Loading branch information
nmlgc committed Nov 15, 2015
1 parent b9edf3b commit b832703
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions thcrap/src/pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ PIMAGE_NT_HEADERS GetNtHeader(HMODULE hMod)

PIMAGE_IMPORT_DESCRIPTOR GetDllImportDesc(HMODULE hMod, const char *DLLName)
{
DWORD ImportVA;
PIMAGE_NT_HEADERS pNTH;
PIMAGE_IMPORT_DESCRIPTOR pImportDesc;

Expand All @@ -50,12 +51,11 @@ PIMAGE_IMPORT_DESCRIPTOR GetDllImportDesc(HMODULE hMod, const char *DLLName)
if(!pNTH) {
return NULL;
}
pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)hMod +
(DWORD)(pNTH->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress));

if(pImportDesc == (PIMAGE_IMPORT_DESCRIPTOR)pNTH) {
ImportVA = pNTH->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
if(ImportVA == 0) {
return NULL;
}
pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD)hMod + ImportVA);
while(pImportDesc->Name) {
char *name = (char*)((DWORD)hMod + (DWORD)pImportDesc->Name);
if(stricmp(name, DLLName) == 0) {
Expand Down

0 comments on commit b832703

Please sign in to comment.