Process.cpp
Code:
#include "Process.h"
LPCTSTR ProcessPrivileges::Tcb = SE_TCB_NAME;
LPCTSTR ProcessPrivileges::Debug = SE_DEBUG_NAME;
LPCTSTR ProcessPrivileges::Backup = SE_BACKUP_NAME;
LPCTSTR ProcessPrivileges::IncreaseQuota = SE_INCREASE_QUOTA_NAME;
Process::Process(const HANDLE& processHandle)
{
this->handle = processHandle;
this->handleOpened = true;
}
Process::Process(const PROCESSENTRY32& processEntry)
{
this->processEntry = processEntry;
this->handleOpened = false;
}
Process::Process()
{
}
Process::~Process()
{
if (handleOpened) CloseHandle(handle);
}
HANDLE Process::getHandle()
{
if (!handleOpened)
{
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, getPid());
handleOpened = true;
}
return handle;
}
DWORD Process::getPid()
{
return processEntry.th32ProcessID;
}
DWORD Process::getParentPid()
{
return processEntry.th32ParentProcessID;
}
DWORD Process::getThreadsCount()
{
return processEntry.cntThreads;
}
DWORD Process::getModuleId()
{
return processEntry.th32ModuleID;
}
char* Process::getExeFilename()
{
return processEntry.szExeFile;
}
bool Process::setPrivilege(LPCTSTR privilege, bool enable)
{
LUID luid;
HANDLE token;
TOKEN_PRIVILEGES tokenPrivileges;
if (!OpenProcessToken(getHandle(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) return false;
if (!LookupPrivilegeValue(NULL, privilege, &luid)) return false;
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges[0].Luid = luid;
if (enable) tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else tokenPrivileges.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(token, FALSE, &tokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
DWORD error;
if ((error = GetLastError()) != ERROR_SUCCESS)
{
std::cout << error;
return false;
}
return true;
}
HANDLE Process::getCurrentProcessHandle()
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
}
DWORD Process::getCurrentProcessId()
{
return GetCurrentProcessId();
}
Main.cpp:
Code:
#include <iostream>
#include "ProcessEnumerator.h"
int main()
{
ProcessEnumerator enumerator;
Process actual;
Process myProcess(Process::getCurrentProcessHandle());
myProcess.setPrivilege(ProcessPrivileges::Debug, true);
enumerator.getFirst(actual);
do
{
std::cout << "Process Name: " << actual.getExeFilename() << '\n';
std::cout << "Process ID : " << actual.getPid() << '\n';
} while (enumerator.getNext(actual));
std::cin.get();
return 0;
}
Everything just fine, debug level has been seted, now whats the next step? (Do i have to close any handle that i havent or anything else in the actual code? )