Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Public Discord Invite: http://public.FutureGadgetLab.net
- // Should be improved. Will add more ways to kill procs as they annoy me.
- // Inconsistent syntax due Copy+pasted snippets. meh.
- #ifndef PROCESSASSASSIN_H_INCLUDED
- #define PROCESSASSASSIN_H_INCLUDED
- #define WINVER _WIN32_WINNT_WIN7
- #include <exception>
- #include <stdexcept>
- #include <cassert>
- #include <windows.h>
- namespace ProcessAssassin
- {
- class ProcessHandle
- {
- private:
- const HANDLE hndl;
- struct PrivInfo
- {
- HANDLE hToken;
- TOKEN_PRIVILEGES PrivOld;
- };
- bool isWIN32NT() const
- {
- OSVERSIONINFO osv;
- osv.dwOSVersionInfoSize = sizeof(osv);
- GetVersionEx(&osv);
- return osv.dwPlatformId == VER_PLATFORM_WIN32_NT;
- }
- struct PrivInfo getDebugPrivilage() const
- {
- PrivInfo pi;
- assert(ANYSIZE_ARRAY > 0); // why is this here? idk. whatever.
- if (!OpenThreadToken(
- GetCurrentThread(),
- TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
- false,
- &pi.hToken))
- {
- if (GetLastError() != ERROR_NO_TOKEN ||
- !OpenProcessToken(
- GetCurrentProcess(),
- TOKEN_QUERY|TOKEN_ADJUST_PRIVILEGES,
- &pi.hToken))
- throw std::runtime_error(
- "ProcessHandle: Failed to get thread token handle");
- }
- TOKEN_PRIVILEGES Priv;
- Priv.PrivilegeCount = 1;
- Priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- unsigned long cbPriv = sizeof(Priv);
- LookupPrivilegeValue(
- nullptr,
- SE_DEBUG_NAME,
- &Priv.Privileges[0].Luid);
- // try to enable the privilege
- if (!AdjustTokenPrivileges(
- pi.hToken,
- false,
- &Priv,
- sizeof(Priv),
- &pi.PrivOld,
- &cbPriv))
- {
- CloseHandle(pi.hToken);
- throw std::runtime_error("ProcessHandle: AdjustTokenPrivs failed");
- }
- if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
- {
- CloseHandle(pi.hToken);
- throw std::runtime_error("ProcessHandle: SE_DEBUG_NAME failed");
- }
- return pi;
- }
- void resetPrivilage(struct PrivInfo pi) const
- {
- AdjustTokenPrivileges(
- pi.hToken,
- false,
- &pi.PrivOld,
- sizeof(pi.PrivOld),
- nullptr,
- nullptr);
- CloseHandle(pi.hToken);
- }
- HANDLE getHandle(unsigned long pid) const
- {
- auto h = OpenProcess(PROCESS_TERMINATE, false, pid);
- if (h == nullptr)
- {
- if (GetLastError() != ERROR_ACCESS_DENIED) // derpy?
- throw std::runtime_error("ProcessHandle: ERROR_ACCESS_DENIED");
- if (!isWIN32NT()) // insane?
- throw std::runtime_error("ProcessHandle: isn't WIN32_NT");
- auto pi = getDebugPrivilage();
- h = OpenProcess(PROCESS_TERMINATE, false, pid);
- resetPrivilage(pi);
- if (h == nullptr)
- throw std::runtime_error("ProcessHandle: OpenProcess failed");
- }
- return h;
- }
- public:
- operator HANDLE() const { return hndl; }
- ProcessHandle(unsigned long pid)
- : hndl(getHandle(pid))
- {}
- ~ProcessHandle()
- { CloseHandle(hndl); }
- };
- void KillProcess(unsigned long pid)
- {
- // should I build in safety to not nuke windows systems? meh.
- // should try softer methods & check using GetExitCodeProcess? meh.
- if (!TerminateProcess(ProcessHandle(pid),0xDEAD))
- throw std::runtime_error("KillProcess: Failed to kill process");
- }
- } // namespace ProcessAssassin
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement