-
Notifications
You must be signed in to change notification settings - Fork 25
/
winapiexec.c
439 lines (352 loc) · 7.93 KB
/
winapiexec.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include <windows.h>
#include "buffer.h"
#define DEF_VERSION L"1.2"
extern DWORD_PTR __stdcall ParseExecFunction(WCHAR ***pp_argv);
DWORD_PTR ParseExecArgs(WCHAR ***pp_argv);
DWORD_PTR __stdcall GetFunctionPtr(WCHAR ***pp_argv);
DWORD_PTR __stdcall GetNextArg(WCHAR ***pp_argv, BOOL *pbNoMoreArgs);
__declspec(noreturn) void __stdcall FatalStackError(WCHAR **p_argv);
FARPROC MyGetProcAddress(WCHAR *pszModuleProcStr);
DWORD_PTR ParseArg(WCHAR *pszArg);
DWORD_PTR ParseArrayArg(WCHAR *pszArrayArg);
WCHAR *StrToDwordPtr(WCHAR *pszStr, DWORD_PTR *pdw);
char *UnicodeToAscii(WCHAR *pszUnicode);
__declspec(noreturn) void FatalExitMsgBox(WCHAR *format, ...);
int argc;
WCHAR **argv;
void main()
{
WCHAR **p_argv;
argv = CommandLineToArgvW(GetCommandLine(), &argc);
if(!argv)
{
ExitProcess(1);
}
if(argc < 2)
{
MessageBox(NULL, L"See readme or visit http://rammichael.com/", L"winapiexec v" DEF_VERSION, MB_ICONASTERISK);
ExitProcess(0);
}
p_argv = &argv[1];
ExitProcess((UINT)ParseExecArgs(&p_argv));
}
DWORD_PTR ParseExecArgs(WCHAR ***pp_argv)
{
DWORD_PTR dwRet;
WCHAR **p_argv;
WCHAR *arg;
p_argv = *pp_argv;
dwRet = ParseExecFunction(&p_argv);
// After calling ParseExecFunction, p_argv is supposed
// to point at the end of the array, or ",", or ")"
while(p_argv != &argv[argc])
{
arg = *p_argv;
if(arg[0] != L',' || arg[1] != L'\0')
break;
p_argv++;
if(p_argv == &argv[argc])
break;
dwRet = ParseExecFunction(&p_argv);
}
*pp_argv = p_argv;
return dwRet;
}
DWORD_PTR __stdcall GetFunctionPtr(WCHAR ***pp_argv)
{
DWORD_PTR dwRet;
WCHAR **p_argv;
WCHAR *arg;
p_argv = *pp_argv;
arg = *p_argv;
dwRet = (DWORD_PTR)MyGetProcAddress(arg);
p_argv++;
*pp_argv = p_argv;
return dwRet;
}
DWORD_PTR __stdcall GetNextArg(WCHAR ***pp_argv, BOOL *pbNoMoreArgs)
{
DWORD_PTR dwRet;
WCHAR **p_argv;
WCHAR *arg;
BOOL bSpecialArg;
p_argv = *pp_argv;
if(p_argv == &argv[argc])
{
*pbNoMoreArgs = TRUE;
return 0;
}
bSpecialArg = FALSE;
arg = *p_argv;
if(arg[0] != L'\0' && arg[1] == L'\0')
{
switch(arg[0])
{
case L',':
case L')':
bSpecialArg = TRUE;
*pbNoMoreArgs = TRUE;
dwRet = 0;
break;
case L'(':
bSpecialArg = TRUE;
p_argv++;
if(p_argv == &argv[argc])
{
*pbNoMoreArgs = TRUE;
dwRet = 0;
break;
}
dwRet = ParseExecArgs(&p_argv);
// After calling ParseExecArgs, p_argv is supposed
// to point at the end of the array, or ")"
if(p_argv != &argv[argc])
{
arg = *p_argv;
if(arg[0] == L')' && arg[1] == L'\0')
p_argv++;
}
*pbNoMoreArgs = FALSE;
break;
}
}
if(!bSpecialArg)
{
dwRet = ParseArg(arg);
*(DWORD_PTR *)p_argv = dwRet;
p_argv++;
*pbNoMoreArgs = FALSE;
}
*pp_argv = p_argv;
return dwRet;
}
__declspec(noreturn) void __stdcall FatalStackError(WCHAR **p_argv)
{
int nArgIndex = (int)(p_argv - argv);
FatalExitMsgBox(L"Stack error on argument number %d", nArgIndex);
}
FARPROC MyGetProcAddress(WCHAR *pszModuleProcStr)
{
WCHAR *psz;
WCHAR *pszModule, *pszProc;
char *pszAsciiProc;
HMODULE hModule;
FARPROC fpProc;
psz = pszModuleProcStr;
while(*psz != L'\0' && *psz != L'@')
psz++;
if(*psz == L'\0')
{
pszModule = L"kernel32.dll";
pszProc = pszModuleProcStr;
}
else
{
*psz = L'\0';
pszProc = psz + 1;
pszModule = pszModuleProcStr;
if(pszModule[0] == L'\0')
{
pszModule = L"kernel32.dll";
}
else if(pszModule[1] == L'\0')
{
if(pszModule[0] == L'k' || pszModule[0] == L'K')
pszModule = L"kernel32.dll";
else if(pszModule[0] == L'u' || pszModule[0] == L'U')
pszModule = L"user32.dll";
}
}
hModule = GetModuleHandle(pszModule);
if(!hModule)
{
hModule = LoadLibrary(pszModule);
if(!hModule)
FatalExitMsgBox(L"Could not load module %s", pszModule);
}
pszAsciiProc = UnicodeToAscii(pszProc);
fpProc = GetProcAddress(hModule, pszAsciiProc);
HeapFree(GetProcessHeap(), 0, pszAsciiProc);
if(!fpProc)
FatalExitMsgBox(L"Could not find procedure %s in module %s", pszProc, pszModule);
return fpProc;
}
DWORD_PTR ParseArg(WCHAR *pszArg)
{
BOOL bParsed;
WCHAR *pszStr;
char *pszAsciiStr;
DWORD_PTR dw, dw2;
bParsed = FALSE;
if(pszArg[0] == L'$' && pszArg[1] != L'\0' && pszArg[2] == L':')
{
switch(pszArg[1])
{
case L's': // ascii string
pszAsciiStr = UnicodeToAscii(pszArg + 3);
lstrcpyA((char *)pszArg, pszAsciiStr);
HeapFree(GetProcessHeap(), 0, pszAsciiStr);
dw = (DWORD_PTR)pszArg;
bParsed = TRUE;
break;
case L'u': // unicode string
dw = (DWORD_PTR)(pszArg + 3);
bParsed = TRUE;
break;
case L'b': // buffer
StrToDwordPtr(pszArg + 3, &dw);
if(dw > 0)
dw = (DWORD_PTR)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY, dw);
else
dw = (DWORD_PTR)pszArg;
bParsed = TRUE;
break;
case L'$': // another arg
pszStr = StrToDwordPtr(pszArg + 3, &dw);
dw = (DWORD_PTR)argv[dw];
while(*pszStr == '@')
{
pszStr = StrToDwordPtr(pszStr + 1, &dw2);
dw = ((DWORD_PTR *)dw)[dw2];
}
bParsed = TRUE;
break;
case L'a': // array
dw = ParseArrayArg(pszArg);
bParsed = TRUE;
break;
}
}
else if(pszArg[0] == L'$' && pszArg[1] == L'a' && pszArg[2] == L'[')
{
// array brackets syntax, e.g. "$a[1,2,3]"
dw = ParseArrayArg(pszArg);
bParsed = TRUE;
}
if(!bParsed)
{
pszStr = StrToDwordPtr(pszArg, &dw);
if(*pszStr == L'\0')
bParsed = TRUE;
}
if(bParsed)
return dw;
return (DWORD_PTR)pszArg;
}
DWORD_PTR ParseArrayArg(WCHAR *pszArrayArg)
{
BOOL bBracketsSyntax;
int nNestingCount;
int nArrayCount;
int i;
DWORD_PTR *pdw;
WCHAR *pszArrayItem, *pszNextItem;
// bBracketsSyntax is TRUE for "$a[1,2,3]", FALSE for "$a:1,2,3"
bBracketsSyntax = (pszArrayArg[2] == L'[');
nNestingCount = 0;
pszArrayArg[2] = ',';
nArrayCount = 0;
for(i = 2; pszArrayArg[i] != L'\0'; i++)
{
if(bBracketsSyntax && pszArrayArg[i] == L']')
{
if(nNestingCount == 0)
{
pszArrayArg[i] = L'\0';
break;
}
nNestingCount--;
}
if(nNestingCount == 0 && pszArrayArg[i] == L',')
{
pszArrayArg[i] = L'\0';
nArrayCount++;
if(bBracketsSyntax &&
pszArrayArg[i + 1] == L'$' &&
pszArrayArg[i + 2] == L'a' &&
pszArrayArg[i + 3] == L'[')
{
nNestingCount++;
i += 3;
}
}
}
pdw = (DWORD_PTR *)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, nArrayCount * sizeof(DWORD_PTR));
pszArrayItem = pszArrayArg + 3;
for(i = 0; i < nArrayCount - 1; i++)
{
pszNextItem = pszArrayItem + lstrlen(pszArrayItem) + 1;
pdw[i] = ParseArg(pszArrayItem);
pszArrayItem = pszNextItem;
}
pdw[i] = ParseArg(pszArrayItem);
return (DWORD_PTR)pdw;
}
WCHAR *StrToDwordPtr(WCHAR *pszStr, DWORD_PTR *pdw)
{
BOOL bMinus;
DWORD_PTR dw, dw2;
if(*pszStr == L'-')
{
bMinus = TRUE;
pszStr++;
}
else
bMinus = FALSE;
dw = 0;
if(pszStr[0] == L'0' && (pszStr[1] == L'x' || pszStr[1] == L'X'))
{
pszStr += 2;
while(*pszStr != L'\0')
{
if(*pszStr >= L'0' && *pszStr <= L'9')
dw2 = *pszStr - L'0';
else if(*pszStr >= L'a' && *pszStr <= L'f')
dw2 = *pszStr - 'a' + 0x0A;
else if(*pszStr >= L'A' && *pszStr <= L'F')
dw2 = *pszStr - 'A' + 0x0A;
else
break;
dw <<= 0x04;
dw |= dw2;
pszStr++;
}
}
else
{
while(*pszStr != L'\0')
{
if(*pszStr >= L'0' && *pszStr <= L'9')
dw2 = *pszStr - L'0';
else
break;
dw *= 10;
dw += dw2;
pszStr++;
}
}
if(bMinus)
*pdw = (DWORD_PTR)-(LONG_PTR)dw; // :)
else
*pdw = dw;
return pszStr;
}
char *UnicodeToAscii(WCHAR *pszUnicode)
{
char *pszAscii;
int size;
size = WideCharToMultiByte(CP_ACP, 0, pszUnicode, -1, NULL, 0, NULL, NULL);
pszAscii = (char *)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, size);
WideCharToMultiByte(CP_ACP, 0, pszUnicode, -1, pszAscii, size, NULL, NULL);
return pszAscii;
}
__declspec(noreturn) void FatalExitMsgBox(WCHAR *format, ...)
{
WCHAR buffer[1024 + 1];
va_list args;
va_start(args, format);
wvsprintf(buffer, format, args);
MessageBox(NULL, buffer, NULL, MB_ICONHAND);
ExitProcess(0);
va_end(args);
}