Advertisement
Guest User

Idan

a guest
Oct 7th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.96 KB | None | 0 0
  1. bool killProcces(const char * path) {
  2. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
  3. PROCESSENTRY32 pEntry;
  4. bool killSucceed = false;
  5.  
  6.     // load the neccery libraries
  7.     _NtQuerySystemInformation NtQuerySystemInformation =
  8.     (_NtQuerySystemInformation)GetLibraryProcAddress("ntdll.dll", "NtQuerySystemInformation");
  9.  
  10.     _NtDuplicateObject NtDuplicateObject =
  11.     (_NtDuplicateObject)GetLibraryProcAddress("ntdll.dll", "NtDuplicateObject");
  12.  
  13.     _NtQueryObject NtQueryObject =
  14.     (_NtQueryObject)GetLibraryProcAddress("ntdll.dll", "NtQueryObject");
  15.  
  16.     NTSTATUS status;
  17.     PSYSTEM_HANDLE_INFORMATION handleInfo;
  18.     ULONG handleInfoSize = 0x10000;
  19.     ULONG pid;
  20.     HANDLE processHandle;
  21.     ULONG i;
  22.  
  23.     pEntry.dwSize = sizeof(pEntry);
  24.     BOOL hRes = Process32First(hSnapShot, &pEntry);
  25.    
  26.     while (hRes && !killSucceed)    // iterate all the run procceses ( if we get new procces and we didn't killed one )
  27.     {
  28.         //if (strcmp(/*new*/(char *)pEntry.szExeFile, path) == 0)   // if current process have the same name of our desired path
  29.         //{
  30.         //  HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, (DWORD)pEntry.th32ProcessID);
  31.         //  cout << "current process id is: " << GetProcessId(hProcess) << "\n";
  32.         //  if (hProcess != NULL)   // close proccess iff it is steel runing
  33.         //  {   // The exit code (=9) to be used by the process and threads terminated as a result of this call
  34.         //      TerminateProcess(hProcess, 9);
  35.         //      CloseHandle(hProcess);
  36.         //      killSucceed = true;
  37.         //  }
  38.         //}
  39.  
  40.         if (killSucceed) {  // don't continue that specific iteration if we have killed the desired procces
  41.             continue;
  42.         }
  43.  
  44.         pid = pEntry.th32ProcessID; // maybe casting is needed !
  45.  
  46.         if (!(processHandle = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid)))     // if the attempet to duplicate current process failed
  47.         {
  48.             printf("Could not open PID %d! (Don't try to open a system process.. line 73)\n", pid);
  49.             return FALSE;
  50.         }
  51.  
  52.         handleInfo = (PSYSTEM_HANDLE_INFORMATION)malloc(handleInfoSize);        // allocate place for handleInfo object
  53.  
  54.         /* NtQuerySystemInformation won't give us the correct buffer size,
  55.         so we guess by doubling the buffer size. */
  56.  
  57.         while ((status = NtQuerySystemInformation(      // getting information about current process
  58.             SystemHandleInformation,                    // SystemInformationClass
  59.             handleInfo,                                 // PVOID - SystemInformation
  60.             handleInfoSize,                             // SystemInformationLength
  61.             NULL                                        // ReturnLength
  62.             )) == STATUS_INFO_LENGTH_MISMATCH)
  63.             handleInfo = (PSYSTEM_HANDLE_INFORMATION)realloc(handleInfo, handleInfoSize *= 2);      //until secceded, duplicate the length
  64.  
  65.         /* NtQuerySystemInformation stopped giving us STATUS_INFO_LENGTH_MISMATCH. */
  66.         if (!NT_SUCCESS(status))    // if the allocation doesn't secceded, even after realloc the space
  67.         {
  68.             printf("NtQuerySystemInformation failed!\n");
  69.             return FALSE;
  70.         }
  71.  
  72.         for (i = 0; i < handleInfo->HandleCount; i++)   // as the number of handels the given process has
  73.         {
  74.             SYSTEM_HANDLE handle = handleInfo->Handles[i];
  75.             HANDLE dupHandle = NULL;
  76.             POBJECT_TYPE_INFORMATION objectTypeInfo;
  77.             PVOID objectNameInfo;
  78.             UNICODE_STRING objectName;
  79.             ULONG returnLength;
  80.  
  81.             /* Check if this handle belongs to the PID the user specified. */
  82.             if (handle.ProcessId != pid) {
  83.                 continue;
  84.             }
  85.             else {  // so maybe we found the match
  86.                 if (strcmp(/*new*/(char *)pEntry.szExeFile, path) == 0) {   // if current process have the same name of our desired path
  87.                     cout << "--------------------------------------------------\nwe found match!!!\n--------------------------------------------------\n";
  88.                     HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, (DWORD)pEntry.th32ProcessID);
  89.                     if (hProcess != NULL)   // close proccess iff it is steel runing
  90.                     {   // The exit code (=9) to be used by the process and threads terminated as a result of this call
  91.                         TerminateProcess(hProcess, 9);
  92.                         CloseHandle(hProcess);
  93.                         killSucceed = true;
  94.                     }   // end inner if
  95.                 }   // end outer if
  96.             }   // end else
  97.  
  98.             /* Duplicate the handle so we can query it. */
  99.             if (!NT_SUCCESS(NtDuplicateObject(
  100.                 processHandle,
  101.                 (HANDLE)handle.Handle,
  102.                 GetCurrentProcess(),
  103.                 &dupHandle,
  104.                 0,
  105.                 0,
  106.                 0
  107.                 )))
  108.             {   // if the atempt to duplicate the current handle failed, move next
  109.                 printf("[%#x] Error!\n", handle.Handle);
  110.                 continue;
  111.             }
  112.  
  113.             /* Query the object type. */
  114.             objectTypeInfo = (POBJECT_TYPE_INFORMATION)malloc(0x1000);
  115.             if (!NT_SUCCESS(NtQueryObject(
  116.                 dupHandle,
  117.                 ObjectTypeInformation,
  118.                 objectTypeInfo,
  119.                 0x1000,
  120.                 NULL
  121.                 )))
  122.             {   // if the atempt to duplicate the current handle failed, move next
  123.                 printf("[%#x] Error!\n", handle.Handle);
  124.                 CloseHandle(dupHandle);
  125.                 continue;
  126.             }
  127.  
  128.             /* Query the object name (unless it has an access of
  129.             0x0012019f, on which NtQueryObject could hang. */
  130.             if (handle.GrantedAccess == 0x0012019f)         // if the current handle isn't accessible...
  131.             {
  132.                 /* We have the type, so display that. */
  133.                 printf(
  134.                     "[%#x] %.*S: (did not get name)\n",
  135.                     handle.Handle,
  136.                     objectTypeInfo->Name.Length / 2,
  137.                     objectTypeInfo->Name.Buffer
  138.                     );
  139.                 free(objectTypeInfo);
  140.                 CloseHandle(dupHandle);
  141.                 continue;
  142.             }
  143.  
  144.             objectNameInfo = malloc(0x1000);
  145.             if (!NT_SUCCESS(NtQueryObject(
  146.                 dupHandle,
  147.                 ObjectNameInformation,
  148.                 objectNameInfo,
  149.                 0x1000,
  150.                 &returnLength
  151.                 )))
  152.             {
  153.                 /* Reallocate the buffer and try again. */
  154.                 objectNameInfo = realloc(objectNameInfo, returnLength);
  155.                 if (!NT_SUCCESS(NtQueryObject(
  156.                     dupHandle,
  157.                     ObjectNameInformation,
  158.                     objectNameInfo,
  159.                     returnLength,
  160.                     NULL
  161.                     )))
  162.                 {
  163.                     /* We have the type name, so just display that. */
  164.                     printf(
  165.                         "[%#x] %.*S: (could not get name)\n",
  166.                         handle.Handle,
  167.                         objectTypeInfo->Name.Length / 2,
  168.                         objectTypeInfo->Name.Buffer
  169.                         );
  170.                     free(objectTypeInfo);
  171.                     free(objectNameInfo);
  172.                     CloseHandle(dupHandle);
  173.                     continue;
  174.                 }
  175.             }
  176.  
  177.             /* Cast our buffer into an UNICODE_STRING. */
  178.             objectName = *(PUNICODE_STRING)objectNameInfo;
  179.  
  180.             /* Print the information! */
  181.             if (objectName.Length)
  182.             {
  183.                 /* The object has a name. */
  184.                 printf(
  185.                     "\n\n\n\n\n-----------------------------------\n[%#x] %.*S: %.*S\n\n\n\n\n-----------------------------------\n",
  186.                     handle.Handle,
  187.                     objectTypeInfo->Name.Length / 2,
  188.                     objectTypeInfo->Name.Buffer,
  189.                     objectName.Length / 2,
  190.                     objectName.Buffer
  191.                     );
  192.  
  193.             }
  194.             else
  195.             {
  196.                 /* Print something else. */
  197.                 printf(
  198.                     "[%#x] %.*S: (unnamed)\n",
  199.                     handle.Handle,
  200.                     objectTypeInfo->Name.Length / 2,
  201.                     objectTypeInfo->Name.Buffer
  202.                     );
  203.             }
  204.  
  205.             free(objectTypeInfo);
  206.             free(objectNameInfo);
  207.             CloseHandle(dupHandle);
  208.         }   // ----------------------------- end for -----------------------------
  209.  
  210.         free(handleInfo);
  211.         CloseHandle(processHandle);
  212.  
  213.  
  214.  
  215.  
  216.         hRes = Process32Next(hSnapShot, &pEntry);
  217.     }
  218.     CloseHandle(hSnapShot);
  219.     return killSucceed;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement