为什么此程序在堆栈上分配的空间超出了要求?

时间:2018-12-19 21:06:21

标签: x86 stack 32-bit buffer-overflow

我目前正在挑战picoCTF 2018。级别“缓冲区溢出1”为您提供了以下源代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "asm.h"

#define BUFSIZE 32
#define FLAGSIZE 64

void win() {
  char buf[FLAGSIZE];
  FILE *f = fopen("flag.txt","r");
  if (f == NULL) {
    printf("Flag File is Missing. Problem is Misconfigured, please contact an Admin if you are running this on the shell server.\n");
    exit(0);
  }

  fgets(buf,FLAGSIZE,f);
  printf(buf);
}

void vuln(){
  char buf[BUFSIZE];
  gets(buf);

  printf("Okay, time to return... Fingers Crossed... Jumping to 0x%x\n", get_return_address());
}

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

  setvbuf(stdout, NULL, _IONBF, 0);

  gid_t gid = getegid();
  setresgid(gid, gid, gid);

  puts("Please enter your string: ");
  vuln();
  return 0;
}

因此,利用gets()中的vuln()覆盖返回地址并跳转到win()。由于buf的大小为32,因此我希望堆栈看起来像这样:

|-------------------|
|        ...        |
|        buf        |
|        buf        |
|        buf        |
|        buf        |
|-------------------|
|     saved ebp     |
|-------------------|
| return after vuln |
|-------------------|
|        ...        |
|-------------------|

因此,我尝试输入36个随机字符,然后输入返回地址,这不起作用。使用gdb我发现堆栈实际上看起来像这样:

|-------------------|
|        ...        |
|        buf        |
|        buf        |
|        buf        |
|        buf        |
|-------------------|
|        ???        |
|        ???        |
|-------------------|
|     saved ebp     |
|-------------------|
| return after vuln |
|-------------------|
|        ...        |
|-------------------|

buf的末尾与保存的ebp之间有8个字节。我的问题是:这8个字节是做什么用的?

0 个答案:

没有答案