简要的理解了一下安卓跨进程读取并修改内存的几种方法
- 通过ptrace这个linux/unix系统提供的系统调用
- 通过/proc虚拟文件系统
- 通过syscall系统调用
#include<stdio.h>
#include<sys/ptrace.h>
#include<unistd.h>
int main()
{
printf("ptrace\n");
int pid=2853;
long address=0xA301550C;
printf("please input pid=>");
scanf("%d",&pid);
printf("please input address=>");
scanf("%lx",&address);
long ret=ptrace(PTRACE_ATTACH,pid,NULL,NULL);
printf("PTRACE_ATTACH:%d\n",ret);
int data=0;
printf("please input new data=>");
scanf("%d",&data);
ptrace(PTRACE_POKEDATA,pid,address,data);
sleep(1);
ret=ptrace(PTRACE_PEEKDATA,pid,address,NULL);
printf("PTRACE_PEEKDATA:%d\n",ret);
ret=ptrace(PTRACE_DETACH,pid,NULL,NULL);
printf("PTRACE_DETACH:%d\n",ret);
printf("\n");
return 0;
}
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdint.h>
int main()
{
printf("proc\n");
char* mem_path="/proc/2864/mem";
int fd =open(mem_path,O_RDWR);
uintptr_t address=0xB0D19E14;
int res;
lseek(fd,address,SEEK_SET);
read(fd,&res,sizeof(int));
printf("%lx read int %d\n",address,res);
lseek(fd,address,SEEK_SET);
int buf[1]={999};
write(fd,buf,sizeof(int));
printf("%lx write int %d\n",address,res);
close(fd);
return 0;
}
- 因为还没有摸清门道,所以地址和pid就写死了,有望大佬指点
- 个人暂时感觉proc最为好用,syscall并不了解(看到代码量有点大,还得提供结构体,直接劝退)
- ptrace在使用时selinux老是搞鬼,得在root权限下暂时关闭,即su setenforce 0(1是开)
- 还有一个注意的点是在使用ptrace调用读取内存时,如果你刚用ce抓完数据一定先把ceserver先关闭,不然进程附加不上怎么试都返回-1(失败)
- 为了保证很高的改写度,我只保留了核心的代码,现在看起来可能很烂,所以大佬勿喷。