Advertisement
GK-Chubbz

BC NtQuerySystemInformation

Apr 11th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bool Detour_NtQuerySystemInformation()
  2. {
  3.     static NtQuerySystemInformation _NtQuerySystemInformation = reinterpret_cast<NtQuerySystemInformation>(GetProcAddress(NTDLL, "NtQuerySystemInformation"));
  4.  
  5.     NtQuerySystemInformation NtQuerySystemInformation_Hook = [](
  6.         _In_      SYSTEM_INFORMATION_CLASS SystemInformationClass,
  7.         _Inout_   PVOID                    SystemInformation,
  8.         _In_      ULONG                    SystemInformationLength,
  9.         _Out_opt_ PULONG                   ReturnLength
  10.         ) -> NTSTATUS
  11.     {
  12.         NTSTATUS ntStatus = _NtQuerySystemInformation(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
  13.  
  14.         if (SystemInformationClass == SystemProcessInformation)
  15.         {
  16.             PSYSTEM_PROCESS_INFORMATION pSystemProcess;
  17.             PSYSTEM_PROCESS_INFORMATION pNextSystemProcess;
  18.  
  19.             pSystemProcess = reinterpret_cast<PSYSTEM_PROCESS_INFORMATION>(reinterpret_cast<PBYTE*>(SystemInformation));
  20.             pNextSystemProcess = reinterpret_cast<PSYSTEM_PROCESS_INFORMATION>(reinterpret_cast<LPBYTE>(pSystemProcess)+pSystemProcess->NextEntryDelta);
  21.  
  22.             while (pNextSystemProcess->NextEntryDelta != 0)
  23.             {
  24.                 if (lstrcmp((&pNextSystemProcess->ProcessName)->Buffer, L"MapleStory.exe") != 0 &&
  25.                     lstrcmp((&pNextSystemProcess->ProcessName)->Buffer, L"BlackCipher.aes") != 0)
  26.                 {
  27.                     printf(L"Process name:[%s]\n", pNextSystemProcess->ProcessName.Buffer);
  28.  
  29.                     pSystemProcess->NextEntryDelta += pNextSystemProcess->NextEntryDelta;
  30.                 }
  31.  
  32.                 pSystemProcess = pNextSystemProcess;
  33.                 pNextSystemProcess = reinterpret_cast<PSYSTEM_PROCESS_INFORMATION>(reinterpret_cast<LPBYTE>(pSystemProcess)+pSystemProcess->NextEntryDelta);
  34.             }
  35.         }
  36.         else if (SystemInformationClass == SystemModuleInformation)
  37.         {
  38.             SYSTEM_MODULE_INFORMATION *pModuleInfo = reinterpret_cast<SYSTEM_MODULE_INFORMATION*>(reinterpret_cast<PBYTE*>(SystemInformation));
  39.  
  40.             CHAR szDriver[MAX_PATH];
  41.  
  42.             for (unsigned int i = 0; i < pModuleInfo->NumberOfModules; i++)
  43.             {
  44.                 StringCchPrintfA(szDriver, MAX_PATH, "%s", pModuleInfo->Modules[i].FullPathName);
  45.  
  46.                 printf("driver:[%d] name:[%s]\n", i, szDriver);
  47.             }
  48.         }
  49.         else if (SystemInformationClass == SystemHandleInformation)
  50.         {
  51.             SYSTEM_HANDLE_INFORMATION *pHandleInfo = reinterpret_cast<SYSTEM_HANDLE_INFORMATION*>(reinterpret_cast<PBYTE*>(SystemInformation));
  52.  
  53.             for (unsigned int i = 0; i < pHandleInfo->NumberOfHandles; i++)
  54.             {
  55.                 if (pHandleInfo->Information[i].ObjectTypeNumber != 0) //HANDLE_TYPE_PROCESS
  56.                 {
  57.                     DWORD dwProcessId = pHandleInfo->Information[i].ProcessId;
  58.                     if (isCSRSS(dwProcessId))
  59.                     {
  60.                         HANDLE hProcess = OpenProcess(PROCESS_DUP_HANDLE, FALSE, dwProcessId);
  61.                         if (hProcess != NULL)
  62.                         {
  63.                             HANDLE hTarget = NULL;
  64.                             if (DuplicateHandle(hProcess, (HANDLE)pHandleInfo->Information[i].Handle, GetCurrentProcess(), &hTarget, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, 0))
  65.                             {
  66.                                 DWORD dwTargetId = GetProcessId(hTarget);
  67.  
  68.                                 if (dwTargetId != (DWORD)-1)
  69.                                     dwTargetId = GetCurrentProcessId();
  70.  
  71.                                 CloseHandle(hTarget);
  72.                             }
  73.                             CloseHandle(hProcess);
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.  
  80.         return ntStatus;
  81.     };
  82.  
  83.     return DetourFunction(true, reinterpret_cast<void**>(&_NtQuerySystemInformation), NtQuerySystemInformation_Hook);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement