例子代码:隐藏
-------------
作者:yoda
E-mail: LordPE@gmx.net
个人主页:www. y0da.cjb.net
日期:2002.07.06
测试系统: WinME 和 Win2000
翻译:bisonal23(05.06.09)
说明:
-----
这个示例说明怎样隐藏基于Windows操作系统中的一个进程,使该进程可以躲避ProcDump (G-RoM, Lorian& Stone)或者 ProcessExplorer (SysInternals)等任务查看器的检查。
它可以防止程序被“dump”。
在NT和9x下实现它的方式是有很大差异的。
Win 95/98/ME
------------
在9x下我们可以通过简单的挂接目标的Toolhelp32 API函数: Process32First/Process32Next来达到我们的目的。如果钩子程序检测到传唤器正在询问我们的进程,我们就执行另一个Process32Next来调用指定的TH32句柄。
另外我们利用未公开的但是大家很熟悉的RegisterServiceProcess API函数:
BOOL STDCALL RegisterServiceProcess(DWORD dwProcessID, BOOL bHide);
来清除CTRL+ALT+DEL弹出的对话框中的进程。这样像ProcDump, LordPE, ProcessExplorer就会成功的被欺骗。在我的系统上,MS Spy能抓拍到我们进程的名称,但是不能得到除了PID外的其他额外信息。
Win NT/2k/XP
------------
这里事情变得机警了。因为一个ring3的进程几乎没有权限向KMD传递一个动作。
首先我们挂接NtQuerySystemInformation。一个进程调用这个函数并且说明询问类5(SystemProcessInformation; thx EliCZ),我们修改返回的进程信息结构链。事实上我没有在整个结构上写得更多。我扩展了在我们进程块前面的进程块中SYSTEM_PROCESS_INFORMATION.SizeOfBlock结构项。
这个挂接不是通过重定位API函数的入口,而是通过修改ServiceDescriptorTable,它的地址可以从NtOsKrnl!KeServiceDescriptorTable导出的一个结构中获得:
SSDT STRUCT
pSSAT LPVOID ? ; System Service Address Table ( LPVOID[] )
Obsolete DWORD ? ; or maybe: API ID base
dwAPICount DWORD ?
pSSPT LPVOID ? ; System Service Parameter Table ( BYTE[] )
SSDT ENDS
由于这些Native API的ID对于不同的NT操作系统,不同的补丁包都是不同的,我们需要找出NtQuerySystemInformation的ID。为了这个目的,我们传递NtDll!NtQuerySystemInformation的地址给驱动程序,将在那里析取Native API的ID……
NtDll!NtQuerySystemInformation:
mov eax, 97h ; EAX == Native API ID
lea edx, [esp+arg_0] ; EDX ->参数列表
int 2Eh ; 执行 Native API 调用
retn 10h
Native API ID是我们在NtOsKrnl!KeServiceDescriptorTable.SSDT.pSSAT找到的函数地址链的索引。我们只要交换我们目标API函数地址和我们挂接程序的线性地址。在我们的例子里:pSSAT[ 0x97 ]。当我们完成上面的,用户再也不会在NT的CTRL+ALT+DEL对话框的进程列表中看到Invisibility.exe。接下来的问题是在TaskMgr.exe的窗口里还有我们进程的一个入口。TaskMgr.exe内部使用user32!EnumWindows API 函数来得到窗口句柄和窗口名称,因此它能在窗口列表里显示名字/图标。
由于我不想单独挂接每一个进程的User32.dll和了解每一个新创建的进程,我只需要挂接NT内核某处的系统进程。EnumWindows对于拥有多参数的无输出函数只是一个大概的枚举,被枚举的函数也可以用User32!EnumChildWindows枚举出来。这个函数使用以下三个Native API函数:
Win32k!NtUserBuildHwndList: - 第一次调用
-只调用一次
- ID: 0x112E 在我的系统里
Win32k!NtUserInternalGetWindowText: - 被调用几次
- ID: 0x11B1在我的系统里
Win32k!NtUserQueryWindow: -被调用几次
- ID: 0x11D2在我的系统里
挂接NtUserBuildHwndList对于实现我们的目的听起来不错。NtUserBuildHwndList有7个参数,它的函数原形大概如下:
NTSTATUS NTAPI
NtUserBuildHwndList( ;我的猜测
IN ARGUMENT_1,
IN hParentHwnd,
IN BOOL,
IN ARGUMENT_4,
IN SpaceForHandlesInBufferCount,
OUT pOutputBuffer,
OUT pbResult
);
这个函数不是从win32k.sys中输出的。所以我们需要找到它的ServiceDescriptorTable。有一个未公开的不易访问的描述符叫ServiceDescriptorTableShadow。我在NtOsKrnl!KeServiceDescriptorTable输出地址下面的几个字节找到了这个函数。
内存片断……
0x0000: SSDT structure for Native API IDs < 0x1000 ; non-shadow SSDT
0x0010: 00000000 00000000 00000000 00000000 ; table terminator
0x0020: 00000000 00000000 00000000 00000000
0x0030: 00000000 00000000 00000000 00000000
0x0040: 00000000 00000000 00000000 00000000
0x0050: SSDT structure for Native API IDs < 0x1000 ; KeServiceDescriptorTableShadow !
0x0060: SSDT structure for Native API IDs >= 0x1000 ; SSDT for win32k.sys
0x0070: 00000000 00000000 00000000 00000000 ; table terminator
现在我们只需要NtUserBuildHwndList这个Native API函数的 ID。运气很好。
IDA显示:
[...]
sub_0_77E0678A proc near
mov eax, 112Eh ; EAX == ID of win32k!NtUserBuildHwndList !!!
lea edx, [esp+arg_0]
int 2Eh
retn 1Ch
sub_0_77E0678A endp
EnumWindows proc near
xor eax, eax
push eax
push eax
push [esp+8+arg_4]
push [esp+0Ch+arg_0]
push eax
push eax
call sub_0_77E06607
retn 8
EnumWindows endp
[...]
……因此我们可以通过上面EnumWindows直接取得API函数的Native API ID,代替程序在SSDTS.pSSAT[ 0x112E ]的地址我们就成功地挂接了程序。我们可以使用user32!GetWindowThreadProcessId来判断在KernelMode中返回的窗口句柄是否属于我们的进程,因为这个API值调用原始的NT结构而不是调用二级API。
修改这个API输出之后,TaskMgr.exe也不能在窗口列表中列举出隐藏消息对话框的标题。
ProcDump, ProcessExplorer也可以被欺骗。MS Snap可以列出隐藏窗口。
查看源代码可以得到更多的信息。
感谢
----------------------
EliCZ - skilled tips as usual...thx man
DAEMON - ditto...
-----------------------------完-------------------------------------
注:第一次翻译,难免有不少错误,恳请大家多多指正。翻译不好的地方请大家看原文,也可以发EMAIL给我:bisonal23@yahoo.com.cn一起讨论