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;
}使用以下命令编译:
从栈中读取
stack_password 将被存储在栈中,因为它是一个局部变量,所以只需滥用 printf 来显示栈的内容就足够了。这是一个利用 BF 前 100 个位置来从栈中泄露密码的漏洞利用:
在图像中,我们可以看到我们可以从堆栈中的第10个位置泄漏密码:


使用%p而不是%s运行相同的利用程序,可以在%5$p处从堆栈中泄漏堆地址:



泄漏地址和密码地址之间的差异为:
最后更新于