Advertisement
alaestor

[FGL Utility] ProcessAssassin.h

Jan 18th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. // Public Discord Invite: http://public.FutureGadgetLab.net
  2. // Should be improved. Will add more ways to kill procs as they annoy me.
  3. // Inconsistent syntax due Copy+pasted snippets. meh.
  4.  
  5. #ifndef PROCESSASSASSIN_H_INCLUDED
  6. #define PROCESSASSASSIN_H_INCLUDED
  7. #define WINVER _WIN32_WINNT_WIN7
  8. #include <exception>
  9. #include <stdexcept>
  10. #include <cassert>
  11. #include <windows.h>
  12.  
  13. namespace ProcessAssassin
  14. {
  15.  
  16. class ProcessHandle
  17. {
  18.     private:
  19.  
  20.     const HANDLE hndl;
  21.  
  22.     struct PrivInfo
  23.     {
  24.         HANDLE hToken;
  25.         TOKEN_PRIVILEGES PrivOld;
  26.     };
  27.  
  28.     bool isWIN32NT() const
  29.     {
  30.         OSVERSIONINFO osv;
  31.         osv.dwOSVersionInfoSize = sizeof(osv);
  32.         GetVersionEx(&osv);
  33.  
  34.         return osv.dwPlatformId == VER_PLATFORM_WIN32_NT;
  35.     }
  36.  
  37.     struct PrivInfo getDebugPrivilage() const
  38.     {
  39.         PrivInfo pi;
  40.  
  41.         assert(ANYSIZE_ARRAY > 0); // why is this here? idk. whatever.
  42.  
  43.         if (!OpenThreadToken(
  44.                 GetCurrentThread(),
  45.                 TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
  46.                 false,
  47.                 &pi.hToken))
  48.         {
  49.             if (GetLastError() != ERROR_NO_TOKEN ||
  50.                 !OpenProcessToken(
  51.                     GetCurrentProcess(),
  52.                     TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
  53.                     &pi.hToken))
  54.                 throw std::runtime_error(
  55.                     "ProcessHandle: Failed to get thread token handle");
  56.         }
  57.  
  58.         TOKEN_PRIVILEGES Priv;
  59.         Priv.PrivilegeCount = 1;
  60.         Priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  61.         unsigned long cbPriv = sizeof(Priv);
  62.  
  63.         LookupPrivilegeValue(
  64.             nullptr,
  65.             SE_DEBUG_NAME,
  66.             &Priv.Privileges[0].Luid);
  67.  
  68.         // try to enable the privilege
  69.         if (!AdjustTokenPrivileges(
  70.                 pi.hToken,
  71.                 false,
  72.                 &Priv,
  73.                 sizeof(Priv),
  74.                 &pi.PrivOld,
  75.                 &cbPriv))
  76.         {
  77.             CloseHandle(pi.hToken);
  78.             throw std::runtime_error("ProcessHandle: AdjustTokenPrivs failed");
  79.         }
  80.  
  81.         if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
  82.         {
  83.             CloseHandle(pi.hToken);
  84.             throw std::runtime_error("ProcessHandle: SE_DEBUG_NAME failed");
  85.         }
  86.  
  87.         return pi;
  88.     }
  89.  
  90.     void resetPrivilage(struct PrivInfo pi) const
  91.     {
  92.         AdjustTokenPrivileges(
  93.             pi.hToken,
  94.             false,
  95.             &pi.PrivOld,
  96.             sizeof(pi.PrivOld),
  97.             nullptr,
  98.             nullptr);
  99.  
  100.         CloseHandle(pi.hToken);
  101.     }
  102.  
  103.     HANDLE getHandle(unsigned long pid) const
  104.     {
  105.         auto h = OpenProcess(PROCESS_TERMINATE, false, pid);
  106.         if (h == nullptr)
  107.         {
  108.             if (GetLastError() != ERROR_ACCESS_DENIED) // derpy?
  109.                 throw std::runtime_error("ProcessHandle: ERROR_ACCESS_DENIED");
  110.  
  111.             if (!isWIN32NT()) // insane?
  112.                 throw std::runtime_error("ProcessHandle: isn't WIN32_NT");
  113.  
  114.             auto pi = getDebugPrivilage();
  115.             h = OpenProcess(PROCESS_TERMINATE, false, pid);
  116.             resetPrivilage(pi);
  117.  
  118.             if (h == nullptr)
  119.                 throw std::runtime_error("ProcessHandle: OpenProcess failed");
  120.         }
  121.  
  122.         return h;
  123.     }
  124.  
  125.     public:
  126.  
  127.     operator HANDLE() const { return hndl; }
  128.  
  129.     ProcessHandle(unsigned long pid)
  130.     : hndl(getHandle(pid))
  131.     {}
  132.  
  133.     ~ProcessHandle()
  134.     { CloseHandle(hndl); }
  135. };
  136.  
  137. void KillProcess(unsigned long pid)
  138. {
  139.     // should I build in safety to not nuke windows systems? meh.
  140.     // should try softer methods & check using GetExitCodeProcess? meh.
  141.     if (!TerminateProcess(ProcessHandle(pid),0xDEAD))
  142.         throw std::runtime_error("KillProcess: Failed to kill process");
  143. }
  144.  
  145. } // namespace ProcessAssassin
  146.  
  147. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement