# 2049 - Pentesting NFS Service

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？ 想要看到您的**公司在HackTricks中做广告**吗？ 或者想要访问**PEASS的最新版本或下载PDF格式的HackTricks**吗？ 请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[NFTs收藏品**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks)**和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud)**提交PR来分享您的黑客技巧**。

</details>

## **基本信息**

**NFS**是一个为**客户端/服务器**设计的系统，使用户能够无缝访问网络上的文件，就像这些文件位于本地目录中一样。

该协议的一个显著特点是其缺乏内置的**身份验证**或**授权机制**。相反，授权依赖于**文件系统信息**，服务器负责准确地将**客户端提供的用户信息**转换为文件系统所需的**授权格式**，主要遵循**UNIX语法**。

身份验证通常依赖于**UNIX `UID`/`GID`标识符和组成员资格**。然而，由于客户端和服务器之间\*\*`UID`/`GID`映射**可能不匹配，因此服务器无法进行额外的验证。因此，该协议最适合在**受信任的网络\*\*中使用，因为它依赖于这种身份验证方法。

**默认端口**：2049/TCP/UDP（除了版本4，它只需要TCP或UDP）。

```
2049/tcp open  nfs     2-3 (RPC #100003
```

### 版本

* **NFSv2**：这个版本以其与各种系统的广泛兼容性而闻名，最初主要通过UDP进行操作。作为系列中**最古老**的版本，它为未来的发展奠定了基础。
* **NFSv3**：引入了一系列增强功能，NFSv3通过支持可变文件大小和提供改进的错误报告机制扩展了其前身。尽管有所进步，但它在与NFSv2客户端的完全向后兼容方面存在限制。
* **NFSv4**：作为NFS系列中的一个里程碑版本，NFSv4带来了一系列旨在现代化网络文件共享的功能。值得注意的改进包括集成Kerberos以实现**高安全性**，能够穿越防火墙并在互联网上运行而无需端口映射器，支持访问控制列表（ACL），以及引入基于状态的操作。其性能改进和采用有状态协议使NFSv4成为网络文件共享技术中的重要进步。

每个NFS版本都是为了满足网络环境不断发展的需求而开发的，逐渐增强安全性、兼容性和性能。

## 枚举

### 有用的nmap脚本

```bash
nfs-ls #List NFS exports and check permissions
nfs-showmount #Like showmount -e
nfs-statfs #Disk statistics and info from NFS share
```

### 有用的metasploit模块

```bash
scanner/nfs/nfsmount #Scan NFS mounts and list permissions
```

### 挂载

要知道服务器上有哪个文件夹可用于挂载，可以使用以下命令询问：

```bash
showmount -e <IP>
```

然后使用以下命令挂载它：

```bash
mount -t nfs [-o vers=2] <ip>:<remote_folder> <local_folder> -o nolock
```

您应该指定**使用版本2**，因为它没有**任何** **身份验证**或**授权**。

**示例:**

```bash
mkdir /mnt/new_back
mount -t nfs [-o vers=2] 10.12.0.150:/backup /mnt/new_back -o nolock
```

## 权限

如果您挂载一个包含**只能被某个用户（通过UID）访问的文件或文件夹**的文件夹。您可以**在本地创建**一个具有该**UID**的用户，并使用该**用户**即可**访问**该文件/文件夹。

## NSFShell

要轻松列出、挂载和更改UID和GID以访问文件，您可以使用[nfsshell](https://github.com/NetDirect/nfsshell)。

[不错的NFSShell教程。](https://www.pentestpartners.com/security-blog/using-nfsshell-to-compromise-older-environments/)

## 配置文件

```
/etc/exports
/etc/lib/nfs/etab
```

### 危险设置

* **读写权限 (`rw`):** 此设置允许对文件系统进行读取和写入操作。必须考虑授予此类广泛访问权限的影响。
* **使用不安全端口 (`insecure`):** 启用此选项后，系统可以利用高于1024的端口。这些范围之上的端口安全性可能较低，增加了风险。
* **嵌套文件系统的可见性 (`nohide`):** 此配置使得即使在导出目录下挂载了另一个文件系统，目录仍然可见。每个目录都需要自己的导出条目以进行正确管理。
* **根文件所有权 (`no_root_squash`):** 使用此设置，由根用户创建的文件将保持其原始的 UID/GID 为 0，不考虑最小权限原则，可能授予过多权限。
* **不压缩所有用户 (`no_all_squash`):** 此选项确保用户身份在整个系统中保持不变，如果处理不当，可能导致权限和访问控制问题。

## 利用 NFS 配置错误进行权限提升

[NFS no\_root\_squash 和 no\_all\_squash 权限提升](/linux-hardening/privilege-escalation/nfs-no_root_squash-misconfiguration-pe.md)

## HackTricks 自动命令

```
Protocol_Name: NFS    #Protocol Abbreviation if there is one.
Port_Number:  2049     #Comma separated if there is more than one.
Protocol_Description: Network File System         #Protocol Abbreviation Spelled out

Entry_1:
Name: Notes
Description: Notes for NFS
Note: |
NFS is a system designed for client/server that enables users to seamlessly access files over a network as though these files were located within a local directory.

#apt install nfs-common
showmount 10.10.10.180      ~or~showmount -e 10.10.10.180
should show you available shares (example /home)

mount -t nfs -o ver=2 10.10.10.180:/home /mnt/
cd /mnt
nano into /etc/passwd and change the uid (probably 1000 or 1001) to match the owner of the files if you are not able to get in

https://book.hacktricks.xyz/pentesting/nfs-service-pentesting

Entry_2:
Name: Nmap
Description: Nmap with NFS Scripts
Command: nmap --script=nfs-ls.nse,nfs-showmount.nse,nfs-statfs.nse -p 2049 {IP}
```

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

* 你在**网络安全公司**工作吗？想要看到你的**公司在HackTricks上做广告**吗？或者想要获取**PEASS的最新版本或下载HackTricks的PDF**吗？查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **通过向**[**hacktricks仓库**](https://github.com/carlospolop/hacktricks)**和**[**hacktricks-cloud仓库**](https://github.com/carlospolop/hacktricks-cloud)**提交PR来分享你的黑客技巧**。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/nfs-service-pentesting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
