linux ptrace 读内存,c - 使用ptrace读取字符串(Linux) - 堆栈内存溢出

我是刚接触栈的人,而对C来说是相对陌生的。我尝试使用ptrace从另一个进程读取进程内存。 到目前为止,我设法从另一个过程中读取和更改数字。 但是用字符串我找不到办法。 这是我的代码:

int errno;

//the string we want to read is 8 bytes long

long len = 8;

const int buflen = (len / sizeof(long)) + 1;

void *buffer;

long *base;

char *data;

int i;

//the process pid i want to track

pid_t pid = 2984;

//the address that the string we want to read resides

void *addr = (unsigned long int*) 0x7ffe03f6e088 ;

buffer = calloc(buflen, sizeof(long));

if (NULL == buffer)

{

perror("Fault at allocation: ");

}

base = (long *) buffer;

ptrace(PTRACE_ATTACH, pid, NULL, NULL);

for (i = 0; i < buflen; i++) {

if(ptrace(PTRACE_PEEKDATA, pid , addr + (sizeof(long) * i),NULL) != -1)

{

*(base + i) = ptrace(PTRACE_PEEKDATA, pid , addr + (sizeof(long) * i),

NULL);

}else

{

fprintf(stderr, "Value of errno: %s\n", strerror(errno));

}

}

ptrace(PTRACE_DETACH, pid, NULL, NULL);

/* Pop a null at the end, since we're printing this string. */

*((char *) buffer + len) = '\0';

data = (char *)buffer;

printf("%p[\"%s\"]\n",addr, data);

free(buffer);

并输出:

errno的值:没有这样的过程

errno的值:没有这样的过程

0x7ffe03f6e088 [“”]

但是,进程ID是正确的,并且当addr指向数字时,类似的代码也可以工作。 请帮我。

[编辑]

贝娄是示踪过程的代码:

char *a = "lala";

pid_t child;

printf("Char length: %ld\n", strlen(a));

printf("Char value: %s\n", a);

//the address I use to read variable

printf("Char address: %p\n", &a);

//make the process sleep so I can halt it manually

sleep(3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值