SUID systemctl (a unit of your own)
Full command arsenal in the cheatsheetsystemctl is the boss of services: it decides which command the machine runs and under which identity. With the SUID bit set, or in a sudo rule, that stops being an administration tool and becomes a root shell with one extra step, because the service it starts is the one we define.
The first thing is spotting it in the list of SUID binaries. Almost everything there ships with the system (passwd, su, sudo, mount, ping, chsh); systemctl does not:
find / -perm -4000 2>/dev/nullThe shortest unit that does the job is three lines long:
[Service]
Type=oneshot
ExecStart=/bin/bash -c "chmod +s /bin/bash"
[Install]
WantedBy=multi-user.targetType=oneshot means it starts, does its thing and exits without staying resident, which is exactly what we want. And what it does is not open a shell: a shell would die with the service, and its output would never reach our terminal. What it does is leave a binary that already exists marked, and that outlives the service.
/bin/systemctl enable /dev/shm/root.service
/bin/systemctl start root
/bin/bash -pThe enable with an absolute path is what lets the unit live in a directory of ours (/dev/shm, /tmp): systemd creates the symlink and accepts the file without any need to write into /etc/systemd/system/. The trailing -p is essential: without it bash drops the EUID and the shell comes out as the same old user (SUID abuse).
Variants of the same idea, in case the payload has to come out elsewhere: an ExecStart that writes to /etc/sudoers, that copies a key into /root/.ssh/authorized_keys, or that launches a reverse shell with an explicit User=root. The difference from a writable .service is where the permission comes from: there you edit a unit that already exists and you need a trigger; here the unit is yours and the trigger is systemctl itself.
Requirements#
systemctl runnable with privileges: the SUID bit set, a sudo rule over it, or the equivalent capability. Plus a writable directory to drop the .service in. No reboot, and nothing to wait for.
How to detect it. journalctl records the unit starting, with its name and its ExecStart, so a service nobody installed, pointing at /dev/shm or /tmp and running exactly once, is the clearest signature there is. systemctl list-unit-files --state=enabled shows it enabled from a path outside /etc and /lib. On the host, a new SUID binary against a baseline of find / -perm -4000 closes the detection, and auditd over execve sees the chmod +s with uid=0 and systemd as its parent.
Defence. systemctl must never be SUID: if it is, that is a misconfiguration, not a requirement. To delegate control of one specific service there are polkit rules, which authorise that service and that action, and a sudo rule of the form systemctl restart <service> with no wildcard. And watch for enabled units outside the system directories, which is where this leaves its trace.
Machines where it's used
- JarvisHackTheBox