HollowsHunter
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
process_privilege.cpp
Go to the documentation of this file.
1#include "process_privilege.h"
2
3#include <iostream>
4
5namespace hhunter {
6 namespace util {
7
8 /*
9 based on: https://support.microsoft.com/en-us/help/131065/how-to-obtain-a-handle-to-any-process-with-sedebugprivilege
10 */
12 HANDLE hToken, // token handle
13 LPCTSTR Privilege, // Privilege to enable/disable
14 BOOL bEnablePrivilege // TRUE to enable. FALSE to disable
15 )
16 {
17 TOKEN_PRIVILEGES tp;
18 LUID luid;
19 TOKEN_PRIVILEGES tpPrevious;
20 DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
21
22 if (!LookupPrivilegeValue(nullptr, Privilege, &luid)) {
23 return FALSE;
24 }
25 // get current privilege
26 tp.PrivilegeCount = 1;
27 tp.Privileges[0].Luid = luid;
28 tp.Privileges[0].Attributes = 0;
29
30 AdjustTokenPrivileges(
31 hToken,
32 FALSE,
33 &tp,
34 sizeof(TOKEN_PRIVILEGES),
35 &tpPrevious,
36 &cbPrevious
37 );
38
39 if (GetLastError() != ERROR_SUCCESS) {
40 return FALSE;
41 }
42 // set privilege based on previous setting
43 tpPrevious.PrivilegeCount = 1;
44 tpPrevious.Privileges[0].Luid = luid;
45
46 if (bEnablePrivilege) {
47 tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
48 }
49 else {
50 tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
51 }
52
53 AdjustTokenPrivileges(
54 hToken,
55 FALSE,
56 &tpPrevious,
57 cbPrevious,
58 NULL,
59 NULL
60 );
61
62 if (GetLastError() != ERROR_SUCCESS) {
63 return FALSE;
64 }
65 return TRUE;
66 }
67
68 };
69};
70
72{
73 HANDLE hToken;
74 if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
75 if (GetLastError() == ERROR_NO_TOKEN) {
76 if (!ImpersonateSelf(SecurityImpersonation)) return false;
77 if(!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)){
78 std::cerr << "Error: cannot open the token" << std::endl;
79 return false;
80 }
81 }
82 }
83 bool is_ok = false;
84 // enable SeDebugPrivilege
85 if (set_privilege(hToken, SE_DEBUG_NAME, TRUE)) {
86 is_ok = true;
87 }
88 // close token handle
89 CloseHandle(hToken);
90 return is_ok;
91}
92
BOOL set_privilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege)