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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <stdint.h> #include <stdarg.h> #include <time.h> #include <dirent.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/inotify.h> #include <elf.h> #include <dlfcn.h> #include <errno.h>
#define LOG_DIR "/data/data/com.ACE2025.Game" #define LOG_FILE LOG_DIR "/cheat_detection.log" #define PAGE_SIZE 4096
#define INOTIFY_BUF_SIZE (1024 * (sizeof(struct inotify_event) + 256))
static uint32_t g_crc32_tab[256];
static void crc32_init(void) { for (uint32_t i = 0; i < 256; i++) { uint32_t c = i; for (int j = 0; j < 8; j++) c = (c >> 1) ^ (0xEDB88320u & (-(c & 1))); g_crc32_tab[i] = c; } }
static uint32_t crc32_calc(const void *data, size_t len) { const uint8_t *p = (const uint8_t *)data; uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) crc = (crc >> 8) ^ g_crc32_tab[(crc ^ p[i]) & 0xFF]; return crc ^ 0xFFFFFFFF; }
static pthread_mutex_t g_log_mutex = PTHREAD_MUTEX_INITIALIZER;
static void log_write(const char *detector, const char *fmt, ...) { pthread_mutex_lock(&g_log_mutex); FILE *f = fopen(LOG_FILE, "a"); if (f) { time_t now = time(NULL); struct tm *tm_info = localtime(&now); char timebuf[64]; strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_info);
fprintf(f, "[%s][%s] ", timebuf, detector); va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); fprintf(f, "\n"); fflush(f); fclose(f); } pthread_mutex_unlock(&g_log_mutex); }
typedef struct { uintptr_t start; uintptr_t end; char perms[8]; uintptr_t offset; char path[512]; } MapEntry;
static int parse_maps_line(const char *line, MapEntry *e) { memset(e, 0, sizeof(*e)); char path_buf[512] = {0}; int n = sscanf(line, "%lx-%lx %7s %lx %*s %*d %511[^\n]", &e->start, &e->end, e->perms, &e->offset, path_buf); if (n < 4) return -1;
char *p = path_buf; while (*p == ' ' || *p == '\t') p++; strncpy(e->path, p, sizeof(e->path) - 1); return 0; }
static int find_module_info(const char *libname, uintptr_t *out_base, char *out_path, size_t path_size) { FILE *f = fopen("/proc/self/maps", "r"); if (!f) return -1;
char line[1024]; int found = 0; *out_base = 0; out_path[0] = '\0';
while (fgets(line, sizeof(line), f)) { MapEntry e; if (parse_maps_line(line, &e) < 0) continue; if (!strstr(e.path, libname)) continue; if (!found || e.start < *out_base) { *out_base = e.start; strncpy(out_path, e.path, path_size - 1); out_path[path_size - 1] = '\0'; } found = 1; } fclose(f); return found ? 0 : -1; }
static int is_system_lib(const char *path) { if (strncmp(path, "/system/", 8) == 0) return 1; if (strncmp(path, "/system_ext/", 12) == 0) return 1; if (strncmp(path, "/vendor/", 8) == 0) return 1; if (strncmp(path, "/apex/", 6) == 0) return 1; if (strncmp(path, "/product/", 9) == 0) return 1; if (strncmp(path, "/odm/", 5) == 0) return 1; if (strstr(path, "linker")) return 1; return 0; }
static void detect_memory_permissions(void) { FILE *f = fopen("/proc/self/maps", "r"); if (!f) return;
char line[1024]; int detected = 0;
while (fgets(line, sizeof(line), f)) { MapEntry e; if (parse_maps_line(line, &e) < 0) continue; if (e.path[0] == '\0' || !strstr(e.path, ".so")) continue; if (is_system_lib(e.path)) continue;
if (strchr(e.perms, 'w') && strchr(e.perms, 'x')) { log_write("MemPerm", "DETECTED -- %s 地址 0x%lx-0x%lx 权限=%s", e.path, e.start, e.end, e.perms); detected = 1; } } fclose(f);
if (!detected) log_write("MemPerm", "PASS"); }
static void detect_code_integrity(const char *libname) { uintptr_t base; char disk_path[512];
if (find_module_info(libname, &base, disk_path, sizeof(disk_path)) < 0) { log_write("CodeInteg", "SKIP -- %s 未加载", libname); return; }
FILE *disk_fp = fopen(disk_path, "rb"); if (!disk_fp) { log_write("CodeInteg", "WARN -- 无法打开磁盘文件: %s", disk_path); return; }
FILE *maps = fopen("/proc/self/maps", "r"); if (!maps) { fclose(disk_fp); return; }
char line[1024]; int tampered_pages = 0; int tampered_bytes = 0; int total_pages = 0;
uint8_t *disk_buf = (uint8_t *)malloc(PAGE_SIZE); if (!disk_buf) { fclose(maps); fclose(disk_fp); return; }
while (fgets(line, sizeof(line), maps)) { MapEntry e; if (parse_maps_line(line, &e) < 0) continue; if (!strstr(e.path, libname)) continue; if (!strchr(e.perms, 'x')) continue;
size_t seg_size = e.end - e.start;
for (size_t off = 0; off < seg_size; off += PAGE_SIZE) { size_t chunk = PAGE_SIZE; if (off + chunk > seg_size) chunk = seg_size - off;
if (fseek(disk_fp, e.offset + off, SEEK_SET) != 0) break; size_t nread = fread(disk_buf, 1, chunk, disk_fp); if (nread != chunk) break;
const uint8_t *mem_ptr = (const uint8_t *)(e.start + off);
uint32_t disk_crc = crc32_calc(disk_buf, chunk); uint32_t mem_crc = crc32_calc(mem_ptr, chunk); total_pages++;
if (disk_crc != mem_crc) { tampered_pages++; for (size_t i = 0; i < chunk; i++) { if (disk_buf[i] != mem_ptr[i]) { uintptr_t vaddr = (e.start + off + i) - base; log_write("CodeInteg", "DETECTED -- %s+0x%lx " "磁盘=0x%02x 内存=0x%02x " "(页CRC: 磁盘=0x%08x 内存=0x%08x)", libname, vaddr, disk_buf[i], mem_ptr[i], disk_crc, mem_crc); tampered_bytes++; } } } } }
free(disk_buf); fclose(maps); fclose(disk_fp);
if (tampered_pages == 0) { log_write("CodeInteg", "PASS -- %s 共 %d 页全部通过 CRC 校验", libname, total_pages); } else { log_write("CodeInteg", "SUMMARY -- %s %d/%d 页异常, %d 字节被修改", libname, tampered_pages, total_pages, tampered_bytes); } }
static int is_whitelisted_path(const char *path) { if (path[0] == '\0' || path[0] == '[') return 1;
if (is_system_lib(path)) return 1; if (strncmp(path, "/dev/", 5) == 0) return 1; if (strncmp(path, "/proc/", 6) == 0) return 1;
if (strstr(path, "/data/app/") && strstr(path, "com.ACE2025.Game")) return 1;
if (strstr(path, "dalvik")) return 1; if (strstr(path, "boot")) return 1; if (strstr(path, "framework")) return 1;
if (strstr(path, ".oat")) return 1; if (strstr(path, ".art")) return 1; if (strstr(path, ".vdex")) return 1; if (strstr(path, ".apk")) return 1;
if (strstr(path, "libAnswer.so")) return 1;
return 0; }
static void detect_abnormal_modules(void) { FILE *f = fopen("/proc/self/maps", "r"); if (!f) return;
char line[1024]; int detected = 0;
char reported[32][512]; int reported_count = 0;
while (fgets(line, sizeof(line), f)) { MapEntry e; if (parse_maps_line(line, &e) < 0) continue; if (e.path[0] == '\0') continue; if (!strstr(e.path, ".so")) continue;
int dup = 0; for (int i = 0; i < reported_count; i++) { if (strcmp(reported[i], e.path) == 0) { dup = 1; break; } } if (dup) continue;
if (!is_whitelisted_path(e.path)) { log_write("ModuleLoad", "DETECTED -- 可疑模块: %s (0x%lx-0x%lx %s)", e.path, e.start, e.end, e.perms); detected = 1; if (reported_count < 32) { strncpy(reported[reported_count], e.path, 511); reported[reported_count][511] = '\0'; reported_count++; } } } fclose(f);
if (!detected) log_write("ModuleLoad", "PASS"); }
static void *inotify_thread(void *arg) { (void)arg;
int ifd = inotify_init(); if (ifd < 0) { log_write("Inotify", "WARN -- inotify_init 失败: %s", strerror(errno)); return NULL; }
const char *watch_dirs[] = { "/data/data/com.ACE2025.Game", "/data/data/com.ACE2025.Game/files", "/data/data/com.ACE2025.Game/cache", "/data/local/tmp", NULL };
int wd_count = 0; for (int i = 0; watch_dirs[i] != NULL; i++) { int wd = inotify_add_watch(ifd, watch_dirs[i], IN_CREATE | IN_MODIFY | IN_MOVED_TO | IN_CLOSE_WRITE); if (wd >= 0) { log_write("Inotify", "监控目录: %s (wd=%d)", watch_dirs[i], wd); wd_count++; } }
if (wd_count == 0) { log_write("Inotify", "WARN -- 没有可监控的目录"); close(ifd); return NULL; }
log_write("Inotify", "文件监控线程已启动, 监控 %d 个目录", wd_count);
char buf[INOTIFY_BUF_SIZE];
while (1) { ssize_t len = read(ifd, buf, sizeof(buf)); if (len <= 0) break;
char *ptr = buf; while (ptr < buf + len) { struct inotify_event *event = (struct inotify_event *)ptr;
if (event->len > 0 && event->name[0] != '\0') { if (!strstr(event->name, ".so")) goto next_event;
static const char *legit_libs[] = { "libAnswer.so", "libUE4.so", "libc++_shared.so", "libOVRPlugin.so", "libplaycore.so", "libtry-alloc-lib.so", "libvrapi.so", NULL };
int legitimate = 0; for (int k = 0; legit_libs[k] != NULL; k++) { if (strcmp(event->name, legit_libs[k]) == 0) { legitimate = 1; break; } }
if (!legitimate) { const char *event_type = "未知"; if (event->mask & IN_CREATE) event_type = "创建"; if (event->mask & IN_MODIFY) event_type = "修改"; if (event->mask & IN_MOVED_TO) event_type = "移入"; if (event->mask & IN_CLOSE_WRITE) event_type = "写入完成";
log_write("Inotify", "DETECTED -- 非法 .so 文件: %s [%s]", event->name, event_type); } }
next_event: ptr += sizeof(struct inotify_event) + event->len; } }
close(ifd); log_write("Inotify", "文件监控线程退出"); return NULL; }
static void run_all_detections(int round) { log_write("Main", "========== 第 %d 轮检测 ==========", round);
detect_memory_permissions(); detect_code_integrity("libUE4.so"); detect_abnormal_modules();
log_write("Main", "========== 第 %d 轮结束 ==========", round); }
static void *detection_thread(void *arg) { (void)arg; log_write("Main", "检测线程已启动, 等待 libUE4.so 加载...");
uintptr_t ue4_base = 0; char ue4_path[512] = {0}; for (int i = 0; i < 120; i++) { if (find_module_info("libUE4.so", &ue4_base, ue4_path, sizeof(ue4_path)) == 0) { log_write("Main", "libUE4.so 已加载: base=0x%lx path=%s", ue4_base, ue4_path); break; } usleep(500000); }
if (ue4_base == 0) { log_write("Main", "WARN -- 等待超时, libUE4.so 未加载"); return NULL; }
sleep(5);
run_all_detections(1); for (int round = 2; round <= 5; round++) { sleep(10); run_all_detections(round); }
log_write("Main", "检测线程退出"); return NULL; }
__attribute__((constructor)) static void libAnswer_init(void) { crc32_init(); log_write("Main", "libAnswer.so 已加载, PID=%d", getpid());
pthread_t tid1; if (pthread_create(&tid1, NULL, detection_thread, NULL) == 0) pthread_detach(tid1);
pthread_t tid2; if (pthread_create(&tid2, NULL, inotify_thread, NULL) == 0) pthread_detach(tid2); }
|