gpt4 book ai didi

c - x86_64 上 C 中的溢出缓冲区调用函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:27 24 4
gpt4 key购买 nike

你好,我有这样的代码

#include <stdio.h>
#define SECRET "1234567890AZXCVBNFRT"
int checksecret(){
char buf[32];
gets(buf);
if(strcmp(SECRET,buf)==0) return 1;
else return 0;
}

void outsecret(){
printf("%s\n",SECRET);
}
int main(int argc, char** argv){
if (checksecret()){
outsecret();
};
}

外泄

(gdb) disassemble outsecret 
Dump of assembler code for function outsecret:
0x00000000004005f4 <+0>: push %rbp
0x00000000004005f5 <+1>: mov %rsp,%rbp
0x00000000004005f8 <+4>: mov $0x4006b4,%edi
0x00000000004005fd <+9>: callq 0x400480 <puts@plt>
0x0000000000400602 <+14>: pop %rbp
0x0000000000400603 <+15>: retq

我假设我不知道 SECRET,所以我尝试使用这样的字符串 python -c 'print "A"* 32 + "\x40\x05\xf4[::-1]'。但它因段错误而失败。我做错了什么?感谢您的帮助。

附言

我想通过覆盖 checksecret 中的返回码来调用函数 outsecret

最佳答案

您必须记住,所有字符串都有一个额外的字符来终止字符串,因此如果您输入 32 个字符,则 gets 会将 33 个字符写入缓冲区。超出数组限制的写入会导致 undefined behavior这通常会导致崩溃。

gets 函数没有边界检查,使用起来非常危险。它早就被弃用了,在最新的 C11 标准中甚至被删除了。

关于c - x86_64 上 C 中的溢出缓冲区调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27227016/

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