繁体   English   中英

如何在ARM上使用ptrace阻止syscall?

[英]How to block syscall with ptrace on ARM?

我已经使用了这个答案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;
}

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

在此处输入图片说明

有关详细信息,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM