缓冲区溢出缓冲区长度

Buffer overflow buffer length

我有一个缓冲区溢出问题需要解决。下面是问题,最下面是我的问题:

#include <stdio.h>
#include <string.h>
void lan(void) {
   printf("Your loyalty to your captors is touching.\n");
}

void vulnerable(char *str) {
   char buf[LENGTH]; //Length is not given
   strcpy(buf, str); //str to fixed size buf (uh-oh)
 }

 int main(int argc, char **argv) {
    if (argc < 2) 
       return -1;
    vulnerable(argv[1]);
    return 0;
 }

 (gdb) disass vulnerable
 0x08048408: push %ebp
 0x08048409: mov %esp, %ebp
 0x0804840b: sub [=10=]x88, %esp
 0x0804840e: mov 0x8(%ebp), %eax
 0x08048411: mov %eax, 0x4(%esp)
 0x08048415: lea -0x80(%ebp), %eax
 0x08048418: mov %eax, (%esp)
 0x0804841b: call 0x8048314 <strcpy>
 0x08048420: leave
 0x08048421: ret
 End of assembler dump.

 (gdb) disass lan
  0x080483f4:   push %ebp
  0x080483f5:   mov %esp, %ebp
  0x080483f7:   sub [=10=]x4, %esp
  0x080483fa:   movl [=10=]x8048514, (%esp)
  0x08048401:   call 0x8048324 <puts>
  0x08048406:   leave
  0x08048407:   ret
  End of assembler dump.

然后我们得到以下信息:

 (gdb) break *0x08048420
 Breakpoint 1 at 0x8048420
 (gdb) run 'perl -e' print "\x90" x Length' 'AAAABBBBCCCCDDDDEEEE'
 Breakpoint 1, 0x08048420 in vulnerable
 (gdb) info reg $ebp
 ebp          0xffffd61c 0xffffd61c
 (gdb) # QUESTION: Where in memory does the buf buffer start?
 (gdb) cont
 Program received signal SIGSEGV, Segmentation fault.

最后,perl 命令是 shorthand 用于写出字符 0x90 的 LENGTH 个副本。

我以前做过几个这类问题,但让我停下来的是以下问题:"By looking at the assembly code, what is the value of LENGTH?"

我不确定如何从给定的汇编代码中找到它。我所知道的是……我们写入的缓冲区位于堆栈中的 -128(%ebp) 位置(其中 -128 是十进制数)。但是,我不确定从这里去哪里获取缓冲区的长度。

What I do know is.. the buffer that we're writing into is on the stack at the location -128(%ebp)

由于局部变量以 %ebp 结尾,而您只有一个局部变量本身是 buffer,因此您可以得出结论,它的长度最多为 128。如果编译器为对齐添加了一些填充,它可能会更短。

让我们看看您的 vulnerable 函数。

首先,编译器创建一个帧并在堆栈上保留 0x88 字节:

 0x08048408: push %ebp
 0x08048409: mov %esp, %ebp
 0x0804840b: sub [=10=]x88, %esp

然后它将两个值放入堆栈:

 0x0804840e: mov 0x8(%ebp), %eax
 0x08048411: mov %eax, 0x4(%esp)
 0x08048415: lea -0x80(%ebp), %eax
 0x08048418: mov %eax, (%esp)

它返回前做的最后一件事是调用 strcpy(buf, str):

 0x0804841b: call 0x8048314 <strcpy>
 0x08048420: leave
 0x08048421: ret

所以我们可以推断出它压入栈的两个值是strcpy的参数。
mov 0x8(%ebp) 将是 char *strlea -0x80(%ebp) 将是指向 char buf[LENGTH].

的指针

因此,我们知道您的缓冲区从 -0x80(%ebp) 开始,因此它的长度为 0x80 = 128 字节,假设编译器没有浪费任何 space。