On an event
udev Rules: Linux Persistence via RUN+= Device Events
How attackers plant udev rules with RUN+= to run code as root at boot or when a device appears, and how to find, verify and remove them on Linux.
- MITRE ATT&CK
- T1546.017
- Tactics
- Persistence, Privilege escalation
- Runs
- On an event
- Set up by
- root
- Locations
- /etc/udev/rules.d/*.rules
- /run/udev/rules.d/*.rules
- /usr/lib/udev/rules.d/*.rules
- /lib/udev/rules.d/*.rules
How it works
systemd-udevd receives every kernel uevent (a device added, removed, changed, bound to a driver) and runs it through the rules. Rule files are the *.rules files of /usr/lib/udev/rules.d (vendor), /usr/local/lib/udev/rules.d on recent systemd, /run/udev/rules.d (runtime) and /etc/udev/rules.d (local). They are sorted together by file name and processed in lexical order whatever their directory; a file with the same name in /etc replaces the one in /run, which replaces the one in /usr/lib. Files without the .rules extension are ignored.
A rule is a comma-separated list of match keys (ACTION=="add", SUBSYSTEM=="net", ATTRS{idVendor}=="…") and assignment keys. Four of them run programs, as root:
| Key | When it runs |
|---|---|
RUN / RUN{program} | After all rules are processed for the event; a name without a path is looked up in /usr/lib/udev |
PROGRAM | During matching; its exit code is a match condition, its output becomes %c |
IMPORT{program} | During matching; KEY=value lines it prints become device properties |
RUN{builtin} | An internal udev function, no process |
udev(7) is explicit about the limits: RUN programs must be short-lived, processes they fork are killed once the event is handled, and the sandbox on systemd-udevd.service blocks network access and mounts. The supported way to start something long-running is TAG+="systemd", ENV{SYSTEMD_WANTS}+="unit.service", which asks systemd to start a unit.
At boot, systemd-udev-trigger.service replays an add event for every device already present (coldplug). A rule that matches a device every machine has therefore fires at every boot, on top of hot-plug events. Debian and Ubuntu without merged /usr keep vendor rules in /lib/udev/rules.d; eudev, used on some non-systemd distributions, reads the same rule syntax.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/udev/rules.d/*.rules | Local rules; override vendor files of the same name | root |
/run/udev/rules.d/*.rules | Runtime rules on tmpfs, gone at reboot | root |
/usr/lib/udev/rules.d/*.rules, /lib/udev/rules.d/*.rules | Vendor rules shipped by packages | root (packages) |
/usr/local/lib/udev/rules.d/*.rules | Locally installed software (recent systemd) | root |
/usr/lib/udev/<helper> | Programs that rules call by bare name | root (packages) |
/etc/udev/udev.conf | Daemon settings (event timeout, log level) | root |
How attackers use it
A rule that fires at every boot
Match a device that always exists and point RUN at the payload. The sedexp malware described by Stroz Friedberg in 2024 used a rule matching /dev/random (major 1, minor 8), which is added at every boot.
# /etc/udev/rules.d/99-entropy.rules
ACTION=="add", ENV{MAJOR}=="1", ENV{MINOR}=="8", RUN+="/usr/local/lib/.entropy/rngd-helper"
Escaping the kill and the sandbox
Because udev kills what RUN leaves behind, a working implant hands off to something outside udevd: an at job, a transient unit from systemd-run, or a unit pulled in with SYSTEMD_WANTS.
ACTION=="add", SUBSYSTEM=="net", KERNEL=="lo", RUN+="/bin/sh -c 'echo /usr/local/sbin/netd | at now'"
ACTION=="add", ENV{MAJOR}=="1", ENV{MINOR}=="8", RUN+="/usr/bin/systemd-run --no-block --unit=rng-sync /usr/local/sbin/netd"
ACTION=="add", SUBSYSTEM=="usb", TAG+="systemd", ENV{SYSTEMD_WANTS}+="usb-sync.service"
The third form leaves no command in the rule at all: the payload is in a systemd service that is enabled nowhere, yet starts whenever a USB device appears. The at form leaves a job in the at spool.
Triggers on demand
A rule keyed on a specific USB vendor and product ID, a network interface appearing or a block device being added gives an attacker with physical or VPN access a trigger only they can pull. udevadm trigger replays events on demand for anyone with root.
Swapping the helper
Packaged rules call helpers in /usr/lib/udev by bare name. Replacing one of those binaries keeps every rule file pristine; only package verification shows it.
How to check a host
On a live host
grep -rnE 'RUN|PROGRAM|IMPORT\{program\}|SYSTEMD_WANTS' /etc/udev/rules.d \
/run/udev/rules.d /usr/local/lib/udev/rules.d /usr/lib/udev/rules.d /lib/udev/rules.d 2>/dev/null
ls -la --time-style=full-iso /etc/udev/rules.d /run/udev/rules.d
dpkg -S /usr/lib/udev/rules.d/* /etc/udev/rules.d/* 2>&1 | grep 'no path found' # Debian/Ubuntu
rpm -qf /usr/lib/udev/rules.d/* /etc/udev/rules.d/* | grep 'not owned' # RHEL/Fedora
dpkg -V udev # or: rpm -V systemd-udev (vendor rules and helpers)
systemctl list-units --all 'run-*'
ls -la /run/systemd/transient/
atq
journalctl -b -u systemd-udevd
udevadm monitor --udev --property # watch events as they happen
udevadm test is not a safe viewer for a suspect rule: it skips RUN, but executes PROGRAM and IMPORT{program} while it simulates the event.
Offline or from a collection
Read every rules directory, not only /etc: a same-name file in /etc silently replaces a vendor rule, and a vendor directory should hold nothing a package did not install. Check ownership with dpkg -S or rpm -qf, checksums with dpkg -V, debsums or rpm -V, then hash and check ownership of every program a RUN, PROGRAM or IMPORT{program} key names. Line up the rule's mtime with /var/spool/cron/atjobs (Debian) or /var/spool/at (RHEL), transient units and the journal around boot.
Linux Persistence does the file part offline: archive the host with the collection command and drop the archive on the page.
What Linux Persistence flags
The parser reads every file in the /etc, /run, /usr/lib and /lib udev/rules.d folders. Each RUN, RUN{program}, PROGRAM or IMPORT{program} key with a quoted value becomes an entry, shown with the device match keys of its line (ACTION, SUBSYSTEM, KERNEL, ATTR{…}, ENV{…}, DRIVER); RUN{builtin} is skipped. There is no udev-specific flag: the command goes through the shared heuristics.
- Downloads and runs code, Reverse or bind shell, Decodes and runs a payload: a fetch piped into a shell, a
/dev/tcpornc -eshell, base64 fed to an interpreter (high). - Interpreter with inline code:
python -c,perl -eand the like (medium). - Runs from a temporary folder (high) and Runs from a hidden folder (medium): the command references
/tmp,/var/tmp,/dev/shm, or a dot-named path. - Detaches from the session:
nohup,setsid,screen -dm(low). Anat noworsystemd-runhand-off is not flagged by itself; read the command. - Embedded encoded blob: a long base64 or hex string (medium).
- Not installed by any package: an unowned rules file, low in
/etc/udev/rules.d, medium in/usr/libor/lib. - Differs from package package: a packaged rules file whose checksum changed, high in the vendor folders, low in
/etc. - Changed date: modified within 7 days before the newest file in the collection (low).
- program also runs from mechanisms: the same absolute program, outside
/bin,/sbin,/usr/binand/usr/sbin, is also started by another mechanism (medium).
Heuristic hits in an unmodified packaged rule are shown as info. The package checks need /var/lib/dpkg in the collection; RPM databases are not read.
Limits: ENV{SYSTEMD_WANTS} and TAG+="systemd" are not reported (the unit itself appears under systemd), helper programs are not hashed, and the tool sees files, not the events udev has processed.
Cleaning up
- Preserve before rebooting:
/run/udev/rules.dand/run/systemd/transientare on tmpfs. Copy the rule and every program it names with their times (cp -a), hash them, and savesystemctl list-units --all 'run-*',atqand the journal. - Stop what the rule started: transient units (
systemctl stop <unit>), at jobs (atrm), the payload's processes. - Delete the rule, or for a modified vendor rule reinstall the package that owns it (
apt install --reinstall <pkg>,dnf reinstall <pkg>), then runudevadm control --reloadso the daemon drops it. - Check where the same payload may also be planted: the unit named in
SYSTEMD_WANTS, cron and at, kernel modules configuration, and any other rule or unit that runs the same program. - Treat everything readable by root as exposed: rotate host secrets, SSH host and user keys, and credentials stored on the machine.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: