針對 macOS 內建 Bash 3.2.51 受 Shellshock (CVE-2014-6271, CVE-2014-7169) 影響,蘋果遲未推出官方修補時,可手動從原始碼編譯 Bash 3.2.53 替換系統二進位檔。完整流程含 Xcode 準備、三階段打補丁編譯、備份替換、驗證。

mkdir bash-fix && cd bash-fix
curl https://opensource.apple.com/tarballs/bash/bash-92.tar.gz | tar zxf -
cd bash-92/bash-3.2
curl https://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-052 | patch -p0
cd ../..
xcodebuild
等待輸出 BUILD SUCCEEDED。
mv build/bash.build/Release/bash.build/DerivedSources/y.tab.* bash-3.2/
cd bash-3.2
curl https://ftp.gnu.org/pub/gnu/bash/bash-3.2-patches/bash32-053 | patch -p0
cd ..
xcodebuild
再次等待 BUILD SUCCEEDED。
# 備份 (需輸入密碼,無回顯)
sudo cp /bin/bash /bin/bash.old
sudo cp /bin/sh /bin/sh.old
# 確認新編譯版本
build/Release/bash --version
build/Release/sh --version
# 應顯示 GNU bash, version 3.2.53(1)-release
# 替換系統 Bash
sudo cp build/Release/bash /bin/bash
sudo cp build/Release/sh /bin/sh
# 測試一
env x='() { :;}; echo vulnerable' bash -c "echo this is a test"
# 應顯示 warning... this is a test (無 vulnerable)
# 測試二
env X='() { (a)=>\' bash -c "echo date"; cat echo; rm -f echo
# 應無日期輸出,顯示 cat: echo: No such file or directory

此指南為歷史技術文獻保留,展示 macOS 核心組件手動修補的完整鏈路。