[C] 纯文本查看 复制代码
#include <asm/page.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ptrace.h>
#include <errno.h>
#include <stdlib.h>
#define TRACE(o,p) printf(o,p)
void change_process_mem(int pid ,long offset,long value)
{
ptrace(PTRACE_ATTACH, pid, NULL, NULL);
waitpid(pid, NULL, 0);
ptrace(PTRACE_POKEDATA, pid,(void *)offset, (void *)value);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
waitpid(pid, NULL, 0);
}
void process_mem_snapshot(int pid,long offset,long size,char* memfilename)
{
long i=0;
FILE* out;
ptrace(PTRACE_ATTACH, pid, NULL, NULL);
waitpid(pid, NULL, 0);
out = fopen(memfilename,"wb+");
for(i=offset;i<offset+size;i+=4)
{
long r = ptrace(PTRACE_PEEKDATA, pid,(void *)i, NULL);
fwrite((char*)&r,4,1,out);
}
fclose(out);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
waitpid(pid, NULL, 0);
}
int printf_help(int argc, char *argv[])
{
if ((argc<5)||(argv[1][0]=='s'&&argv[1][1]==0&&argc<6))
{
printf("\r\n\r\n"
"Usage:\ttt <sm> pid offset size <FILE>\r\n\r\n"
"\ttt s 1000 10000 1222 \"/sdcard/temp\"\r\n"
"\ttt m 1000 20000 1234\r\n");
return 0;
}
return 1;
}
int main_s(int argc, char *argv[])
{
int pid;
long offset;
long size;
char* memfilename;
pid = atoi(argv[2]);
offset = atol(argv[3]);
size = atol(argv[4]);
memfilename = argv[5];
process_mem_snapshot(pid,offset, size,memfilename);
return 0;
}
int main_m(int argc, char *argv[])
{
int pid;
long offset;
long value;
pid = atoi(argv[2]);
offset = atol(argv[3]);
value = atol(argv[4]);
change_process_mem(pid , offset, value);
return 0;
}
void main(int argc, char *argv[])
{
int pid = 0;
if (!printf_help(argc,argv))
{
return;
}
if (argv[1][0]=='s'&&argv[1][1]==0)
{
main_s(argc,argv);
return;
}
if (argv[1][0]=='m'&&argv[1][1]==0)
{
main_m(argc,argv);
return;
}
return ;
}