NFS is a Network File Sharing protocol that allows users to share directories and files over the network across different operating systems. In addition, it will enable remote systems to be accessible locally when the shares are mounted. The accessibility of the NFS shares is based on whatever privileges are assigned to the share.
Goal: Gain unauthorized access to sensitive data or obtain a shell on the system.
From the previous chapter, we have found out that ports: 111 and 2049 ports are open.
Let's again, examine the Metasploitable 2 server with the help of the Nmap scripting engine:
We see that both of them are open, and on port 111, a “/” directory is shown under NFS mount that we can try to mount. The “/” file system is usually owned by the root. If we are allowed to mount to the “/” path, we will be able to read root files and maybe write as well.
Mounting the '/' share
Let’s start mounting the “/” share and see what type of privileges we have.
1. We will first start with creating a new directory locally in the “/mnt” directory, and I’ll call it metasploitable_share for the sake of this demo.
2. Mount the remote the NFS share “/” locally with the mount utility on Linux. Before that make sure you have enabled the superuser privileges:
3. On successful mount, you can list the directories and files on metasploitable_share directory:
4. Let's change the directories to ".ssh " and list the files and directories:
From the above, we have found that there is an authorized_keys:
An authorized key in SSH is a public key used for granting login access to users. The authentication mechanism is called public-key authentication. Authorized keys are configured separately for each user - usually in the . ssh/authorized_keys file in the user's home directory.
5. Open a terminal and run the below command to generate SSH key pair.
6. Copy the meta2_rsa.pub file to the mounted share (/mnt/metasploitable_share/root/.ssh):
7. Again go back to Terminal 1 and run ls to list the files and directories:
8. Dump the meta2_rsa.pub content to authorized_keys:
9. Again go back to terminal 2, and Connect to the Metasploitable machine with the SSH key created earlier.
As we see, we can connect to the machine as root.
Prevention
- Place the appropriate restrictions on all NFS shares like restricting the IP addresses which can mount the exposed shares.
- Allow read-only access to the exported shares if possible.
- Do not export home directories.
- Use the “root_squash” option in the NFS setting to prevent remote root users from accessing the share with high privileges.