linux内存替换,[Ptrace]Linux内存替换(二)信息获取

对ptrace有了初步了解后,到网上找了一个简单例程熟悉一下,实现效果为B程序附加到A程序上获取A程序EIP等相关信息后输出。

【环境】

CentOS 6.4 RC

Linux version 2.6.32-358.el6.i686

Gcc version 4.4.7 20120313

【A程序:counter.c】

#include

int main()

{

int i;

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

printf("My counter: %d \n", i);

sleep(1);

}

return 0;

}

gcc -o counter counter.c

【B程序:attach.c】

#include

#include

#include

#include

#include

int main(int argc, char *argv[])

{

pid_t traced_process;

struct user_regs_struct regs;

long ins;

if(argc != 2) {

printf("PID?");

return 1;

}

traced_process = atoi(argv[1]);

ptrace(PTRACE_ATTACH, traced_process,

NULL, NULL);

wait(NULL);

ptrace(PTRACE_GETREGS, traced_process,

NULL, &regs);

ins = ptrace(PTRACE_PEEKTEXT,

traced_process, regs.eip, NULL);

printf("EIP: %lx Instruction executed: %lx ", regs.eip, ins);

ptrace(PTRACE_DETACH, traced_process,

NULL, NULL);

return 0;

}

gcc -o attach attach.c

【执行】

1. run counter

./counter

2. find pid of counter

ps aux | grep counter

3. run attach

./attach %pid%

【结果】

EIP:717424 Instruction executed: c3595a5d

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值