Skip to content

Commit 9e541ee

Browse files
committed
Enable SeDebugPrivilege when needed
1 parent 621cd3c commit 9e541ee

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

windows_memory_extractor.cpp

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct ArgumentManager {
3030
void validateArguments(int argc, char* argv[]) {
3131

3232
namespace po = boost::program_options;
33-
std::string version = "v1.0.3";
33+
std::string version = "v1.0.4";
3434
po::options_description description("Windows memory extractor " + version + "\nUsage");
3535

3636
description.add_options()
@@ -215,7 +215,27 @@ struct MemoryExtractionManager {
215215

216216
HANDLE processHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, argumentManager.getPid());
217217
if (processHandle == NULL) {
218-
throw std::exception{ "A handle to the specified process could not be obtained" };
218+
219+
// Try to enable SeDebugPrivilege and call OpenProcess again
220+
HANDLE accessToken;
221+
222+
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &accessToken) == FALSE) {
223+
throw std::exception{ "An error has occurred trying to enable SeDebugPrivlege at function OpenProcessToken" };
224+
}
225+
226+
if (!SetPrivilege(accessToken, SE_DEBUG_NAME, true)) {
227+
CloseHandle(accessToken);
228+
throw std::exception{ "An error has occurred trying to enable SeDebugPrivlege at function SetPrivilege" };
229+
}
230+
231+
processHandle = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, argumentManager.getPid());
232+
233+
CloseHandle(accessToken);
234+
235+
if (processHandle == NULL) {
236+
throw std::exception{ "A handle to the specified process could not be obtained" };
237+
}
238+
219239
}
220240

221241
directoryName = createDirectory();
@@ -448,6 +468,56 @@ struct MemoryExtractionManager {
448468
resultsFile << ", Memory protection: " << memoryProtection << "\n";
449469
}
450470

471+
// Function found here: https://docs.microsoft.com/en-us/windows/win32/secauthz/enabling-and-disabling-privileges-in-c--
472+
BOOL SetPrivilege(
473+
HANDLE hToken, // access token handle
474+
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
475+
BOOL bEnablePrivilege // to enable or disable privilege
476+
)
477+
{
478+
TOKEN_PRIVILEGES tp;
479+
LUID luid;
480+
481+
if (!LookupPrivilegeValue(
482+
NULL, // lookup privilege on local system
483+
lpszPrivilege, // privilege to lookup
484+
&luid)) // receives LUID of privilege
485+
{
486+
printf("LookupPrivilegeValue error: %u\n", GetLastError());
487+
return FALSE;
488+
}
489+
490+
tp.PrivilegeCount = 1;
491+
tp.Privileges[0].Luid = luid;
492+
if (bEnablePrivilege)
493+
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
494+
else
495+
tp.Privileges[0].Attributes = 0;
496+
497+
// Enable the privilege or disable all privileges.
498+
499+
if (!AdjustTokenPrivileges(
500+
hToken,
501+
FALSE,
502+
&tp,
503+
sizeof(TOKEN_PRIVILEGES),
504+
(PTOKEN_PRIVILEGES)NULL,
505+
(PDWORD)NULL))
506+
{
507+
printf("AdjustTokenPrivileges error: %u\n", GetLastError());
508+
return FALSE;
509+
}
510+
511+
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
512+
513+
{
514+
printf("The token does not have the specified privilege. \n");
515+
return FALSE;
516+
}
517+
518+
return TRUE;
519+
}
520+
451521
ArgumentManager& argumentManager;
452522
std::string directoryName; // The directory where the memory data files will be placed
453523
unsigned int dmpFilesGeneratedCount;

0 commit comments

Comments
 (0)