網路安全live第二期:網路安全-Heartbleed攻擊

網路安全live第二期:網路安全-Heartbleed攻擊

來自專欄 ath0的安全筆記

本期live互動、答疑相關問題歸檔(有視頻):

【網路安全】Heartbleed漏洞分析?

www.zhihu.com圖標


Heartbleed攻擊防範

概述

Heartbleed是一個出現在加密程序庫OpenSSL的安全漏洞,該程序庫廣泛用於實現互聯網的傳輸層(TLS)協議.它於2012年被引入了軟體中,2014年4月首次向公眾披露.只要使用的是存在缺陷的OpenSSL實例,無論是伺服器還是客戶端,都可能因此而受到攻擊.此問題的原因是在實現TLS的心跳協議時沒有對輸入進行適當驗證(缺少邊界檢查),讀取的數據比應該允許讀取的還多.當前主流OpenSSL中已經不存在這樣的漏洞了,本期實驗需要在陳舊的Ubuntu12系統中完成.

實驗環境

本期實驗基於2台Linux系統,一台作為攻擊者,一台作為受害者.

攻擊者為咱們的主機kali linux,受害者為咱們提供的SEED Ubuntu.

實驗中我們需要用到開源的HTTPS網站ELGG社交網站,在SEED Ubuntu中已經搭建好了.

SEED Ubuntu下載地址如下:

鏈接: https://pan.baidu.com/s/1qRFpF8d5pz6MWNDRW164Bg

密碼: yi4s

實驗環境下載好了之後,咱們需要在攻擊者系統上配置一下,以便於成功訪問ELGG網站.

咱們的SEED Ubuntu系統的IP地址為192.168.59.148.

etchosts中添加配置192.168.59.148 www.heartbleedlabelgg.com.

現在,我們可以在攻擊者主機成功訪問ELGG網站了. 如圖所示:

任務1:實現Heartbleed攻擊

  • 理解心跳協議

心跳協議包含2種報文,心跳請求報文和心跳應答報文.客戶端向服務端發送心跳請求報文,當服務端收到請求報文之後,將請求報文的消息段copy,在心跳應答報文中返回給客戶端.心跳協議的目的是確保連接的有效性.如圖所示:

  • 實現Heartbleed攻擊

1.訪問https://www.heartbleedlabelgg.com.

2.登錄(用戶名:admin,密碼:seedelgg).

3.添加好友(點擊More -> Members 點擊 Boby -> Add Friend).

4.發送消息.

5.多次運行攻擊腳本($ ./attack.py www.heartbleedlabelgg.com).

獲取用戶名和密碼:

獲取發送消息:

任務2:找出Heartbleed漏洞原因

Heartbleed攻擊基於Heartbeat請求.這個請求只是發送一些數據到伺服器,伺服器會將數據複製到它的響應數據包中,所有的數據都會被回顯.

在正常情況下,假設請求包含3個位元組的數據「ABC」,長度欄位的值為3.伺服器將數據放入內存中,並從數據的開頭複製3個位元組到其響應包.

在攻擊場景中,請求可能包含3個位元組的數據,但長度欄位可能表示為1003.當伺服器構造其響應數據包時,它從數據的起始處(即「ABC」)複製,但它複製1003位元組,這些額外的1000位元組顯然不是來自請求包,它們來自伺服器的私有內存,並且可能包含用戶名,密碼等隱私數據.

良性請求:

惡意請求:

嘗試不同的payload長度值,當載荷長度減小的時候,我們獲取到的額外數據量在減少,

當載荷值小於等於22的時候,獲取不到額外的數據.

$./attack.py www.heartbleedlabelgg.com --length 22

任務3:修復Heartbleed漏洞

升級OpenSSL.

$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get dist-upgrade

源代碼分析:

心跳請求應答報文數據結構:

struct {HeartbeatMessageType type; // 1 byte: request or the responseuint16 payload_length; // 2 byte: the length of the payloadopaque payload[HeartbeatMessage.payload_length];opaque padding[padding_length];} HeartbeatMessage;

處理心跳請求,構造心跳應答的過程:

1 /* Allocate memory for the response, size is 1 byte2 * message type, plus 2 bytes payload length, plus3 * payload, plus padding4 */56 unsigned int payload;7 unsigned int padding = 16; /* Use minimum padding */89 // Read from type field first10 hbtype = *p++; /* After this instruction, the pointer11 * p will point to the payload_length field *.1213 // Read from the payload_length field14 // from the request packet15 n2s(p, payload); /* Function n2s(p, payload) reads 16 bits16 * from pointer p and store the value17 * in the INT variable "payload". */181920 pl=p; // pl points to the beginning of the payload content2122 if (hbtype == TLS1_HB_REQUEST)23 {24 unsigned char *buffer, *bp;25 int r;2627 /* Allocate memory for the response, size is 1 byte28 * message type, plus 2 bytes payload length, plus29 * payload, plus padding30 */3132 buffer = OPENSSL_malloc(1 + 2 + payload + padding);33 bp = buffer;3435 // Enter response type, length and copy payload36 *bp++ = TLS1_HB_RESPONSE;37 s2n(payload, bp);3839 // copy payload40 memcpy(bp, pl, payload); /* pl is the pointer which41 * points to the beginning42 * of the payload content */4344 bp += payload;4546 // Random padding47 RAND_pseudo_bytes(bp, padding);4849 // this function will copy the 3+payload+padding bytes50 // from the buffer and put them into the heartbeat response51 // packet to send back to the request client side.52 OPENSSL_free(buffer);53 r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,54 3 + payload + padding);55 }

推薦閱讀:

為什麼radare2普及度不及ida,作為開源工具它有什麼值得改進的地方嗎?
請警惕遠邪帶路黨,他們有可能是專業的網路恐怖分子
Gartner WAF 2015 魔力象限節選翻譯
手把手教你用JS寫XSS cookie stealer來竊取密碼
Facebook好友也不可信,分分鐘會黑掉你

TAG:網路安全 | 信息安全 | 黑客Hacker |