Advertisement
wYWxacldqRldS

Untitled

Mar 1st, 2022
14,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * Terminates a process.
  4.  *
  5.  * \param ProcessHandle A handle to a process.
  6.  * \param ExitStatus A status value which indicates why the process is being terminated.
  7.  * \param Key An access key.
  8.  * \li If a L2 key is provided, no access checks are performed.
  9.  * \li If no valid L2 key is provided, the function fails.
  10.  * \param Client The client that initiated the request.
  11.  * \param AccessMode The mode IN which to perform access checks.
  12.  */
  13. NTSTATUS KpiTerminateProcess(
  14.     _In_ HANDLE ProcessHandle,
  15.     _In_ NTSTATUS ExitStatus,
  16.     _In_opt_ KPH_KEY Key,
  17.     _In_ PKPH_CLIENT Client,
  18.     _In_ KPROCESSOR_MODE AccessMode
  19.     )
  20. {
  21.     NTSTATUS status;
  22.     PEPROCESS process;
  23.  
  24.     PAGED_CODE();
  25.  
  26.     if (!NT_SUCCESS(status = KphValidateKey(KphKeyLevel2, Key, Client, AccessMode)))
  27.         return status;
  28.  
  29.     status = ObReferenceObjectByHandle(
  30.         ProcessHandle,
  31.         0,
  32.         *PsProcessType,
  33.         AccessMode,
  34.         &process,
  35.         NULL
  36.         );
  37.  
  38.     if (!NT_SUCCESS(status))
  39.         return status;
  40.  
  41.     if (process != PsGetCurrentProcess())
  42.     {
  43.         HANDLE newProcessHandle;
  44.  
  45.         // Re-open the process to get a kernel handle.
  46.         if (NT_SUCCESS(status = ObOpenObjectByPointer(
  47.             process,
  48.             OBJ_KERNEL_HANDLE,
  49.             NULL,
  50.             PROCESS_TERMINATE,
  51.             *PsProcessType,
  52.             KernelMode,
  53.             &newProcessHandle
  54.             )))
  55.         {
  56.             status = ZwTerminateProcess(newProcessHandle, ExitStatus);
  57.             ZwClose(newProcessHandle);
  58.         }
  59.     }
  60.     else
  61.     {
  62.         status = STATUS_CANT_TERMINATE_SELF;
  63.     }
  64.  
  65.     ObDereferenceObject(process);
  66.  
  67.     return status;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement