Linux Privilege Escalation - NFS Root Squashing for CTF Creators
NFS
Root squash is an NFS feature that maps the remote root user (UID 0) to a local user with minimal privileges, typically the nobody user (UID 65534). This prevents remote root users from having root privileges on the NFS server, enhancing security.
The NFS configuration file is /etc/exports
. Here are the relevant options:
no_root_squash: This option disables root squash, allowing the root user on the client to access files on the NFS server as root. This can be risky, as it allows the creation of malicious files on the NFS share with root privileges.
no_all_squash: This option is similar to no_root_squash but applies to non-root users, preventing their UIDs from being mapped to the nobody user.
Privilege Escalation via NFS Root Squashing
On the victim machine we’ll find the directory in which NFS is hosting files:
|
|
On your adversary host, we’ll install the NFS client package:
|
|
In our adversary host, we’ll create a directory to host the NFS share:
|
|
In our adversary host, we will mount the remote share in the /tmp/nfs
directory of our adversary host, make sure to run this command with sudo.
|
|
The following error means that we don’t have permission to mount the share, try it with sudo instead.
mount.nfs: failed to apply fstab options
The following error means that we need to try another protocol version.
mount.nfs: Protocol not supported
Alternatively, we can mount it this way:
|
|
If we receive the error down by the user:
mount: /tmp/nfs: bad option; for several file systems (e.g. nfs) we might need a /sbin/mount.
This means that we don’t have an NFS client installed:
|
|
Once the share is mounted we can attempt to create a payload.
We can create a bash binary with a SUID bit:
|
|
Another payload that we can use is a custom C code:
|
|
Compile this payload and remove any existing if any, then copy the payload to the share:
|
|
Add the SUID bit to the payload executable:
|
|
Now in the victim host, we can execute any of the previous payloads to escalate privileges:
|
|
Once we’re done we can unmount the shared directory in our adversary host:
|
|