gpt4 book ai didi

c++ - 绕行 LoadLibraryA 时 appcrash

转载 作者:行者123 更新时间:2023-11-28 03:35:05 26 4
gpt4 key购买 nike

我绕过 LoadLibraryA,以阻止该函数被调用到我的应用程序中。它旨在阻止'dll注入(inject)'。如果您从未见过这些,请引用著名的 CDetour 库。

它hook了加载库函数甚至成功返回,也阻止了未知的dll被加载到内存中。有什么建议吗?

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;
}

最佳答案

根据 MSDN,您可以在 DllMain() 中安全地执行的操作有严格的限制。 LoadLibrary() 在那里肯定不安全。

来自 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx :

Because Kernel32.dll is guaranteed to be loaded in the process address space when the entry-point function is called, calling functions in Kernel32.dll does not result in the DLL being used before its initialization code has been executed. Therefore, the entry-point function can call functions in Kernel32.dll that do not load other DLLs. For example, DllMain can create synchronization objects such as critical sections and mutexes, and use TLS. Unfortunately, there is not a comprehensive list of safe functions in Kernel32.dll.

(大胆强调是我的)

关于c++ - 绕行 LoadLibraryA 时 appcrash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com