幽靈漏洞(GHOST)檢測方法及修復建議
0x01 前言
昨日,【CVE 2015-0235: GNU glibc gethostbyname 緩沖區溢出漏洞 】全面爆發,該漏洞的產生是Qualys公司在進行內部代碼審核時,發現了一個在GNU C庫(glibc)中存在的__nss_hostname_digits_dots函數導致的緩沖區溢出漏洞。這個bug可以通過gethostbyname *()函數來觸發,本地和遠程均可行。該漏洞(幽靈漏洞)造成了遠程代碼執行,攻擊者可以利用此漏洞遠程獲取系統進程當前的權限。
鑒于網上諸多glibc的原理普及,本文章就不多啰嗦了,直接實踐如何檢測服務器是否存在該漏洞,以及修復漏洞的方法。
0x02 檢測是否存在幽靈漏洞(GHOST)
檢測方法1【RedHat官方檢測方法】:
ghost_check.sh源碼:
- #!/bin/bash
- vercomp () {
- if [[ $1 == $2 ]]
- then
- return 0
- fi
- local IFS=.
- local i ver1=($1) ver2=($2)
- # fill empty fields in ver1 with zeros
- for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
- do
- ver1[i]=0
- done
- for ((i=0; i<${#ver1[@]}; i++))
- do
- if [[ -z ${ver2[i]} ]]
- then
- # fill empty fields in ver2 with zeros
- ver2[i]=0
- fi
- if ((10#${ver1[i]} > 10#${ver2[i]}))
- then
- return 1
- fi
- if ((10#${ver1[i]} < 10#${ver2[i]}))
- then
- return 2
- fi
- done
- return 0
- }
- glibc_vulnerable_version=2.17
- glibc_vulnerable_revision=54
- glibc_vulnerable_version2=2.5
- glibc_vulnerable_revision2=122
- glibc_vulnerable_version3=2.12
- glibc_vulnerable_revision3=148
- echo "Vulnerable glibc version <=" $glibc_vulnerable_version"-"$glibc_vulnerable_revision
- echo "Vulnerable glibc version <=" $glibc_vulnerable_version2"-"$glibc_vulnerable_revision2
- echo "Vulnerable glibc version <=" $glibc_vulnerable_version3"-1."$glibc_vulnerable_revision3
- glibc_version=$(rpm -q glibc | awk -F"[-.]" '{print $2"."$3}' | sort -u)
- if [[ $glibc_version == $glibc_vulnerable_version3 ]]
- then
- glibc_revision=$(rpm -q glibc | awk -F"[-.]" '{print $5}' | sort -u)
- else
- glibc_revision=$(rpm -q glibc | awk -F"[-.]" '{print $4}' | sort -u)
- fi
- echo "Detected glibc version" $glibc_version" revision "$glibc_revision
- vulnerable_text=$"This system is vulnerable to CVE-2015-0235. <https://access.redhat.com/security/cve/CVE-2015-0235>
- Update the glibc and ncsd packages on your system using the packages released with the following:
- yum install glibc"
- if [[ $glibc_version == $glibc_vulnerable_version ]]
- then
- vercomp $glibc_vulnerable_revision $glibc_revision
- elif [[ $glibc_version == $glibc_vulnerable_version2 ]]
- then
- vercomp $glibc_vulnerable_revision2 $glibc_revision
- elif [[ $glibc_version == $glibc_vulnerable_version3 ]]
- then
- vercomp $glibc_vulnerable_revision3 $glibc_revision
- else
- vercomp $glibc_vulnerable_version $glibc_version
- fi
- case $? in
- 0) echo "$vulnerable_text";;
- 1) echo "$vulnerable_text";;
- 2) echo "Not Vulnerable.";;
- esac
檢測方法2【簡單的檢測方法】:
檢測方法2源碼:
- /usr/sbin/clockdiff `python -c "print '0' * $((0x10000-16*1-2*4-1-4))"`
檢測方法3【二進制檢測方法】:
ghost.c源碼:
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #define CANARY "in_the_coal_mine"
- struct {
- char buffer[1024];
- char canary[sizeof(CANARY)];
- } temp = { "buffer", CANARY };
- int main(void) {
- struct hostent resbuf;
- struct hostent *result;
- int herrno;
- int retval;
- /*** strlen (name) = size_needed - sizeof (*host_addr) - sizeof (*h_addr_ptrs) - 1; ***/
- size_t len = sizeof(temp.buffer) - 16*sizeof(unsigned char) - 2*sizeof(char *) - 1;
- char name[sizeof(temp.buffer)];
- memset(name, '0', len);
- name[len] = '\0';
- retval = gethostbyname_r(name, &resbuf, temp.buffer, sizeof(temp.buffer), &result, &herrno);
- if (strcmp(temp.canary, CANARY) != 0) {
- puts("vulnerable");
- exit(EXIT_SUCCESS);
- }
- if (retval == ERANGE) {
- puts("not vulnerable");
- exit(EXIT_SUCCESS);
- }
- puts("should not happen");
- exit(EXIT_FAILURE);
- }
#p#
0x03 在線修復方案
CentOS, Red Hat, Fedora等系列衍生版本(RHN建議):
- yum update glibc
Debian, Ubuntu等系列衍生版本:
- apt-get clean && apt-get update && apt-get upgrade
#p#
0x04 離線修復方案
Centos6.5離線補丁
先檢查本地glibc包安裝了哪些相關包
- rpm -qa|grep glibc
然后,到阿里源下載對應版本
- cat > update.txt << EOF
- http://mirrors.aliyun.com/centos/6.6/updates/x86_64/Packages/glibc-2.12-1.149.el6_6.5.i686.rpm
- http://mirrors.aliyun.com/centos/6.6/updates/x86_64/Packages/glibc-2.12-1.149.el6_6.5.x86_64.rpm
- http://mirrors.aliyun.com/centos/6.6/updates/x86_64/Packages/glibc-common-2.12-1.149.el6_6.5.x86_64.rpm
- http://mirrors.aliyun.com/centos/6.6/updates/x86_64/Packages/glibc-devel-2.12-1.149.el6_6.5.x86_64.rpm
- http://mirrors.aliyun.com/centos/6.6/updates/x86_64/Packages/glibc-headers-2.12-1.149.el6_6.5.x86_64.rpm
- EOF
進行后臺斷點下載補丁包
- wget -b -i update.txt -c
使用yum本地安裝
- yum localinstall glibc-*
或是rpm安裝
- rpm -ivUh glibc-*
Red Had系列衍生版本
使用方法參考上文【Centos6.5離線補丁】的修補方法
http://mirrors.aliyun.com/centos/7/updates/x86_64/Packages/glibc-2.17-55.el7_0.5.i686.rpm
http://mirrors.aliyun.com/centos/7/updates/x86_64/Packages/glibc-2.17-55.el7_0.5.x86_64.rpm
0x05 修復完成檢測
ghost_check.sh腳本檢測
ghost.c腳本檢測
注意:打好補丁后必須立即重啟操作系統,否則會造成應用業務無法使用。
0x06 參考來源
redhat官方
https://access.redhat.com/articles/1332213
redhat官方補丁介紹:
https://rhn.redhat.com/errata/RHSA-2015-0090.html
https://rhn.redhat.com/errata/RHSA-2015-0092.html
ubuntu官方補丁介紹:
http://www.ubuntu.com/usn/usn-2485-1/