$ cat writeup.md…
$ cat writeup.md…
hackthebox
Task: Analyze a PCAP and obfuscated PHP webshell to reconstruct attacker commands and find the flag. Solution: Deobfuscate a Weevely3 webshell to extract XOR key and markers, decrypt captured HTTP traffic to reveal attacker exfiltrated a KeePass database, crack it with John the Ripper, and read the flag from the database entry.
| Field | Value |
|---|---|
| Event | HackTheBox |
| Challenge | Obscure |
| Category | Forensics |
| Difficulty | Medium |
| Flag | HTB{REDACTED} |
An attacker has found a vulnerability in our web server that allows arbitrary PHP file upload in our Apache server. The hacker has uploaded what seems to be an obfuscated shell (support.php). We monitor our network 24/7 and generate logs from tcpdump (we provided the log file for the period of two minutes before we terminated the HTTP service for investigation). We need help analyzing and identifying commands the attacker wrote to understand what was compromised.
| File | Description |
|---|---|
19-05-21_22532255.pcap | Network capture file containing HTTP traffic |
support.php | Obfuscated PHP webshell uploaded by attacker |
to-do.txt | Task description |
The support.php file contained heavily obfuscated PHP code. The obfuscation used multiple layers of string replacement techniques to hide the actual functionality.
Obfuscation techniques observed:
str_replace('u)','',...) - Removes obfuscation characters scattered throughout stringsstr_replace('FD','','FDcreFDateFD_fFDuncFDFDtion') - Reconstructs create_functionAfter careful deobfuscation, the code revealed a Weevely3-style webshell with the following encryption parameters:
| Parameter | Value | Purpose |
|---|---|---|
| XOR Key | 80e32263 | Encryption/decryption key |
| Key Header (kh) | 6f8af44abea0 | Marks start of encrypted data |
| Key Footer (kf) | 351039f4a7b5 | Marks end of encrypted data |
| Prefix | 0UlYyJHG87EJqEz6 | Session identifier |
Webshell Communication Flow:
[Attacker] --> POST request with encrypted command --> [Webshell]
|
v
1. Read php://input
2. Extract data between kh...kf markers
3. Base64 decode
4. XOR decrypt with key
5. Gzip decompress
6. eval() the command
|
v
[Attacker] <-- Encrypted response (gzip->XOR->base64) <-- [Webshell]
Used tshark to identify HTTP requests targeting the webshell:
# Find TCP streams containing webshell traffic tshark -r 19-05-21_22532255.pcap -Y "http.request.uri contains support.php" -T fields -e tcp.stream
Result: Found 4 TCP streams (1, 23, 24, 25) containing webshell communications.
# Extract full HTTP conversations tshark -r 19-05-21_22532255.pcap -z follow,tcp,ascii,1 tshark -r 19-05-21_22532255.pcap -z follow,tcp,ascii,23 tshark -r 19-05-21_22532255.pcap -z follow,tcp,ascii,24 tshark -r 19-05-21_22532255.pcap -z follow,tcp,ascii,25
Created a Python script to decrypt the Weevely3 communications:
#!/usr/bin/env python3 """ Weevely3 Webshell Traffic Decryptor Decrypts commands and responses from captured network traffic """ import base64 import zlib # Encryption key extracted from deobfuscated webshell k = "80e32263" # Markers for encrypted data boundaries kh = "6f8af44abea0" # Header marker kf = "351039f4a7b5" # Footer marker def xor_decrypt(data: bytes, key: str) -> bytes: """XOR decrypt data with repeating key""" result = bytearray() for i, byte in enumerate(data): result.append(byte ^ ord(key[i % len(key)])) return bytes(result) def decrypt_data(encrypted_b64: str) -> str: """ Full decryption pipeline: Base64 decode -> XOR decrypt -> Gzip decompress """ # Step 1: Base64 decode decoded = base64.b64decode(encrypted_b64) # Step 2: XOR decrypt with key decrypted = xor_decrypt(decoded, k) # Step 3: Gzip decompress decompressed = zlib.decompress(decrypted) return decompressed.decode('utf-8') def extract_encrypted_data(raw_data: str) -> str: """Extract encrypted payload between kh and kf markers""" start = raw_data.find(kh) end = raw_data.find(kf) if start == -1 or end == -1: return None # Data is between header and footer markers return raw_data[start + len(kh):end] # Example usage with captured traffic if __name__ == "__main__": # Process each captured request/response captured_data = [ # Add extracted base64 data from PCAP here ] for i, data in enumerate(captured_data): encrypted = extract_encrypted_data(data) if encrypted: decrypted = decrypt_data(encrypted) print(f"[{i}] {decrypted}")
After decrypting all captured traffic, the following attacker activity was revealed:
| # | Command | Response | Purpose |
|---|---|---|---|
| 1 | id | uid=33(www-data) gid=33(www-data) groups=33(www-data) | Identify current user |
| 2 | ls -lah /home/* | Listed files including pwdb.kdbx in /home/developer | Enumerate home directories |
| 3 | chdir('/home/developer') | Changed directory successfully | Navigate to target |
| 4 | base64 -w 0 pwdb.kdbx | Base64-encoded KeePass database | Data exfiltration |
Key Finding: The attacker discovered and exfiltrated a KeePass password database (pwdb.kdbx) from the developer's home directory.
Decoded the base64 output from command #4 to recover the KeePass database:
# Extract base64 data and decode echo "<base64_data_from_pcap>" | base64 -d > pwdb.kdbx # Verify file file pwdb.kdbx # Output: pwdb.kdbx: Keepass password database 2.x KDBX ls -la pwdb.kdbx # Output: -rw-r--r-- 1 user user 1614 ... pwdb.kdbx
Used keepass2john to extract the password hash:
keepass2john pwdb.kdbx > kdbx_hash.txt
Hash format:
pwdb:$keepass$*2*6000*0*204c86ee9d2e89b4d5ff55ebe51c3cd4bf1a904ccea385ab892c9ce8400cc785*...
# Using rockyou.txt wordlist john --wordlist=/usr/share/wordlists/rockyou.txt kdbx_hash.txt # Check results john --show kdbx_hash.txt
Password found: chainsaw
#!/usr/bin/env python3 """Extract entries from cracked KeePass database""" from pykeepass import PyKeePass # Open database with cracked password kp = PyKeePass('pwdb.kdbx', password='chainsaw') # List all entries print("=" * 50) print("KeePass Database Contents") print("=" * 50) for entry in kp.entries: print(f"\nTitle: {entry.title}") print(f"Username: {entry.username}") print(f"Password: {entry.password}") print(f"URL: {entry.url}") print("-" * 50)
Output:
==================================================
KeePass Database Contents
==================================================
Title: Flag
Username: artikrh
Password: HTB{REDACTED}
URL: https://github.com/epinna/weevely3
--------------------------------------------------
[Timeline of Attack]
1. Attacker exploits file upload vulnerability
|
v
2. Uploads obfuscated Weevely3 webshell as support.php
|
v
3. Executes 'id' command - confirms www-data access
|
v
4. Executes 'ls -lah /home/*' - discovers pwdb.kdbx
|
v
5. Changes directory to /home/developer
|
v
6. Exfiltrates pwdb.kdbx via base64 encoding
|
v
[Connection terminated - investigation started]
| Technique | Application |
|---|---|
| PHP Deobfuscation | Reverse-engineered string replacement obfuscation to understand webshell functionality |
| PCAP Analysis | Used tshark to extract HTTP traffic and identify malicious communications |
| Custom Decryption | Implemented XOR + gzip + base64 decryption matching Weevely3 protocol |
| Password Cracking | Used John the Ripper with keepass2john to crack KeePass master password |
| Database Extraction | Used PyKeePass library to read decrypted database contents |
| Tool | Purpose |
|---|---|
| tshark | CLI packet analysis and HTTP stream extraction |
| Python 3 | Custom decryption scripts |
| pykeepass | Python library for reading KeePass databases |
| John the Ripper | Password hash cracking |
| keepass2john | KeePass hash extraction for John |
| file | File type identification |
| base64 | Encoding/decoding operations |
$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar