-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRadRunAs.cpp
320 lines (289 loc) · 8.48 KB
/
RadRunAs.cpp
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
#include <Windows.h>
#include <Shlwapi.h>
#include <WinCred.H>
// To set credential:
// cmdkey /generic:RadRunAs:<Cred> /user:<User> /pass
// Note: Executable needs to be placed in a location accessible by the RunAs user
#define PROGRAM L"RadRunAs"
const LPCWSTR PARAM_ELEVATED = L"/elevated";
const LPCWSTR PARAM_USER = L"/user:";
const LPCWSTR PARAM_PASSWORD = L"/password:";
const LPCWSTR PARAM_CRED = L"/cred:";
void ErrorMessage(_In_ LPCWSTR lpFormat, ...)
{
WCHAR buf[1024];
va_list vl;
va_start(vl, lpFormat);
wvsprintf(buf, lpFormat, vl);
va_end(vl);
MessageBox(NULL, buf, PROGRAM, MB_OK | MB_ICONERROR);
}
void ShowError(_In_ LPCWSTR lpWhere)
{
DWORD errorCode = GetLastError();
LPWSTR msg = NULL;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorCode, 0, (LPWSTR) &msg, 0, NULL) == 0)
{
ErrorMessage(L"Error: %i FormatMessage.", errorCode);
}
else
{
ErrorMessage(L"Error: %i %s.\n%s", errorCode, lpWhere, msg);
LocalFree(msg);
}
}
bool IsValidArg(LPCWSTR lpArg, _In_ LPCWSTR lpParam)
{
int nLen = lstrlen(lpParam);
if (nLen >= 1 && lpParam[nLen - 1] == L':')
{
return StrCmpNI(lpArg, lpParam, nLen) == 0;
}
else
{
return StrCmpI(lpArg, lpParam) == 0;
}
}
LPCWSTR FindArg(const LPCWSTR* szArgs, int nArgs, _In_ LPCWSTR lpParam)
{
int nLen = lstrlen(lpParam);
if (nLen >= 1 && lpParam[nLen - 1] == L':')
{
for (int i = 0; i < nArgs; ++i)
{
LPCWSTR lpArg = szArgs[i];
if (StrCmpNI(lpArg, lpParam, nLen) == 0)
return lpArg + nLen;
}
}
else
{
for (int i = 0; i < nArgs; ++i)
{
LPCWSTR lpArg = szArgs[i];
if (StrCmpI(lpArg, lpParam) == 0)
return lpArg;
}
}
return nullptr;
}
int FindCommandLine(LPCWSTR* szArgs, int nArgs)
{
for (int i = 0; i < nArgs; ++i)
{
LPCWSTR lpArg = szArgs[i];
if (lpArg[0] != L'/')
return i;
}
return -1;
}
BOOL MyPathFindOnPath(_Inout_updates_(MAX_PATH) LPWSTR pszPath, _In_opt_ PZPCWSTR ppszOtherDirs)
{
// TODO Use PATHEXT
int nLen = lstrlen(pszPath);
if (nLen <= 4 || StrCmpI(pszPath + nLen - 4, L".exe") != 0)
{
StrCat(pszPath, L".exe");
}
if (StrCmpI(pszPath, L"regedit.exe") == 0)
{
StrCpy(pszPath, L"C:\\Windows\\regedit.exe");
return TRUE;
}
else
{
return PathFindOnPath(pszPath, ppszOtherDirs);
}
}
LPWSTR CopyString(const LPCWSTR s, const size_t l)
{
LPWSTR r = new WCHAR[l + 1];
//wcscpy_s(r, l, s);
StrCpyN(r, s, (int) l + 1);
r[l] = L'\0';
return r;
}
LPWSTR CopyString(const LPCWSTR s)
{
return CopyString(s, wcslen(s));
}
struct Credentials
{
bool valid;
const LPCWSTR fulluser;
const LPCWSTR password;
};
const Credentials GetCredentials(const LPCWSTR* szArgs, int nArgs)
{
const LPCWSTR cred = FindArg(szArgs, nArgs, PARAM_CRED);
if (cred != nullptr)
{
PCREDENTIAL pCred = nullptr;
if (CredRead(cred, CRED_TYPE_GENERIC, 0, &pCred))
{
// Note: Both of these CopyString leak memory, but due to short life of process, shouldn't be a problem
Credentials credentials{
true,
CopyString(pCred->UserName),
CopyString((LPTSTR) pCred->CredentialBlob, pCred->CredentialBlobSize / sizeof(TCHAR))
};
CredFree(pCred);
return credentials;
}
else
{
ErrorMessage(L"Cannot find credentials for: %s", cred);
return Credentials{
false,
nullptr,
nullptr
};
}
}
else
{
return Credentials{
true,
FindArg(szArgs, nArgs, PARAM_USER),
FindArg(szArgs, nArgs, PARAM_PASSWORD)
};
}
}
int CALLBACK wWinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE /*hPrevInstance*/,
_In_ LPWSTR lpCmdLine,
_In_ int /*nCmdShow*/
)
{
int nArgs = 0;
LPCWSTR* szArgs = (LPCWSTR*) CommandLineToArgvW(lpCmdLine, &nArgs);
if (szArgs == nullptr)
{
ShowError(L"CommandLineToArgvW");
return 5;
}
const int command = FindCommandLine(szArgs, nArgs);
for (int i = 0; i < command; ++i)
{
LPCWSTR lpArg = szArgs[i];
if (lpArg[0] == L'/'
&& !(IsValidArg(lpArg, PARAM_ELEVATED) || IsValidArg(lpArg, PARAM_USER) || IsValidArg(lpArg, PARAM_PASSWORD) || IsValidArg(lpArg, PARAM_CRED)))
{
ErrorMessage(L"Invalid Argument: %s", lpArg);
return 1;
}
}
const Credentials cred = GetCredentials(szArgs, nArgs);
if (!cred.valid)
return 1;
bool elevated = FindArg(szArgs, nArgs, PARAM_ELEVATED) != nullptr;
if (cred.fulluser != nullptr && command >= 0)
{
const LPWSTR origCommandLine = StrStr(lpCmdLine, szArgs[command]);
WCHAR title[MAX_PATH] = L"";
StrCpy(title, origCommandLine);
StrCat(title, L" (");
StrCat(title, cred.fulluser);
StrCat(title, L")");
WCHAR username[1024] = L"";
WCHAR domain[1024] = L"";
CredUIParseUserName(cred.fulluser, username, 1024, domain, 1024);
if (!elevated)
{
WCHAR application[MAX_PATH] = L"";
StrCpy(application, szArgs[command]);
MyPathFindOnPath(application, nullptr);
const LPWSTR commandLine = origCommandLine;
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.lpTitle = title;
PROCESS_INFORMATION pi = {};
if (!CreateProcessWithLogonW(
username,
domain,
cred.password,
LOGON_WITH_PROFILE,
application,
commandLine,
0,
NULL,
NULL,
&si,
&pi))
{
if (!elevated && GetLastError() == ERROR_ELEVATION_REQUIRED)
{
elevated = true;
}
else
{
ShowError(L"CreateProcessWithLogonW");
LocalFree(szArgs);
return 1;
}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
WCHAR application[MAX_PATH] = L"";
WCHAR commandLine[MAX_PATH] = L"";
GetModuleFileName(hInstance, application, MAX_PATH);
StrCpy(commandLine, PathFindFileName(application));
StrCat(commandLine, L" /elevated ");
StrCat(commandLine, origCommandLine);
STARTUPINFO si = { sizeof(STARTUPINFO) };
si.lpTitle = title;
PROCESS_INFORMATION pi = {};
if (!CreateProcessWithLogonW(
username,
domain,
cred.password,
LOGON_WITH_PROFILE,
application,
commandLine,
0,
NULL,
NULL,
&si,
&pi))
{
ShowError(L"CreateProcessWithLogonW");
LocalFree(szArgs);
return 1;
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
else if (elevated && command >= 0)
{
const LPCWSTR application = szArgs[command];
const LPCWSTR parameters = (command + 1) < nArgs ? StrStr(lpCmdLine, szArgs[command + 1]) : nullptr;
if ((INT_PTR) ShellExecute(NULL, L"runas", application, parameters, nullptr, SW_SHOWNORMAL) < 32)
{
ShowError(L"ShellExecute");
LocalFree(szArgs);
return 1;
}
}
else
{
WCHAR buf[] =
PROGRAM L" Usage:\n"
L"\n"
PROGRAM L" /user:<UserName> [/password:<Password>] [/elevated] <program> [parameters]\n"
L"\n"
PROGRAM L" /cred:RadRunAs:<Credential> [/elevated] <program> [parameters]\n"
L"\n"
L" Created using: cmdkey /generic:RadRunAs:<Cred> /user:<User> /pass\n"
L"\n"
PROGRAM L" /elevated <program> [parameters]";
MessageBox(NULL, buf, PROGRAM, MB_OK | MB_ICONINFORMATION);
return 1;
}
LocalFree(szArgs);
return 0;
}