RegSetValueExW hook causes explorer.exe startup crash

c++ / delphi package - dll injection and api hooking
Post Reply
televes
Posts: 13
Joined: Mon Jul 27, 2009 4:10 pm

RegSetValueExW hook causes explorer.exe startup crash

Post by televes »

Hello!

Hope somebody can help me with this. I think its an easy one.

I want to hook RegSetValueExW so I wrote this code:

Code: Select all

	//"Next" function definition:
	LONG (WINAPI *RegSetValueExWNext)(HKEY hKey, LPCWSTR lpValueName, DWORD Reserved, DWORD dwType, const BYTE *lpData, DWORD cbData);

	//Callback function:
	LONG WINAPI RegSetValueExWCallback(HKEY hKey, LPCWSTR lpValueName, DWORD Reserved, DWORD dwType, const BYTE *lpData, DWORD cbData){
		if( lpValueName != NULL && _wcsicmp(L"TWAMSvrAutoRun", lpValueName) == 0)
			return ERROR_ACCESS_DENIED;
		else
			return RegSetValueExWNext(hKey, lpValueName, Reserved, dwType, lpData, cbData);
	}

	//Hook installation:
	HookAPI("Advapi32.dll", "RegSetValueExW", RegSetValueExWCallback, (PVOID*) &RegSetValueExWNext);
It works great, and does exactly what I want. But it causes crashes on some programs, specifically when starting explorer.exe (there isnt any problem when hooking an existing instance).

Am I doing something wrong?

Thanks for any help!
madshi
Site Admin
Posts: 10766
Joined: Sun Mar 21, 2004 5:25 pm

Re: RegSetValueExW hook causes explorer.exe startup crash

Post by madshi »

That sounds weird. Your code looks alright to me. I can only guess that for some reason Explorer.exe is calling RegSetValueExW with invalid parameters during initialization. Try adding a "IsBadReadPtr" call to check whether "lpValueName" can really be read. If that doesn't help, try commenting out the "if" in your callback and always just call the original API. Does the crash go away then?
televes
Posts: 13
Joined: Mon Jul 27, 2009 4:10 pm

Re: RegSetValueExW hook causes explorer.exe startup crash

Post by televes »

Hello madshi

Seems that IsBadReadPtr solved the problem.

Thank you!
Post Reply