Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 32位intel堆栈帧格式字符串漏洞_C_Assembly_X86_Intel - Fatal编程技术网

C 32位intel堆栈帧格式字符串漏洞

C 32位intel堆栈帧格式字符串漏洞,c,assembly,x86,intel,C,Assembly,X86,Intel,我有一个类似这样的程序 测试程序: #include <stdio.h> void foo(char *input) { char buffer[64]; strncpy(buffer, input, sizeof(buffer)); printf(buffer); } int main(int argc, char **argv) { foo(argv[1]); } #包括 void foo(字符*输入) { 字符缓冲区[64]; strncp

我有一个类似这样的程序

测试程序:

#include <stdio.h>

void foo(char *input)
{
    char buffer[64];
    strncpy(buffer, input, sizeof(buffer));
    printf(buffer);
}

int main(int argc, char **argv)
{
    foo(argv[1]);
}
#包括
void foo(字符*输入)
{
字符缓冲区[64];
strncpy(buffer,input,sizeof(buffer));
printf(缓冲区);
}
int main(int argc,字符**argv)
{
foo(argv[1]);
}
我编译程序时关闭了所有与堆栈相关的保护

gcc fmthck.c-w-m32-O0-ggdb-std=c99-static-D_FORTIFY_SOURCE=0-fno pie-Wno格式-Wno格式安全性-fno堆栈保护器-z norelro-z execstack-o hacks

sudo sysctl-w内核。随机化\u va\u空间=0

然后,我为编译后的程序提供以下参数:

./hacks“AAAA%p%p%p%p%p%p%p%p%p”

我得到的结果是这样的

AAAA 0xffffd3cb 0x40 0x8048d3c 0x41414141 0x2e702520 0x252e7025 0x70252e70 0x2e70252e 0x252e7025 0x70252e70

我知道在十六进制中A=0x41。我猜缓冲区的起始地址对应于第四个%p我只是想知道它周围的东西意味着什么。我得到了一个看起来像这样的堆栈图。我知道0x8048d3c对应于返回地址,但有些东西不一致

高内存地址

  • 输入参数
  • 回信地址
  • 保存的帧指针
  • 局部变量
  • 保存的寄存器
低内存地址


有人能在我这样做时详细说明一下堆栈发生了什么吗?

如果使用选项-S编译程序,编译器将生成可供检查的程序集输出。在您的情况下,生成的相关代码是:

foo:
    pushl   %ebp
    movl    %esp, %ebp
     subl    $88, %esp
    movl    $64, 8(%esp)
    movl    8(%ebp), %eax
    movl    %eax, 4(%esp)
    leal    -72(%ebp), %eax
    movl    %eax, (%esp)
    call    strncpy
    leal    -72(%ebp), %eax
    movl    %eax, (%esp)
    call    printf
您的输出是

    AAAA 0xffffd3cb 0x40 0x8048d3c 0x41414141 0x2e702520 0x252e7025 0x70252e70 0x2e70252e 0x252e7025 0x70252e70
在调用
printf
时,必须将其解释为系统堆栈的一部分。输出的含义是:

AAAA       == string from argument printed by printf
0xffffd3cb == garbage left from previous function invocation, in fact the input argument argv[1] previously sent to `strncpy`
0x40       == garbage left from previous function invocation, in fact it is the constant 64 previously sent to strncpy
0x8048d3c  == ??? I don't know why gcc left this unused space.
0x41414141 0x2e70252 ... is the content of local variable `buffer` which contains the string "AAAA %p %p %p %p %p %p %p %p %p %p".
函数的返回地址以及保存的基指针超出了输出范围