【问题标题】:Segmentation fault even when buffer is large enough?即使缓冲区足够大,也会出现分段错误?
【发布时间】:2021-10-04 10:36:26
【问题描述】:

我正在尝试测试缓冲区溢出,但即使缓冲区足够大,程序似乎也会崩溃,我不明白为什么

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

int bowfunc(char *string) {

    char buffer[1024];
    strcpy(buffer, string);
    return 1;
}

int main(int argc, char *argv[]) {

    bowfunc(argv[1]);
    printf("Done.\n");
    return 1;
}

bowfun() 正确调整rsp 通过在gdb 反汇编中减去0x410。构建为gcc -o exec1 -fno-stack-protector -z execstack -g exec1.c 运行为./exec1 $(python3 -c "print('\xaa' * 600)") 导致崩溃,实际崩溃似乎发生在(500 到 600 字节)之间。我看不到,为什么 gdb 返回这个错误

0x000055555555519e in bowfunc (string=0x7fffffffdcd4 'ª' <repeats 100 times>...) at exec1.c:10

xargs --show-limits &lt;/dev/null 给出的最大命令长度和参数大小似乎也没问题:

Your environment variables take up 4531 bytes
POSIX upper limit on argument length (this system): 2090573
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2086042
Size of command buffer we are actually using: 131072
Maximum parallelism (--max-procs must be no greater): 2147483647

【问题讨论】:

  • 字符串正在使用 UTF-8 编码打印,所以它是 1201 字节。
  • @Barmar 为什么当我告诉 python 以十六进制值打印数据时会发生这种情况 \xaa 是一个字节,而 a 是 UTF 编码

标签: c linux gdb buffer-overflow


【解决方案1】:

您的 Python 命令正在打印 1201 个字节。

$ python3 -c "print('\xaa' * 600)" | wc -c
    1201

'\xaa' 使用 UTF-8 编码打印为 2 个字节,并以换行符结尾。换行符被$(...) 删除,但argv[1] 仍然是1200 字节。

添加

printf("%d\n", strlen(argv[1]));

向程序确认这一点。

【讨论】:

  • 这对我来说很奇怪,因为 python2 会按预期打印数据,但 python3 似乎会导致这种变化
  • @Khaled:这——strbytes 之间的区别——是从 Python 2 到 Python 3 的最大变化之一。在 Python 3 中,标准流以文本形式打开 (@987654328 @) 模式而不是二进制 (bytes),因此您只能输出为用户字符集正确编码的数据,在我们的例子中恰好是 UTF-8。碰巧 UTF-8 中的 Unicode 代码点 U+00aa, ª 编码为两个字节,0xC2 0xAA。
猜你喜欢
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2021-01-02
  • 2012-07-06
  • 2011-09-03
  • 1970-01-01
相关资源
最近更新 更多