2014 年席捲伺服器圈的 Bash 漏洞(Shellshock,CVE-2014-6271 及其後續變體)影響範圍極廣——只要是 Linux 與 macOS 主機,幾乎很難倖免。當時在沒有官方更新可下的情況下,社群裡流傳著各種應急腳本,下面這份是其中寫得比較完整的一個。
它其實是一個互動式的選單:執行後列出各個發行版選項,使用者輸入編號,腳本再呼叫對應的套件管理器或下載修補過的 bash 套件來安裝。最後還提供了兩個不靠更新套件的替代方案。
#/bin/bash #Author Alex Fang. Updates may apply soon. clear echo "Press any key to continue..." $anykey ; read anykey echo "ShellShockFixer v0.1 by Alex Fang. Liscence: GNU GPLv2" echo "######################################################" echo "Select on option:" echo "1) CentOS" echo "2) Debian Wheezy" echo "3) Debian Squeeze x64(Provided by Aliyun)" echo "4) Debian Squeeze x32(Provided by Aliyun)" echo "5) Ubuntu" echo "6) OpenSuSE x64(Beta, provided by Aliyun)" echo "7) OpenSuSE x32(Beta Aliyun)" echo "8) Aliyun Linux x64" echo "9) Aliyun Linux x32" echo "0) iptables way" echo "11) Temporily disable bash through chmod" echo "######################################################" read x
RHEL 系用 yum:
if test $x -eq 1; then clear echo "Fixing......" yum clean all yum makecache yum update bash echo "Finished!"
Debian/Ubuntu 系用 apt,只升級 bash 一個套件:
elif test $x -eq 2; then clear echo "Fixing for Debian Wheezy..." apt-get update apt-get -y install --only-upgrade bash echo "Finished!" elif test $x -eq 5; then echo "Fixing for Ubuntu..." apt-get update apt-get -y install --only-upgrade bash echo "Finished!"
已經停止維護、官方源裡拿不到修好的版本的發行版,就直接抓預先打包好的 deb/rpm 來裝:
elif test $x -eq 3; then
echo "Fixing for Debian Squeeze x64..."
wget https://mirrors.aliyun.com/debian/pool/main/b/bash/bash_4.1-3+deb6u2_amd64.deb && \
dpkg -i bash_4.1-3+deb6u2_amd64.deb
echo "Finished!"
elif test $x -eq 8; then
echo "Fixing for Aliyun Linux x64..."
wget https://mirrors.aliyun.com/centos/5/updates/x86_64/RPMS/bash-3.2-33.el5_10.4.x86_64.rpm && \
rpm -Uvh bash-3.2-33.el5_10.4.x86_64.rpm
echo "Finished!"
用 iptables 攔截攻擊字元。Shellshock 的利用方式是構造形如 () { 的字串塞進環境變數,因此在串流層次比對這個特徵就能擋掉大部分攻擊:
elif test $x -eq 0; then echo "Deploying iptables rules..." iptables --append INPUT -m string --algo kmp --hex-string '|28 29 20 7B|' --jump DROP iptables using -m string --hex-string '|28 29 20 7B|' echo "Finishing..."
直接拿掉 bash 的執行權限。這是應急中的應急:
elif test $x -eq 11; then echo "Chmod way configuring..." chmod o-x bash echo "Finishing..." else echo "Invalid Operation." exit fi
這份腳本有其時代背景,直接照抄執行在今天並不合適,原因有幾個:
1. 腳本裡那些 aliyun 鏡像的檔案路徑早已失效,wget 會直接失敗。
2. --only-upgrade、Squeeze 的 apt 源等,在現行發行版上都已不適用。
3. 用 iptables 字串比對攔的是「特徵」,不是漏洞本身;變形 payload 可以繞過,而且會誤傷正常流量。
4. chmod o-x bash 會讓非 root 使用者完全無法使用 bash,很多服務會直接掛掉,這是最後手段。
真正遇到類似級別的漏洞時,正確做法只有一個:確認發行版仍在支援週期內,然後用官方源更新受影響的套件。這份腳本的價值在於示範應急排序——先更新套件,其次才是暫時性的流量攔截,最後才是犧牲可用性換取時間。