Table of Contents


Summery

systemd is the Linux init and service-management system. systemctl is the main command used to interact with systemd.

From a penetration-testing perspective, systemd is useful for enumerating services, identifying custom services, inspecting ExecStart commands, finding writable service files or binaries, and identifying services that execute with elevated privileges.

Basic usage

systemctl status <service>
systemctl start <service>
systemctl stop <service>
systemctl restart <service>
systemctl enable <service>
systemctl disable <service>

Service enumeration

systemctl list-units --type=service --all - List currently loaded services
systemctl list-unit-files --type=service - List installed service unit files
systemctl list-unit-files --type=service --state=enabled - Show only enabled services
systemctl cat <service> - Show a service’s configuration
systemctl show <service> - Show detailed properties
systemctl show <service> -p ExecStart - Check what a given service executes

Finding custom services

Local/custom systemd services are commonly placed in:

/etc/systemd/system/
/usr/local/lib/systemd/system/

Package-provided services commonly reside in:

/usr/lib/systemd/system/
/lib/systemd/system/

Find local service files:

find /etc/systemd/system /usr/local/lib/systemd/system -type f -name '*.service' 2>/dev/null

Find the executable configured by local services:

grep -R '^ExecStart=' /etc/systemd/system/ 2>/dev/null

Pentesting enumeration

systemctl cat <service> - Inspect an interesting service
systemctl show <service> -p ExecStart - Check what command it executes
systemctl show <service> -p User - Check which user runs it
systemctl is-enabled <service> - Check whether it starts automatically
systemctl is-active <service> - Check its current state
systemctl list-dependencies <service> - Check dependencies

Privilege escalation

Misconfigured service abuse

Pay particular attention to services running as root.
Then inspect a given service with systemctl cat <service>.

Look for:

User=root
ExecStart=/path/to/program

Then check whether the service file, executable, script, configuration, or directories involved are writable by your current user - for example:

ls -la /path/to/program
ls -la /path/to/script
ls -la /path/to/config

A root service executing a writable script or binary can potentially provide a privilege-escalation path.

Sudo privilege and less abuse

If the terminal screen is not big enough to handle the full output of systemctl, it gets passed to less. If systemctl runs as root - for example, when you had sudo right over it in some way - less inherits root privileges as well. You can then run :sh in that less instance and get access as root.

Service files

A basic service may look like:

[Unit]
Description=Example Service
After=network.target

[Service]
User=root
ExecStart=/opt/example/app
Restart=always

[Install]
WantedBy=multi-user.target

Important directives for enumeration:

User=
Group=
ExecStart=
ExecStartPre=
ExecStartPost=
Environment=
EnvironmentFile=
WorkingDirectory=
Restart=

Logs

journalctl -u <service> - Show service logs:
journalctl -u <service> -n 50 - Show just recent logs:
journalctl -u <service> -f - Follow logs in real time:

Persistence

A service configured with:

[Install]
WantedBy=multi-user.target

Then, it can be enabled with systemctl enable <service>.
For pentesting, inspect existing services before considering persistence mechanisms.