浅谈ARM上的Ptrace
//weibo: @少仲
0x0 前言
你想过怎么实现对系统调用的拦截吗?你尝试过通过改变系统调用的参数来愚弄你的系统kernel吗?你想过调试器是如何使运行中的进程暂停并且控制它吗?你可能会开始考虑怎么使用复杂的kernel编程来达到目的,那么,你错了.实际上Linux提供了一种优雅的机制来完成这些:ptrace系统函数ptrace提供了一种使父进程得以监视和控制其它进程的方式,它还能够改变子进程中的寄存器和内核映像,因而可以实现断点调试和系统调用的跟踪.使用ptrace,你可以在用户层拦截和修改系统调用(sys call).关于ptrace不做过多的赘述,<<玩转PTRACE>>写的已经十分详细,这篇文章重点是分析如何实现arm版的ptrace.
0x1 实现原理
(1).首先写一个简单的target程序,代码如下:
- //target.c
- #include <stdio.h>
- int flag = 1;
- int count = 0;
- int main(int argc,char* argv[])
- {
- while(flag)
- {
- printf("Target is running:%d\n", count);
- count++;
- sleep(3);
- }
- return 0;
- }
- 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的代码:
- orig_eax =ptrace(PTRACE_PEEKUSER,child,4*ORIG_EAX,NULL);
- printf("system call %ld\n",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代码如下:
- #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, ®s);
- printf("Target syscall no:%ld\n",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"
- void tracePro(int pid)
- {
- long scno=0;
- struct pt_regs regs;
- scno = getSysCallNo(pid, ®s);
- if (scno == __NR_write)
- {
- printf("hahaha...\n");
- }
- printf("Target syscall no:%ld\n",scno);
- }
参考文章:
<<玩转PTRACE>>
http://hi.baidu.com/harry_lime/item/cce5161e4af86d4a71d5e8ff