【问题标题】:How to block syscall with ptrace on ARM?如何在 ARM 上使用 ptrace 阻止系统调用?
【发布时间】:2016-03-06 18:13:27
【问题描述】:

我已经使用了这个答案https://stackoverflow.com/a/12016223/2536878

并且正在开发 x86。

我已在 ARM 手机上尝试过此代码,但无法正常工作(系统调用错误)。

我猜 ORIG_EAX 对 ARM 无效,那么用什么值代替 ORIG_EAX?

现在我有了这段代码(适用于 x86):

#include <signal.h>
#include <syscall.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
        int i;
        pid_t child;
        int status;
        long orig_eax;
        int kill_ret = 0;
    char c[100];

        child = fork();

        if(child == 0)
        {
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execvp(argv[1], argv + 1);
        }
        else
        {
                i = 0;
                while(1)
                {
                        wait(&status);
                        if (WIFEXITED(status) || WIFSIGNALED(status) )
                                break;

                        orig_eax = ptrace(PTRACE_PEEKUSER, child, 4 * ORIG_EAX, NULL);
                        if (orig_eax == 2)
                        {
                                fprintf(stderr, "Got it\n");
                                kill_ret = kill(child, SIGKILL);
                                if (kill_ret == -1)
                                {
                                    fprintf(stderr, "Failed to kill ---> %s\n", strerror(errno));
                                }
                        }
                        printf("%d time, system call %ld\n", i++, orig_eax);
                        syscall_prompt: 
            printf("Allow syscall? Y/N: ");
            fflush(stdout);
            read(0, c, 100);
            if(c[0] != 'Y' && c[0] != 'N') {printf("\n"); goto syscall_prompt;}
            if(c[0] == 'N') { kill_ret = kill(child, SIGKILL); if(kill_ret < 0) { fprintf(stderr, "Cannot kill child\n"); } exit(1); }
            ptrace(PTRACE_SYSCALL, child, NULL, NULL);
                }
        }
    printf("return\n");
        return 0;
}

【问题讨论】:

    标签: c linux


    【解决方案1】:

    使用r7寄存器获取系统调用号,使用r0寄存器获取系统调用的返回值。

    For details refer here

    【讨论】:

    • 我仍然想知道(我认为是 OP),如何为 ptrace 系统调用找到它的常量?
    猜你喜欢
    • 2011-06-09
    • 2012-10-23
    • 2017-01-03
    • 2012-08-14
    • 1970-01-01
    • 2013-12-24
    • 2013-09-08
    • 2012-04-26
    • 1970-01-01
    相关资源
    最近更新 更多