漏洞說明:php
用一張GIF圖片就可致使服務器發生崩潰直至宕機,在現實中很是容易利用。html
影響版本:服務器
PHP 5 < 5.6.33函數
PHP 7.0 < 7.0.27oop
PHP 7.1 < 7.1.13測試
PHP 7.2 < 7.2.1spa
漏洞細節: 漏洞存在於文件ext/gd/libgd/gdgifin.c中,其中在LWZReadByte_函數中存在一個循環(while-loop):code
do { sd->firstcode = sd->oldcode = GetCode(fd, &sd->scd, sd->code_size, FALSE, ZeroDataBlockP); } while (sd->firstcode == sd->clear_code);
GetCode函數僅只是一個包裝類,GetCode_纔是真正的執行體:htm
static int GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP) { int i, j, ret; unsigned char count; ... if ((count = GetDataBlock(fd, &scd->buf[2], ZeroDataBlockP)) <= 0) scd->done = TRUE; ... }
GetCode_ 會調用GetDataBlock來讀取GIF圖片中的數據:blog
static int GetDataBlock_(gdIOCtx *fd, unsigned char *buf, int *ZeroDataBlockP) { unsigned char count; if (! ReadOK(fd,&count,1)) { return -1; } *ZeroDataBlockP = count == 0; if ((count != 0) && (! ReadOK(fd, buf, count))) { return -1; } return count;
該漏洞依賴於從整形(int)到無符號字符(unsigned char)的類型轉換。就像上述的:若是GetDataBlock_返回-1,則第400行中的scd->done將會被設置爲True,並中止while循環。可是其定義的count是無符號字符,它老是從0到255的正數,因此這種循環中止動做是不會被觸發執行的。
所以,最終結果就是,一張GIF圖片就能夠實現無限循環,致使服務器資源耗盡,直到崩潰宕機。
開始測試:
測試環境win10,php 7.0.12;
下載好poc.gif圖片文件,放到桌面上。
win+R,輸入cmd,打開cmd窗口。
cd Desktop
進入桌面位置。
輸入
php -r imagecreatefromgif('poc.gif');
(注:本機已配置好php環境變量)
開了多個cmd窗口進行操做,能夠看到CPU佔用很是大。
另:
在window10裝了phpstudy進行測試。
開了五個網頁測試,而後訪問本地服務器其餘網頁,能夠訪問獲得,就是很是慢。
-- end;
參考連接:http://www.freebuf.com/vuls/161262.html