Torna al Thread
struct LUID {
public UInt32 LowPart;
public Int32 HighPart;
}
struct LUID_AND_ATTRIBUTES {
public LUID Luid;
public uint Attributes;
}
struct TOKEN_PRIVILEGES {
public uint PrivilegeCount;
public LUID_AND_ATTRIBUTES Privileges_1;
}
[DllImport("advapi32.dll")]
extern static bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
[DllImport("advapi32.dll")]
extern static bool LookupPrivilegeValue(string lpSystemName, string lpTokenName, out LUID lpLuid);
[DllImport("advapi32.dll")]
extern static bool AdjustTokenPrivileges(IntPtr tokenHandle,
uint disableAllPrivileges,
ref TOKEN_PRIVILEGES newState,
int bufferLength,
out TOKEN_PRIVILEGES previousState,
out int returnLength);
[DllImport("kernel32.dll")]
extern static bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
extern static uint GetLastError();
const uint SE_PRIVILEGE_ENABLED = 0x2;
const uint TOKEN_ADJUST_PRIVILEGES = 0x20;
const string SE_DEBUG_NAME = "SeDebugPrivilege";
const uint FALSE = 0;
const uint ERROR_SUCCESS = 0;
bool EnableDebugPrivilege(IntPtr processHandle)
{
IntPtr hToken;
TOKEN_PRIVILEGES tokPriv, newState;
LUID_AND_ATTRIBUTES attr;
int retLen;
bool res;
// Ottiene il token per il processo
if(!OpenProcessToken(processHandle, TOKEN_ADJUST_PRIVILEGES, out hToken)) {
return false;
}
res = false;
// Ottiene il LUID per il privilegio
if(LookupPrivilegeValue(null, SE_DEBUG_NAME, out attr.Luid)) {
// Un privilegio va impostato
tokPriv.PrivilegeCount = 1;
// E va abilitato
attr.Attributes = SE_PRIVILEGE_ENABLED;
tokPriv.Privileges_1 = attr;
// Aggiusta il privilegio
if(AdjustTokenPrivileges(hToken, FALSE, ref tokPriv, 0, out newState, out retLen)) {
res = (GetLastError() == ERROR_SUCCESS);
}
}
CloseHandle(hToken);
return res;
}