我的工作是Debian /Hurd与马赫。我被要求编写一个程序,给出一个PID和一个地址,对地址执行vm_read并打印结果。
这是我写的代码:
#include <mach_error.h>
#include <mach/mig_errors.h>
#include <mach/thread_status.h>
#include <mach/processor_info.h>
#include <mach/i386/vm_param.h>
#include <stdio.h>
#include <stdlib.h>
#include <hurd.h>
#include <string.h>
int main(int argc, char * argv[]) {
if(argc != 3) {
printf ("Wrong arguments: ./vm_read PID address\n");
exit(1);
}
int res;
mach_port_t target_task = pid2task(atoi(argv[1]));
vm_address_t addr = atoi(argv[2]);
vm_offset_t *data;
mach_msg_type_number_t data_count;
res = vm_read (target_task, addr, sizeof(int), &data, &data_count);
if (res != KERN_SUCCESS) {
printf ("Error reading virtual mem (0x%x), %s \n", res,
mach_error_string(res));
exit(1);
}
printf("done\n");
for (int i=0; i<data_count; ++i){
printf("byte %d : %x\n",i,((char*)data)[i]);
}
}它工作正常,但现在我被问到是否可以为Unix/Linux编写一个版本,为Windows编写另一个版本。
我一直在搜索,看起来应该不会有任何问题,因为两者都在过程中使用虚拟内存,但我不确定权限或其他任何东西是否会出现复杂的情况。
发布于 2021-03-20 20:19:02
对于Windows,如果需要从进程读取内存,则需要在获得进程句柄时请求PROCESS_VM_READ (ReadProcessMemory是适当的调用)。为了获得这个句柄,使用OpenProcess启动流程通常更容易。
发布于 2021-03-21 01:41:38
在UNIX上没有访问另一个进程内存的标准方法,但是在Linux上,可以通过读取特殊文件/proc/pid/mem来实现:
char memfile[32];
snprintf(memfile, sizeof(memfile), "/proc/%s/mem", argv[1]);
int mfd = open(memfile, O_RDONLY);
if (mfd < 0) {
perror("Can't open pid/mem file");
exit(1); }
if (lseek(mfd, (off_t)strtoull(argv[2], 0, 0), SEEK_SET) {
perror("Can't seek to address");
exit(1); }
if (read(mfd, &data, sizeof(data)) <= 0) {
fprintf(stderr, "No data at address %s\n", argv[2]);
exit(1); }https://stackoverflow.com/questions/66725565
复制相似问题