基于ARM的Ptrace

转:http://hi.baidu.com/harry_lime/item/cce5161e4af86d4a71d5e8ff

前面提到过打算研究一下基于ARM的Ptrace,并在Mobile上实现Hook. 今天程序调通了,记录如下.

平台:Android 2.3.3, 具体Linux Kernel和ARM的版本大家可以自己去查

目标:实现两个程序target和trace. target循环用printf打印语句,trace追踪target的系统调用并替换target的打印语句


在写程序之前查资料的过程中发现一个奇怪的事情,对于ARM ptrace研究实践的文章非常少,仅有的几篇也基本上都是胡言乱语,互相抄袭,根本无法测试通过。所以还是不要希望坐享其成,老老实实根据理论完成实践。


好,先从target开始. target还是相当简单的,写代码,下载ndk,交叉编译,上传到Android,运行,搞定. 以下是target代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
                                                                                                                             
int flag = 1;
int count = 0;
int main()
{
        char * str = "abcdef" ;
        while (flag)
        {
                printf ( "Target is running:%d\n" , count);
                count++;
                sleep(3);
        }
        return 0;
}

在Android上的运行情况:


rbserver@rbserver:~/ndk_test$ adb shell

# /data/harry/target

Target is running:0

Target is running:1

Target is running:2

Target is running:3


接下来是trace, 这个花了点时间, 主要是网上误导的资料太多, 轻信于人走了不少弯路。

第一步是要能成功attach并能捕获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
29
30
int main( int argc, char *argv[])
{
    if (argc != 2) {
        printf ( "Usage: %s <pid to be traced>\n" , argv[0], argv[1]);
        return 1;
    }
                                                                                                 
    pid_t traced_process;
    int status;
    traced_process = atoi (argv[1]);
    if (0 != ptrace(PTRACE_ATTACH, traced_process, NULL, NULL))
    {
        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的调用号。这一点ARM和x86有很大的不同。

先看x86原先的代码:

1
2
3
4
5
orig_eax = ptrace(PTRACE_PEEKUSER, pid, 4 * ORIG_EAX, NULL);
if (orig_eax == SYS_write)
{
...
}

这样做的原因是在x86架构上,Linux所有的系统调用都是通过软中断 int 0x80来实现的,而系统调用号是存在寄存器EAX中的. 所以如果想获取系统调用号,只需要获取ORIG_EAX的值就可以了。


而在ARM架构上呢,所有的系统调用都是通过SWI来实现的. 虽然也是软中断,但方式不同,因为在ARM 架构中有两个SWI指令,分别针对EABI和OABI (关于EABI和OABI 大家可以搜索相关资料,它们是Linux针对ARM架构的两种系统调用指令):

[EABI]

机器码:1110 1111 0000 0000 -- SWI 0

具体的调用号存放在寄存器r7中.


[OABI]

机器码:1101 1111 vvvv vvvv -- SWI immed_8

调用号进行转换以后得到指令中的立即数。立即数=调用号 | 0x900000


既然需要兼容两种方式的调用,我们在代码上就要分开处理。首先要获取SWI指令判断是EABI还是OABI,如果是EABI,可从r7中获取调用号。如果是OABI,则从SWI指令中获取立即数,反向计算出调用号。具体代码如下:

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
long getSysCallNo( int pid)
{
        long scno = 0;
        struct pt_regs regs;
        ptrace(PTRACE_GETREGS, pid, NULL, &regs);
        scno = ptrace(PTRACE_PEEKTEXT, pid, ( void *)(regs.ARM_pc - 4), NULL);
        if (scno == 0)
                return 0;
        /* Handle the EABI syscall convention.  We do not
           bother converting structures between the two
           ABIs, but basic functionality should work even
           if strace and the traced program have different
           ABIs.  */
        if (scno == 0xef000000) {
                scno = regs.ARM_r7;
        } else {
                if ((scno & 0x0ff00000) != 0x0f900000) {
                        return -1;
                }
                                                                                  
                /*
                 * Fixup the syscall number
                 */
                scno &= 0x000fffff;
        }
        return scno;
                                                                                  
}

完成了这一步以后我们就可以利用trace打印出target所有的系统调用了。运行结果如下:


从结果可以看出,target每调用一次printf,会引发两次__NR_write调用(调用号为4)。


接下来我们也照葫芦画瓢,翻转__NR_write的输入字符串,全部代码如下:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#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>
    
int long_size = sizeof ( long );
    
void reverse( char *str)
{  
    int i, j;
    char temp;
    for (i = 0, j = strlen (str) - 2; i <= j; ++i, --j) {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
    }
    
}
    
void getdata(pid_t pid, long addr,
        char *str, int len)
{  
    char *laddr;
    int i, j;
    union u {
        long val;
        char chars[long_size];
    }data;
    i = 0;
    j = len / long_size;
    laddr = str;
    while (i < j) {
        data.val = ptrace(PTRACE_PEEKDATA,
                pid, addr + i * 4,
                NULL);
        memcpy (laddr, data.chars, long_size);
        ++i;
        laddr += long_size;
    }
    j = len % long_size;
    if (j != 0) {
        data.val = ptrace(PTRACE_PEEKDATA,
                pid, addr + i * 4,
                NULL);
        memcpy (laddr, data.chars, j);
    }
    str[len] = '\0' ;
}
    
void putdata(pid_t pid, long addr,
        char *str, int len)
{  
    char *laddr;
    int i, j;
    union u {
        long val;
        char chars[long_size];
    }data;
    i = 0;
    j = len / long_size;
    laddr = str;
    while (i < j) {
        memcpy (data.chars, laddr, long_size);
        ptrace(PTRACE_POKEDATA, pid,
                addr + i * 4, data.val);
        ++i;
        laddr += long_size;
    }
    j = len % long_size;
    if (j != 0) {
        memcpy (data.chars, laddr, j);
        ptrace(PTRACE_POKEDATA, pid,
                addr + i * 4, data.val);
    }
}
    
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;
        }
    
        /*
         * Fixup the syscall number
         */
        scno &= 0x000fffff;
    }
    return scno;
    
}
    
void tracePro( int pid)
{
    long scno=0;
    long regV=0;
    struct pt_regs regs;
    char * str;
    
    scno = getSysCallNo(pid, &regs);
    if (scno == __NR_write)
    {
        str = ( char *) calloc (1, (regs.ARM_r2+1) * sizeof ( char ));
        getdata(pid, regs.ARM_r1, str, regs.ARM_r2);
        reverse(str);
        putdata(pid, regs.ARM_r1, str, regs.ARM_r2);
    
        printf ( "Reverse str.\n" );
    
    }
}
    
int main( int argc, char *argv[])
{  
    if (argc != 2) {
        printf ( "Usage: %s <pid to be traced>\n" , argv[0], argv[1]);
    return 1;
    }
        
    pid_t traced_process;
    int status;
    traced_process = atoi (argv[1]);
    if (0 != ptrace(PTRACE_ATTACH, traced_process, NULL, NULL))
    {
        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;
}

测试通过,输出如下:


好了,到目前为止我们在Linux+ARM的架构上实现了一个完整的ptrace hook应用,下一步考虑进行实战,hook系统的常驻进程,达到干预其它程序的效果。

 
基于ARM的Ptrace (二)
 

基于ARM的Ptrace (二)

上次在研究Ptrace for Android的时候漏了一个东西,如何hook并修改除了Syscall 以外的函数,今天顺便实现一下。

平台:Android 2.3.3

目标:利用Ptrace拦截进程的自定义函数并修改逻辑。


先看目标进程,代码相当简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
       
int flag = 1;
int count = 0;
       
int sub()
{
    printf ( "Sub call.\n" );
    return 1;
}
       
int main()
{  
    while (flag)
    {
        printf ( "Sub return:%d\n" , sub());
        count++;
        sleep(3);
    }
    return 0;
}

我们要做的是拦截自定义函数sub(),修改函数,跳过printf语句并把返回值改成2.

基本思路是利用Ptrace attach 以后找到函数代码段的入口点,修改相应的代码即可。如何找到函数入口点?静态看或者动态调都可以。我们代码简单,静态看就好了。


静态看的过程并不如想象的顺利,原因是IDA这货真心坑爹,解析Thumb和ARM的混合代码竟然会出错:



只好手动修改一下便于查看:


一目了然,0x84D0处开始返回指针压栈,我们只需要从0x84D2开始把代码改成如下就可以了:

1
2
MOVS R0, #2
POP {R3, PC}


因而得出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
66
67
#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>
  
int long_size = sizeof ( long );
  
void putdata(pid_t pid, long addr,
        char *str, int len)
{  
    char *laddr;
    int i, j;
    union u {
        long val;
        char chars[long_size];
    }data;
    i = 0;
    j = len / long_size;
    laddr = str;
    while (i < j) {
        memcpy (data.chars, laddr, long_size);
        ptrace(PTRACE_POKEDATA, pid,
                addr + i * 4, data.val);
        ++i;
        laddr += long_size;
    }
    j = len % long_size;
    if (j != 0) {
        memcpy (data.chars, laddr, j);
        ptrace(PTRACE_POKEDATA, pid,
                addr + i * 4, data.val);
    }
}
  
void tracePro( int pid)
{
    int len = 4;
    char insertcode[] = "\x02\x20\x08\xBD" ;
      
    putdata(pid, 0x84d2, insertcode, len);
}
  
int main( int argc, char *argv[])
{  
    if (argc != 2) {
        printf ( "Usage: %s <pid to be traced>\n" , argv[0], argv[1]);
    return 1;
    }
      
    pid_t traced_process;
    int status;
    traced_process = atoi (argv[1]);
    if (0 != ptrace(PTRACE_ATTACH, traced_process, NULL, NULL))
    {
        printf ( "Trace process failed:%d.\n" , errno );
    return 1;
    }
      
    tracePro(traced_process);
    ptrace(PTRACE_DETACH, traced_process, NULL, NULL);
    return 0;
}


上传至模拟器调试,一次成功:



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
shim ptrace是一个用于操作进程的系统调用接口。它允许一个进程跟踪、控制和检查另一个进程的执行情况。通过shim ptrace,一个进程可以检查另一个进程的寄存器、内存和系统调用,并有能力修改它们的执行过程。 shim ptrace通常用于调试、监视和诊断进程。调试器可以使用shim ptrace来实现断点、单步执行和修改变量等调试功能。另外,shim ptrace还可以用于分析和检查进程的运行,例如查找进程中的内存泄漏、跟踪系统调用和信号处理。 在使用shim ptrace时,一个进程可以作为被跟踪进程,另一个进程则作为跟踪进程。跟踪进程使用ptrace系统调用来发送指令,而被跟踪进程则接收并执行指令。通过这种方式,跟踪进程可以获取被跟踪进程的状态信息,并对其进行操作。 对于被跟踪进程,它会在指令执行之前接收到跟踪进程发送的指令,并根据指令的要求进行操作。例如,跟踪进程可以用ptrace(PTRACE_PEEKDATA, pid, addr, data)来读取被跟踪进程中地址为addr的内存数据,并将结果保存在data中。类似地,跟踪进程也可以使用ptrace(PTRACE_POKEDATA, pid, addr, data)来修改被跟踪进程的内存值。 总之,shim ptrace是一个强大的工具,允许进程间相互跟踪、控制和修改执行过程。它在调试、监视和诊断进程方面扮演着重要角色,为开发人员提供了有效的方法来分析和改进程序的执行。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值