CTF/Samsung CTF 2017
[SCTF 2017] Coding - Buildingblocks(250 pts)
Choiys
2017. 7. 24. 11:46
Coding - Buildingblocks(250 pts)
문제를 확인해보면 별다른 설명은 없고 nc 주소만 나와있다.
해당 서버에 접속을 해보면 10개의 스테이지로 나뉘어진 문제가 나타나며, 아래의 리스트는 base64로 인코딩 된 x64 코드 블록이며, segmentation fault가 없도록 잘 조합하여 sha256으로 hash하여 보내라고 한다. 약 1분~2분정도의 timeout이 걸려있으며 이 시간 내에 코드 블록들을 조합하여 보내야한다.
#coding: utf-8 from pwn import * import hashlib import sys
def divide(t, idx, s, d, g): if t[idx] == '\xb8': a = 'mov eax,' b = '0x'+t[idx+1:idx+5][::-1].encode('hex') c = 5 s = int(b,16) elif t[idx] == '\x2d': a = 'sub eax,' b = '0x'+t[idx+1:idx+5][::-1].encode('hex') c = 5 s -= int(b,16) elif t[idx] == '\x05': a = 'add eax,' b = '0x'+t[idx+1:idx+5][::-1].encode('hex') c = 5 s += int(b,16) elif t[idx] == '\x74': a = 'je 0xf\nmov eax, 0x0\nmov eax,' b = 'DWORD PTR [eax]' c = 10 elif t[idx] == '\xf7': a = 'mul' b = 'edx' c = 2 s *= d elif t[idx] == '\xba': a = 'mov edx,' b = '0x'+t[idx+1:idx+5][::-1].encode('hex') c = 5 d = int(b,16) elif t[idx] == '\x3d': a = 'cmp eax,' b = '0x'+t[idx+1:idx+5][::-1].encode('hex') c = 5 g = int(b,16) elif t[idx] == '\x48': a = 'mov rax, 0x3c\nmov rdi,' b = '0x0\nsyscall' c = 16 s &= 0xffffffff return a, b, c, s, d, g
r = remote("buildingblocks.eatpwnnosleep.com", 46115) for q in range(10): print r.recvuntil(")\n") data = r.recvuntil("]") data = eval(data)
blocks = []
res = "" for d in data: blocks.append(base64.b64decode(d).encode('hex'))
#print blocks
sum_list = [] goal_list = [] start = 0
for b in blocks: block = b.decode('hex') idx = 0 d = 0 s = 0 g = 0 print "[*] {} block.".format(blocks.index(b)) if block[0] == '\xb8': start = blocks.index(b) while idx < len(block): inst, value, i, s, d, g = divide(block, idx, s, d, g) idx += i print inst, value sum_list.append(s) goal_list.append(g) print "[+] sum : {}\n".format(hex(s))
print sum_list print goal_list print start
cnt = 0 now = start res = [start,] while cnt < len(blocks)-1: next = goal_list.index(sum_list[now]) now = next cnt += 1 res.append(now)
key = "" for i in range(len(blocks)): key += blocks[res[i]].decode('hex')
key = hashlib.sha256(key).hexdigest() r.sendline(key)
print r.recvuntil("}") |
payload.py
위의 페이로드를 날려주면 아래와 같이 플래그를 얻어낼 수 있다.
FLAG : SCTF{45m_d1545m_f0r3v3r}