💻Linux Privilege Escalation

This is a write-up of the "Linux Privilege Escalation" room of tryhackme.com and is only for educational purposes.

Service Exploits

The MySQL service is running as root and the "root" user for the service does not have a password assigned. We can use a popular exploitarrow-up-right that takes advantage of User Defined Functions (UDFs) to run system commands as root via the MySQL service.

After exploiting with raptor_udf2.c

After exploiting raptor_udf2, we can connect to the MySQL service as the root user with a blank password. We can execute commands on the MySQL shell to get root privilege. Use the function to copy /bin/bash to /tmp/rootbash and set the SUID permission:

Getting root privilege with mySQL shell

Weak File Permissions - Readable /etc/shadow

The /etc/shadow file contains user password hashes and is usually readable only by the root user.

Note that the /etc/shadow file on the VM is world-readable:

Each line of the file represents a user. A user's password hash (if they have one) can be found between the first and second colons (:) of each line.

We found the root user's password hash: $6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVlaXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0:17298

After we saved the root user's hash to a file called hash.txt and cracked it with john. Hash type is "sha512crypt" and the root password is "password123".

Weak File Permissions - Writable /etc/shadow

The /etc/shadow file contains user password hashes and is usually readable only by the root user. But when it is writable, we can easily change the root's password and log in with our new password.

Generated new password with mkpasswd and replaced it

Weak File Permissions - Writable /etc/passwd

The /etc/passwd file contains information about user accounts. It is world-readable, but usually only writable by the root user. Historically, the /etc/passwd file contained user password hashes, and some versions of Linux will still allow password hashes to be stored there.

Generated new password with openssl
replace it in /etc/passwd

After that replacement we can "su root" and write our just generated password. In this case: newpasswordhaha

Sudo - Shell Escape Sequences

List the programs which sudo allows your user to run:

sudo -l

Visit GTFOBins (https://gtfobins.github.ioarrow-up-right) and search for some of the program names. If the program is listed with "sudo" as a function, you can use it to elevate privileges, usually via an escape sequence.

11 programs can be executed with sudo

We can visit GTFOBins and gain a root shell with those sudo programs.

Sudo - Environment Variables

LD_PRELOAD and LD_LIBRARY_PATH are both inherited from the user's environment. LD_PRELOAD loads a shared object before any others when a program is run. LD_LIBRARY_PATH provides a list of directories where shared libraries are searched for first.

we could get a root shell through this preload.

Cron Jobs - File Permissions

Cron jobs are programs or scripts which users can schedule to run at specific times or intervals. Cron table files (crontabs) store the configuration for cron jobs. The system-wide crontab is located at /etc/crontab.

Replace the contents of the overwrite.sh file with the following after changing the IP address to that of your Kali box.

Set up a netcat listener on your Kali box on port 4444 and wait for the cron job to run (should not take longer than a minute). A root shell should connect back to your netcat listener.

nc -nvlp 4444

Cron Jobs - PATH Environment Variable

View the contents of the system-wide crontab:

Note that the PATH variable starts with /home/user which is our user's home directory.

Create a file called overwrite.sh in your home directory with the following contents:

Run the /tmp/rootbash command with -p to gain a shell running with root privileges:

SUID / SGID Executables - Known Exploits

Find all the SUID/SGID executables on the Debian VM:

Note that /usr/sbin/exim-4.84-3 appears in the results. Try to find a known exploit for this version of exim. Exploit-DBarrow-up-right, Google, and GitHub are good places to search! cve-2016-1531.sh is good CVE in here.

Passwords & Keys - History Files

If a user accidentally types their password on the command line instead of into a password prompt, it may get recorded in a history file.

View the contents of all the hidden history files in the user's home directory:

cat ~/.*history | less

Note that the user has tried to connect to a MySQL server at some point, using the "root" username and a password submitted via the command line. Note that there is no space between the -p option and the password!

Passwords & Keys - Config Files

Config files often contain passwords in plaintext or other reversible formats.

Passwords & Keys - SSH Keys

Sometimes users make backups of important files but fail to secure them with the correct permissions.

Look for hidden files & directories in the system root:

ls -la /

Note that there appears to be a hidden directory called .ssh. View the contents of the directory:

ls -l /.ssh

Note that there is a world-readable file called root_key. Further inspection of this file should indicate it is a private SSH key. The name of the file suggests it is for the root user.

Copy the key over to your Kali box (it's easier to just view the contents of the root_key file and copy/paste the key) and give it the correct permissions, otherwise your SSH client will refuse to use it:

chmod 600 root_key

Use the key to login to the Debian VM as the root account:

ssh -i root_key root@10.10.68.66

Kernel Exploits

Kernel exploits can leave the system in an unstable state, which is why you should only run them as a last resort.

Run the Linux Exploit Suggester 2 tool to identify potential kernel exploits on the current system:

perl /home/user/tools/kernel-exploits/linux-exploit-suggester-2/linux-exploit-suggester-2.pl

The popular Linux kernel exploit "Dirty COW" should be listed. Exploit code for Dirty COW can be found at /home/user/tools/kernel-exploits/dirtycow/c0w.c. It replaces the SUID file /usr/bin/passwd with one that spawns a shell (a backup of /usr/bin/passwd is made at /tmp/bak).

Compile the code and run it (note that it may take several minutes to complete):

gcc -pthread /home/user/tools/kernel-exploits/dirtycow/c0w.c -o c0w ./c0w

Once the exploit completes, run /usr/bin/passwd to gain a root shell:

/usr/bin/passwd

Last updated