Click here to Skip to main content
15,888,051 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I have been trying to hook FindNextFileW in Explorer.exe.
I have used IAT hooking and I manage to successfully change the address of FindNextfileW in the import table but when I execute the program, after the address is changed, explorer.exe restarts.
I get a message box ,Data Execution Prevention, and then explorer restarts. So I turned off the Data Execution Prevention for Explorer.exe and now I don't get any erro message and explorer doesn't restart but when I open a directory nothing happens.
Is there a problem in the definition
of myFindFirstFileExW or is there something else I am missing?



C++
BOOL myFindNextFileW (HANDLE hFindFile,LPWIN32_FIND_DATA lpFindFileData);
int WINAPI DllEntryPoint(HINSTANCE hinstDll, unsigned long fdwReason,
	void* fImpLoad) {
	char lib_name[50];
	GetModuleFileName(hinstDll, lib_name, 50);
	LoadLibrary(lib_name);
	switch(fdwReason) {
	case DLL_PROCESS_ATTACH: {
	       ShowMessage("DLL_PROCESS_ATTACH");
       	       HookAPI("FindNextFileW", (DWORD)myFindNextFileW);
		}
	case DLL_PROCESS_DETACH: {
			break;
		}
	case DLL_THREAD_ATTACH: {
			break;
		}
	case DLL_THREAD_DETACH: {
			break;
		}
	}
	return(TRUE);
}

BOOL myFindNextFileW (HANDLE hFindFile,LPWIN32_FIND_DATA lpFindFileData)
{
FILE *hfptr4;
	hfptr4 = fopen("c:\\hookedCP.txt", "a");
	fprintf(hfptr4, "%s", "findNextFile\n");
	fclose(hfptr4);
	ShowMessage("FindNextFileW");
	return FindNextFileW(hFindFile, (_WIN32_FIND_DATAW *)lpFindFileData);
}
Posted
Updated 4-May-12 20:55pm
v6
Comments
Code-o-mat 5-May-12 4:06am    
Not quite sure about this, so i will not add it as a solution, just guessing, but: try adding __stdcall or WINAPI calling convention specifier to your myFindNextFileW. Another important thing here is that -as far as i can see- you are using LoadLibrary in the DLLEntryPoint which is a big NO-NO, if you read the remarks here: http://msdn.microsoft.com/en-us/library/ms886736.aspx . Instead of doing that in the entry point, start a secondary thread and use that to load your hook-dll.
Code-o-mat 5-May-12 4:40am    
Another thing that comes to my mind, some time ago i was also experimenting with hooking explorer and i found out that the CreateRemoteThread method didn't work, however with SetWindowsHookEx i had much better luck. Don't know what method you use to inject your DLL, just thought i mention it...
lilyNaz 5-May-12 5:34am    
Thank you.
I use createremotethread to inject my dll.
I added stdcall and I removed the code regarding loadlibrary but it still doesn't work.
Code-o-mat 5-May-12 9:48am    
Are you sure the actual hooking is successful? There can be multiple reasons why your hooked method doesn't get invoked. E.g. explorer could use LoadLibrary/GetProcAddress to get access to the function instead of loading the DLL at startup and accessing the function using the IAT, or it might call into another DLL that imports FindNextFileW instead of calling it directly.
lilyNaz 6-May-12 1:00am    
Yeah, I guess it wasn't invoked so I did the same for cmd.exe and this time I monitored cmd.exe in API monitor, when I typed dir, cmd crashes and I can see that MyDLL has been loaded but it returns invalid_handle _value, Error 2:The system could not find the file specified ... This time my function is definitely called

1 solution

Hi,

There is only a single possibility. When you get the Data Execution Prevention error message... it means that the instructions located in memory were not marked with execute access[^].

It sounds like you are using an old hook library that was written prior to 2004 and intended for use with Windows XP SP1 and below. Windows XP SP2 was released in 2004 and brought DEP and broke many of the old hook libraries that were not using the VirtualProtect function[^] to mark the memory as executable.

Best Wishes,
-David Delaune

[Updated dates and Service pack numbers]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900