-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy patheval.cpp
More file actions
518 lines (415 loc) · 11.4 KB
/
eval.cpp
File metadata and controls
518 lines (415 loc) · 11.4 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#undef min
#undef max
#include <asmjit/asmjit.h>
#include <asmtk/asmtk.h>
#include <regex>
#include "repl.h"
static std::string get_register(std::string instruction)
{
std::string reg;
for (int i = 4; i < instruction.size(); i++)
if (instruction[i] == ',')
break;
else reg += instruction[i];
return reg;
}
static inline unsigned int value(char c)
{
if (c >= '0' && c <= '9') { return c - '0'; }
if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
return -1;
}
std::string str_xor(std::string const& s1, std::string const& s2)
{
static char const alphabet[] = "0123456789abcdef";
std::string result;
result.reserve(s1.length());
for (std::size_t i = 0; i != s1.length(); ++i)
{
unsigned int v = value(s1[i]) ^ value(s2[i]);
result.push_back(alphabet[v]);
}
return result;
}
std::vector<std::string> shelldev_parse_string(std::string reg, std::string value) // Currently only works on x86!
{
std::string key = "11111111";
std::vector<std::string> stringParts;
for (size_t i = 0; i < value.size(); i += 4)
stringParts.push_back(value.substr(i, 4));
std::vector<std::string> hex;
for (std::string part : stringParts)
{
std::stringstream ss;
for (int i = part.size() - 1; i >= 0; i--)
ss << std::hex << static_cast<int>(part[i]);
hex.push_back(ss.str());
}
if(xorNulls == TRUE)
for (int i = 0; i < hex.size(); i++)
if (hex[i].size() < 8)
for (int j = 0; j < (8 - hex[i].size()); j++)
hex[i].insert(0, "00");
std::vector<_str_parser_t> parsers;
for (int i = 0; i < hex.size(); i++)
{
_str_parser_t parser;
if (xorNulls == TRUE && hex[i].find("0") != std::string::npos)
{
parser.instruction = str_xor(hex[i], key);
parser.xored = TRUE;
parsers.push_back(parser);
}
else
{
parser.instruction = hex[i];
parser.xored = FALSE;
parsers.push_back(parser);
}
}
std::vector<std::string> instructions;
for (int i = parsers.size() - 1; i >= 0; i--)
{
if (parsers[i].xored)
{
instructions.push_back("mov " + reg + ", 0x" + parsers[i].instruction);
instructions.push_back("xor " + reg + ", 0x" + key);
instructions.push_back("push " + reg);
}
else
{
instructions.push_back("push 0x" + parsers[i].instruction);
}
}
#ifdef _M_X64
instructions.push_back("mov " + reg + ", rsp");
#elif defined(_M_IX86)
instructions.push_back("mov " + reg + ", esp");
#endif
return instructions;
}
static void shelldev_fix_rip(shell_t* sh)
{
// fix RIP because of \xcc
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_ALL;
GetThreadContext(sh->procInfo.hThread, &ctx);
#ifdef _M_X64
ctx.Rip = ctx.Rip - 1;
#elif defined(_M_IX86)
ctx.Eip = ctx.Eip - 1;
#endif
SetThreadContext(sh->procInfo.hThread, &ctx);
}
BOOL shelldev_write_shellcode(shell_t* sh, unsigned char* encode, size_t size)
{
DWORD dwOldProtect = 0;
SIZE_T nBytes;
CONTEXT ctx = { 0 };
shelldev_print_assembly(encode, size);
ctx.ContextFlags = CONTEXT_ALL;
if (!GetThreadContext(sh->procInfo.hThread, &ctx))
return FALSE;
#ifdef _M_X64
LPVOID addr = (LPVOID)ctx.Rip;
#elif defined(_M_IX86)
LPVOID addr = (LPVOID)ctx.Eip;
#endif
if (!VirtualProtectEx(sh->procInfo.hProcess, (LPVOID)addr, size + 1, PAGE_READWRITE, &dwOldProtect))
return FALSE;
if (!WriteProcessMemory(sh->procInfo.hProcess, (LPVOID)addr, (LPCVOID)encode, size, &nBytes))
return FALSE;
if (!WriteProcessMemory(sh->procInfo.hProcess, (LPVOID)((LPBYTE)addr + size), (LPCVOID)"\xcc", 1, &nBytes))
return FALSE;
if (!VirtualProtectEx(sh->procInfo.hProcess, (LPVOID)addr, size + 1, dwOldProtect, &dwOldProtect))
return FALSE;
FlushInstructionCache(sh->procInfo.hProcess, (LPCVOID)addr, size + 1);
return TRUE;
}
void shelldev_debug_shellcode(shell_t* sh)
{
BOOL go = TRUE;
while (go)
{
ContinueDebugEvent(sh->procInfo.dwProcessId, sh->procInfo.dwThreadId, DBG_CONTINUE);
DEBUG_EVENT dbg = { 0 };
if (!WaitForDebugEvent(&dbg, INFINITE))
break;
if (dbg.dwThreadId != sh->procInfo.dwThreadId)
{
ContinueDebugEvent(dbg.dwProcessId, dbg.dwThreadId, DBG_CONTINUE);
continue;
}
if (dbg.dwDebugEventCode == EXCEPTION_DEBUG_EVENT && dbg.dwThreadId == sh->procInfo.dwThreadId)
{
go = FALSE;
switch (dbg.u.Exception.ExceptionRecord.ExceptionCode)
{
case EXCEPTION_ACCESS_VIOLATION:
break;
case EXCEPTION_PRIV_INSTRUCTION:
break;
case EXCEPTION_BREAKPOINT:
break;
default:
break;
}
}
if (dbg.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT)
{
if (dbg.u.LoadDll.hFile)
CloseHandle(dbg.u.LoadDll.hFile);
}
}
shelldev_fix_rip(sh);
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_ALL;
GetThreadContext(sh->procInfo.hThread, &ctx);
memcpy(&sh->prev, &sh->curr, sizeof(CONTEXT));
memcpy(&sh->curr, &ctx, sizeof(CONTEXT));
}
static BOOL shelldev_assemble(const char* instruction, std::vector<unsigned char>& data, size_t address)
{
using namespace asmjit;
using namespace asmtk;
// Setup CodeInfo
JitRuntime jr;
// Setup CodeHolder
CodeHolder code;
Error err = code.init(jr.environment());
if (err != kErrorOk)
{
printf("ERROR: %s\n", DebugUtils::errorAsString(err));
return FALSE;
}
// Attach an assembler to the CodeHolder.
x86::Assembler a(&code);
// Create AsmParser that will emit to X86Assembler.
AsmParser p(&a);
// Parse some assembly.
err = p.parse(instruction);
// Error handling
if (err != kErrorOk)
{
printf("ERROR: %s (instruction: \"%s\")\n", DebugUtils::errorAsString(err), instruction);
return FALSE;
}
// If we are done, you must detach the Assembler from CodeHolder or sync
// it, so its internal state and position is synced with CodeHolder.
code.detach(&a);
// Now you can print the code, which is stored in the first section (.text).
CodeBuffer& buffer = code.sectionById(0)->buffer();
for (size_t i = 0; i < buffer.size(); i++)
data.push_back(buffer.data()[i]);
return TRUE;
}
static BOOL shelldev_jump(asmjit::Label loop, asmjit::x86::Assembler* a, std::string instruction)
{
// Jump instruction checker
std::string jump;
for (int i = 0; i < instruction.size(); i++)
if (instruction[i] != ' ')
jump += instruction[i];
else break;
if (jump == "jmp")
a->jmp(loop);
else if (jump == "je")
a->je(loop);
else if (jump == "jz")
a->jz(loop);
else if (jump == "jne")
a->jne(loop);
else if (jump == "jnz")
a->jnz(loop);
else if (jump == "jg")
a->jg(loop);
else if (jump == "jnle")
a->jnle(loop);
else if (jump == "jge")
a->jge(loop);
else if (jump == "jnl")
a->jnl(loop);
else if (jump == "jl")
a->jl(loop);
else if (jump == "jnge")
a->jnge(loop);
else if (jump == "jle")
a->jle(loop);
else if (jump == "jng")
a->jng(loop);
else if (jump == "ja")
a->ja(loop);
else if (jump == "jnbe")
a->jnbe(loop);
else if (jump == "jae")
a->jae(loop);
else if (jump == "jnb")
a->jnb(loop);
// Add more options
else
return FALSE;
return TRUE;
}
// If jump instruction detected, reassemble everything
BOOL shelldev_assemble_loop(std::vector<asm_t>* assemblies, std::vector<unsigned char>& data, size_t address)
{
using namespace asmjit;
using namespace asmtk;
struct Loop
{
std::string name;
Label label;
};
// Setup CodeInfo
JitRuntime jr;
// Setup CodeHolder
CodeHolder code;
Error err = code.init(jr.environment());
if (err != kErrorOk)
{
printf("ERROR: %s\n", DebugUtils::errorAsString(err));
return FALSE;
}
// Attach an assembler to the CodeHolder.
x86::Assembler a(&code);
std::vector<Loop> loops;
AsmParser p(&a);
for (int i = 0; i < assemblies->size(); i++)
{
std::string instruction = assemblies->at(i).instruction;
if (instruction[instruction.size() - 1] == ':')
{
Loop loop;
loop.name = instruction.erase(instruction.size() - 1, 1); // Remove : from label
loop.label = a.newLabel();
a.bind(loop.label);
loops.push_back(loop);
}
else if (instruction[0] == 'j')
{
std::string labelName;
for (int i = instruction.size() - 1; i >= 0; i--)
{
if (instruction[i] != ' ')
labelName += instruction[i];
else break;
}
std::reverse(labelName.begin(), labelName.end());
Label label;
for (int i = 0; i < loops.size(); i++)
if (loops.at(i).name == labelName)
label = loops.at(i).label;
if (!shelldev_jump(label, &a, instruction))
return FALSE;
}
else
{
err = p.parse(instruction.c_str());
}
}
code.detach(&a);
// Now you can print the code, which is stored in the first section (.text).
CodeBuffer& buffer = code.sectionById(0)->buffer();
for (size_t i = 0; i < buffer.size(); i++)
data.push_back(buffer.data()[i]);
return TRUE;
}
BOOL shelldev_run_shellcode(shell_t* sh, std::vector<asm_t>* assemblies)
{
#ifdef _M_X64
size_t addr = sh->curr.Rip;
#elif defined(_M_IX86)
size_t addr = sh->curr.Eip;
#endif
for (int i = 0; i < assemblies->capacity(); i++)
{
if (assemblies->at(i).size == 0)
i++;
std::vector<unsigned char> data;
if (!shelldev_assemble(assemblies->at(i).instruction.c_str(), data, addr + data.size()))
return TRUE;
assemblies->at(i).bytes = data;
assemblies->at(i).size = sizeof(data);
if (!shelldev_write_shellcode(sh, data.data(), data.size()))
return FALSE;
shelldev_debug_shellcode(sh);
}
shelldev_print_registers(sh);
return TRUE;
}
BOOL shelldev_run_shellcode(shell_t* sh, std::string assembly, std::vector<asm_t>* assemblies)
{
std::vector<std::string> instructions = split(assembly, ";");
std::vector<unsigned char> data;
#ifdef _M_X64
size_t addr = sh->curr.Rip;
#elif defined(_M_IX86)
size_t addr = sh->curr.Eip;
#endif
for (int i = 0; i < instructions.size(); i++)
{
std::vector<std::string> itms = split(instructions[i], "\"");
for (std::vector<std::string>::iterator it = itms.begin() + 1; it != itms.end(); it += 2)
{
std::string reg = get_register(instructions[i]);
std::vector<std::string> parse = shelldev_parse_string(reg, *it);
instructions.insert(instructions.end(), parse.begin(), parse.end());
instructions.erase(instructions.begin() + i);
}
}
for (std::string& instruction : instructions)
{
std::vector<unsigned char> temp;
if(instruction[instruction.size() - 1] != ':')
if (!shelldev_assemble(instruction.c_str(), temp, addr + temp.size()))
return FALSE;
asm_t a;
a.instruction = instruction;
a.bytes = temp;
a.size = sizeof(temp);
assemblies->push_back(a);
data.insert(data.end(), temp.begin(), temp.end());
}
if (!shelldev_write_shellcode(sh, data.data(), data.size()))
return FALSE;
shelldev_debug_shellcode(sh);
shelldev_print_registers(sh);
return TRUE;
}
BOOL shelldev_loop_eval(std::string jump, shell_t* sh, std::vector<asm_t>* assemblies)
{
#ifdef _M_X64
size_t addr = sh->curr.Rip;
#elif defined(_M_IX86)
size_t addr = sh->curr.Eip;
#endif
std::vector<unsigned char> data;
asm_t asmt;
asmt.instruction = jump;
assemblies->push_back(asmt);
if (!shelldev_assemble_loop(assemblies, data, addr + data.size()))
return FALSE;
// assemblies->at(assemblies->size() - 1).bytes;
if (!shelldev_write_shellcode(sh, data.data(), data.size()))
return FALSE;
shelldev_debug_shellcode(sh);
shelldev_print_registers(sh);
return TRUE;
}
BOOL shelldev_eval(shell_t* sh, std::string command, std::vector<asm_t>* assemblies)
{
try
{
if (command.at(0) == '.')
return shelldev_run_command(sh, command, assemblies);
else if (command.at(0) == 'j')
return shelldev_loop_eval(command, sh, assemblies);
return shelldev_run_shellcode(sh, command, assemblies);
}
catch (...)
{
shelldev_print_errors("An unhandled C++ exception occurred.");
}
return TRUE;
}