Linux PrivEsc
Room Link: https://tryhackme.com/room/linuxprivesc
Deploy the Vulnerable Debian VM
Kali
Service Exploits
Victim
Compile the raptor_udf2.c exploit code using the following commands
Victim
Connect to the MySQL service as the root user with a blank password
Victim
Execute the following commands on the MySQL shell to create a User Defined Function (UDF) "do_system" using our compiled exploit.
Victim - mysql
Use the function to copy /bin/bash to /tmp/rootbash and set the SUID permission.
Victim - mysql
Exit out of the MySQL shell (type exit or \q and press Enter) and run the /tmp/rootbash executable with -p to gain a shell running with root privileges.
Victim
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.
Victim
View the contents of the /etc/shadow file.
Victim
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.
Save the root user's hash to a file called hash.txt on your Kali VM and use john the ripper to crack it. You may have to unzip /usr/share/wordlists/rockyou.txt.gz first and run the command using sudo depending on your version of Kali.
Kali
Weak File Permissions - Writable /etc/shadow
Note that the /etc/shadow file on the VM is world-writable
Victim
Generate a new password hash with a password of your choice.
Victim
Comment out the old one and add our output as the hash.
Victim
Victim
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. Note that the /etc/passwd file is world-writable.
Victim
Generate a new password hash with a password of your choice.
Victim
Edit the /etc/passwd file and place the generated password hash between the first and second colon (:) of the root user's row (replacing the "x"). Switch to the root user, using the new password
Victim
Victim
Sudo - Shell Escape Sequences
Victim
apache2
We can use apache2 to read files and crack hashes.
Victim
nmap
Option 1
Victim
Option 2
Victim
ftp
Victim
more
Need to enter the second command while scrolling through the output.
Victim
less
Option 1
Victim
This option did not work on this box as when you click v it opens nano instead of vi or vim. If you can someone set the environment variables VISUAL and/or EDITOR, you could get this to work but it would be difficult.
Option 2
Victim
Option 3
Victim
awk
Victim
man
Victim
VIM
Victim
find
Victim
iftop
Victim
Sudo - Environment Variables
preload.c - code
library_path.c
Sudo can be configured to inherit certain environment variables from the user's environment. Check which environment variables are inherited (look for the env_keep options):
Victim
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.
Create a shared object using the code located at /home/user/tools/sudo/preload.c.
Victim
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.
Create a shared object using the code located at /home/user/tools/sudo/preload.c
Victim
A root shell should spawn. Exit out of the shell before continuing. Depending on the program you chose, you may need to exit out of this as well.
Run ldd against the apache2 program file to see which shared libraries are used by the program.
Victim
Create a shared object with the same name as one of the listed libraries (libcrypt.so.1) using the code located at /home/user/tools/sudo/library_path.c.
Victim
Create a shared object with the same name as one of the listed libraries (libcrypt.so.1) using the code located at /home/user/tools/sudo/library_path.c.
Victim
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.
View the contents of the system-wide crontab.
Victim
There should be two cron jobs scheduled to run every minute. One runs overwrite.sh, the other runs /usr/local/bin/compress.sh.
Locate the full path of the overwrite.sh file.
Victim
Note that the file is world-writable.
Victim
Replace the contents of the overwrite.sh file with the following after changing the IP address to that of your Kali box
overwrite.sh
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. If it doesn't recheck the permissions of the file, is anything missing?
Kali
Cron Jobs - PATH Environment Variable
View the contents of the system-wide crontab
Victim
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
overwrite.sh
Make sure that the file is executable
Victim
Wait for the cron job to run (should not take longer than a minute). Run the /tmp/rootbash command with -p to gain a shell running with root privileges:
Victim
Cron Jobs - Wildcards
View the contents of the other cron job script
Victim
Note that the tar command is being run with a wildcard (*) in your home directory.
Take a look at the GTFOBins page for tar. Note that tar has command line options that let you run other commands as part of a checkpoint feature.
Use msfvenom on your Kali box to generate a reverse shell ELF binary. Update the LHOST IP address accordingly:
Kali
Transfer the shell.elf file to /home/user/ on the Debian VM (you can use scp or host the file on a webserver on your Kali box and use wget). Make sure the file is executable.
Kali
Victim
Create these two files in /home/user
Victim
When the tar command in the cron job runs, the wildcard (*) will expand to include these files. Since their filenames are valid tar command line options, tar will recognize them as such and treat them as command line options rather than filenames.
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.
Kali
SUID / SGID Executables - Known Exploits
Find all the SUID/SGID executables on the Debian VM.
Victim
Note that /usr/sbin/exim-4.84-3 appears in the results. Try to find a known exploit for this version of exim. Exploit-DB, Google, and GitHub are good places to search!
A local privilege escalation exploit matching this version of exim exactly should be available. A copy can be found on the Debian VM at /home/user/tools/suid/exim/cve-2016-1531.sh.
Run the exploit script to gain a root shell:
Victim
SUID / SGID Executables - Shared Object Injection
The /usr/local/bin/suid-so SUID executable is vulnerable to shared object injection.
First, execute the file and note that currently it displays a progress bar before exiting.
Victim
Run strace on the file and search the output for open/access calls and for "no such file" errors.
Victim
Note that the executable tries to load the /home/user/.config/libcalc.so shared object within our home directory, but it cannot be found.
Create the .config directory for the libcalc.so file.
Victim
Example shared object code can be found at /home/user/tools/suid/libcalc.c. It simply spawns a Bash shell. Compile the code into a shared object at the location the suid-so executable was looking for it.
Victim
Execute the suid-so executable again, and note that this time, instead of a progress bar, we get a root shell.
Victim
SUID / SGID Executables - Environment Variables
The /usr/local/bin/suid-env executable can be exploited due to it inheriting the user's PATH environment variable and attempting to execute programs without specifying an absolute path.
First, execute the file and note that it seems to be trying to start the apache2 webserver.
Victim
Run strings on the file to look for strings of printable characters.
Victim
One line ("service apache2 start") suggests that the service executable is being called to start the webserver, however the full path of the executable (/usr/sbin/service) is not being used.
Compile the code located at /home/user/tools/suid/service.c into an executable called service. This code simply spawns a Bash shell.
Victim
service.c
Prepend the current directory (or where the new service executable is located) to the PATH variable, and run the suid-env executable to gain a root shell.
Victim
SUID / SGID Executables - Abusing Shell Features (#1)
The /usr/local/bin/suid-env2 executable is identical to /usr/local/bin/suid-env except that it uses the absolute path of the service executable (/usr/sbin/service) to start the apache2 webserver.
Verify this with strings
Victim
In Bash versions <4.2-048 it is possible to define shell functions with names that resemble file paths, then export those functions so that they are used instead of any actual executable at that file path.
Verify the version of Bash installed on the Debian VM is less than 4.2-048.
Victim
Create a Bash function with the name "/usr/sbin/service" that executes a new Bash shell (using -p so permissions are preserved) and export the function:
Victim
Run the suid-env2 executable to gain a root shell.
Victim
SUID / SGID Executables - Abusing Shell Features (#2)
Note: This will not work on Bash versions 4.4 and above.
When in debugging mode, Bash uses the environment variable PS4 to display an extra prompt for debugging statements. Run the /usr/local/bin/suid-env2 executable with bash debugging enabled and the PS4 variable set to an embedded command which creates an SUID version of /bin/bash.
Victim
Run the /tmp/rootbash executable with -p to gain a shell running with root privileges.
Victim
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.
Victim
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!
Switch to the root user, using the password.
Victim
Passwords & Keys - Config Files
Config files often contain passwords in plaintext or other reversible formats.
List the contents of the user's home directory.
Victim
Note the presence of a myvpn.ovpn config file. View the contents of the file.
Victim
The file should contain a reference to another location where the root user's credentials can be found. Switch to the root user, using the credentials.
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.
Victim
Note that there appears to be a hidden directory called .ssh. View the contents of the directory.
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.
Victim
Use the key to login to the Debian VM as the root account (note that due to the age of the box, some additional settings are required when using SSH):
Kali
NFS
Files created via NFS inherit the remote user's ID. If the user is root, and root squashing is enabled, the ID will instead be set to the "nobody" user.
Check the NFS share configuration on the Debian VM.
Victim
Note that the /tmp share has root squashing disabled.
On your Kali box, switch to your root user if you are not already running as root.
Kali
Using Kali's root user, create a mount point on your Kali box and mount the /tmp share (update the IP accordingly).
Kali
Still using Kali's root user, generate a payload using msfvenom and save it to the mounted share (this payload simply calls /bin/bash).
Kali
Still using Kali's root user, make the file executable and set the SUID permission.
Kali
Back on the Debian VM, as the low privileged user account, execute the file to gain a root shell.
Victim
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.
Victim
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).
Victim
Once the exploit completes, run /usr/bin/passwd to gain a root shell:
Victim
Last updated