Lab-02 格式化字符串漏洞
FSB1
查看代码,printf(content);
处存在格式化字符串漏洞。
题目需要修改局部变量x的值,并给出了x的地址。
| void vuln_code(void)
{
printf("[BINGO]\n");
execve("/bin/sh", NULL, NULL);
}
void func()
{
char content[128];
long long x = 0xbeaf;
printf("address of x is: %p\n", &x);
memset(content, '\0', 128);
read(0, content, 128);
printf(content);
if (x != 0xbeaf) {
vuln_code();
}
}
int main()
{
init();
func();
return 0;
}
|
使用GDB调试文件,在printf(content);
处设置断点,查看栈上内容,发现content是第8个参数。

FSB2
查看代码,printf(buffer);
处为格式化字符串漏洞,且可以重复利用。
| int main()
{
int sz;
char buffer[BUFFER_SIZE];
while (1)
{
memset(buffer, '\0', BUFFER_SIZE);
read(0, buffer, BUFFER_SIZE);
printf(buffer);
}
return 0;
}
|
攻击思路为先通过GOT表得到printf
的地址,根据libc求出偏移量,然后得到system
的地址。第二次循环将printf
的GOT表指向的内容使用%n
修改为system
。第三次循环调用printf
时会导向system
函数,进而得到shell。