【问题标题】:32 bit intel stack frame format string exploit32位英特尔堆栈帧格式字符串漏洞利用
【发布时间】:2014-02-08 17:57:03
【问题描述】:

我有一个看起来像这样的程序

测试程序:

#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]);
}

我编译我的程序时关闭了所有与堆栈相关的保护。

gcc fmthck.c -w -m32 -O0 -ggdb -std=c99 -static -D_FORTIFY_SOURCE=0 -fno-pie -Wno-format -Wno-format-security -fno-stack-protector -z norelro -z execstack -o hacks

sudo sysctl -w kernel.randomize_va_space=0

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

./hacks "AAAA %p %p %p %p %p %p %p %p %p %p"

我得到一个看起来像这样的输出

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

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

高内存地址

  • 输入参数
  • 退货地址
  • 保存的帧指针
  • 局部变量
  • 保存的寄存器

低内存地址

有人可以详细说明我这样做时堆栈发生了什么吗?

【问题讨论】:

    标签: c assembly x86 intel


    【解决方案1】:

    如果您使用选项 -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".
    

    函数的返回地址以及保存的基指针超出了您的输出。

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 1970-01-01
      • 2020-10-26
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多