【问题标题】:Bypass memory corruption limitations绕过内存损坏限制
【发布时间】:2014-01-30 17:54:56
【问题描述】:

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

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

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

【问题讨论】:

  • buffer 是如何定义的?
  • 你为什么不使用 strncpy 和一个基于 buffer 大小的参数而不是你有的 if 语句。

标签: c security audit


【解决方案1】:

一个明显的答案是,如果buffer 至少不是 101 chars 长,一种特殊情况是程序员忘记了空终止符也被复制(如果buffer 正好是 100 chars 长)。我能想到还有两个更微妙的攻击向量:

  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;

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多