gpt4 book ai didi

c - 绕过内存损坏限制

转载 作者:太空狗 更新时间:2023-10-29 16:12:43 24 4
gpt4 key购买 nike

假设你是安全代码审计人员并且你已经陷入了一些代码如下:

if(strlen(data) < 100) {
strcpy(buffer, data);
}

为了破坏缓冲区,你会怎么做?那可能吗?如果是这样,如何?为什么不使用该条件来确保代码安全?

最佳答案

一个明显的答案是如果 buffer 不是至少 101 chars 长,一个特殊的情况是程序员忘记了空终止符也被复制(如果 buffer 正好是 100 chars 长)。我可以从脑海中看到两个更微妙的攻击 vector :

  1. data 可能与不可读内存相邻并且不包含空终止符。这会导致段错误或访问冲突,但不会直接导致内存损坏。

  2. databuffer 在被视为字符串时可能会重叠。在这种情况下,行为是未定义的。

以第二种攻击为例,拿下面的代码:

#include <stdio.h>
#include <string.h>

int main(void)
{
char someImportantString[] = "Something that should not be overwritten";
char buffer[101] = "\0goodbye cruel world";
char data[16] = {'h', 'e', 'l', 'l', 'o',' ','w','o','r','l','d',
'x','x','x','x','x'};

if(strlen(data) < 100)
{
printf("Probably not good\n");
strcpy(buffer, data);
}

return 0;

}

这样做的可能结果是覆盖大量内存,然后出现段错误。

关于c - 绕过内存损坏限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com