1 |
using System; |
2 |
using System.Runtime.InteropServices; |
3 |
using System.Security; |
4 |
using System.Diagnostics; |
5 |
|
6 |
public class TokenAdjuster |
7 |
{ |
8 |
// obtained from: http://www.dotnet247.com/247reference/msgs/58/292150.aspx |
9 |
|
10 |
// PInvoke stuff required to set/enable security privileges |
11 |
[DllImport("advapi32", SetLastError = true), |
12 |
SuppressUnmanagedCodeSecurityAttribute] |
13 |
static extern int OpenProcessToken( |
14 |
System.IntPtr ProcessHandle, // handle to process |
15 |
int DesiredAccess, // desired access to process |
16 |
ref IntPtr TokenHandle // handle to open access token |
17 |
); |
18 |
|
19 |
[DllImport("kernel32", SetLastError = true), |
20 |
SuppressUnmanagedCodeSecurityAttribute] |
21 |
static extern bool CloseHandle(IntPtr handle); |
22 |
|
23 |
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true), |
24 |
SuppressUnmanagedCodeSecurityAttribute] |
25 |
static extern int AdjustTokenPrivileges( |
26 |
IntPtr TokenHandle, |
27 |
int DisableAllPrivileges, |
28 |
IntPtr NewState, |
29 |
int BufferLength, |
30 |
IntPtr PreviousState, |
31 |
ref int ReturnLength); |
32 |
|
33 |
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true), |
34 |
SuppressUnmanagedCodeSecurityAttribute] |
35 |
static extern bool LookupPrivilegeValue( |
36 |
string lpSystemName, |
37 |
string lpName, |
38 |
ref LUID lpLuid); |
39 |
|
40 |
[StructLayout(LayoutKind.Sequential)] |
41 |
internal struct LUID |
42 |
{ |
43 |
internal int LowPart; |
44 |
internal int HighPart; |
45 |
} |
46 |
|
47 |
[StructLayout(LayoutKind.Sequential)] |
48 |
struct LUID_AND_ATTRIBUTES |
49 |
{ |
50 |
LUID Luid; |
51 |
int Attributes; |
52 |
} |
53 |
|
54 |
[StructLayout(LayoutKind.Sequential)] |
55 |
struct _PRIVILEGE_SET |
56 |
{ |
57 |
int PrivilegeCount; |
58 |
int Control; |
59 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] // ANYSIZE_ARRAY = 1 |
60 |
LUID_AND_ATTRIBUTES[] Privileges; |
61 |
} |
62 |
|
63 |
[StructLayout(LayoutKind.Sequential)] |
64 |
internal struct TOKEN_PRIVILEGES |
65 |
{ |
66 |
internal int PrivilegeCount; |
67 |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] |
68 |
internal int[] Privileges; |
69 |
} |
70 |
const int SE_PRIVILEGE_ENABLED = 0x00000002; |
71 |
const int TOKEN_ADJUST_PRIVILEGES = 0X00000020; |
72 |
const int TOKEN_QUERY = 0X00000008; |
73 |
const int TOKEN_ALL_ACCESS = 0X001f01ff; |
74 |
const int PROCESS_QUERY_INFORMATION = 0X00000400; |
75 |
|
76 |
public static bool SetPrivilege(string lpszPrivilege, bool |
77 |
bEnablePrivilege) |
78 |
{ |
79 |
bool retval = false; |
80 |
int ltkpOld = 0; |
81 |
IntPtr hToken = IntPtr.Zero; |
82 |
TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); |
83 |
tkp.Privileges = new int[3]; |
84 |
TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES(); |
85 |
tkpOld.Privileges = new int[3]; |
86 |
LUID tLUID = new LUID(); |
87 |
tkp.PrivilegeCount = 1; |
88 |
if (bEnablePrivilege) |
89 |
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED; |
90 |
else |
91 |
tkp.Privileges[2] = 0; |
92 |
if (LookupPrivilegeValue(null, lpszPrivilege, ref tLUID)) |
93 |
{ |
94 |
Process proc = Process.GetCurrentProcess(); |
95 |
if (proc.Handle != IntPtr.Zero) |
96 |
{ |
97 |
if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, |
98 |
ref hToken) != 0) |
99 |
{ |
100 |
tkp.PrivilegeCount = 1; |
101 |
tkp.Privileges[2] = SE_PRIVILEGE_ENABLED; |
102 |
tkp.Privileges[1] = tLUID.HighPart; |
103 |
tkp.Privileges[0] = tLUID.LowPart; |
104 |
const int bufLength = 256; |
105 |
IntPtr tu = Marshal.AllocHGlobal(bufLength); |
106 |
Marshal.StructureToPtr(tkp, tu, true); |
107 |
if (AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref |
108 |
ltkpOld) != 0) |
109 |
{ |
110 |
// successful AdjustTokenPrivileges doesn't mean privilege could bechanged |
111 |
if (Marshal.GetLastWin32Error() == 0) |
112 |
{ |
113 |
retval = true; // Token changed |
114 |
} |
115 |
} |
116 |
TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(tu, |
117 |
typeof(TOKEN_PRIVILEGES)); |
118 |
Marshal.FreeHGlobal(tu); |
119 |
} |
120 |
} |
121 |
} |
122 |
if (hToken != IntPtr.Zero) |
123 |
{ |
124 |
CloseHandle(hToken); |
125 |
} |
126 |
return retval; |
127 |
} |
128 |
} |
129 |
// End class |