2021-09-09 · 2분
FSB(Format String Bug)
格式化字符串漏洞入门:printf 缺少参数如何让你用 %p 和 %[n]$p 泄露栈、用 %n 破坏内存,printf 格式字符表,以及用 pwntools 的 fmtstr_payload 进行利用。
2021-09-09 · 2분
这篇文章发布已超过两年,内容可能已过时。
BUFFER OVERFLOW
是当能接收比所设缓冲区大小更大的输入时产生的漏洞。
int __cdecl main(int argc, const char **argv, const char **envp)
{
char s[40]; // [esp+4h] [ebp-34h] BYREF
int v5; // [esp+2Ch] [ebp-Ch]
v5 = 0x4030201;
fgets(s, 45, stdin);
printf("\n[buf]: %s\n", s);
printf("[check] %p\n", v5);
if ( v5 != 0x4030201 && v5 != 0xDEADBEEF )
puts("\nYou are on the right way!");
if ( v5 == 0xDEADBEEF )
{
puts("Yeah dude! You win!\nOpening your shell...");
system("/bin/dash");
puts("Shell closed! Bye.");
}
return 0;
}
我把 HackCTF 第 1 题的代码拿了过来。s 数组的大小是 40,但 fgets(s, 45, stdin); 接收 45 字节的缓冲区输入。BOF 就在这里触发。

第 1 栈
由于当前正往 s[40] 输入,且总共能输入 45,所以可以填满整个 s[40] 甚至改掉 v5。
如果那个 v5 变成 0xDEADBEEF,exploit 就成功了。
//gcc -fno-stack-protector -z execstack -no-pie -o bof bof.c
#include <stdio.h>
int main(void) {
char buf[40];
scanf("%s", buf);
printf("%s", buf);
}
这里有段更简单的代码。其实我们初学 C 语言时,会写成上面 scanf("%s", buf); 这样的代码。那种形式的代码非常危险。由于没有像 %40s 那样指定长度,就能覆盖 buf[40] + sfp 甚至覆盖返回地址。
这里 sfp 是 Stack Frame Pointer 的缩写,顾名思义指上层栈帧的基址指针。
在 32bit 占 4byte,64bit 占 8byte。如果 bof 长到能覆盖返回地址,就要写成 buffer + sfp[4 or 8] + return address。
到这里就是 bof。是非常简单的概念,感觉也没什么好解释的 ,;,ㅡ,;,
原文(韩语): tistory — 发布于 2021-09-09,已迁移至本博客。本翻译由 AI 协助完成。
…