Skip to content

Commit bbaa360

Browse files
author
xvt-void
committed
listing privs of processes running as different user with the same or lower integrity level should work now
1 parent 9dc7a24 commit bbaa360

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

EnableAllTokenPrivs.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ namespace EnableAllTokenPrivs
66
{
77
class EnableAllTokenPrivs
88
{
9+
[DllImport("kernel32.dll", SetLastError = true)]
10+
internal static extern IntPtr OpenProcess(uint DesiredAccess, bool bInheritHandle, int dwProcessId);
11+
912
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
1013
internal static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, ref IntPtr TokenHandle);
1114

15+
private const UInt32 PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
16+
1217
private static void printUsage()
1318
{
1419
Console.Write(
@@ -74,19 +79,31 @@ public static void Main(string[] args)
7479
if (processId == -1) {
7580
hProcess = ProcessChild.GetParentProcess().Handle;
7681
} else {
77-
hProcess = Process.GetProcessById(processId).Handle;
82+
try {
83+
hProcess = Process.GetProcessById(processId).Handle;
84+
} catch {
85+
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, processId);
86+
if (hProcess.Equals(-1)) {
87+
throw new Exception("OpenProcess failed. Error: " + Marshal.GetLastWin32Error());
88+
}
89+
}
7890
}
7991

8092
IntPtr hToken = IntPtr.Zero;
81-
if (!OpenProcessToken(hProcess, (AccessToken.TOKEN_ADJUST_PRIVILEGES | AccessToken.TOKEN_QUERY), ref hToken))
82-
throw new Exception("OpenProcessToken failed. Error: " + Marshal.GetLastWin32Error());
8393

8494
if (listPrivs == true)
8595
{
96+
if (!OpenProcessToken(hProcess, (AccessToken.TOKEN_QUERY), ref hToken))
97+
throw new Exception("OpenProcessToken failed. Error: " + Marshal.GetLastWin32Error());
98+
8699
AccessToken.TOKEN_PRIVILEGES TokenPrivileges = AccessToken.GetTokenPrivileges(hToken);
87100
AccessToken.PrintTokenPrivileges(TokenPrivileges);
88101
return;
89102
}
103+
104+
if (!OpenProcessToken(hProcess, (AccessToken.TOKEN_ADJUST_PRIVILEGES | AccessToken.TOKEN_QUERY), ref hToken))
105+
throw new Exception("OpenProcessToken failed. Error: " + Marshal.GetLastWin32Error());
106+
90107
if (privilege.Length > 0)
91108
{
92109
AccessToken.SetTokenPrivilege(hToken, disable, privilege);

0 commit comments

Comments
 (0)