-
Notifications
You must be signed in to change notification settings - Fork 16
/
superUser.c
333 lines (270 loc) · 8.79 KB
/
superUser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
superUser
A simple and lightweight utility to start any process
with TrustedInstaller privileges.
https://github.com/mspaintmsi/superUser
superUser.c
*/
#include <windows.h>
#include <stdio.h>
#include "tokens.h" // Defines tokens and privileges management functions
// Program options
static struct {
unsigned int bReturnCode : 1; // Whether to return process exit code
unsigned int bSeamless : 1; // Whether child process shares parent's console
unsigned int bVerbose : 1; // Whether to print debug messages or not
unsigned int bWait : 1; // Whether to wait to finish created process
} options = {0};
#define wputs _putws
#define wprintfv(...) \
if (options.bVerbose) wprintf(__VA_ARGS__); // Only use when bVerbose in scope
/*
Return codes (without /r option):
1 - Invalid argument
2 - Failed acquiring SeDebugPrivilege
3 - Could not open/start the TrustedInstaller service
4 - Process creation failed
5 - Another fatal error occurred
If /r option is specified, exit code of the child process is returned.
If superUser fails, it returns the code -(EXIT_CODE_BASE + errCode),
where errCode is one of the codes listed above.
*/
#define EXIT_CODE_BASE 1000000
static int nChildExitCode = 0;
static int createTrustedInstallerProcess( wchar_t* pwszImageName )
{
int errCode = 0;
HANDLE hTIProcess = NULL, hTIToken = NULL;
if (options.bSeamless) {
// Start the TrustedInstaller service and get its process token
errCode = getTrustedInstallerToken( &hTIToken );
if (errCode) return errCode;
// Get the console session id and set it in the token
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
if (dwSessionId != (DWORD) -1) {
SetTokenInformation( hTIToken, TokenSessionId, (PVOID) &dwSessionId,
sizeof( DWORD ) );
}
// Set all privileges in the child process token
setAllPrivileges( hTIToken, options.bVerbose );
}
else {
// Start the TrustedInstaller service and get its process handle
errCode = getTrustedInstallerProcess( &hTIProcess );
if (errCode) return errCode;
}
// Initialize startupInfo
STARTUPINFOEX startupInfo = {0};
startupInfo.StartupInfo.cb = sizeof( STARTUPINFOEX );
startupInfo.StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.StartupInfo.wShowWindow = SW_SHOWNORMAL;
if (! options.bSeamless) {
// Initialize attribute lists for "parent assignment"
SIZE_T attributeListLength = 0;
InitializeProcThreadAttributeList( NULL, 1, 0, (PSIZE_T) &attributeListLength );
startupInfo.lpAttributeList = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
attributeListLength );
InitializeProcThreadAttributeList( startupInfo.lpAttributeList, 1, 0,
(PSIZE_T) &attributeListLength );
UpdateProcThreadAttribute( startupInfo.lpAttributeList, 0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &hTIProcess, sizeof( HANDLE ), NULL, NULL );
}
// Create process
PROCESS_INFORMATION processInfo = {0};
DWORD dwCreationFlags = 0;
if (! options.bSeamless)
dwCreationFlags = CREATE_SUSPENDED | EXTENDED_STARTUPINFO_PRESENT |
CREATE_NEW_CONSOLE;
wprintfv( L"[D] Creating specified process\n" );
BOOL bCreateResult = FALSE;
if (options.bSeamless)
bCreateResult = CreateProcessAsUser(
hTIToken,
NULL,
pwszImageName,
NULL,
NULL,
FALSE,
dwCreationFlags,
NULL,
NULL,
&startupInfo.StartupInfo,
&processInfo
);
else
bCreateResult = CreateProcess(
NULL,
pwszImageName,
NULL,
NULL,
FALSE,
dwCreationFlags,
NULL,
NULL,
&startupInfo.StartupInfo,
&processInfo
);
DWORD dwCreateError = bCreateResult ? 0 : GetLastError();
if (options.bSeamless) CloseHandle( hTIToken );
else
{
DeleteProcThreadAttributeList( startupInfo.lpAttributeList );
HeapFree( GetProcessHeap(), 0, startupInfo.lpAttributeList );
}
if (bCreateResult) {
if (! options.bSeamless) {
HANDLE hProcessToken = NULL;
OpenProcessToken( processInfo.hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hProcessToken );
// Set all privileges in the child process token
setAllPrivileges( hProcessToken, options.bVerbose );
CloseHandle( hProcessToken );
ResumeThread( processInfo.hThread );
}
wprintfv( L"[D] Created process ID: %ld\n", processInfo.dwProcessId );
if (options.bWait) {
wprintfv( L"[D] Waiting for process to exit\n" );
WaitForSingleObject( processInfo.hProcess, INFINITE );
wprintfv( L"[D] Process exited\n" );
// Get the child's exit code
DWORD dwExitCode;
if (GetExitCodeProcess( processInfo.hProcess, &dwExitCode )) {
nChildExitCode = dwExitCode;
wprintfv( L"[D] Process exit code: %ld\n", dwExitCode );
}
}
CloseHandle( processInfo.hProcess );
CloseHandle( processInfo.hThread );
}
else {
// Most commonly - 0x2 - The system cannot find the file specified.
fwprintf( stderr, L"[E] Process creation failed. Error code: 0x%08X\n",
dwCreateError );
return 4;
}
return 0;
}
static BOOL getArgument( wchar_t** ppArgument, wchar_t** ppArgumentIndex )
{
// Current pointer to the remainder of the line to be parsed.
// Initialized with the full command line on the first call.
static wchar_t* p = NULL;
if (! p) {
p = GetCommandLine();
// Skip program name
BOOL bQuote = FALSE;
while (*p) {
if (*p == L'"') bQuote = ! bQuote;
else if (! bQuote && (*p == L' ' || *p == L'\t')) break;
p++;
}
}
// Free the previous argument (if it exists)
if (*ppArgument) HeapFree( GetProcessHeap(), 0, *ppArgument );
*ppArgument = NULL;
// Search argument
// Skip spaces
while (*p == L' ' || *p == L'\t') p++;
if (*p) {
// Argument found
wchar_t* pBegin = p;
// Search the end of the argument
while (*p && *p != L' ' && *p != L'\t') p++;
size_t nArgSize = (p - pBegin) * sizeof( wchar_t );
*ppArgument = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
nArgSize + sizeof( wchar_t ) );
memcpy( *ppArgument, pBegin, nArgSize );
*ppArgumentIndex = pBegin;
return TRUE;
}
// Argument not found
return FALSE;
}
static int getExitCode( int code )
{
if (code == -1) code = 0; // Print help, exit with code 0
if (options.bReturnCode) {
if (code) code = -(EXIT_CODE_BASE + code);
else code = nChildExitCode;
}
return code;
}
static void printHelp( void )
{
wputs(
L"superUser.exe [options] [command_to_run]\n\
Options: (You can use either '-' or '/')\n\
/h - Display this help message.\n\
/r - Return exit code of child process. Requires /w.\n\
/s - Child process shares parent's console. Requires /w.\n\
/v - Display verbose messages.\n\
/w - Wait for the created process to finish before exiting." );
}
int wmain( int argc, wchar_t* argv[] )
{
int errCode = 0; // superUser error code
// Command to run (executable file name of process to create, followed by
// parameters) - basically the first non-option argument or "cmd.exe".
wchar_t* pwszCommandLine = NULL;
wchar_t* pwszArgument = NULL; // Command line argument
wchar_t* pwszArgumentIndex = NULL; // Pointer to argument in command line
// Parse command line options
while (getArgument( &pwszArgument, &pwszArgumentIndex )) {
// Check for an at-least-two-character string beginning with '/' or '-'
if ((*pwszArgument == L'/' || *pwszArgument == L'-') && pwszArgument[ 1 ]) {
int j = 1;
wchar_t opt;
while ((opt = pwszArgument[ j ])) {
// Multiple options can be grouped together (eg: /wrs)
switch (opt) {
case 'h':
printHelp();
errCode = -1;
goto done_params;
case 'r':
options.bReturnCode = 1;
break;
case 's':
options.bSeamless = 1;
break;
case 'v':
options.bVerbose = 1;
break;
case 'w':
options.bWait = 1;
break;
default:
fwprintf( stderr, L"[E] Invalid option\n" );
errCode = 1;
goto done_params;
}
j++;
}
}
else {
// First non-option argument found
pwszCommandLine = pwszArgumentIndex;
break;
}
}
done_params:
// Free the last argument (if it exists)
if (pwszArgument) HeapFree( GetProcessHeap(), 0, pwszArgument );
if (errCode) return getExitCode( errCode );
// Check the consistency of the options
if ((options.bReturnCode || options.bSeamless) && ! options.bWait) {
fwprintf( stderr, L"[E] /r or /s option requires /w\n" );
return getExitCode( 1 );
}
if (! pwszCommandLine) pwszCommandLine = L"cmd.exe";
wprintfv( L"[D] Your commandline is \"%ls\"\n", pwszCommandLine );
// pwszCommandLine may be read-only. It must be copied to a writable area.
size_t nCommandLineBufSize = (wcslen( pwszCommandLine ) + 1) * sizeof( wchar_t );
wchar_t* pwszImageName = HeapAlloc( GetProcessHeap(), 0, nCommandLineBufSize );
memcpy( pwszImageName, pwszCommandLine, nCommandLineBufSize );
errCode = acquireSeDebugPrivilege();
if (! errCode && options.bSeamless) errCode = createSystemContext();
if (! errCode) errCode = createTrustedInstallerProcess( pwszImageName );
HeapFree( GetProcessHeap(), 0, pwszImageName );
return getExitCode( errCode );
}