| 5 |
| #include <stdlib.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <unistd.h> |
| 8 | #include <dlfcn.h> |
| 9 | #include <sys/system_properties.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <android/log.h> |
| 12 | #include <errno.h> |
| 13 |
| 14 | #include "plt.h" |
| 15 | #include "inline.h" |
| 16 | #include "apk-sign-v2.h" |
| 17 | #include "am-proxy.h" |
| 18 | #include "common.h" |
| 19 | #include "openat.h" |
| 20 | #include "path.h" |
| 21 |
| 22 | #ifdef CHECK_MOUNT |
| 23 | #include "mount.h" |
| 24 | #endif |
| 25 |
| 26 | static int genuine = CHECK_TRUE; |
| 27 |
| 28 | static int sdk; |
| 29 |
| 30 | static int uid; |
| 31 |
| 32 | static inline void fill_maps(char v[]) { |
| 33 | static unsigned int m = 0; |
| 34 | if (m == 0) { |
| 35 | m = 13; |
| 36 | } else if (m == 17) { |
| 37 | m = 19; |
| 38 | } |
| 39 | v[0x0] = '-'; |
| 40 | v[0x1] = 's'; |
| 41 | v[0x2] = 'v'; |
| 42 | v[0x3] = 'j'; |
| 43 | v[0x4] = 'e'; |
| 44 | v[0x5] = '('; |
| 45 | v[0x6] = '{'; |
| 46 | v[0x7] = 'l'; |
| 47 | v[0x8] = 'f'; |
| 48 | v[0x9] = 'm'; |
| 49 | v[0xa] = '#'; |
| 50 | v[0xb] = 'm'; |
| 51 | v[0xc] = '`'; |
| 52 | v[0xd] = 'r'; |
| 53 | v[0xe] = 'p'; |
| 54 | for (unsigned int i = 0; i < 0xf; ++i) { |
| 55 | v[i] ^= ((i + 0xf) % m); |
| 56 | } |
| 57 | v[0xf] = '\0'; |
| 58 | } |
| 59 |
| 60 | #ifndef NO_CHECK_MAPS |
| 61 | static inline void fill_cannot_open_proc_self_maps(char v[]) { |
| 62 | // cannot open /proc/self/maps |
| 63 | static unsigned int m = 0; |
| 64 |
| 65 | if (m == 0) { |
| 66 | m = 23; |
| 67 | } else if (m == 29) { |
| 68 | m = 31; |
| 69 | } |
| 70 |
| 71 | v[0x0] = 'g'; |
| 72 | v[0x1] = 'd'; |
| 73 | v[0x2] = 'h'; |
| 74 | v[0x3] = 'i'; |
| 75 | v[0x4] = 'g'; |
| 76 | v[0x5] = '}'; |
| 77 | v[0x6] = '*'; |
| 78 | v[0x7] = 'd'; |
| 79 | v[0x8] = '|'; |
| 80 | v[0x9] = 'h'; |
| 81 | v[0xa] = '`'; |
| 82 | v[0xb] = '/'; |
| 83 | v[0xc] = '?'; |
| 84 | v[0xd] = 'a'; |
| 85 | v[0xe] = '`'; |
| 86 | v[0xf] = '|'; |
| 87 | v[0x10] = 'w'; |
| 88 | v[0x11] = ':'; |
| 89 | v[0x12] = 'e'; |
| 90 | v[0x13] = 'e'; |
| 91 | v[0x14] = 'm'; |
| 92 | v[0x15] = 'd'; |
| 93 | v[0x16] = ','; |
| 94 | v[0x17] = 'i'; |
| 95 | v[0x18] = 'd'; |
| 96 | v[0x19] = 'v'; |
| 97 | v[0x1a] = 't'; |
| 98 | for (unsigned int i = 0; i < 0x1b; ++i) { |
| 99 | v[i] ^= ((i + 0x1b) % m); |
| 100 | } |
| 101 | v[0x1b] = '\0'; |
| 102 | } |
| 103 |
| 104 | static inline void fill_r(char v[]) { |
| 105 | static unsigned int m = 0; |
| 106 |
| 107 | if (m == 0) { |
| 108 | m = 2; |
| 109 | } else if (m == 3) { |
| 110 | m = 5; |
| 111 | } |
| 112 |
| 113 | v[0x0] = 's'; |
| 114 | for (unsigned int i = 0; i < 0x1; ++i) { |
| 115 | v[i] ^= ((i + 0x1) % m); |
| 116 | } |
| 117 | v[0x1] = '\0'; |
| 118 | } |
| 119 |
| 120 | static inline void rstrip(char *line) { |
| 121 | char *path = line; |
| 122 | if (line != NULL) { |
| 123 | while (*path && *path != '\r' && *path != '\n') { |
| 124 | ++path; |
| 125 | } |
| 126 | if (*path) { |
| 127 | *path = '\0'; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |
| 132 | static inline bool isapk(const char *str) { |
| 133 | const char *dot = strrchr(str, '.'); |
| 134 | return dot != NULL |
| 135 | && *++dot == 'a' |
| 136 | && *++dot == 'p' |
| 137 | && *++dot == 'k' |
| 138 | && (*++dot == '\0' || *dot == '\r' || *dot == '\n'); |
| 139 | } |
| 140 |
| 141 | static inline bool isdex(const char *str) { |
| 142 | const char *dot = strrchr(str, '.'); |
| 143 | return dot != NULL |
| 144 | && *++dot == 'd' |
| 145 | && *++dot == 'e' |
| 146 | && *++dot == 'x' |
| 147 | && (*++dot == '\0' || *dot == '\r' || *dot == '\n'); |
| 148 | } |
| 149 |
| 150 | static inline bool isodex(const char *str) { |
| 151 | const char *dot = strrchr(str, '.'); |
| 152 | return dot != NULL |
| 153 | && *++dot == 'o' |
| 154 | && *++dot == 'd' |
| 155 | && *++dot == 'e' |
| 156 | && *++dot == 'x' |
| 157 | && (*++dot == '\0' || *dot == '\r' || *dot == '\n'); |
| 158 | } |
| 159 |
| 160 | static inline bool isso(const char *str) { |
| 161 | const char *dot = strrchr(str, '.'); |
| 162 | return dot != NULL |
| 163 | && *++dot == 's' |
| 164 | && *++dot == 'o' |
| 165 | && (*++dot == '\0' || *dot == '\r' || *dot == '\n'); |
| 166 | } |
| 167 |
| 168 | #ifdef ANTI_ODEX |
| 169 | static inline size_t fill_dex2oat_cmdline(char v[]) { |
| 170 | // dex2oat-cmdline |
| 171 | static unsigned int m = 0; |
| 172 |
| 173 | if (m == 0) { |
| 174 | m = 13; |
| 175 | } else if (m == 17) { |
| 176 | m = 19; |
| 177 | } |
| 178 |
| 179 | v[0x0] = 'f'; |
| 180 | v[0x1] = 'f'; |
| 181 | v[0x2] = '|'; |
| 182 | v[0x3] = '7'; |
| 183 | v[0x4] = 'i'; |
| 184 | v[0x5] = 'f'; |
| 185 | v[0x6] = '|'; |
| 186 | v[0x7] = '$'; |
| 187 | v[0x8] = 'i'; |
| 188 | v[0x9] = 'f'; |
| 189 | v[0xa] = 'h'; |
| 190 | v[0xb] = 'l'; |
| 191 | v[0xc] = 'h'; |
| 192 | v[0xd] = 'l'; |
| 193 | v[0xe] = 'f'; |
| 194 | for (unsigned int i = 0; i < 0xf; ++i) { |
| 195 | v[i] ^= ((i + 0xf) % m); |
| 196 | } |
| 197 | v[0xf] = '\0'; |
| 198 | return 0xf; |
| 199 | } |
| 200 |
| 201 | static inline size_t fill_dex_file(char v[]) { |
| 202 | // --dex-file |
| 203 | static unsigned int m = 0; |
| 204 |
| 205 | if (m == 0) { |
| 206 | m = 7; |
| 207 | } else if (m == 11) { |
| 208 | m = 13; |
| 209 | } |
| 210 |
| 211 | v[0x0] = '.'; |
| 212 | v[0x1] = ')'; |
| 213 | v[0x2] = 'a'; |
| 214 | v[0x3] = 'c'; |
| 215 | v[0x4] = 'x'; |
| 216 | v[0x5] = ','; |
| 217 | v[0x6] = 'd'; |
| 218 | v[0x7] = 'j'; |
| 219 | v[0x8] = 'h'; |
| 220 | v[0x9] = '`'; |
| 221 | for (unsigned int i = 0; i < 0xa; ++i) { |
| 222 | v[i] ^= ((i + 0xa) % m); |
| 223 | } |
| 224 | v[0xa] = '\0'; |
| 225 | return 0xa; |
| 226 | } |
| 227 |
| 228 | static inline int checkOdex(const char *path) { |
| 229 | size_t len; |
| 230 | char *cmdline; |
| 231 | char buffer[0x400], find[64]; |
| 232 |
| 233 | int ret = 0; |
| 234 | int fd = open(path, (unsigned) O_RDONLY | (unsigned) O_CLOEXEC); |
| 235 | if (fd == -1) { |
| 236 | return 1; |
| 237 | } |
| 238 |
| 239 | lseek(fd, 0x1000, SEEK_SET); |
| 240 | read(fd, buffer, 0x400); |
| 241 |
| 242 | cmdline = buffer; |
| 243 | len = fill_dex2oat_cmdline(find) + 1; |
| 244 | for (int i = 0; i < 0x200; ++i, ++cmdline) { |
| 245 | if (memcmp(cmdline, find, len) == 0) { |
| 246 | cmdline += len; |
| 247 | fill_dex_file(find); |
| 248 | if ((ret = (strstr(cmdline, find) != NULL))) { |
| 249 | fill_dex2oat_cmdline(find); |
| 250 | LOGE(find); |
| 251 | LOGE(cmdline); |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 |
| 257 | close(fd); |
| 258 |
| 259 | return ret; |
| 260 | } |
| 261 | #endif |
| 262 |
| 263 | static inline bool isSameFile(const char *path1, const char *path2) { |
| 264 | struct stat stat1, stat2; |
| 265 | if (path1 == NULL || path2 == NULL) { |
| 266 | return false; |
| 267 | } |
| 268 | if (lstat(path1, &stat1)) { |
| 269 | return false; |
| 270 | } |
| 271 | if (lstat(path2, &stat2)) { |
| 272 | return false; |
| 273 | } |
| 274 | return stat1.st_dev == stat2.st_dev && stat1.st_ino == stat2.st_ino; |
| 275 | } |
| 276 |
| 277 | static inline void fill_ba88(char v[]) { |
| 278 | // ba887869b4a4a6d1e915d383fad97c49 |
| 279 | static unsigned int m = 0; |
| 280 | if (m == 0) { |
| 281 | m = 13; |
| 282 | } else if (m == 17) { |
| 283 | m = 19; |
| 284 | } |
| 285 | v[0x0] = 'f'; |
| 286 | v[0x1] = 'o'; |
| 287 | v[0x2] = '['; |
| 288 | v[0x3] = 'l'; |
| 289 | v[0x4] = 'r'; |
| 290 | v[0x5] = 'b'; |
| 291 | v[0x6] = 'z'; |
| 292 | v[0x7] = 'h'; |
| 293 | v[0x8] = '~'; |
| 294 | v[0x9] = 'n'; |
| 295 | v[0xa] = 'S'; |
| 296 | v[0xb] = 'p'; |
| 297 | v[0xc] = 'i'; |
| 298 | v[0xd] = 'f'; |
| 299 | v[0xe] = 'q'; |
| 300 | for (unsigned int i = 0; i < 0xf; ++i) { |
| 301 | v[i] ^= ((i + 0xf) % m); |
| 302 | } |
| 303 | v[0xf] = '\0'; |
| 304 | } |
| 305 |
| 306 | static inline bool isSame(const char *path1, const char *path2) { |
| 307 | if (path1[0] == '/') { |
| 308 | return strcmp(path1, path2) == 0; |
| 309 | } else { |
| 310 | return strcmp(path1, strrchr(path2, '/') + 1) == 0; |
| 311 | } |
| 312 | } |
| 313 |
| 314 | static inline void fill_cannot_find_s(char v[]) { |
| 315 | // cannot find %s |
| 316 | static unsigned int m = 0; |
| 317 |
| 318 | if (m == 0) { |
| 319 | m = 13; |
| 320 | } else if (m == 17) { |
| 321 | m = 19; |
| 322 | } |
| 323 |
| 324 | v[0x0] = 'b'; |
| 325 | v[0x1] = 'c'; |
| 326 | v[0x2] = 'm'; |
| 327 | v[0x3] = 'j'; |
| 328 | v[0x4] = 'j'; |
| 329 | v[0x5] = 'r'; |
| 330 | v[0x6] = '\''; |
| 331 | v[0x7] = 'n'; |
| 332 | v[0x8] = '`'; |
| 333 | v[0x9] = 'd'; |
| 334 | v[0xa] = 'o'; |
| 335 | v[0xb] = ','; |
| 336 | v[0xc] = '%'; |
| 337 | v[0xd] = 'r'; |
| 338 | for (unsigned int i = 0; i < 0xe; ++i) { |
| 339 | v[i] ^= ((i + 0xe) % m); |
| 340 | } |
| 341 | v[0xe] = '\0'; |
| 342 | } |
| 343 |
| 344 | static inline void fill_invalid_signature_path_s(char v[]) { |
| 345 | // invalid signature, path: %s |
| 346 | static unsigned int m = 0; |
| 347 |
| 348 | if (m == 0) { |
| 349 | m = 23; |
| 350 | } else if (m == 29) { |
| 351 | m = 31; |
| 352 | } |
| 353 |
| 354 | v[0x0] = 'm'; |
| 355 | v[0x1] = 'k'; |
| 356 | v[0x2] = 'p'; |
| 357 | v[0x3] = 'f'; |
| 358 | v[0x4] = 'd'; |
| 359 | v[0x5] = '`'; |
| 360 | v[0x6] = 'n'; |
| 361 | v[0x7] = '+'; |
| 362 | v[0x8] = '\x7f'; |
| 363 | v[0x9] = 'd'; |
| 364 | v[0xa] = 'i'; |
| 365 | v[0xb] = 'a'; |
| 366 | v[0xc] = 'q'; |
| 367 | v[0xd] = 'e'; |
| 368 | v[0xe] = 'g'; |
| 369 | v[0xf] = 'a'; |
| 370 | v[0x10] = 'q'; |
| 371 | v[0x11] = '9'; |
| 372 | v[0x12] = '6'; |
| 373 | v[0x13] = 'p'; |
| 374 | v[0x14] = '`'; |
| 375 | v[0x15] = 'v'; |
| 376 | v[0x16] = 'k'; |
| 377 | v[0x17] = '>'; |
| 378 | v[0x18] = '%'; |
| 379 | v[0x19] = '#'; |
| 380 | v[0x1a] = 't'; |
| 381 | for (unsigned int i = 0; i < 0x1b; ++i) { |
| 382 | v[i] ^= ((i + 0x1b) % m); |
| 383 | } |
| 384 | v[0x1b] = '\0'; |
| 385 | } |
| 386 |
| 387 | enum { |
| 388 | TYPE_NON, |
| 389 | TYPE_APK, |
| 390 | TYPE_DEX, |
| 391 | TYPE_SO, |
| 392 | }; |
| 393 |
| 394 | static inline int checkMaps(const char *packageName) { |
| 395 | FILE *fp = NULL; |
| 396 | char line[PATH_MAX]; |
| 397 | char maps[0x10]; |
| 398 | int check = genuine; |
| 399 | bool loaded = false; |
| 400 |
| 401 | fill_maps(maps); |
| 402 | int fd = (int) openAt(AT_FDCWD, maps, O_RDONLY); |
| 403 | #ifdef DEBUG_OPENAT |
| 404 | LOGI("openat %s returns %d", maps, fd); |
| 405 | #endif |
| 406 | if (fd < 0) { |
| 407 | fill_cannot_open_proc_self_maps(line); |
| 408 | LOGE(line); |
| 409 | return CHECK_ERROR; |
| 410 | } |
| 411 |
| 412 | Symbol symbol; |
| 413 | fill_ba88(line); |
| 414 | memset(&symbol, 0, sizeof(Symbol)); |
| 415 | symbol.check = (PLT_CHECK_PLT_APP | PLT_CHECK_NAME); |
| 416 | symbol.symbol_name = (char *) line; |
| 417 | if (dl_iterate_phdr_symbol(&symbol)) { |
| 418 | LOGE(line); |
| 419 | check = CHECK_ERROR; |
| 420 | goto clean2; |
| 421 | } |
| 422 |
| 423 | #if __ANDROID_API__ >= 21 || !defined(__arm__) |
| 424 | if (symbol.size == 0) { |
| 425 | LOGE(line); |
| 426 | check = CHECK_ERROR; |
| 427 | goto clean2; |
| 428 | } |
| 429 | #endif |
| 430 |
| 431 | char mode[0x2]; |
| 432 | fill_r(mode); |
| 433 |
| 434 | fp = fdopen(fd, mode); |
| 435 | if (fp == NULL) { |
| 436 | fill_cannot_open_proc_self_maps(line); |
| 437 | LOGE(line); |
| 438 | check = CHECK_ERROR; |
| 439 | goto clean3; |
| 440 | } |
| 441 |
| 442 | while (fgets(line, PATH_MAX - 1, fp) != NULL) { |
| 443 | int type; |
| 444 | char *path = line; |
| 445 | if (strchr(line, '/') == NULL) { |
| 446 | continue; |
| 447 | } |
| 448 | while (*path != '/') { |
| 449 | ++path; |
| 450 | } |
| 451 | rstrip(path); |
| 452 | if (isapk(path)) { |
| 453 | type = TYPE_APK; |
| 454 | } else if (isodex(path) || isdex(path)) { |
| 455 | type = TYPE_DEX; |
| 456 | } else if (isso(path)) { |
| 457 | type = TYPE_SO; |
| 458 | if (symbol.size > 0) { |
| 459 | bool found = false; |
| 460 | for (int i = 0; i < symbol.size; ++i) { |
| 461 | if (symbol.names[i] != NULL) { |
| 462 | if (isSame(symbol.names[i], path)) { |
| 463 | free(symbol.names[i]); |
| 464 | symbol.names[i] = NULL; |
| 465 | } else { |
| 466 | found = true; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | if (!found) { |
| 471 | symbol.size = 0; |
| 472 | } |
| 473 | } |
| 474 | } else { |
| 475 | type = TYPE_NON; |
| 476 | } |
| 477 | #ifdef DEBUG_MAPS |
| 478 | LOGI("type: %d, line: %s", type, line); |
| 479 | #endif |
| 480 | if (strstr(path, packageName) != NULL) { |
| 481 | if (type == TYPE_APK) { |
| 482 | #ifdef DEBUG |
| 483 | LOGI("check %s", path); |
| 484 | #endif |
| 485 | if (isDataApp(path) || isExternalSdApp(path)) { |
| 486 | loaded = true; |
| 487 | int sign = checkSignature(path); |
| 488 | if (sign) { |
| 489 | fill_invalid_signature_path_s(line); |
| 490 | LOGE(line, path); |
| 491 | #ifndef DEBUG_FAKE |
| 492 | check = sign < 0 ? CHECK_NOAPK : CHECK_FAKE; |
| 493 | #endif |
| 494 | } |
| 495 | } |
| 496 | } else if (type == TYPE_DEX) { |
| 497 | #ifdef ANTI_ODEX |
| 498 | #ifdef DEBUG |
| 499 | LOGI("check %s", path); |
| 500 | #endif |
| 501 | if (checkOdex(path)) { |
| 502 | LOGE(path); |
| 503 | check = CHECK_ODEX; |
| 504 | } |
| 505 | #endif |
| 506 | } |
| 507 | } else if (isDataApp(path)) { |
| 508 | if (type == TYPE_DEX) { |
| 509 | //LOGE(path); |
| 510 | //check = CHECK_DEX; |
| 511 | } else if (type == TYPE_APK) { |
| 512 | #ifdef ANTI_OVERLAY |
| 513 | LOGE(path); |
| 514 | check = CHECK_OVERLAY; |
| 515 | #endif |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 |
| 520 | fclose(fp); |
| 521 |
| 522 | clean3: |
| 523 | close(fd); |
| 524 |
| 525 | clean2: |
| 526 | fill_cannot_find_s(line); |
| 527 | if (!loaded) { |
| 528 | LOGE(line, ""); |
| 529 | check = CHECK_ERROR; |
| 530 | } |
| 531 | for (int i = 0; i < symbol.size; ++i) { |
| 532 | if (symbol.names[i] != NULL) { |
| 533 | if (strchr(symbol.names[i], '!') == NULL) { |
| 534 | LOGE(line, symbol.names[i]); |
| 535 | check = CHECK_ERROR; |
| 536 | } |
| 537 | free(symbol.names[i]); |
| 538 | symbol.names[i] = NULL; |
| 539 | } |
| 540 | } |
| 541 | if (symbol.names != NULL) { |
| 542 | free(symbol.names); |
| 543 | symbol.names = NULL; |
| 544 | } |
| 545 |
| 546 | return check; |
| 547 | } |
| 548 | #endif |
| 549 |
| 550 | #ifdef CHECK_HOOK |
| 551 | static inline void fill_jniRegisterNativeMethods(char v[]) { |
| 552 | // jniRegisterNativeMethods |
| 553 | static unsigned int m = 0; |
| 554 |
| 555 | if (m == 0) { |
| 556 | m = 23; |
| 557 | } else if (m == 29) { |
| 558 | m = 31; |
| 559 | } |
| 560 |
| 561 | v[0x0] = 'k'; |
| 562 | v[0x1] = 'l'; |
| 563 | v[0x2] = 'j'; |
| 564 | v[0x3] = 'V'; |
| 565 | v[0x4] = '`'; |
| 566 | v[0x5] = 'a'; |
| 567 | v[0x6] = 'n'; |
| 568 | v[0x7] = '{'; |
| 569 | v[0x8] = '}'; |
| 570 | v[0x9] = 'o'; |
| 571 | v[0xa] = 'y'; |
| 572 | v[0xb] = 'B'; |
| 573 | v[0xc] = 'l'; |
| 574 | v[0xd] = 'z'; |
| 575 | v[0xe] = 'f'; |
| 576 | v[0xf] = 'f'; |
| 577 | v[0x10] = 't'; |
| 578 | v[0x11] = '_'; |
| 579 | v[0x12] = 'v'; |
| 580 | v[0x13] = '`'; |
| 581 | v[0x14] = '}'; |
| 582 | v[0x15] = 'y'; |
| 583 | v[0x16] = 'd'; |
| 584 | v[0x17] = 'r'; |
| 585 | for (unsigned int i = 0; i < 0x18; ++i) { |
| 586 | v[i] ^= ((i + 0x18) % m); |
| 587 | } |
| 588 | v[0x18] = '\0'; |
| 589 | } |
| 590 |
| 591 | static inline void fill___openat(char v[]) { |
| 592 | // __openat |
| 593 | static unsigned int m = 0; |
| 594 |
| 595 | if (m == 0) { |
| 596 | m = 7; |
| 597 | } else if (m == 11) { |
| 598 | m = 13; |
| 599 | } |
| 600 |
| 601 | v[0x0] = '^'; |
| 602 | v[0x1] = ']'; |
| 603 | v[0x2] = 'l'; |
| 604 | v[0x3] = 't'; |
| 605 | v[0x4] = '`'; |
| 606 | v[0x5] = 'h'; |
| 607 | v[0x6] = 'a'; |
| 608 | v[0x7] = 'u'; |
| 609 | for (unsigned int i = 0; i < 0x8; ++i) { |
| 610 | v[i] ^= ((i + 0x8) % m); |
| 611 | } |
| 612 | v[0x8] = '\0'; |
| 613 | } |
| 614 |
| 615 | static inline void fill_openat(char v[]) { |
| 616 | // openat |
| 617 | static unsigned int m = 0; |
| 618 |
| 619 | if (m == 0) { |
| 620 | m = 5; |
| 621 | } else if (m == 7) { |
| 622 | m = 11; |
| 623 | } |
| 624 |
| 625 | v[0x0] = 'n'; |
| 626 | v[0x1] = 'r'; |
| 627 | v[0x2] = 'f'; |
| 628 | v[0x3] = 'j'; |
| 629 | v[0x4] = 'a'; |
| 630 | v[0x5] = 'u'; |
| 631 | for (unsigned int i = 0; i < 0x6; ++i) { |
| 632 | v[i] ^= ((i + 0x6) % m); |
| 633 | } |
| 634 | v[0x6] = '\0'; |
| 635 | } |
| 636 |
| 637 | static inline void fill_open(char v[]) { |
| 638 | // open |
| 639 | static unsigned int m = 0; |
| 640 |
| 641 | if (m == 0) { |
| 642 | m = 3; |
| 643 | } else if (m == 5) { |
| 644 | m = 7; |
| 645 | } |
| 646 |
| 647 | v[0x0] = 'n'; |
| 648 | v[0x1] = 'r'; |
| 649 | v[0x2] = 'e'; |
| 650 | v[0x3] = 'o'; |
| 651 | for (unsigned int i = 0; i < 0x4; ++i) { |
| 652 | v[i] ^= ((i + 0x4) % m); |
| 653 | } |
| 654 | v[0x4] = '\0'; |
| 655 | } |
| 656 |
| 657 | #endif |
| 658 |
| 659 | static inline void fill_sdk_d_genuine_d(char v[]) { |
| 660 | // sdk: %d, genuine: %d |
| 661 | static unsigned int m = 0; |
| 662 |
| 663 | if (m == 0) { |
| 664 | m = 19; |
| 665 | } else if (m == 23) { |
| 666 | m = 29; |
| 667 | } |
| 668 |
| 669 | v[0x0] = 'r'; |
| 670 | v[0x1] = 'f'; |
| 671 | v[0x2] = 'h'; |
| 672 | v[0x3] = '>'; |
| 673 | v[0x4] = '%'; |
| 674 | v[0x5] = '#'; |
| 675 | v[0x6] = 'c'; |
| 676 | v[0x7] = '$'; |
| 677 | v[0x8] = ')'; |
| 678 | v[0x9] = 'm'; |
| 679 | v[0xa] = 'n'; |
| 680 | v[0xb] = 'b'; |
| 681 | v[0xc] = 'x'; |
| 682 | v[0xd] = 'g'; |
| 683 | v[0xe] = 'a'; |
| 684 | v[0xf] = 'u'; |
| 685 | v[0x10] = '+'; |
| 686 | v[0x11] = '2'; |
| 687 | v[0x12] = '%'; |
| 688 | v[0x13] = 'e'; |
| 689 | for (unsigned int i = 0; i < 0x14; ++i) { |
| 690 | v[i] ^= ((i + 0x14) % m); |
| 691 | } |
| 692 | v[0x14] = '\0'; |
| 693 | } |
| 694 |
| 695 | static inline void fill_add_sigcont(char v[]) { |
| 696 | // add sigcont handler |
| 697 | static unsigned int m = 0; |
| 698 |
| 699 | if (m == 0) { |
| 700 | m = 17; |
| 701 | } else if (m == 19) { |
| 702 | m = 23; |
| 703 | } |
| 704 |
| 705 | v[0x0] = 'c'; |
| 706 | v[0x1] = 'g'; |
| 707 | v[0x2] = '`'; |
| 708 | v[0x3] = '%'; |
| 709 | v[0x4] = 'u'; |
| 710 | v[0x5] = 'n'; |
| 711 | v[0x6] = 'o'; |
| 712 | v[0x7] = 'j'; |
| 713 | v[0x8] = 'e'; |
| 714 | v[0x9] = 'e'; |
| 715 | v[0xa] = 'x'; |
| 716 | v[0xb] = '-'; |
| 717 | v[0xc] = 'f'; |
| 718 | v[0xd] = 'n'; |
| 719 | v[0xe] = '~'; |
| 720 | v[0xf] = 'd'; |
| 721 | v[0x10] = 'm'; |
| 722 | v[0x11] = 'g'; |
| 723 | v[0x12] = 'q'; |
| 724 | for (unsigned int i = 0; i < 0x13; ++i) { |
| 725 | v[i] ^= ((i + 0x13) % m); |
| 726 | } |
| 727 | v[0x13] = '\0'; |
| 728 | } |
| 729 |
| 730 | static inline void fill_received_sigcont(char v[]) { |
| 731 | // received sigcont |
| 732 | static unsigned int m = 0; |
| 733 |
| 734 | if (m == 0) { |
| 735 | m = 13; |
| 736 | } else if (m == 17) { |
| 737 | m = 19; |
| 738 | } |
| 739 |
| 740 | v[0x0] = 'q'; |
| 741 | v[0x1] = 'a'; |
| 742 | v[0x2] = 'f'; |
| 743 | v[0x3] = 'c'; |
| 744 | v[0x4] = 'n'; |
| 745 | v[0x5] = '~'; |
| 746 | v[0x6] = 'l'; |
| 747 | v[0x7] = 'n'; |
| 748 | v[0x8] = '+'; |
| 749 | v[0x9] = '\x7f'; |
| 750 | v[0xa] = 'i'; |
| 751 | v[0xb] = 'f'; |
| 752 | v[0xc] = 'a'; |
| 753 | v[0xd] = 'l'; |
| 754 | v[0xe] = 'j'; |
| 755 | v[0xf] = 'q'; |
| 756 | for (unsigned int i = 0; i < 0x10; ++i) { |
| 757 | v[i] ^= ((i + 0x10) % m); |
| 758 | } |
| 759 | v[0x10] = '\0'; |
| 760 | } |
| 761 |
| 762 | #if defined(CHECK_ARM64) && defined(__arm__) |
| 763 | static inline void fill_ro_product_cpu_abi(char v[]) { |
| 764 | // ro.product.cpu.abi |
| 765 | static unsigned int m = 0; |
| 766 |
| 767 | if (m == 0) { |
| 768 | m = 17; |
| 769 | } else if (m == 19) { |
| 770 | m = 23; |
| 771 | } |
| 772 |
| 773 | v[0x0] = 's'; |
| 774 | v[0x1] = 'm'; |
| 775 | v[0x2] = '-'; |
| 776 | v[0x3] = 't'; |
| 777 | v[0x4] = 'w'; |
| 778 | v[0x5] = 'i'; |
| 779 | v[0x6] = 'c'; |
| 780 | v[0x7] = '}'; |
| 781 | v[0x8] = 'j'; |
| 782 | v[0x9] = '~'; |
| 783 | v[0xa] = '%'; |
| 784 | v[0xb] = 'o'; |
| 785 | v[0xc] = '}'; |
| 786 | v[0xd] = '{'; |
| 787 | v[0xe] = '!'; |
| 788 | v[0xf] = 'q'; |
| 789 | v[0x10] = 'b'; |
| 790 | v[0x11] = 'h'; |
| 791 | for (unsigned int i = 0; i < 0x12; ++i) { |
| 792 | v[i] ^= ((i + 0x12) % m); |
| 793 | } |
| 794 | v[0x12] = '\0'; |
| 795 | } |
| 796 |
| 797 | static inline bool isArm64V8a(const char *str) { |
| 798 | return str != NULL |
| 799 | && *str == 'a' |
| 800 | && *++str == 'r' |
| 801 | && *++str == 'm' |
| 802 | && *++str == '6' |
| 803 | && *++str == '4' |
| 804 | && *++str == '-' |
| 805 | && *++str == 'v' |
| 806 | && *++str == '8' |
| 807 | && *++str == 'a'; |
| 808 | } |
| 809 |
| 810 | static inline void fill_32_64(char v[]) { |
| 811 | // run in 32 on 64 machine |
| 812 | static unsigned int m = 0; |
| 813 |
| 814 | if (m == 0) { |
| 815 | m = 19; |
| 816 | } else if (m == 23) { |
| 817 | m = 29; |
| 818 | } |
| 819 |
| 820 | v[0x0] = 'v'; |
| 821 | v[0x1] = 'p'; |
| 822 | v[0x2] = 'h'; |
| 823 | v[0x3] = '\''; |
| 824 | v[0x4] = 'a'; |
| 825 | v[0x5] = 'g'; |
| 826 | v[0x6] = '*'; |
| 827 | v[0x7] = '8'; |
| 828 | v[0x8] = '>'; |
| 829 | v[0x9] = '-'; |
| 830 | v[0xa] = 'a'; |
| 831 | v[0xb] = 'a'; |
| 832 | v[0xc] = '0'; |
| 833 | v[0xd] = '\''; |
| 834 | v[0xe] = '&'; |
| 835 | v[0xf] = ' '; |
| 836 | v[0x10] = 'l'; |
| 837 | v[0x11] = 'c'; |
| 838 | v[0x12] = '`'; |
| 839 | v[0x13] = 'l'; |
| 840 | v[0x14] = 'l'; |
| 841 | v[0x15] = 'h'; |
| 842 | v[0x16] = 'b'; |
| 843 | for (unsigned int i = 0; i < 0x17; ++i) { |
| 844 | v[i] ^= ((i + 0x17) % m); |
| 845 | } |
| 846 | v[0x17] = '\0'; |
| 847 | } |
| 848 | #endif |
| 849 |
| 850 | static void handler(int sig __unused) { |
| 851 | char v[0x11]; |
| 852 | fill_received_sigcont(v); |
| 853 | LOGI(v); |
| 854 | } |
| 855 |
| 856 | #ifdef DEBUG_HOOK_IO |
| 857 |
| 858 | static void check_hook_function(void *handle, const char *name) { |
| 859 | void *symbol = dlsym(handle, name); |
| 860 | #ifdef DEBUG |
| 861 | void *symbol2 = plt_dlsym(name, NULL); |
| 862 | LOGI("symbol: %s, dlsym: %p, dl_iterate_phdr: %p", name, symbol, symbol2); |
| 863 | #endif |
| 864 | if (symbol != NULL && setRead(symbol) && isInlineHooked(symbol)) { |
| 865 | LOGI("%s is hooked", name); |
| 866 | } |
| 867 | } |
| 868 |
| 869 | #define HOOK_SYMBOL(x, y) check_hook_function(x, #y) |
| 870 |
| 871 | static void check_inline_hook_io() { |
| 872 | void *handle = dlopen("libc.so", RTLD_NOW); |
| 873 | if (handle) { |
| 874 | HOOK_SYMBOL(handle, faccessat); |
| 875 | HOOK_SYMBOL(handle, __openat); |
| 876 | HOOK_SYMBOL(handle, fchmodat); |
| 877 | HOOK_SYMBOL(handle, fchownat); |
| 878 | HOOK_SYMBOL(handle, renameat); |
| 879 | HOOK_SYMBOL(handle, fstatat64); |
| 880 | HOOK_SYMBOL(handle, __statfs); |
| 881 | HOOK_SYMBOL(handle, __statfs64); |
| 882 | HOOK_SYMBOL(handle, mkdirat); |
| 883 | HOOK_SYMBOL(handle, mknodat); |
| 884 | HOOK_SYMBOL(handle, truncate); |
| 885 | HOOK_SYMBOL(handle, linkat); |
| 886 | HOOK_SYMBOL(handle, readlinkat); |
| 887 | HOOK_SYMBOL(handle, unlinkat); |
| 888 | HOOK_SYMBOL(handle, symlinkat); |
| 889 | HOOK_SYMBOL(handle, utimensat); |
| 890 | HOOK_SYMBOL(handle, __getcwd); |
| 891 | HOOK_SYMBOL(handle, chdir); |
| 892 | HOOK_SYMBOL(handle, execve); |
| 893 | } |
| 894 | dlclose(handle); |
| 895 | } |
| 896 |
| 897 | #endif |
| 898 |
| 899 | static inline void fill_openat_is_hooked(char v[]) { |
| 900 | // openAt is hooked |
| 901 | static unsigned int m = 0; |
| 902 |
| 903 | if (m == 0) { |
| 904 | m = 13; |
| 905 | } else if (m == 17) { |
| 906 | m = 19; |
| 907 | } |
| 908 |
| 909 | v[0x0] = 'l'; |
| 910 | v[0x1] = 't'; |
| 911 | v[0x2] = '`'; |
| 912 | v[0x3] = 'h'; |
| 913 | v[0x4] = 'F'; |
| 914 | v[0x5] = '|'; |
| 915 | v[0x6] = ')'; |
| 916 | v[0x7] = 'c'; |
| 917 | v[0x8] = 'x'; |
| 918 | v[0x9] = ','; |
| 919 | v[0xa] = 'h'; |
| 920 | v[0xb] = 'n'; |
| 921 | v[0xc] = 'm'; |
| 922 | v[0xd] = 'h'; |
| 923 | v[0xe] = 'a'; |
| 924 | v[0xf] = 'a'; |
| 925 | for (unsigned int i = 0; i < 0x10; ++i) { |
| 926 | v[i] ^= ((i + 0x10) % m); |
| 927 | } |
| 928 | v[0x10] = '\0'; |
| 929 | } |
| 930 |
| 931 | bool has_native_libs() { |
| 932 | #if __ANDROID_API__ >= 24 |
| 933 | return true; |
| 934 | #else |
| 935 | Symbol symbol; |
| 936 | char v[0x10]; |
| 937 | fill_ba88(v); |
| 938 | memset(&symbol, 0, sizeof(Symbol)); |
| 939 | symbol.check = (PLT_CHECK_PLT_APP | PLT_CHECK_NAME); |
| 940 | symbol.symbol_name = (char *) v; |
| 941 | if (dl_iterate_phdr_symbol(&symbol)) { |
| 942 | #ifdef DEBUG_NATIVE |
| 943 | LOGW("cannot dl_iterate_phdr_symbol"); |
| 944 | #endif |
| 945 | return false; |
| 946 | } |
| 947 |
| 948 | bool extractNativeLibs = true; |
| 949 | for (int i = 0; i < symbol.size; ++i) { |
| 950 | if (symbol.names[i] != NULL) { |
| 951 | #ifdef DEBUG_NATIVE |
| 952 | LOGW("%s: %s", v, symbol.names[i]); |
| 953 | #endif |
| 954 | if (extractNativeLibs && strstr(symbol.names[i], "apk!") != NULL) { |
| 955 | extractNativeLibs = false; |
| 956 | } |
| 957 | free(symbol.names[i]); |
| 958 | symbol.names[i] = NULL; |
| 959 | } |
| 960 | } |
| 961 | if (symbol.names != NULL) { |
| 962 | free(symbol.names); |
| 963 | symbol.names = NULL; |
| 964 | } |
| 965 |
| 966 | #ifdef DEBUG_NATIVE |
| 967 | LOGW("has_native_libs: %s", extractNativeLibs ? "true" : "false"); |
| 968 | #endif |
| 969 | return extractNativeLibs; |
| 970 | #endif |
| 971 | } |
| 972 |
| 973 | bool checkGenuine(JNIEnv *env) { |
| 974 | char v1[0x20]; |
| 975 |
| 976 | signal(SIGCONT, handler); |
| 977 | fill_add_sigcont(v1); |
| 978 | LOGI(v1); // 0x14 |
| 979 |
| 980 | sdk = getSdk(); |
| 981 |
| 982 | if (sdk < 21) { |
| 983 | genuine = CHECK_FATAL; |
| 984 | goto done; |
| 985 | } |
| 986 |
| 987 | uid = getuid(); |
| 988 |
| 989 | #ifdef DEBUG_HOOK_SELF |
| 990 | LOGI("+++ whale +++"); |
| 991 | check_inline_hook_whale(); |
| 992 | LOGI("--- whale ----"); |
| 993 | #if defined(__arm__) || defined(__aarch64__) |
| 994 | LOGI("+++ hookzz-b +++"); |
| 995 | check_inline_hook_hookzz_b(); |
| 996 | LOGI("--- hookzz-b ---"); |
| 997 | LOGI("+++ hookzz +++"); |
| 998 | check_inline_hook_hookzz(); |
| 999 | LOGI("--- hookzz ---"); |
| 1000 | #endif |
| 1001 | #if defined(__arm__) |
| 1002 | LOGI("+++ hookzz-b +++"); |
| 1003 | check_inline_hook_hookzz_b(); |
| 1004 | LOGI("--- hookzz-b ---"); |
| 1005 | LOGI("+++ substrate +++"); |
| 1006 | check_inline_hook_substrate(); |
| 1007 | LOGI("--- substrate ---"); |
| 1008 | #endif |
| 1009 | #endif |
| 1010 |
| 1011 | #ifdef DEBUG |
| 1012 | LOGI("JNI_OnLoad start, sdk: %d, uid: %d", sdk, uid); |
| 1013 | #endif |
| 1014 |
| 1015 | #ifdef DEBUG_HOOK_IO |
| 1016 | check_inline_hook_io(); |
| 1017 | #endif |
| 1018 |
| 1019 | #ifdef DEBUG_MAPS |
| 1020 | LOGI("openAt: %p", &openAt); |
| 1021 | #endif |
| 1022 | if (isInlineHooked(&openAt)) { |
| 1023 | fill_openat_is_hooked(v1); |
| 1024 | LOGE(v1); |
| 1025 | genuine = CHECK_FATAL; |
| 1026 | goto done; |
| 1027 | } |
| 1028 |
| 1029 | char *packageName = getGenuinePackageName(); |
| 1030 |
| 1031 | if (uid < 10000) { |
| 1032 | goto clean; |
| 1033 | } |
| 1034 |
| 1035 | #ifdef CHECK_MOUNT |
| 1036 | checkMount(maps); |
| 1037 | #endif |
| 1038 |
| 1039 | #ifndef NO_CHECK_MAPS |
| 1040 | #ifdef DEBUG |
| 1041 | LOGI("checkMaps start"); |
| 1042 | #endif |
| 1043 | genuine = checkMaps(packageName); |
| 1044 | #endif |
| 1045 |
| 1046 | #ifdef CHECK_HOOK |
| 1047 | #define CHECK_HOOK_SYMBOL(x) do {\ |
| 1048 | if (genuine == CHECK_TRUE) {\ |
| 1049 | fill_##x(v1);\ |
| 1050 | if (isPltHooked(v1, true)) {\ |
| 1051 | genuine = CHECK_FALSE;\ |
| 1052 | }\ |
| 1053 | }\ |
| 1054 | } while(0); |
| 1055 | CHECK_HOOK_SYMBOL(open); |
| 1056 | CHECK_HOOK_SYMBOL(openat); |
| 1057 | CHECK_HOOK_SYMBOL(__openat); |
| 1058 | CHECK_HOOK_SYMBOL(jniRegisterNativeMethods); |
| 1059 | #endif |
| 1060 |
| 1061 | #if defined(CHECK_ARM64) && defined(__arm__) |
| 1062 | if (genuine == CHECK_TRUE) { |
| 1063 | fill_ro_product_cpu_abi(v1); |
| 1064 | __system_property_get(v1, prop); |
| 1065 | if (isArm64V8a(prop)) { |
| 1066 | fill_32_64(v1); |
| 1067 | LOGW(v1); |
| 1068 | genuine = CHECK_FALSE; |
| 1069 | } |
| 1070 | } |
| 1071 | #endif |
| 1072 |
| 1073 | clean: |
| 1074 | #ifdef GENUINE_NAME |
| 1075 | free(packageName); |
| 1076 | #endif |
| 1077 | #ifdef DEBUG |
| 1078 | LOGI("JNI_OnLoad end, genuine: %d", genuine); |
| 1079 | #endif |
| 1080 |
| 1081 | if (genuine != CHECK_PROXY && isAmProxy(env, sdk)) { |
| 1082 | genuine = CHECK_PROXY; |
| 1083 | } |
| 1084 |
| 1085 | done: |
| 1086 | fill_sdk_d_genuine_d(v1); // 0x15 |
| 1087 | LOGI(v1, sdk, genuine); |
| 1088 |
| 1089 | return setGenuine(env, genuine); |
| 1090 | } |
| 1091 |