android反调试源码实现

公众号:小道安全
• 阅读 1685

反调试的方法很多,不过由于android系统是开源的,所以反调试其实也不是很神秘的东西。 下面是常见的也是很多厂商都在使用,包括我们项目组也在使用的。多个方案相互结合可以实现更好反调试。

1.1 ptrace自己,使得android_server附加不上

void anti_ptrace()
{
ptrace(PTRACE_TRACEME, 0, 0, 0);
}

1.2. 检测Tracepid的值

void anti_Tracepid()
{
try
{
const int bufsize = 1024;
char filename[bufsize];
char line[bufsize];
int pid = getpid();
sprintf(filename, “/proc/%d/status”, pid);
FILE* fd = fopen(filename, “r”);
if (fd !=NULL)
{
while (fgets(line, bufsize, fd))
{
if (strncmp(line, “TracerPid”, 9) == 0)
{
int statue = atoi(&line[10]);
if (statue != 0)
{
fclose(fd);
int ret = kill(pid, SIGKILL);
}
break;
}
}
fclose(fd);
} else
{
// LOGD(“open %s fail…”, filename);
}
} catch (…)
{

}

}

1.3 检测端口号,针对android_server这个端口号

void anti_serverport() {
const int bufsize=512;
char filename[bufsize];
char line[bufsize];
int pid =getpid();
sprintf(filename,"/proc/net/tcp");
FILE* fd=fopen(filename,“r”);
if(fd!=NULL){
while(fgets(line,bufsize,fd)){
if (strncmp(line, “5D8A”, 4)==0){
int ret = kill(pid, SIGKILL);
}
}
}
fclose(fd);

}

1.4 检测这些调试进程的名字

void anti_processstatus(){
const int bufsize = 1024;
char filename[bufsize];
char line[bufsize];
char name[bufsize];
char nameline[bufsize];
int pid = getpid();
//先读取Tracepid的值
sprintf(filename, “/proc/%d/status”, pid);
FILE *fd=fopen(filename,“r”);
if(fd!=NULL){
while(fgets(line,bufsize,fd)){
if(strstr(line,“TracerPid”)!=NULL)
{
int statue =atoi(&line[10]);
if(statue!=0){
sprintf(name,"/proc/%d/cmdline",statue);
FILE *fdname=fopen(name,“r”);
if(fdname!= NULL){
while(fgets(nameline,bufsize,fdname)){
if(strstr(nameline,“android_server”)!=NULL){
int ret=kill(pid,SIGKILL);
}
}
}
fclose(fdname);
}
}
}
}
fclose(fd);
}

1.5. 检测常放目录:/data/local/tmp

void anti_localtmp(){
int pid=getpid();
const int bufsize=1024;
char line[bufsize];
char filename[bufsize];
sprintf(filename,"/data/local/tmp");
FILE *fd=fopen(filename,“r”);
if(fd!=NULL){
while(fgets(line,bufsize,fd)){
if(strstr(line,“android_server”)!=NULL){
int ret=kill(pid,SIGKILL);
}
}
}
fclose(fd);
}

1.6 检测break point指令

unsigned long GetLibAddr() {
unsigned long ret = 0;
char name[] = “libptrace.so”;
char buf[4096], *temp;
int pid;
FILE *fp;
pid = getpid();
sprintf(buf, “/proc/%d/maps”, pid);
fp = fopen(buf, “r”);
if (fp == NULL) {
puts(“open failed”);
goto _error;
}
while (fgets(buf, sizeof(buf), fp)) {
if (strstr(buf, name)) {
temp = strtok(buf, “-”);
ret = strtoul(temp, NULL, 16);
break;
}
}
_error: fclose(fp);
return ret;
}

1.7 通过使用Linux inotify特性来对文件的读写,以及打开等权限进行监控

void anti_debug06() {
int ret, len, i;
int pid6 = getpid();
const int MAXLEN = 2048;
char buf[1024];
char readbuf[MAXLEN];
int fd, wd;
fd_set readfds;
fd = inotify_init();
sprintf(buf, “/proc/%d/maps”, pid6);
wd = inotify_add_watch(fd, buf, IN_ALL_EVENTS);
if(wd>=0){
while (1) {
i = 0;
FD_ZERO(&readfds);//使得readfds清零
FD_SET(fd, &readfds);//将fd加入readfds集合
ret = select(fd + 1, &readfds, 0, 0, 0);
if(ret==-1){
break;
}
if (ret) {
len = read(fd, readbuf, MAXLEN);
while (i < len) {
struct inotify_event *event = (struct inotify_event *) &readbuf[i];
if ((event->mask & IN_ACCESS) || (event->mask & IN_OPEN)) {
int ret = kill(pid6, SIGKILL);
return;
}
i += sizeof(struct inotify_event) + event->len;
}
}

}
}
inotify_rm_watch(fd, wd);
close(fd);

}

1.8.检测被调试代码的前后时间的差异;

int gettimeofday(struct timeval *tv,struct timezone *tz);
void anti_debug07(){
int pid=getpid();
struct timeval t1;
struct timeval t2;
struct timezone tz;
gettimeofday(&t1,&tz);
gettimeofday(&t2,&tz);
// int timeoff=gettimeofday(&t1,0)-gettimeofday(&t2,0);
int timeoff=(t2.tv_sec)-(t1.tv_sec);
if(timeoff>1){
int ret=kill(pid,SIGKILL);
return ;
}
}

更多安全技术文章,请关注 “小道安全”公众号,一起交流,一起进步。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Stella981 Stella981
2年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Stella981 Stella981
2年前
Linux查看GPU信息和使用情况
1、Linux查看显卡信息:lspci|grepivga2、使用nvidiaGPU可以:lspci|grepinvidia!(https://oscimg.oschina.net/oscnet/36e7c7382fa9fe49068e7e5f8825bc67a17.png)前边的序号"00:0f.0"是显卡的代
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Nginx反向代理upstream模块介绍
!(https://oscimg.oschina.net/oscnet/1e67c46e359a4d6c8f36b590a372961f.gif)!(https://oscimg.oschina.net/oscnet/819eda5e7de54c23b54b04cfc00d3206.jpg)1.Nginx反
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
公众号:小道安全
公众号:小道安全
Lv1
擅长各种安全开发逆向破解黑客技术,欢迎交流技术。
文章
8
粉丝
0
获赞
1