the_open_door
coffee
ESEN
← Back to index

apt privilege escalation via sudo (Pre-Invoke hook)

ATT&CKT1548.003
Full command arsenal in the cheatsheet

apt accepts hooks: lists of commands it hands to /bin/sh before or after its work. They exist so an administrator can chain tasks (mount a partition, notify a service, rebuild a cache) and they do not distinguish between a task and an interactive shell. The update hook is APT::Update::Pre-Invoke, and apt's own is DPkg::Pre-Invoke.

Half the trick is that the hook can be passed on the command line with -o, so there is no need to be able to write into /etc/apt/apt.conf.d/:


sudo apt update -o APT::Update::Pre-Invoke::="/bin/bash"

The bash is born with the identity apt is running as, which is the one sudo just gave it. And that does not have to be root: a rule like (deamon) /usr/bin/apt update is just as usable to become deamon, which is the lateral move almost nobody watches.


sudo -u <user> apt update -o APT::Update::Pre-Invoke::="/bin/bash"

State gets in the way before permissions do. apt wants to write into /var/lib/apt/lists and /var/cache/apt/archives, and the user we jump to need not be able to: the first attempt usually dies on an unable to lock directory. Do not fight the permissions, tell apt to work somewhere else:


sudo -u <user> apt update -o Dir::State=/tmp -o Dir::Cache=/tmp -o APT::Update::Pre-Invoke::="/bin/bash"

The update does not even have to succeed. On a machine with no internet access the command returns four Err: lines with Temporary failure resolving and even a Permission denied while clearing the cache: it does not matter. The hook runs anyway, because what is abused is not the download but the order in which apt does things. It is apt's entry in GTFOBins and textbook sudo abuse: it exploits no flaw, it uses apt for what apt is meant to do.

Requirements#

A sudo rule that allows running apt or apt-get as another user accepting arguments (a *, or apt update in a form that lets a -o through). No network, no reachable repositories, and no need for the target user to be root.

How to detect it. /var/log/auth.log keeps the sudo ... COMMAND=/usr/bin/apt with the whole line: an apt with Pre-Invoke, Post-Invoke or Dir::State among its arguments is the signature, because an administrator who needs a hook puts it in an apt.conf.d file, not on the command line. On the host, auditd over execve shows a sh or bash that is a child of apt. And the package history (/var/log/apt/history.log) will record no installation that justifies the call.

Defence. Do not grant sudo over package managers: apt, dpkg, yum and pip run third-party code by design and any of them is a shell with extra steps. If updating must be delegated, delegate a purpose-built script that takes no arguments, and make sure that script is not writable by whoever runs it. NOEXEC is no help here, because apt launches the hook itself.

Machines where it's used