linux - 为什么 seccomp 禁止我正常的系统调用

标签 linux nasm shellcode seccomp

这是 pwnable.kr 中的最新问题,asm.c 使用 seccomp 来限制我的系统调用,除了:write()、open()、read() 和 exit()。

asm.c:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <seccomp.h>
#include <sys/prctl.h>
#include <fcntl.h>

#define LENGTH 128

void sandbox(){
    scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
    if (ctx == NULL) {
        printf("seccomp error\n");
        exit(0);
    }

    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit), 0);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);

    if (seccomp_load(ctx) < 0){
        seccomp_release(ctx);
        printf("seccomp error\n");
        exit(0);
    }
    seccomp_release(ctx);
}

char stub[] = "\x48\x31\xc0\x48\x31\xdb\x48\x31\xc9\x48\x31\xd2\x48\x31\xf6\x48\x31\xff\x48\x31\xed\x4d\x31\xc0\x4d\x31\xc9\x4d\x31\xd2\x4d\x31\xdb\x4d\x31\xe4\x4d\x31\xed\x4d\x31\xf6\x4d\x31\xff";
unsigned char filter[256];
int main(int argc, char* argv[]){

    setvbuf(stdout, 0, _IONBF, 0);
    setvbuf(stdin, 0, _IOLBF, 0);

    printf("Welcome to shellcoding practice challenge.\n");
    printf("In this challenge, you can run your x64 shellcode under SECCOMP sandbox.\n");
    printf("Try to make shellcode that spits flag using open()/read()/write() systemcalls only.\n");
    printf("If this does not challenge you. you should play 'asg' challenge :)\n");

    char* sh = (char*)mmap(0x41414000, 0x1000, 7, MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0);
    memset(sh, 0x90, 0x1000);
    memcpy(sh, stub, strlen(stub));

    int offset = sizeof(stub);
    printf("give me your x64 shellcode: ");
    read(0, sh+offset, 1000);

    alarm(10);
    sandbox();
    ((void (*)(void))sh)();
    return 0;
}

the flag file named this_is_pwnable.kr_flag_file_please_read_this_file.sorry_the_file_name_is_very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000000000000ooooooooooooooooooooooo000000000000o0o0o0o0o0o0ong

所以我只使用打开、读取和写入来制作一个程序并且它可以工作(我自己制作了一个标志文件并且程序成功地打印了它),但是当我将 shellcode 放入 ./asm 时,它通常说 错误的系统调用。我不知道我在哪里犯了错误?

这是我的代码:

[SECTION .text]

global _start

_start:

    xor eax, eax
    xor ebx, ebx
    xor edx, edx
    xor ecx, ecx
    xor ebp, ebp

    mov eax, 5  ;open file
    mov ebx, file_name
    mov ecx, 0
    mov edx, 0777
    int 0x80

    mov [fd_in], eax

    xor eax, eax  ;read from file
    mov eax, 3
    mov ebx, [fd_in]
    mov ecx, info
    mov edx, 26
    int 0x80

    mov eax, 4  ;write flag
    mov ebx, 1
    mov ecx, info
    mov edx, 26
    int 0x80

    mov al, 1  ;exit
    int 0x80


[SECTION .data]
    file_name db 'this_is_pwnable.kr_flag_file_please_read_this_file.sorry_the_file_name_is_very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000000000000ooooooooooooooooooooooo000000000000o0o0o0o0o0o0ong'

[SECTION .bss]
    fd_in resb 1
    info resb  26

谢谢

最佳答案

这是一个 64 位的二进制文件,而你的 shellcode 是 32 位的。

关于linux - 为什么 seccomp 禁止我正常的系统调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864841/

相关文章:

linux - Ubuntu 14.04 预置 LVM 磁盘配置

r - 如何在 Ubuntu 18.04 上安装 Rmpfr?

c - X86-64 NASM 调用 extern c 函数

c - 打印两次 NASM

linux - 从 shellcode 运行文件

delphi - 如何获取编译时已知的汇编指令的机器代码?

c++ - 为太空模拟器/游戏旋转 spaceship 模型

linux - ssh主机名和ssh[user@]主机名之间的区别

assembly - GNU 程序集引导加载程序无法引导

c - 将 shellcode 声明为 char[] 数组和 char* 之间的区别?