本文讲的是
Android 反调试技巧之Self-Debuging/proc 文件系统检测、调试断点探测,
JNIEXPORT jboolean JNICALL Java_com_example_disable(JNIENV* env, jobject dontuse ){ // gDvm==struct DvmGlobals gDvm.jdwpState = NULL; return JNI_TRUE; }
#include <jni.h> #include <string> #include <android/log.h> #include <dlfcn.h> #include <sys/mman.h> #include <jdwp/jdwp.h>#define log(FMT, ...) __android_log_print(ANDROID_LOG_VERBOSE, "JDWPFun", FMT, ##__VA_ARGS__)// Vtable structure. Just to make messing around with it more intuitivestruct VT_JdwpAdbState { unsigned long x; unsigned long y; void * JdwpSocketState_destructor; void * _JdwpSocketState_destructor; void * Accept; void * showmanyc; void * ShutDown; void * ProcessIncoming; };extern "C"JNIEXPORT void JNICALL Java_sg_vantagepoint_jdwptest_MainActivity_JDWPfun( JNIEnv *env, jobject /* this */) { void* lib = dlopen("libart.so", RTLD_NOW); if (lib == NULL) { log("Error loading libart.so"); dlerror(); }else{ struct VT_JdwpAdbState *vtable = ( struct VT_JdwpAdbState *)dlsym(lib, "_ZTVN3art4JDWP12JdwpAdbStateE"); if (vtable == 0) { log("Couldn't resolve symbol '_ZTVN3art4JDWP12JdwpAdbStateE'.n"); }else { log("Vtable for JdwpAdbState at: %08xn", vtable); // Let the fun begin! unsigned long pagesize = sysconf(_SC_PAGE_SIZE); unsigned long page = (unsigned long)vtable & ~(pagesize-1); mprotect((void *)page, pagesize, PROT_READ | PROT_WRITE); vtable->ProcessIncoming = vtable->ShutDown; // Reset permissions & flush cache mprotect((void *)page, pagesize, PROT_READ); } } }
Pyramidal Neuron:~ berndt$ adb jdwp2926Pyramidal Neuron:~ berndt$ adb forward tcp:7777 jdwp:2926Pyramidal Neuron:~ berndt$ jdb -attach localhost:7777java.io.IOException: handshake failed - connection prematurally closed at com.sun.tools.jdi.SocketTransportService.handshake(SocketTransportService.java:136) at com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:232) at com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:116) at com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:90) at com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:519) at com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:328) at com.sun.tools.example.debug.tty.Env.init(Env.java:63) at com.sun.tools.example.debug.tty.TTY.main(TTY.java:1066)
#include <sys/ptrace.h> long ptrace(enum __ptrace_request request, pid_t pid, void *addr, void *data)
1.enum __ptrace_request request:指示了ptrace要执行的命令。 2.pid_t pid: 指示ptrace要跟踪的进程。 3.void *addr: 指示要监控的内存地址。 4.void *data: 存放读取出的或者要写入的数据。
void anti_debug() { child_pid = fork(); if (child_pid == 0) { int ppid = getppid(); int status; if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) { waitpid(ppid, &status, 0); ptrace(PTRACE_CONT, ppid, NULL, NULL); while (waitpid(ppid, &status, 0)) { if (WIFSTOPPED(status)) { ptrace(PTRACE_CONT, ppid, NULL, NULL); } else { // Process has exited for some reason _exit(0); } } } } }
root@android:/ # ps | grep -i anti u0_a151 18190 201 1535844 54908 ffffffff b6e0f124 S sg.vantagepoint.antidebug u0_a151 18224 18190 1495180 35824 c019a3ac b6e0ee5c S sg.vantagepoint.antidebug
root@android:/ # ./gdbserver --attach localhost:12345 18190 warning: process 18190 is already traced by process 18224 Cannot attach to lwp 18190: Operation not permitted (1) Exiting
root@android:/ # kill -9 18224
root@android:/ # ./gdbserver --attach localhost:12345 18190 Attached; pid = 18190 Listening on port 12345
分别跟踪彼此的多个进程 跟踪运行过程,监视子进程 监视/ proc文件系统中的值,例如/ proc / pid / status中的TracerPID。
#include <jni.h> #include <string> #include <unistd.h> #include <sys/ptrace.h> #include <sys/wait.h> static int child_pid; void *monitor_pid(void *) { int status; waitpid(child_pid, &status, 0); /* Child status should never change. */ _exit(0); // Commit seppuku } void anti_debug() { child_pid = fork(); if (child_pid == 0) { int ppid = getppid(); int status; if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) { waitpid(ppid, &status, 0); ptrace(PTRACE_CONT, ppid, NULL, NULL); while (waitpid(ppid, &status, 0)) { if (WIFSTOPPED(status)) { ptrace(PTRACE_CONT, ppid, NULL, NULL); } else { // Process has exited _exit(0); } } } } else { pthread_t t; /* Start the monitoring thread */ pthread_create(&t, NULL, monitor_pid, (void *)NULL); } } extern "C" JNIEXPORT void JNICALL Java_sg_vantagepoint_antidebug_MainActivity_antidebug( JNIEnv *env, jobject /* this */) { anti_debug(); }
root@android:/ # ps | grep -i anti-debug u0_a152 20267 201 1552508 56796 ffffffff b6e0f124 S sg.vantagepoint.anti-debug u0_a152 20301 20267 1495192 33980 c019a3ac b6e0ee5c S sg.vantagepoint.anti-debug
root@android:/ # kill -9 20301 root@android:/ # ./gdbserver --attach localhost:12345 20267 gdbserver: unable to open /proc file '/proc/20267/status' Cannot attach to lwp 20267: No such file or directory (2) Exiting
原文发布时间为:2017年4月14日
本文作者:xiaohui
本文来自云栖社区合作伙伴嘶吼,了解相关信息可以关注嘶吼网站。