我目前正在尝试为我的一项任务找出buffer overflow
,我正在关注本教程(https://dhavalkapil.com/blogs/Buffer-Overflow-Exploit/)
Source of code:
#include <stdio.h>
void secretFunction()
{
printf("Congratulations!\n");
printf("You have entered in the secret function!\n");
}
void echo()
{
char buffer[20];
printf("Enter some text:\n");
scanf("%s", buffer);
printf("You entered: %s\n", buffer);
}
int main()
{
echo();
return 0;
}
但是,我在导出有效载荷方面遇到困难。
本教程显示:
推论:
The address of secretFunction is 0804849d in hex.
^对于这一部分,我了解它是如何派生的
80484c0: 83 ec 38 sub $0x38,%esp
38 in hex or 56 in decimal bytes are reserved for the local variables of echo function.
^对于这一部分,我假设我减去该函数的第一个地址和最后一个地址,以推断该函数保留了多少个十进制字节
80484cf: 8d 45 e4 lea -0x1c(%ebp),%eax
The address of buffer starts 1c in hex or 28 in decimal bytes before %ebp.
This means that 28 bytes are reserved for buffer even though we asked for 20 bytes.
^这是我不知道如何获取保留字节的地方。
有人可以启发我吗?他从哪里得到28个字节?
短版:(GCC)编译器很可能在局部变量之后添加了一些填充,以提供对缓冲区溢出和已知漏洞的最小保护。
长期版本:
有多个因素会影响堆栈上数据的分配:
main
函数将初始SP强制为16字节边界(8的倍数)。底线,因为我们不知道程序是如何编译的(优化,命令行选项等),所以我们也不知道为什么要分配额外的空间-我们只能尝试解释一下。在任何情况下,它都没有专门分配给'缓冲区'使用。我认为一个更好的说法是说“缓冲区变量在BP下方的堆栈上放置了28个字节,在“缓冲区”和BP之间增加了8个字节。
更多详细信息,请参见:https://en.wikipedia.org/wiki/Buffer_overflow_protection尤其是关于gcc'-fstack-protector-strong'的部分(在许多发行版中,它似乎对于新版gcc会自动打开)。
还请参阅secretFunction,它在堆栈上分配了24(0x18)个字节,但仅使用8个字节。