我是靠谱客的博主 开朗哈密瓜,这篇文章主要介绍浅谈ARM上的Ptrace,现在分享给大家,希望可以做个参考。

 

 

0x0 前言

 

 

你想过怎么实现对系统调用的拦截吗?你尝试过通过改变系统调用的参数来愚弄你的系统kernel吗?你想过调试器是如何使运行中的进程暂停并且控制它吗?你可能会开始考虑怎么使用复杂的kernel编程来达到目的,那么,你错了.实际上Linux提供了一种优雅的机制来完成这些:ptrace系统函数
ptrace提供了一种使父进程得以监视和控制其它进程的方式,它还能够改变子进程中的寄存器和内核映像,因而可以实现断点调试和系统调用的跟踪.使用ptrace,你可以在用户层拦截和修改系统调用(sys call).关于ptrace不做过多的赘述,<<玩转PTRACE>>写的已经十分详细,这篇文章重点是分析如何实现arm版的ptrace.

 

 

0x1 实现原理

(1).首先写一个简单的target程序,代码如下:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//target.c #include <stdio.h> int flag = 1; int count = 0; int main(int argc,char* argv[]) { while(flag) { printf("Target is running:%dn", count); count++; sleep(3); } return 0; }

(2).接下来写trace程序来捕获syscall

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main(int argc, char *argv[]) { if(argc != 2) { printf("please input pid...n"); return 1; } pid_t traced_process; int status; traced_process = atoi(argv[1]); if( ptrace(PTRACE_ATTACH, traced_process, NULL, NULL) != 0) { printf("Trace process failed:%d.n", errno); return 1; } while(1) { wait(&status); if(WIFEXITED(status)) { break; } tracePro(traced_process); ptrace(PTRACE_SYSCALL, traced_process, NULL, NULL); } ptrace(PTRACE_DETACH, traced_process, NULL, NULL); return 0; }


这部分代码和x86基本没有区别.因为没有设计到寄存器和中断这些架构问题.下面就是要考虑如何获取syscall的调用号.
先看x86的代码:

 

 

复制代码
1
2
orig_eax =ptrace(PTRACE_PEEKUSER,child,4*ORIG_EAX,NULL); printf("system call %ldn",orig_eax);


在x86架构上,Linux的系统调用都是通过int 0x80软中断实现,而触发软中断前会把调用号装入eax寄存器.这样系统调用处理程序一旦运行,就可以从eax中得到数据.由于系统调用表中的表项是以32位(4字节)类型存放的,所以内核需要将给定的系统调用号乘以4,然后用所得到的结果在该表中查询器位置.

 

 

而ARM架构上,也是通过SWI软中断实现,但是方式不同,因为有两个指令,EABI和OABI.

 

 

关于两者的区别请参考 http://bbs.chinaunix.net/thread-1950213-1-1.html 说的十分详细

 

[EABI]
机器码:1110 1111 0000 0000 -- SWI 0
具体的调用号存放在寄存器r7中.

[OABI]
机器码:1101 1111 vvvv vvvv -- SWI immed_8
调用号进行转换以后得到指令中的立即数.立即数=调用号 | 0x900000

所以在获取syscall调用号的时候要做区分,要获取SWI指令判断是EABI还是OABI,如果是EABI就直接在r7中获取调用号,如果是OABI则需要获取立即数然后反向计算.

完整的trace代码如下:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <sys/ptrace.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <sys/syscall.h> long getSysCallNo(int pid, struct pt_regs *regs) { long scno = 0; ptrace(PTRACE_GETREGS, pid, NULL, regs); scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs->ARM_pc - 4), NULL); if(scno == 0) return 0; if (scno == 0xef000000) { scno = regs->ARM_r7; } else { if ((scno & 0x0ff00000) != 0x0f900000) { return -1; } scno &= 0x000fffff; } return scno; } void tracePro(int pid) { long scno=0; struct pt_regs regs; scno = getSysCallNo(pid, &regs); printf("Target syscall no:%ldn",scno); } int main(int argc, char *argv[]) { if(argc != 2) { printf("please input pid...n"); return 1; } pid_t traced_process; int status; traced_process = atoi(argv[1]); if( ptrace(PTRACE_ATTACH, traced_process, NULL, NULL) != 0) { printf("Trace process failed:%d.n", errno); return 1; } while(1) { wait(&status); if(WIFEXITED(status)) { break; } tracePro(traced_process); ptrace(PTRACE_SYSCALL, traced_process, NULL, NULL); } ptrace(PTRACE_DETACH, traced_process, NULL, NULL); return 0; }

运行结果如图

 

从结果上得出,每次printf都会引发两次__NR_write调用,每次sleep都会调用两次__NR_clock_nanosleep.

 

0x2 具体实例

在__NR_write上动手,添加一个"hahaha"

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
void tracePro(int pid) { long scno=0; struct pt_regs regs; scno = getSysCallNo(pid, &regs); if (scno == __NR_write) { printf("hahaha...n"); } printf("Target syscall no:%ldn",scno); }

结果如下:

 



参考文章:
<<玩转PTRACE>>
http://hi.baidu.com/harry_lime/item/cce5161e4af86d4a71d5e8ff

 

 

最后

以上就是开朗哈密瓜最近收集整理的关于浅谈ARM上的Ptrace的全部内容,更多相关浅谈ARM上内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部