I detoured LoadLibraryA, in order to block the function from being called into my app. It is meant to block’dll injection’. Please refer to the well-known CDetour library if you’ve never seen these.
It hooks the load library function and even returns sucessfully, also blocking unknown dll’s from being loaded into the memory. Any tips?
bool ( __stdcall* LoadLibraryA ) ( LPCSTR );
bool LoadLibraryADetoured( LPCSTR szMsg )
{
if( strcmp( szMsg, "MyAllowedDll.dll" ) )
return TRUE;
return FALSE;
}
INT APIENTRY DllMain( HMODULE hModule, DWORD dwReason, LPVOID Reserved )
{
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
{
DWORD dwRetAddress = (DWORD)GetProcAddress( GetModuleHandleA( "kernel32.dll" ), "LoadLibraryA" );
ZChatInput = ( bool ( __stdcall* ) ( ) )LoadLibraryA( ( PBYTE )dwRetAddress, ( PBYTE )LoadLibraryADetoured );
DisableThreadLibraryCalls( hModule );
break;
}
case DLL_THREAD_ATTACH:
case DLL_PROCESS_DETACH:
DetourRemove( ( PBYTE )dwRetAddress, ( PBYTE )LoadLibraryADetoured );
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
According to MSDN, there are severe limitations of what you can safely do in
DllMain().LoadLibrary()is not safe there for sure.From http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx:
(Bold emphasis is mine)