缓冲区溢出;避免溢出攻击

问题描述 投票:0回答:3

我正在看这个类的代码示例,我是缓冲区溢出的新手。如何修改示例以避免缓冲区溢出攻击?另外,如果有人知道关于缓冲区溢出的好文章,请发表。谢谢!

void GetProfileFor( const char *name,
              char *profile,
              int profileLen );

int main() {
              char *profile = malloc( 1024 );
              char name[128];

              printf( “Enter your name: ” );
              gets( name );

              GetProfileFor( name, profile, 1024 );
              printf( “\nYour profile: %s\n”, profile );
return 0; }
c buffer buffer-overflow
3个回答
0
投票

要确定缓冲区溢出发生的位置,您必须确定所有输入路径及其填充的缓冲区-内部缓冲区是否足以满足所有可能的输入?或对允许的输入量有任何限制吗?

在您的情况下,gets(name)在内部缓冲区中有一个限制,但是gets()本身在它可以接受的输入中没有限制:

http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm

因此可能发生缓冲区溢出。

防止这种攻击的特定解决方案是使用fgets():

http://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

确实限制了所允许的外部输入。


0
投票

仅查看您发布的代码,我发现您可以更改一行代码。

替换

gets(name);

with

fgets(name, 128, stdin);

gets不会检查name的大小来决定何时停止读取。它将尝试读取比name更大的字符。另一方面,fgets遇到换行符或已读取127字符(以先到者为准)时,将停止读取。

结帐Why gets() is bad / Buffer Overflows了解更多详细信息。


0
投票

[最新的编译器正在添加堆栈保护程序,例如。金丝雀到二进制。您仍然可以溢出缓冲区,但是不能跳转到堆栈并执行shellcode等。您可以蛮力地使用金丝雀,但这可能需要很长时间。

© www.soinside.com 2019 - 2024. All rights reserved.