Skip to content

Commit e8f7f7e

Browse files
committed
Added string snippet and fixed function names
1 parent 16372af commit e8f7f7e

5 files changed

Lines changed: 126 additions & 7 deletions

File tree

Shellcodev/command.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,12 @@ BOOL shelldev_command_registers(shell_t* sh, std::vector<std::string> parts)
295295
return TRUE;
296296
}
297297

298-
static BOOL shelldev_command_reset(shell_t* sh)
298+
static BOOL shelldev_command_reset(shell_t* sh, std::vector<asm_t>* assemblies)
299299
{
300300
shelldev_print_good("Resetting the environment.");
301301
TerminateProcess(sh->procInfo.hProcess, 0);
302302
DebugActiveProcessStop(sh->procInfo.dwProcessId);
303+
assemblies->clear();
303304
return FALSE;
304305
}
305306

@@ -424,7 +425,7 @@ BOOL shelldev_run_command(shell_t* sh, std::string command, std::vector<asm_t>*
424425
else if (mainCmd == ".kernel32")
425426
return shelldev_command_kernel32(sh, parts);
426427
else if (mainCmd == ".reset")
427-
return shelldev_command_reset(sh);
428+
return shelldev_command_reset(sh, assemblies);
428429
else if (mainCmd == ".shellcode")
429430
return shelldev_command_shellcode(sh, parts);
430431
else if (mainCmd == ".peb")

Shellcodev/eval.cpp

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,105 @@
44
#undef max
55
#include <asmjit/asmjit.h>
66
#include <asmtk/asmtk.h>
7+
#include <regex>
78

8-
static void winrepl_fix_rip(shell_t* sh)
9+
static std::string get_register(std::string instruction)
10+
{
11+
std::string reg;
12+
for (int i = 4; i < instruction.size(); i++)
13+
if (instruction[i] == ',')
14+
break;
15+
else reg += instruction[i];
16+
17+
return reg;
18+
}
19+
20+
static inline unsigned int value(char c)
21+
{
22+
if (c >= '0' && c <= '9') { return c - '0'; }
23+
if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
24+
if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
25+
return -1;
26+
}
27+
28+
std::string str_xor(std::string const& s1, std::string const& s2)
29+
{
30+
static char const alphabet[] = "0123456789abcdef";
31+
32+
std::string result;
33+
result.reserve(s1.length());
34+
35+
for (std::size_t i = 0; i != s1.length(); ++i)
36+
{
37+
unsigned int v = value(s1[i]) ^ value(s2[i]);
38+
39+
result.push_back(alphabet[v]);
40+
}
41+
42+
return result;
43+
}
44+
45+
std::vector<std::string> shelldev_parse_string(std::string reg, std::string value) // Currently only works on x86!
46+
{
47+
std::vector<std::string> stringParts;
48+
for (size_t i = 0; i < value.size(); i += 4)
49+
stringParts.push_back(value.substr(i, 4));
50+
51+
std::vector<std::string> hex;
52+
for (std::string part : stringParts)
53+
{
54+
std::stringstream ss;
55+
for (int i = part.size() - 1; i >= 0; i--)
56+
ss << std::hex << static_cast<int>(part[i]);
57+
58+
hex.push_back(ss.str());
59+
}
60+
61+
for (int i = 0; i < hex.size(); i++)
62+
if (hex[i].size() < 8)
63+
for (int j = 0; j < (8 - hex[i].size()); j++)
64+
hex[i].insert(0, "00");
65+
66+
std::string key = "11111111";
67+
std::vector<_str_parser_t> parsers;
68+
for (int i = 0; i < hex.size(); i++)
69+
{
70+
_str_parser_t parser;
71+
if (hex[i].find("0") != std::string::npos)
72+
{
73+
parser.instruction = str_xor(hex[i], key);
74+
parser.xored = TRUE;
75+
parsers.push_back(parser);
76+
}
77+
else
78+
{
79+
parser.instruction = hex[i];
80+
parser.xored = FALSE;
81+
parsers.push_back(parser);
82+
}
83+
}
84+
85+
std::vector<std::string> instructions;
86+
for (int i = parsers.size() - 1; i >= 0; i--)
87+
{
88+
if (parsers[i].xored)
89+
{
90+
instructions.push_back("mov " + reg + ", 0x" + parsers[i].instruction);
91+
instructions.push_back("xor " + reg + ", 0x" + key);
92+
instructions.push_back("push " + reg);
93+
}
94+
else
95+
{
96+
instructions.push_back("push 0x" + parsers[i].instruction);
97+
}
98+
}
99+
100+
instructions.push_back("mov " + reg + ", esp");
101+
102+
return instructions;
103+
}
104+
105+
static void shelldev_fix_rip(shell_t* sh)
9106
{
10107
// fix RIP because of \xcc
11108
CONTEXT ctx = { 0 };
@@ -99,7 +196,7 @@ void shelldev_debug_shellcode(shell_t* sh)
99196
}
100197
}
101198

102-
winrepl_fix_rip(sh);
199+
shelldev_fix_rip(sh);
103200

104201
CONTEXT ctx = { 0 };
105202
ctx.ContextFlags = CONTEXT_ALL;
@@ -194,17 +291,32 @@ static BOOL shelldev_run_shellcode(shell_t* sh, std::string assembly, std::vecto
194291
size_t addr = sh->curr.Eip;
195292
#endif
196293

294+
for (int i = 0; i < instructions.size(); i++)
295+
{
296+
std::vector<std::string> itms = split(instructions[i], "\"");
297+
for (std::vector<std::string>::iterator it = itms.begin() + 1; it != itms.end(); it += 2)
298+
{
299+
std::string reg = get_register(instructions[i]);
300+
std::vector<std::string> parse = shelldev_parse_string(reg, *it);
301+
302+
instructions.insert(instructions.end(), parse.begin(), parse.end());
303+
instructions.erase(instructions.begin() + i);
304+
}
305+
}
306+
197307
for (std::string& instruction : instructions)
198308
{
199-
if (!shelldev_assemble(instruction.c_str(), data, addr + data.size()))
309+
std::vector<unsigned char> temp;
310+
if (!shelldev_assemble(instruction.c_str(), temp, addr + temp.size()))
200311
return TRUE;
201312

202313
asm_t a;
203314
a.instruction = instruction;
204-
a.bytes = data;
205-
a.size = sizeof(data);
315+
a.bytes = temp;
316+
a.size = sizeof(temp);
206317

207318
assemblies->push_back(a);
319+
data.insert(data.end(), temp.begin(), temp.end());
208320
}
209321

210322
if (!shelldev_write_shellcode(sh, data.data(), data.size()))

Shellcodev/repl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ typedef struct _asm_context_t {
2222
int size;
2323
} asm_t;
2424

25+
typedef struct _parser_context_t {
26+
std::string instruction;
27+
BOOL xored;
28+
} _str_parser_t;
29+
2530
BOOL shelldev_init(shell_t* sh);
2631
BOOL shelldev_loop(shell_t* sh);
2732

@@ -31,6 +36,7 @@ BOOL shelldev_eval(shell_t* sh, std::string command, std::vector<asm_t>* assembl
3136
BOOL shelldev_write_shellcode(shell_t* sh, unsigned char* encode, size_t size);
3237
void shelldev_debug_shellcode(shell_t* sh);
3338

39+
std::vector<std::string> shelldev_parse_string(std::string value);
3440
BOOL shelldev_run_shellcode(shell_t* sh, std::vector<asm_t>* assemblies);
3541
BOOL shelldev_run_command(shell_t* sh, std::string command, std::vector<asm_t>* assemblies);
3642

screenshots/1.gif

161 KB
Loading

screenshots/2.gif

156 KB
Loading

0 commit comments

Comments
 (0)