11 HMODULE hndl = GetModuleHandleA(name);
13 hndl = LoadLibraryA(name);
29 TOKEN_PRIVILEGES tpPrevious;
30 DWORD cbPrevious =
sizeof(TOKEN_PRIVILEGES);
32 if (!LookupPrivilegeValueA(
nullptr, Privilege, &luid)) {
36 tp.PrivilegeCount = 1;
37 tp.Privileges[0].Luid = luid;
38 tp.Privileges[0].Attributes = 0;
40 AdjustTokenPrivileges(
44 sizeof(TOKEN_PRIVILEGES),
49 if (GetLastError() != ERROR_SUCCESS) {
53 tpPrevious.PrivilegeCount = 1;
54 tpPrevious.Privileges[0].Luid = luid;
56 if (bEnablePrivilege) {
57 tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
60 tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
63 AdjustTokenPrivileges(
72 if (GetLastError() != ERROR_SUCCESS) {
82 if (!kernelLib)
return FALSE;
84 FARPROC procPtr = GetProcAddress(kernelLib,
"GetProcessDEPPolicy");
85 if (!procPtr)
return FALSE;
87 BOOL(WINAPI *_GetProcessDEPPolicy)(HANDLE, LPDWORD,
PBOOL) = (
BOOL(WINAPI *)(HANDLE, LPDWORD,
PBOOL))procPtr;
88 return _GetProcessDEPPolicy(processHandle, &flags, &is_permanent);
95 if (!kernelLib)
return DEPPolicyAlwaysOff;
97 FARPROC procPtr = GetProcAddress(kernelLib,
"GetSystemDEPPolicy");
98 if (!procPtr)
return DEPPolicyAlwaysOff;
100 DEP_SYSTEM_POLICY_TYPE(WINAPI *_GetSystemDEPPolicy)(VOID) = (DEP_SYSTEM_POLICY_TYPE(WINAPI *)(VOID))procPtr;
101 return _GetSystemDEPPolicy();
110 if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)) {
111 if (GetLastError() == ERROR_NO_TOKEN) {
112 if (!ImpersonateSelf(SecurityImpersonation))
return false;
113 if(!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, &hToken)){
114 std::cerr <<
"Error: cannot open the token" << std::endl;
131 DWORD dwIntegrityLevel = *GetSidSubAuthority(pTIL->Label.Sid,
132 (DWORD)(UCHAR)(*GetSidSubAuthorityCount(pTIL->Label.Sid) - 1));
134 if (dwIntegrityLevel == SECURITY_MANDATORY_LOW_RID)
139 if (dwIntegrityLevel >= SECURITY_MANDATORY_MEDIUM_RID &&
140 dwIntegrityLevel < SECURITY_MANDATORY_HIGH_RID)
145 if (dwIntegrityLevel >= SECURITY_MANDATORY_HIGH_RID &&
146 dwIntegrityLevel < SECURITY_MANDATORY_SYSTEM_RID)
151 if (dwIntegrityLevel >= SECURITY_MANDATORY_SYSTEM_RID)
165 HANDLE hToken = NULL;
166 if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) {
170 DWORD dwLength =
sizeof(TOKEN_GROUPS);
171 TOKEN_MANDATORY_LABEL *ptg = (TOKEN_MANDATORY_LABEL*) calloc(1, dwLength);
173 if (!GetTokenInformation(
181 free(ptg); ptg =
nullptr;
182 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
186 ptg = (TOKEN_MANDATORY_LABEL*)calloc(1, dwLength);
194 if (GetTokenInformation(
205 free(ptg); ptg =
nullptr;
213 if (global_dep == DEPPolicyAlwaysOff) {
216 if (global_dep == DEPPolicyAlwaysOn) {
221 BOOL is_permanent = FALSE;
225 BOOL isRemoteWow64 = FALSE;
227 if (!isRemoteWow64) {
232 std::cerr <<
"Could not fetch the DEP status\n";
236 const bool is_DEP = (flags & PROCESS_DEP_ENABLE) || (flags & PROCESS_DEP_DISABLE_ATL_THUNK_EMULATION);
process_integrity_t get_integrity_level(HANDLE hProcess)
bool set_debug_privilege()
DEP_SYSTEM_POLICY_TYPE _get_system_DEP_policy()
BOOL is_process_wow64(IN HANDLE processHandle, OUT BOOL *isProcWow64)
bool is_DEP_enabled(HANDLE hProcess)
BOOL set_privilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege)
BOOL _get_process_DEP_policy(HANDLE processHandle, DWORD &flags, BOOL &is_permanent)
BOOL(CALLBACK *_MiniDumpWriteDump)(HANDLE hProcess
DWORD(__stdcall *_PssCaptureSnapshot)(HANDLE ProcessHandle
HMODULE get_or_load_module(const char *name)
pesieve::util::process_integrity_t translate_integrity_level(TOKEN_MANDATORY_LABEL *pTIL)