爲了避免dll預加載,我嘗試使用SetDllDirectory("")
,因爲它在MSDN中提到要從搜索路徑中刪除當前目錄。避免通過重新加載DLL,任何更好的方式預加載的漏洞?
要添加到搜索路徑的目錄。如果此參數是空字符串(「」) ,則調用將從 默認DLL搜索順序中刪除當前目錄。如果此參數爲NULL,則功能 將恢復默認搜索順序。
令人驚訝的是它沒有奏效。我可以看到DLL仍然從當前目錄加載,如果DLL出現在那裏。我也厭倦將dll目錄路徑設置爲系統32路徑,但它仍然從當前目錄中選取dll。最後,我決定讓所有模塊加載到我的應用程序中並重新加載它們。我的代碼看起來像這樣,
if(wcsstr(szModName,L"TestLibrary.dll"))
{
FreeLibrary(hMods[i]);
LoadLibrary("SomeRelavantPath\TestLibrary.dll");
}
你看到我的代碼什麼問題?
@Edit: 我的完整POC代碼,
int main(void)
{
SetDllDirectory(L"");
LPWSTR s = new WCHAR[100];
GetDllDirectory(100,s);
HINSTANCE myDLL = LoadLibrary(L"TestLibrary.dll");
//myDLL returns non-null, there is file in current dir and not at any other location, it should have return null.
return 0;
}
在SetDllDirectory(「」)「之後立即嘗試」GetDllDirectory「,並立即在LoadLibrary之前。 – iehrlich
@iehrlich沒有工作。有沒有任何理由調用'GetDllDirectory'應該工作? –
@PranitKothari,我懷疑@iehrlich的意思是使用'GetDllDirectory'來確保搜索路徑確實已經改變爲你想要的。它改變了嗎? – yzt