[原创]注入技术系列:一个批量验证DLL劫持的工具

发布者:anhkgg
发布于:2020-01-31 11:00
&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



2020安全开发者峰会(2020 SDC)议题征集 中国.北京 7月!

&https://bbs.pediy.com/3;
最后于 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;

声明:该文观点仅代表作者本人,转载请注明来自看雪