This article was reproduced from: http://www.zhaoxiaodan.com/java/android/android-native%E5%8F%8D%E8%B0%83%E8%AF%95.html
Thinking
Before studying how to debug and try to reverse a person's encrypted things, so now the experience is:
In fact the focus is not how you encrypt, the point is how not to let others know how you encrypt
Because like this 自己加密的资源运行的时候自己解密之后拿来用 program, I do not even care about how you exactly encrypt, encryption algorithm is what, I just need to know, after you have decrypted, the memory of the resource where the block, write a dumper to get it all;
Encryption does not prevent being cracked, just increase the difficulty and threshold of the crack, encryption and decryption is a mutual game process
Add a secret to the resources, then for those who are more basic technology, and want to simple copy of the people, they take you have no way; But for those who know a little bit about the principle of IDA, there's no such thing as an egg.
So good, now I'm going to look at how to add a little bit more threshold
Before the decryption of an important premise is debugging, so the most directly think of the increase in the threshold is 反调试 .
Test
Build an Android project first, and join native support
Android-anti-debug
First the PID and the parent PID come out to see
#include <jni.h>#include<stdio.h>#include<sys/ptrace.h>#include<unistd.h>#include"log.h"voidAnti_debug () {intPID =Getpid (); intPpid =Getppid (); LOGD ("pid:%d,ppid:%d", pid,ppid);} Jint Jni_onload (JAVAVM* VMS,void*reserved) {Anti_debug (); JNIEnv*env; if(Vm->getenv (reinterpret_cast<void**> (&env), jni_version_1_6)! =JNI_OK) { return-1; } returnjni_version_1_6;}
06-11 06:42:24.411: D/[android-anti-debug](1451): pid:1451,ppid:192
PID 192 is:
root 192 1 492204 38400 ffffffff b756598c S zygote
In Android, all application processes and system service processes are systemserver by the zygote process (fork), which is perhaps why it is called the zygote (fertilized egg).
See also: Source code analysis of the Android system process zygote boot process
Look down./proc/1451/stat
[email protected]:/data/local/tmp # cat /proc/17204//statusName: ndroidantidebugState: S (sleeping)Tgid: 17204Pid: 17204PPid: 2146TracerPid: 0Uid: 10058 10058 10058 10058Gid: 10058 10058 10058 10058FDSize: 256...
Go attach with gdbserver and see what happens:
[email protected]:/data/local/tmp # ps |grep blogu0_a58 30678 2146 907884 59720 ffffffff 4005778c S com.zhaoxiaodan.blog.androidantidebug[email protected]:/data/local/tmp # ./gdbserver --attach 127.0.0.1:1234 30678Attached; pid = 30678Listening on port 1234[email protected]:/data/local/tmp # cat /proc/17204//statusName: ndroidantidebugState: t (tracing stop)Tgid: 17204Pid: 17204PPid: 2146TracerPid: 20337Uid: 10058 10058 10058 10058Gid: 10058 10058 10058 10058
Discovery TracerPid row changed from 0 to 20337
Ida these debugging tools are actually used ptrace , Ptrace has a very important specific:
A process can only be debugged by one process.
So, the simplest way is to be in the JNI_OnLoad directptrace(PTRACE_TRACEME, 0, 0, 0);
Method 1, directlyptrace(PTRACE_TRACEME, 0, 0, 0);
#include <jni.h>#include<stdio.h>#include<sys/ptrace.h>#include<unistd.h>#include"log.h"voidAnti_debug () {ptrace (Ptrace_traceme,0,0,0);} Jint Jni_onload (JAVAVM* VMS,void*reserved) {Anti_debug (); JNIEnv*env; if(Vm->getenv (reinterpret_cast<void**> (&env), jni_version_1_6)! =JNI_OK) { return-1; } returnjni_version_1_6;}
And then use Gdbserver to attach:
[email protected]:/data/local/tmp # ./gdbserver --attach 127.0.0.1:1234 31092Cannot attach to lwp 31092: Operation not permitted (1)Exiting
Okay, I can't hang up.
But this method, with anti-compilation open, it is easy to find ptrace the place to call, do not know the changes 汇编指令 (such as to Nilnil), skip this call
Method 2, Dark piles
According to the above mentioned /proc/$pid/status in the TracerPid line shows the principle of the debugger pid , you can write a method to check the value, if!=0 to exit the program
Check the functions as follows:
voidBe_attached_check () {Try { Const intBufSize =1024x768; CharFilename[bufsize]; CharLine[bufsize]; intPID =Getpid (); sprintf (filename,"/proc/%d/status", PID); FILE* fd = fopen (filename,"R"); if(FD! =nullptr) { while(Fgets (line, bufsize, FD)) {if(STRNCMP (line,"Tracerpid",9) ==0) { intStatue = atoi (&line[Ten]); LOGD ("%s", line); if(Statue! =0) {LOGD ("Be attached!! Kill%d", PID); Fclose (FD); intRET =Kill (PID, SIGKILL); } Break; }} fclose (FD); } Else{logd ("Open%s fail ...", filename); } } Catch (...) { }}
You can make this function into a macro, and then write a program randomly insert the macro into the source of the various places, as the code continues to execute, you will encounter each of these checkpoints
In fact, there are no eggs, but more stakes, you pull up on the trouble point
The following is just the process of using a threading simulation check:
#include"android-anti-debug.h"#include<string>#include<sys/ptrace.h>#include<unistd.h>#include<stdlib.h>#include<chrono>#include<thread>#include"log.h"voidBe_attached_check () {Try { Const intBufSize =1024x768; CharFilename[bufsize]; CharLine[bufsize]; intPID =Getpid (); sprintf (filename,"/proc/%d/status", PID); FILE* fd = fopen (filename,"R"); if(FD! =nullptr) { while(Fgets (line, bufsize, FD)) {if(STRNCMP (line,"Tracerpid",9) ==0) { intStatue = atoi (&line[Ten]); LOGD ("%s", line); if(Statue! =0) {LOGD ("Be attached!! Kill%d", PID); Fclose (FD); intRET =Kill (PID, SIGKILL); } Break; }} fclose (FD); } Else{logd ("Open%s fail ...", filename); } } Catch (...) { }}//Check threads, check every secondvoidThread_task (intN) { while(true) {LOGD ("Start Be_attached_check ..."); Be_attached_check (); Std::this_thread::sleep_for (Std::chrono::seconds (n)); }}voidAnti_debug () {//ptrace (ptrace_traceme, 0, 0, 0);Auto Checkthread = Std::thread (Thread_task,1); Checkthread.detach ();} Jint Jni_onload (JAVAVM* VMS,void*reserved) {Anti_debug (); JNIEnv*env; if(Vm->getenv (reinterpret_cast<void**> (&env), jni_version_1_6)! =JNI_OK) { return-1; } returnjni_version_1_6;}
Reproduced Android Native Anti-debug