No Root Squashing Privilege Escalation

 Network File System (NFS) is a protocol which allows files to be shared so that remote users can access and interact with files and directories as if they were working locally on the remote machine. It can be used with different operating systems, but Service Message Block is more typical for Windows machines.


Once we have gained access to a machine as a non-root user, we need to find a way to escalate to root if possible. One way to do this is to check the exports on the machine to see if any directories are being exported (shared) without having root squash enabled. We can check this with the command cat /etc/exports


We might see that a directory is being exported with no_root_squash. This can be seen in the picture below - check the /tmp directory:




Root squash changes the root user to a non-root user when sharing directories using NFS. This is a security feature which prevents remote users acting as root on the machine serving the shares. If we see no_root_squash, this means we have an opportunity to exploit this to escalate our privileges to root.


First of all, we need to make a directory on our attacking machine using mkdir /tmp/mounty (I am using /tmp/mounty as my example). Then, we can mount the exported directory which has no root squash into the newly created directory mount -o rw,vers=2 10.10.193.118:/tmp /tmp/mounty The rw means read / write. Next, we can create a one-liner C program which will open a bash as root:


echo 'int main() { setgid(0); setuid(0); system("/bin/bash"); return 0;}' > exploit.c


This can then be compiled:


gcc /tmp/mounty/exploit.c -o /tmp/mounty/exploit


Since we want root to be the owner of this file, we need to make sure that we compile it either as root or using super-user privileges (sudo).


We then need to set the SUID bit on the file using chmod +s /tmp/mounty/exploit


The following picture shows this process (ensuring sudo is used to gain super-user privileges):




Finally, we can execute the malicious file on the victim machine. We will be able to find the file in the directory which we mounted on our attacking machine - in this example it is /tmp:




Taking advantage of no_root_squash on an export is an easy way to escalate privileges on a machine. As well as looking for it manually, we can use automated tools such as linpeas which will check it for us.


Popular posts from this blog

Hacking Reset (thm)

Hacking Year of the Owl (thm)

Simple CTF (tryhackme)