获取和检测android的父进程

简介

每个App的用于程序都是通过父进程zygote进行fork出来的子进程,所以zygote也是所有app的父进程。
那么对于zygote进程可以用于检测判断app应该是否处于被调试状态。

检测父进程原理

有的时候不使用apk附加调试的方法进行逆向,而是写一个.out可执行文件直接加载so进行
调试,这样程序的父进程名和正常启动apk的父进程名是不一样的。

实验测试

1、正常启动的apk程序:父进程是zygote
2、调试启动的apk程序:在AS中用LLDB调试发现父进程还是zygote
3、附加调试的apk程序:父进程是zygote
4、vs远程调试 用可执行文件加载so:父进程名为gdbserver
实验结论:父进程名非zygote的,判定为调试状态

代码实现



int CheckApkParents()
 {
    
     

// 设置buf 
 char strPpidCmdline[0x100]={
    
    0};
 snprintf(strPpidCmdline, sizeof(strPpidCmdline), "/proc/%d/cmdl ine", getppid()); 
 // 调用系统的open函数进行打开文件 
 int file=open(strPpidCmdline,O_RDONLY); if(file<0) 
 {
    
     
     //打开失败,
     LOGA("CheckApkParents open错误!\n");
     return -1;
 }
 
 // 文件内容读入内存
 memset(strPpidCmdline,0,sizeof(strPpidCmdline)); 
 //调用系统read函数进行读内存操作
 ssize_t ret=read(file,strPpidCmdline,sizeof(strPpidCmdline));
 if(-1==ret) 
 {
    
     
 //读取内存数据失败
     LOGA("CheckApkParents read错误!\n");
     return -1; 
 }
 // 没找到返回0
 char sRet=strstr(strPpidCmdline,"zygote"); 
 if(NULL==sRet)
 {
    
     
 
 // 执行到这里,判定为调试状态 
     LOGA("父进程cmdline没有zygote子串!\n");
     return 0; 
 }

 return 1;
 }
 

Supongo que te gusta

Origin blog.csdn.net/c_kongfei/article/details/119378718
Recomendado
Clasificación