
Linux PrivEsc
This guide contains the answer and steps necessary to get to them for the Linux PrivEsc room.
Table of contents
- Deploy the Vulnerable Debian VM
- Service Exploits
- Weak File Permissions - Readable /etc/shadow
- Weak File Permissions - Writable /etc/passwd
- Sudo - Shell Escape Sequences
- Sudo - Environment Variables
- Cron Jobs - File Permissions
- Cron Jobs - PATH Environment Variable
- Cron Jobs - Wildcards
- SUID / SGID Executables - Known Exploits
- SUID / SGID Executables - Shared Object Injection
- SUID / SGID Executables - Environment Variables
- SUID / SGID Executables - Abusing Shell Features (#1)
- SUID / SGID Executables - Abusing Shell Features (#2)
- Passwords & Keys - History Files
- Passwords & Keys - Config Files
- Passwords & Keys - SSH Keys
- NFS
- Kernel Exploits
- Privilege Escalation Scripts
Deploy the Vulnerable Debian VM
Username: user
Password: password321
https://www.exploit-db.com/exploits/1518

Service Exploits
In this taask we will exploit the fact that the MySQL service runs as root and this user doesn't have a password assigned to it.
Read and follow along with the above.
First we navigate to the folder containing the exploit files.
Now we compile the exploit code with the following code:
gcc -g -c raptor_udf2.c -fPIC
gcc -g -shared -Wl,-soname,raptor_udf2.so -o raptor_udf2.so raptor_udf2.o -lc


Now we can connect to the MySQL service as the root user with a blank password.

First we create a new table in the mysql database:

Then we insert the exploit code into the table and dump the output.
insert into foo values(load_file('/home/user/tools/mysql-udf/raptor_udf2.so'));
select * from foo into dumpfile '/usr/lib/mysql/plugin/raptor_udf2.so';


Lastly, we creat a User Defined Function 'do_system'.

Now we can user this function to copy /bin/bash to /tmp/rootbash.

Finally, we can navigate to the copied file and with it to get a root shell.


After we are finished, we should remove the file again.

Weak File Permissions - Readable /etc/shadow
In this task we utilize insecure read permissions for the /etc/shadow file.
- What is the root user's password hash?
We first need to find the permission we have for this file as a normal user.

Looks like the file as read and write permissions for all users. We can now view the file.
Here we can find the has for the user root between the first two :.

Click for answer
$6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVlaXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0
- What hashing algorithm was used to produce the root user's password hash?
THe first thing we can try is hash-identifier to find the hashing algorithm.

Looks like it is a SHA256 hash. However, using examples from hashcat we can find the exact hash by looking at the format.

Click for answer
sha512crypt
- What is the root user's password?
The next step is to crack the password with either hashcat or john.


Now we can use the found password to switch to the root user.
Click for answer
password123
Weak File Permissions - Writable /etc/shadow
In this task we utilize insecure write permissions for the /etc/shadow file.
Read and follow along with the above.
Again we can use ls -lh /etc/shadow to find out what the permissions for this file are.

Instead of cracking the password, we can simply add our own, since we have write permissions for this file. We can use mkpasswd to create the hashed password.
Next we can replace the root users password with our own password.

Now we can switch to the root user with our own password.

Weak File Permissions - Writable /etc/passwd
In this task we utilize insecure write permissions for the /etc/passwd file.
We first use the following command to find the permissions we have for the /etc/passwd file.

Looks like we have write access. Lets create a new password for the root user we can substitute. This can be done on our target machine or attack machine. Due to the way the machines are setup the resulting hashes may be different as they use a different method. However, the outcome should be the same.

We can do two things now. We can either replace the root users password with our new one. Or we can copy the root user line in the passwd file and change the name and password. I will use the second option here.

Now we can switch to our new user with the following and enter the newly created password:

- Run the "id" command as the newroot user. What is the result?

Click for answer
uid=0(root) gid=0(root) groups=0(root)
Sudo - Shell Escape Sequences
In this task we will abuse the insecure sudo settings for various bins on the file system.
- How many programs is "user" allowed to run via sudo?
We can use sudo -l to view all the executables we can run with sudo.

Click for answer
11
- One program on the list doesn't have a shell escape sequence on GTFOBins. Which is it?
After going through the entire list in GTFOBins, there is one binary that was not listed on the website.
Click for answer
apache2
Consider how you might use this program with sudo to gain root privileges without a shell escape sequence.

https://touhidshaikh.com/blog/2018/04/abusing-sudo-linux-privilege-escalation/
Extra challenge: We can use (https://gtfobins.github.io/) to find out how to get an elevated shell with each binary.
awk

iftop

find

ftp

less

man

more

nano

nmap

vim

Sudo - Environment Variables
In this task we will use the environmental variable settings for sudo.
Using sudo -l we can check which environment variables are inherited.

Read and follow along with the above.
First we create a shared object using the code provided:
Next, we run one of the programs we are allowed to run with sudo

We run ldd to check which shared libraries are used by the program.

Now we created another shared object with the same name as one of the listed libraries.
And now we run apache with sudo

Cron Jobs - File Permissions
In this task we will use weak file permissions for scheduled tasks.
First we look at the contents of the system-wide crontab.

Looks like one of the tasks executes a script. We can easily locate it with locate.

Now that we know its location, lets look at the permissions we have for this file.

It seems like we have write access to it. How convenient. Lets open it up to edit the contents of the file.
Now we add the following code to the file.

Then we start a listener on our device and wait for the task to execute a create a shell for us.

Cron Jobs - PATH Environment Variable
In this task we use the PATH variable to execute our own code.
First we look at the contents of the system-wide crontab.

Looks like it looks for the scripts in /home/user. Lets create our own script in this folder.

Now we add the following code to this file (similar to the previous task)

Next, we must make sure this script is executable with chmod.

If executed properly, this should create an executable we can run in the /tmp/ folder. Running the following command should give us a root shell.

Before moving on, we must remove the script.

- What is the value of the PATH variable in /etc/crontab?
Click for answer
/home/user:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Cron Jobs - Wildcards
In this task we will utilize the fact that tar can be used with a wildcard to run extra commands.
Read and follow along with the above.
Lets look at the other file in the crontab.

On GTFO Bins we can see which commands we can run with tar. We can use msfvenom to create the necessary payload.
Now we use scp to transfer this file to our target machine.

We must make this file executable:
Next we must create two files in the user folder.

Now we just set up a listener on our machine a wait for the task to run.

Lastly, we must remove the files again.
rm /home/user/shell.elf
rm /home/user/--checkpoint=1
rm /home/user/--checkpoint-action=exec=shell.elf
SUID / SGID Executables - Known Exploits
In this task we will abuse known exploits for binaries with their SUID bit set.
Read and follow along with the above.
We use the following command to find all executables with their SUID/SGID bit set.

Another method I use myself is:

Looks like there is an exploit we can use.

We can use the pre-made script from the machine.

However, we can also create this file ourselves from the exploit database.


Make sure it is executable and then run the script.

SUID / SGID Executables - Shared Object Injection
In this task we exploit an executable that is vulnerable to shared object injection.
Read and follow along with the above.
The /usr/local/bin/suid-so executbale is vulnerable and when running it will give a progress bar.

We now run strace to search the file for open/access calls and 'no such file' errors.

Now we create the folder in which it is looking for a file (libcalc.so).
Next, we compile a shared object that will give us a root shell.

If we now run the executable again, we get a rootshell instead.

SUID / SGID Executables - Environment Variables
In this task we exploit an executable due to it inheriting the user's PATH environment variable.
Read and follow along with the above.
First, we run the file to see what it tries to run.

Looks like it tries running apache2 webservers. Running the following command we look for anything related to apache2.

The full path for the apache2 service is not used. So we create an executable that will be run inplace of the real one.

Now we must add the current directory to the PATH variable.

Finally, we can run the executable again to get a root shell.

SUID / SGID Executables - Abusing Shell Features (#1)
In this task we have a similar situation as the previous one. Only this time the executable uses the absolute path.
Read and follow along with the above.
We can verify this we the following command:

We need to check the bash version as well as prior to 4.2-048 we can define a shell function with a name that resembles file paths.

Now, we create a bash function /usr/sbin/service that executes a bash shell.

Finally, we can run the executable to get a root shell.

SUID / SGID Executables - Abusing Shell Features (#2)
In this task we will exploit a debugging feature in bash 4.3 and prior.
Read and follow along with the above.
First, we 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:
env -i SHELLOPTS=xtrace PS4='$(cp /bin/bash /tmp/rootbash; chmod +xs /tmp/rootbash)' /usr/local/bin/suid-env2

Now we can run the executable to get a root shell.

After we are finished, we should remove the file for this challenge.
Passwords & Keys - History Files
In this task we utilize commands being saved in a file and accidentally entered credentials.
- What is the full mysql command the user executed?
We can look at the history of commands typed with the following command:

This efectively looks through all files with 'history' in its name.

With the credentials we found here, we can switch to the root user.

Click for answer
mysql -h somehost.local -uroot -ppassword123
Passwords & Keys - Config Files
In this task we use the fact that passwords may sometimes be stored in files as cleartext.
- What file did you find the root user's credentials in?
Looking through the folder we can see there is a vpn config file.

We can read the file to see if we can find anything interesting.

Looks like there could be credentials stored in one of the files mentioned here.

Now we can switch to the root user with this password.

Click for answer
/etc/openvpn/auth.txt
Passwords & Keys - SSH Keys
In this task we make use of lingering backup files with credentials.
Read and follow along with the above.

We can see several hidden folders, of which the following might be of interest.

Lets look at the key contents.

With can copy this key to our attack machine to ssh into the target with it a get root access.

We must give the key the right permissions, otherwise the ssh client will not accept it.


Now it does work.

NFS
In this task we use tha fact that files created via NFS inherit the remote user's ID.
- What is the name of the option that disables root squashing?
First we check the NFS share configuration.

It seems root squaching is not enabled.
On our attack machine we switch to the root user and create a mount point.

Now we can mount the target machines /tmp/ folder to our machine.

Still on our attack machine we create a payload with msfvenom.
sudo msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o /tmp/nfs/shell.elf
sudo chmod +xs /tmp/nfs/shell.elf


We can check to see if the file is indeed in the /tmp/ folder.

Finally, we can run this file from the target machine.

Click for answer
no_root_squash
Kernel Exploits
In this task we look for any kernel exploits we can use on this machine. Dirty cow in this situation.
Read and follow along with the above.
First, we run the Linux Exploit Suggester 2 tool.

Looks like we can use the Dirty COW exploit. Lets compile the code and run it.


Now that the exploit has been run, we can run the passwd file to get a root shell.

Lastly, we should remove the created files before we continue.

Privilege Escalation Scripts
In this final task we experiment with some automation scripts that look for these privelege escalation methods. Most if not all of those we manually found are listed by these scripts.
Read and follow along with the above.
In the privesc-scripts folder we find three automation scripts.
Running LinEnum first, we can see everything it comes up with, including things we previously found. Like the SUID and SGID files.

Running Linpeas, we can see everything it comes up with, including things we previously found. Like the NFS root squashing.

Running LSE, we can see everything it comes up with, including things we previously found. Like sudo permissions.
