我已经研究这个代码几个小时了,但我没有想法。可能是我使用 read()
函数的方式,或者但程序的输出始终是相同的。
OUTPUT: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}
尾随一些垃圾字符
可能是我的编码中出现了一些愚蠢的错误,在今天之前已经将近两年没有用 c 语言编码了......
无论如何,如果有人能指出我做错了什么,我将不胜感激。
int attach(int start, int pid)
{
long ret;
if ( start == 1 )
{
ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
}else{
ret = ptrace (PTRACE_DETACH, pid, NULL, NULL);
}
if ( ret < 0 )
{
printf("Could not attach to pid\n"); exit(-1);
}
waitpid(pid, NULL, WUNTRACED);
return(ret);
}
int dump(int pid, unsigned long long addr, unsigned long long endaddr, char *outfile)
{
int output;
int mem_fd;
char mem_file_name[2048];
unsigned long long size = endaddr - addr;
void *buf=malloc(size);
memset(buf,0,size);
// attach
attach(1,pid);
// open the memory
sprintf(mem_file_name, "/proc/%d/mem", pid);
mem_fd = open(mem_file_name,O_RDONLY);
lseek(mem_fd,addr,SEEK_SET);
read(mem_fd,buf,size);
// Cleanup
attach(0,pid);
close(mem_fd);
free(buf);
}
编辑:我还通过 shell 脚本传递/proc/$pid/maps 中的地址...他们似乎正在计算正确的开始和结束地址。
ptrace
正在附加到进程并停止它..
EDIT2:还要检查它是否正在将正确的字节数读入缓冲区,它是......并报告它正在寻找正确的地址......这对于内存地址来说似乎确实是一个相当高的数字。 ...
EDIT3:这让我想到/proc/$pid/maps 中的十六进制地址是相反的顺序或在 4 字节标记处交换...系统字节序...
EDIT4:刚刚意识到地址只有 6 个字节长...但仍然没有让它工作
EDIT5:我有一个来自这些论坛的 python 程序,它可以工作,并且我重复了 c 中的步骤,但是我的程序失败了......检查了 python 程序,它正在读取与我的程序完全相同的地址....让我的程序对 python 调用运行并行调用,但仍然无法读取正确的内存...链接到 python 示例 here
最佳答案
感谢 Severin Pappadeux 指出我出错的地方,下面是最终的工作代码。
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <malloc.h>
#include <fcntl.h>
int dump(int pid, long long, long long, char *outfile);
int main(int argc, char *argv[])
{
if ( argc < 5 )
return(-1);
int pid=strtol(argv[1],NULL,10);
long long addr=strtoll(argv[2],NULL,16);
long long endaddr=strtoll(argv[3],NULL,16);
#if DEBUG
printf("Accessing pid memory %d from %llu to %llu\n",pid,addr,endaddr);
#endif
dump(pid,addr,endaddr,argv[4]);
return(0);
}
int attach(int start, int pid)
{
long ret;
if ( start == 1 )
{
ret = ptrace (PTRACE_ATTACH, pid, NULL, NULL);
}else{
ret = ptrace (PTRACE_DETACH, pid, NULL, NULL);
}
#if DEBUG
if ( ret < 0 )
{
printf("Could not attach to pid\n"); exit(-1);
}
#endif
waitpid(pid, NULL, WUNTRACED);
return(ret);
}
int dump(int pid, long long addr, long long endaddr, char *outfile)
{
int output;
int mem_fd;
char mem_file_name[2048];
long long size = endaddr - addr;
void *buf=malloc(size);
memset(buf,0,size);
long long readErr,writeErr,seekErr;
// attach
attach(1,pid);
// open the memory
sprintf(mem_file_name, "/proc/%d/mem", pid);
mem_fd = open64(mem_file_name,O_RDONLY);
output = open64(outfile,O_WRONLY|O_CREAT);
seekErr=lseek64(mem_fd,addr,SEEK_SET);
readErr=read(mem_fd,buf,size);
writeErr=write(output,buf,size);
#if DEBUG
printf("Allocated %lu usable bytes\n",malloc_usable_size(buf));
printf("File %s block size is %llu bytes\n",mem_file_name,size);
printf("Memory file value %d\n",mem_fd);
printf("Seeked %llu Bytes\n",seekErr);
printf("Read %llu Bytes\n",readErr);
printf("Wrote %llu Bytes\n",writeErr);
printf("\n\n");
#endif
// Cleanup
attach(0,pid);
close(mem_fd);
close(output);
free(buf);
}
关于c - 在c中使用ptrace从另一个进程读取一 block 内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28686517/