Linux Privilege Escalation - Services for CTF Creators
Services
Services are programs that run in the background waiting to be used or carrying out some tasks.
Service configuration files should never be writable by other users.
Programs used by service configuration files should never be writable by other users.
Some programs that are running as a service may be vulnerable.
Service Configuration Files
The service configuration files end with .service and define how services are managed by the operating system.
If you can write to any .service file, as an attacker you could modify it so it executes a backdoor when the service is started, restarted or stopped(sometimes you will need to wait until the machine is rebooted if you don’t have privileges to reboot the machine).
For more information regarding units read this tutorial.
The multi-user.target means that the systemd-service will start when the system reaches runlevel 2.
Here’s a table of the targets and their run levels:
Run Level | Target Units | Description |
---|---|---|
0 | runlevel0.target, poweroff.target | Shut down and power off |
1 | runlevel1.target, rescue.target | Set up a rescue shell |
2,3,4 | runlevel[234].target, multi-user.target | Set up a non-gfx multi-user shell |
5 | runlevel5.target, graphical.target | Set up a gfx multi-user shell |
6 | runlevel6.target, reboot.target | Shut down and reboot the system |
Privilege Escalation via Writable .service files
If we can write any .service
file, we could modify it so it executes your backdoor when the service is started, restarted or stopped.
We’ll create a service configuration file:
|
|
We’ll add write permissions to the service so that other users can write to this file:
|
|
We can create simple payload:
|
|
We’ll add execution permissions:
|
|
Modify the ExecStart
variable which we’ll hold the script absolute path:
|
|
Since this service is configured to run as the root user. The payload will be executed under the security context of the root user.
|
|
We’ll then receive a connection as the root user:
|
|
We can mitigate this risk by removing the other writable permissions:
|
|
Privilege Escalation via Writable service binaries
If we have write permissions over binaries being executed by services, we can change them for backdoors so that when the services get re-executed the backdoors will be executed.
When a binary has writable permissions, we can just replace the binary:
|
|
Create a backup of the original binary:
|
|
Generate a reverse shell payload:
|
|
Transfer the file to the target:
|
|
Add execute permissions:
|
|
Replace the original binary with the reverse shell payload:
|
|
Setup a listener:
|
|
Since this service is configured to run as the root user. The payload will be executed under the security context of the root user.
|
|
Then we’ll a receive a shell as the root user:
|
|
Afterwards, we can restore the backup:
|
|
Remove the binary writable permissions:
|
|
Privilege Escalation via systemd PATH
We can see the path
environment variable used by systemd with the following command:
|
|
We’ll then verify if we have write permissions in any of the previous directories before the /usr/bin
directory:
|
|
Here is how this works:

We’ll append write permissions:
|
|
If we find that we can write in any of the folders of the path we may be able to escalate privileges. we need to search for relative paths being used on service configurations files like:
|
|
Then, we’ll create an executable with the same name as the relative path binary inside the systemd PATH folder we can write, and when the service is asked to execute the vulnerable action (Start, Stop, Reload), the backdoor will be executed.
Generate a reverse shell file in our adversary host:
|
|
In our adversary host, we’ll set up an HTTP server:
|
|
On the victim host, we download the payload:
|
|
Copy the payload to replace the original binary:
|
|
Since the service is configured to run as the root user, it’ll execute the payload under security context of the root user:
|
|
We’ll then wait for the connection as the root user:
|
|
After we have elevated privileges. We can then remove the payload:
|
|
We can mitigate this risk by adding the absolute path of the binary:
|
|
Privilege Escalation via Vulnerable Service
We’ll download a vulnerable version of GNU Screen through exploit-db official site. Then we’ll install it in our victim system. We’ll start by installing cmake
with our package manager:
|
|
We’ll download GNU Screen 4.5.0 to our victim system:
|
|
Then we’ll install it with the command line using the following commands:
|
|
We’ll then verify if the program has been installed in the system:
|
|
Next, we’ll attempt to exploit the GNU Screen
program:
|
|
As shown above, vulnerable service can lead us to higher privileges.