[原創]注入技術系列:一個批次驗證DLL劫持的工具

anhkgg發表於2020-09-03
&https://bbs.pediy.com/3; &https://bbs.pediy.com/3; &https://bbs.pediy.com/3;
&https://bbs.pediy.com/3;

作者:anhkgg
日期:2019年11月3日

&https://bbs.pediy.com/3;
&https://bbs.pediy.com/3;
 

很多時候,可能會對某個軟體進行DLL劫持。

&https://bbs.pediy.com/3;
 

而這個軟體是否存在DLL劫持漏洞,需要去分析驗證。

&https://bbs.pediy.com/3;
 

比如透過IDA檢視匯入的DLL,或者LoadLibrary的DLL,然後慢慢排除某些KnownDlls,排除某些絕對路徑載入的DLL...

&https://bbs.pediy.com/3;
 

或者透過Windbg分析。

&https://bbs.pediy.com/3;
 

雖然技術難度不高,但是挺費事的。

&https://bbs.pediy.com/3;
 

本篇文章分享我找DLL劫持的方法,不一定是最佳,不過很方便。

&https://bbs.pediy.com/3;

1

首先,透過windbg啟動軟體,設定(預設開啟的):

&https://bbs.pediy.com/3;
Debug->Event Filters->Load module,勾選Output&https://bbs.pediy.com/3;

然後go執行。這樣我們可以看到執行後,軟體匯入表匯入的DLL,以及LoadLibrary載入的DLL的所有檔案,如下所示:

&https://bbs.pediy.com/3;
ModLoad: 75090000 750cb000   C:\Windows\system32\rsaenh.dll&https://bbs.pediy.com/3;
ModLoad: 757c0000 757cc000   C:\Windows\system32\CRYPTBASE.dll&https://bbs.pediy.com/3;
ModLoad: 778b0000 778da000   C:\Windows\system32\imagehlp.dll&https://bbs.pediy.com/3;
ModLoad: 77860000 778a5000   C:\Windows\system32\WLDAP32.dll&https://bbs.pediy.com/3;

2

寫一個測試的DLL,只用下面的程式碼:

&https://bbs.pediy.com/3;
BOOL APIENTRY DllMain( HMODULE hModule,&https://bbs.pediy.com/3;
                       DWORD  ul_reason_for_call,&https://bbs.pediy.com/3;
                       LPVOID lpReserved&https://bbs.pediy.com/3;
                     )&https://bbs.pediy.com/3;
{&https://bbs.pediy.com/3;
    char path[MAX_PATH] = { 0 };&https://bbs.pediy.com/3;
    switch (ul_reason_for_call)&https://bbs.pediy.com/3;
    {&https://bbs.pediy.com/3;
    case DLL_PROCESS_ATTACH:&https://bbs.pediy.com/3;
        OutputDebugStringA(path);&https://bbs.pediy.com/3;
        OutputDebugStringA("success!");&https://bbs.pediy.com/3;
        MessageBoxA(NULL, "success!", "Tips", MB_OK);&https://bbs.pediy.com/3;
        ExitProcess(0);&https://bbs.pediy.com/3;
        break;&https://bbs.pediy.com/3;
    case DLL_THREAD_ATTACH:&https://bbs.pediy.com/3;
    case DLL_THREAD_DETACH:&https://bbs.pediy.com/3;
    case DLL_PROCESS_DETACH:&https://bbs.pediy.com/3;
        break;&https://bbs.pediy.com/3;
    }&https://bbs.pediy.com/3;
    return TRUE;&https://bbs.pediy.com/3;
}&https://bbs.pediy.com/3;

然後講DLL改名成劫持目標DLL,放到軟體目錄下,執行驗證即可。

&https://bbs.pediy.com/3;
 

如果出現提示success則表示該目標DLL劫持成功。

&https://bbs.pediy.com/3;

3

如果DLL太多了,一一驗證排除肯定很麻煩,所以程式設計師又要寫程式碼了。

&https://bbs.pediy.com/3;
 

寫一個自動批次驗證的工具,基本思路如下:

&https://bbs.pediy.com/3;
  • 把windbg拿到的dll列表儲存下來
  • &https://bbs.pediy.com/3;
  • 準備好測試DLL,DLL中加入寫log功能
  • &https://bbs.pediy.com/3;
  • 分析dll列表,一一把測試DLL複製為目標dll,啟動軟體
  • &https://bbs.pediy.com/3;
  • 然後把log提取出來,可以看到成功劫持的dll
  • &https://bbs.pediy.com/3;

&https://bbs.pediy.com/3;
 

工具介面如上,一鍵驗證所有DLL,分分鐘拿到結果。

&https://bbs.pediy.com/3;
 

工具會分享到https://github.com/anhkgg/anhkgg-tools下,歡迎使用,如有bug,請聯絡我。

&https://bbs.pediy.com/3;
 

歡迎關注公眾號:漢客兒

&https://bbs.pediy.com/3;
 

交流群:753894145

&https://bbs.pediy.com/3;

&https://bbs.pediy.com/3; 看雪論壇2020激勵機制:能力值、活躍值和雪幣體系!會員積分、許可權和會員發帖、回帖活躍程度關聯!

最後於 2019-11-4 16:11 被Angelxf編輯 ,原因:
&https://bbs.pediy.com/3; &https://bbs.pediy.com/3; &https://bbs.pediy.com/3; &https://bbs.pediy.com/3;

相關文章