Persistence is how an attacker survives a reboot, and a rootkit is persistence plus concealment. On Linux the mechanisms are mostly ordinary system features used quietly: a systemd service, a cron job, an SSH key, a library preload, a kernel module. This skill is detection-first and is written for authorized work only: your own systems, systems you administer, or forensic lab images you are permitted to examine. The order of operations matters more than any single command. Preserve evidence first, identify every persistence mechanism second, and only then remove anything. Cleaning the first mechanism you find before looking for the others is how a second backdoor survives and how an investigation loses its timeline.
Quick Workflow
- Freeze the scene: snapshot the disk or image if possible, record the current time, and work from a copy for destructive analysis.
- Inventory persistence surfaces: systemd units and timers, cron, shell startup files, SSH keys, and scheduled jobs for every user.
- Check integrity: package verification, file hashes against a known good baseline, and kernel module inventory against the expected set.
- Check concealment: LD_PRELOAD and library paths, hidden processes and ports, and discrepancies between two independent views of the same data (for example, process list versus /proc).
- Correlate: tie every suspicious artifact to a timestamp, a user, and a log entry, and build a timeline before concluding anything.
- Preserve: copy suspicious files, unit files, and logs to offline storage with hashes before removal.
- Remediate only after the inventory is complete: remove all mechanisms in one pass, then re-verify the inventory is clean.
1. Systemd and cron persistence inventory
Systemd services and timers are the most common modern persistence mechanism, and cron remains common because it is simple and rarely audited. List everything enabled, everything with a timer, and every drop-in override, then read what each one executes and as which user. Do the same for system and per-user crontabs and for the cron directories, and check that the scripts they run are owned by root and not writable by anyone else.
systemctl list-unit-files --state=enabled --type=service
systemctl list-timers --all --no-pager
ls -la /etc/cron.d/ /etc/cron.daily/ /etc/cron.hourly/ /var/spool/cron/crontabs/ 2>/dev/null
for u in $(cut -d: -f1 /etc/passwd); do crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"; done
find /etc/systemd/system -type f -newer /etc/hostname 2>/dev/null | head -20
A service or timer you cannot tie to a package or a change ticket is a
finding, not a curiosity. Pay special attention to units that run
scripts in world-writable directories, units with ExecStartPre
pointing at a shell script, and timers with odd schedules (for example,
every minute at all hours on a host that has no such workload). The
same scrutiny applies to shell startup files: ~/.bashrc,
~/.profile, /etc/profile.d/, and ~/.ssh/rc all execute code at
login, and a single appended line there is easy to miss.
grep -rn "curl\|wget\|nc \|bash -i\|/dev/tcp" /etc/profile.d/ ~/.bashrc ~/.profile ~/.ssh/rc 2>/dev/null | head -20
ls -la /etc/profile.d/
find /home /root -maxdepth 2 -name ".bashrc" -o -maxdepth 2 -name ".profile" 2>/dev/null | xargs ls -la 2>/dev/null
2. SSH keys and account persistence
An attacker who adds one SSH key keeps access indefinitely without any
running process. Audit every authorized_keys file, every user account
(including ones with UID 0 that are not named root), and every sudoers
entry. A key with no comment, a key added recently, or a key for a user
who should not have remote access is a finding until proven otherwise.
find /home /root -name "authorized_keys" 2>/dev/null | xargs ls -la 2>/dev/null
awk -F: '$3 == 0 {print $1}' /etc/passwd
grep -v "^#" /etc/sudoers /etc/sudoers.d/* 2>/dev/null | grep -v "^$"
ls -la /root/.ssh/ /home/*/.ssh/ 2>/dev/null
Compare each key against your provisioning records. Keys are also a
timeline artifact: the file modification time of authorized_keys
often brackets the intrusion window, and the SSH daemon logs show which
key fingerprint authenticated, so correlate with journalctl -u sshd
or /var/log/auth.log before deciding a key is legitimate.
3. LD_PRELOAD and library path tampering
User-space rootkits commonly hide by preloading a malicious shared
library that intercepts libc calls, so ps, ls, and netstat lie
about what is running. Check the preload configuration and the library
search path in every place it can be set, and compare tool output
against a trusted baseline: run the same listing from a statically
linked binary or from a known-good live image and diff the results.
cat /etc/ld.so.preload 2>/dev/null
env | grep -i "LD_PRELOAD\|LD_LIBRARY_PATH"
grep -r "LD_PRELOAD" /etc/profile.d/ /etc/environment /etc/bash.bashrc 2>/dev/null
ldconfig -p | grep -v "^$" | head -20
ls -la /usr/local/lib/ /lib/x86_64-linux-gnu/ 2>/dev/null | grep -E "^-" | awk '$5 < 100000 {print}' | head -20
Any entry in /etc/ld.so.preload on a host where you did not put one
there is a high-confidence compromise indicator. Do not trust the
output of dynamically linked tools on such a host: copy the suspicious
library to offline storage, then analyze it on an isolated machine
with strings and disassembly tooling rather than executing it.
4. Kernel modules and binary integrity
Kernel-level rootkits hide by loading a malicious module or patching the running kernel. List loaded modules and compare against the expected set for the host role, check for modules with no backing file on disk (a module whose file was deleted after loading), and verify system packages against their vendor signatures and hashes.
lsmod | awk '{print $1}' | sort > /tmp/loaded-modules.txt
find /lib/modules/$(uname -r) -name "*.ko*" | xargs -n1 basename 2>/dev/null | sed 's/\.ko.*//' | sort > /tmp/disk-modules.txt
comm -23 /tmp/loaded-modules.txt /tmp/disk-modules.txt
rpm -Va 2>/dev/null | grep -E "^..5" | head -30
debsums -c 2>/dev/null | head -30
On RPM systems, rpm -Va flags files whose size, digest, or
permissions differ from the package database; on Debian systems,
debsums -c lists changed files. A changed ps, ls, ss, or sshd
binary is a classic trojaning signal, but note the caveat: legitimate
configuration files also show up, so filter for binaries and libraries
before concluding compromise. If the package database itself may be
tampered with, verify against vendor packages downloaded fresh, not
against the local database.
5. Hidden processes, ports, and cross-view checks
A rootkit that hides processes cannot hide them from every view at
once. Cross-check independent sources: the process list against
/proc entries, the listening sockets against the processes that
claim them, and the network view from the host against the view from
the switch or firewall. Discrepancies are the detection.
ls /proc | grep -E "^[0-9]+$" | wc -l
ps aux | wc -l
ss -tulpn
cat /proc/net/tcp | awk 'NR>1 {print $2}' | head -20
ls -la /proc/*/exe 2>/dev/null | grep -c "deleted"
A process that appears in /proc but not in ps output, a listening
port with no owning process, or a binary marked (deleted) still
running in memory are all concealment indicators. Rootkit scanner
tools such as rkhunter and chkrootkit automate some of these checks and
are useful as a second opinion, but they are signature-based and miss
novel rootkits; treat a clean scanner result as "no known rootkit
found", never as "host is clean".
6. Memory and timeline correlation
For a serious incident, disk artifacts alone may not be enough: a fileless implant lives only in memory. Capture memory with a trusted tool before rebooting (reboot destroys it), and analyze the image offline with a memory forensics framework, looking for injected code, hidden processes, and network connections that disk analysis missed. Build the timeline last, from all sources together: file timestamps, journal entries, authentication logs, and package changes.
journalctl --since "2026-10-20" --until "2026-10-27" --no-pager | grep -iE "ssh|sudo|systemd|accepted|failed" | tail -40
stat -c "%y %n" /etc/systemd/system/*.service 2>/dev/null | sort | tail -20
lastlog
last -F | head -30
Work the timeline backwards from the first confirmed malicious artifact. Every persistence mechanism found gets a timestamp, and any mechanism whose timestamp falls outside the known intrusion window suggests either a second intrusion or a legitimate artifact you have not yet explained. Do not close the investigation until every unexplained artifact has one of those two explanations.
Edge cases
- Legitimate administration looks like persistence: configuration management agents, monitoring services, and backup jobs all create units, timers, and keys, so baseline every host role before hunting and compare against the baseline, not against intuition.
- Container hosts multiply the surfaces: a container with a host mount or a privileged flag can write host systemd units, so a host finding may trace back to a container misconfiguration rather than a host compromise.
- Deleted-but-running binaries are also normal after package upgrades (the old binary keeps running until the service restarts), so check whether the deleted binary matches a recent upgrade before treating it as a rootkit.
rpm -Vaanddebsumsboth report legitimate configuration drift, so filter to system binaries and libraries and confirm against a freshly downloaded vendor package before escalating.- Rootkit scanners produce false positives on hardened hosts (for example, on systems with custom kernels or hardened binaries), so confirm every scanner hit with an independent manual check.
- Analyzing a suspected malicious library or module on the infected host risks executing it; always copy it to an isolated analysis machine and never run it on production hardware.
- A live system lies to you once a rootkit is active, so the strongest evidence comes from offline analysis of a disk image and a memory capture, not from tools run on the suspect host itself.
- Timestamps can be forged (timestomping), so a clean-looking timeline is not proof of innocence; corroborate file times with log entries and network records that the attacker could not easily rewrite.
Key references
- MITRE ATT&CK T1543 Create or Modify System Process: https://attack.mitre.org/techniques/T1543/
- MITRE ATT&CK T1547 Boot or Logon Autostart Execution: https://attack.mitre.org/techniques/T1547/
- MITRE ATT&CK T1014 Rootkit: https://attack.mitre.org/techniques/T1014/
- MITRE ATT&CK T1070 Indicator Removal: https://attack.mitre.org/techniques/T1070/
- SANS DFIR Linux persistence references: https://www.sans.org/white-papers/
- Linux systemd unit documentation: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
