|
4 | 4 | #undef max |
5 | 5 | #include <asmjit/asmjit.h> |
6 | 6 | #include <asmtk/asmtk.h> |
| 7 | +#include <regex> |
7 | 8 |
|
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) |
9 | 106 | { |
10 | 107 | // fix RIP because of \xcc |
11 | 108 | CONTEXT ctx = { 0 }; |
@@ -99,7 +196,7 @@ void shelldev_debug_shellcode(shell_t* sh) |
99 | 196 | } |
100 | 197 | } |
101 | 198 |
|
102 | | - winrepl_fix_rip(sh); |
| 199 | + shelldev_fix_rip(sh); |
103 | 200 |
|
104 | 201 | CONTEXT ctx = { 0 }; |
105 | 202 | ctx.ContextFlags = CONTEXT_ALL; |
@@ -194,17 +291,32 @@ static BOOL shelldev_run_shellcode(shell_t* sh, std::string assembly, std::vecto |
194 | 291 | size_t addr = sh->curr.Eip; |
195 | 292 | #endif |
196 | 293 |
|
| 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 | + |
197 | 307 | for (std::string& instruction : instructions) |
198 | 308 | { |
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())) |
200 | 311 | return TRUE; |
201 | 312 |
|
202 | 313 | asm_t a; |
203 | 314 | a.instruction = instruction; |
204 | | - a.bytes = data; |
205 | | - a.size = sizeof(data); |
| 315 | + a.bytes = temp; |
| 316 | + a.size = sizeof(temp); |
206 | 317 |
|
207 | 318 | assemblies->push_back(a); |
| 319 | + data.insert(data.end(), temp.begin(), temp.end()); |
208 | 320 | } |
209 | 321 |
|
210 | 322 | if (!shelldev_write_shellcode(sh, data.data(), data.size())) |
|
0 commit comments