[Ptrace]Linux内存替换(二)信息获取
2015-07-30 10:55
351 查看
对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】
gcc -o counter counter.c
【B程序:attach.c】
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
【参考】
http://www.cnblogs.com/wangkangluo1/archive/2012/06/05/2535484.html
【环境】
CentOS 6.4 RC
Linux version 2.6.32-358.el6.i686
Gcc version 4.4.7 20120313
【A程序:counter.c】
#include <stdio.h> 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 <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/user.h> #include <stdio.h> 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, ®s); 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
【参考】
http://www.cnblogs.com/wangkangluo1/archive/2012/06/05/2535484.html
相关文章推荐
- CentOS7 + owncloud8.1.0 搭建企业私有云(基础服务)
- Android5.0——Linux内核存储协议栈图
- linux环境下cc和gcc工具
- centos 7 安装开源数据统计piwik
- Linux下C语言修改进程名称的方法
- CentOS 6.3下Samba服务器的安装与配置
- centos 安装jdk
- linux CentOS安装telnet
- 解决运行service iptables * 这类的命令报“ iptables unrecognized service”的错误
- Linux下查看CPU核数
- Linux下查看CPU核数
- Linux 查看系统硬件信息(实例详解)
- 如何在Linux下使用Markdown进行文档工作
- 只是为了好玩——Linux之父林纳斯自传
- Centos7.1 安装Sqlite3
- linux awk命令详解
- linux记录 ---- 添加开机启动运行脚本
- Linux命令详解 -- alias
- Linux下比较文件夹大小
- windows和centos下安装ActiveMQ