βοΈPWNLAB
Welcome to "PwnLab: init", a Boot2Root virtual machine. . The purpose of this CTF is to get root and read the flag.
1. Reconnaissance
Scanning the network to find vulnerable machine's IP
arp-scan -l
Interface: enp0s3, type: EN10MB, MAC: 08:00:27:02:ad:e6, IPv4: 192.168.10.22
Starting arp-scan 1.9.7 with 256 hosts (https://github.com/royhills/arp-scan)
192.168.10.1 52:54:00:12:35:00 QEMU
192.168.10.2 52:54:00:12:35:00 QEMU
192.168.10.3 08:00:27:ba:30:20 PCS Systemtechnik GmbH
192.168.10.14 08:00:27:94:3a:a6 PCS Systemtechnik GmbH
4 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9.7: 256 hosts scanned in 2.432 seconds (105.26 hosts/sec). 4 respondedWe find the IP of the vulnerable machine to be
192.168.10.14
2. Scanning
2.1 Nmap
Using
nmapto find the open ports and their services
We find open ports
22,80,111,3306,45450with servicesssh,rpcbind,mysql, and avirtual boxrunning respectively.
2.2 Directory enumeration
Enumerating the web directories using
gobuster
We find an interesting directory
/upload
2.3 Vulnerability scanning
Enumerating vulnerable services using
niko
We get a mention of
/config.phpfile which could contain the database IDs and passwords
3. Initial Foothold
3.1 Local File Inclusion

Accessing the webpage, we see that there are three links that can be accessed :
Home,Login, andUploadAs we access each of them we see that the url gets appended with
/?page=nameFor eg: If we go toLoginpage, we see the url change fromhttp://192.168.10.14tohttp://192.168.10.14/?page=loginand similarly foruploadThis is a misconfiguration know as Local File Inclusion (LFI) and it allows an attacker to manipulate the targetβs web server by including malicious files remotely or even access sensitive files present onto that server. You can read more about LFI here
When trying to access
config.phpdirectly by appending it to the url(http://192.168.10.14/?page=config) and also by adding a null byte (http://192.168.10.14/?page=config%00), does not yield any resultsThen we try base64 encoding using a php wrapper:
http://192.168.10.14/?page=php://filter/read=convert.base64-encode/resource=config, which gives us the base64 encodeconfig.phppage

Decoding it, we get the login credentials for the mysq database
3.2 Dumping credentials
Dumping the database using the credentials
root : H4u%QJ_H99obtained from theconfig.phpfile for possible usernames and/or password
We get 3 users
kent,mikeandkaneand their base64 encode passwordsDecoding them, we get possible credentials for login in to the webpage and/or the box
kent : JWzXuBJJNymike : SIfdsTEn6Ikane : iSv5Ym2GRoUsing any of the credentials, we can login to the web portal which will take you to the
Upload page.

3.3 Reverse shell
Trying to upload a
php-reverse-shell.phpreverse shell from pentestmonkey (Make sure to change the IP and port)

We find that there are certain restricting in uploading files. It accept only images files
We can now rename
.phpextension of the reverse shell to.gif;php-reverse-shell.gifAlso add the header
GIF 98at the start of the file to edit the magic byte (the first byte that identified the type of the file) to make it seem like a.giffile , from a text editorUploading the modified reverse shell, it goes through

we can find the uploaded file by going to
/uploaddirectory

Navigating to our uploaded GIF file, produces an error

There seems to be more filters in place
3.4 Filter Bypass
In order to find out how files are processed, we need the source code.
Getting
index.phpsource fromhttp://192.168.10.14/?page=php://filter/read=convert.base64-encode/resource=indexand base64 decoding it
Here we see that, there is an attribute
langwhich, when passed to the cookie, can be used for multilingual support. As this function is not yet implemented, we need to set any other language manually through the cookieThere is an
include()function, so there might be another possible LFI. It takes in a parameter oflang=anypathin the cookie value.Capturing the upload file in
burpsuitand modifying the cookie value to include the path of our uploaded reverse shell.Cookie:
lang=../upload/3208fd203ca8fdfa13bc98a4832c1396.gif
Burp Request :

Starting
ncin the attacker system to capture the shell and forwarding the modified request in burp.
Stabilizing the shell
We are logged in as
www-data
4. Privilege Escalation
4.1 SUID
We can try to switch users with the credentials obtained from the database.
We are able to switch to user
kanewith the credentialskane : iSv5Ym2GRosudo -ldoes not give any results as sudo is disabled in the box.
Using SUID bit to switch to other user. -> A file with SUID always executes as the user who owns the file, regardless of the user passing the command
find / -perm -04000 -type f -ls 2>/dev/nullshows us the files with SUID bit set for the current user.
Files at
/usr/binand/usr/libmight not help us in privesc via SUID. There is another interesting filemsgmikeat/home/kaneChecking the file type using
file
The file is an executable file.
Executing the file gives us an error
4.2 PATH Variable.
From the error shown above, we see that the executable is trying to read a file called
msg.txtusing thecatcommand.In order to execute any Linux command, it checks the
PATHvariable for paths of the command. If the command is not found in the first location, it moves on to the next and so on. Read more about it here
Checking the
PATHvariable as well as the location of thecatcommand
We can see that the
catcommand is in the third location (/bin) of thePATHvariable.It checks the first two locations and moves on as it does not find the
catcommand there.In order to exploit the PATH variable, we need to provide a pseudo
catcommand which can spawn a shell when called. And its path should be before the original path of thecatcommand in thePATHvariable for it to get executed first.Creating an executable file named
catin the current directory, which will spawn a bash shell.
Exporting the current directory path to the beginning of the
PATHvariable.
Now there is a
catcommand at the first location of thePATHvariable, so it will get executed first when the command is called, and thus we get a shell as usermike.
We became the user
mikebecause the owner of the filemsgmikewith SUID set wasmike
4.3 Command Injection
We find another vulnerable executable at
mike's home folder calledmsg2rootwith SUID set and ownerroot
Executing
msg2root
It asks for some user input
Running
stringsonmsg2root
We find that any command the user inputs is passed to the
/bin/echocommand and eventually, appended to thatmessages.txt.Here command injection is possible.We can directly pass in the command
;/bin/shto execute and spawn a shell as root we use;to break out of the echo command.
We are
ROOTViewing
flag.txt
Last updated