Every program start
LD_PRELOAD and ld.so.preload: Linux Persistence
How attackers use /etc/ld.so.preload, ld.so.conf.d and LD_PRELOAD to load a library into every program, and how to find and remove it on a Linux host.
- MITRE ATT&CK
- T1574.006
- Tactics
- Persistence, Privilege escalation, Defense evasion
- Runs
- Every program start
- Set up by
- Users and root
- Locations
- /etc/ld.so.preload
- /etc/ld.so.conf
- /etc/ld.so.conf.d/*.conf
- /etc/environment
How it works
Every dynamically linked program starts in the dynamic linker (ld.so, for example /lib64/ld-linux-x86-64.so.2). Before it resolves the program's own dependencies, it loads preload objects, in this order: the libraries named in the LD_PRELOAD environment variable, those given with --preload when ld.so is run directly, and those listed, whitespace-separated, in /etc/ld.so.preload. A preloaded library's symbols take precedence over libc's, so it can replace readdir(), fopen(), accept() or pam_authenticate() in every process that loads it.
The two preload routes differ in reach:
/etc/ld.so.preloadapplies to every dynamically linked program on the host, set-user-ID binaries included, from the nextexecve(). No restart, no reboot.LD_PRELOAD(and its siblingLD_AUDIT, which loads rtld-audit libraries) applies only to processes that inherit the variable. In secure-execution mode (setuid, setgid, file capabilities),ld.soignores entries containing a slash and only loads set-user-ID libraries from the standard directories.
The library search path is a third lever. ld.so resolves dependencies through DT_RPATH, LD_LIBRARY_PATH, DT_RUNPATH, then the cache /etc/ld.so.cache, then the default directories. ldconfig builds that cache from /etc/ld.so.conf, which on Debian, Ubuntu, RHEL and Fedora is little more than include /etc/ld.so.conf.d/*.conf. Package installs that ship libraries rerun ldconfig, so a planted directory goes live at the next one. Statically linked binaries ignore all of this.
Where it lives
| Path | What | Who can write it |
|---|---|---|
/etc/ld.so.preload | Libraries loaded into every dynamic program; absent on a stock install | root |
/etc/ld.so.conf | Top-level library path configuration, normally one include line | root |
/etc/ld.so.conf.d/*.conf | Extra library directories (Debian: libc.conf, x86_64-linux-gnu.conf) | root |
/etc/ld.so.cache | Compiled cache that ld.so actually reads | root, via ldconfig |
/etc/environment | KEY=value pairs set by pam_env at login | root |
/etc/profile, /etc/profile.d/*, /etc/bash.bashrc | System-wide shell startup | root |
~/.bashrc, ~/.profile, ~/.zshrc | Per-user shell startup | the user |
~/.ssh/environment | Read at SSH login when PermitUserEnvironment is on | the user |
/etc/systemd/system/<unit>.d/*.conf | Environment=LD_PRELOAD=… for one service | root |
/etc/sudoers, /etc/sudoers.d/* | env_keep decides whether LD_PRELOAD survives sudo | root |
How attackers use it
System-wide userland rootkit
The classic: drop a shared object and list it in /etc/ld.so.preload. Its hooks filter readdir() to hide files and /proc/<pid> entries, filter fopen() on /proc/net/tcp to hide connections, and usually hide /etc/ld.so.preload itself. The open-source libprocesshider, used this way by TeamTNT's cryptomining campaigns, hides a miner from ps and top. Symbiote (2022) was likewise preloaded into every process, to hide itself and steal credentials.
$ cat /etc/ld.so.preload
/usr/local/lib/libprocesshider.so
A library folder of the attacker's own
A file in /etc/ld.so.conf.d adds a directory to the cache. A library placed there with the same soname as a system library can shadow it for every program that links it, without touching /etc/ld.so.preload.
# /etc/ld.so.conf.d/00-runtime.conf
/var/lib/.sysd/lib
Ebury took a related route: a trojanized libkeyutils.so that OpenSSH loaded as an ordinary dependency.
LD_PRELOAD in an environment file
Narrower but quieter: a variable that loads a hooking library into one user's shells, or into one service such as sshd to capture passwords.
# appended to ~/.bashrc
export LD_PRELOAD=/home/alice/.cache/fonts/libfc-ext.so
# /etc/systemd/system/ssh.service.d/override.conf (sshd.service on RHEL)
[Service]
Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libsshd-ext.so
Carry-overs into privileged commands
Defaults env_keep += "LD_PRELOAD" in sudoers lets a user inject a library into commands run as root; PermitUserEnvironment yes in sshd_config lets ~/.ssh/environment or an environment="…" key option set it at login. See sudoers and sshd configuration.
How to check a host
On a live host
Every dynamically linked tool you run loads the preloaded library too, so a rootkit can lie to cat, ls and grep. Read the key files with a statically linked binary (busybox from busybox-static; check with file "$(command -v busybox)"), and prefer the offline route below.
busybox cat /etc/ld.so.preload
busybox ls -la /etc/ld.so.preload /etc/ld.so.conf.d/
cat /etc/ld.so.conf /etc/ld.so.conf.d/*.conf
ldconfig -p | grep -vE '=> /(usr/)?lib(32|64)?/' # cached libraries outside the usual folders
ldd /bin/true # preloaded objects are listed too
grep -laE 'LD_(PRELOAD|AUDIT)=' /proc/[0-9]*/environ
systemctl show-environment
grep -rnsE 'LD_(PRELOAD|AUDIT|LIBRARY_PATH)' /etc/environment /etc/environment.d \
/etc/profile /etc/profile.d /etc/bash.bashrc /etc/security/pam_env.conf /etc/systemd \
/etc/sudoers /etc/sudoers.d /root/.bashrc /root/.profile /home/*/.bashrc \
/home/*/.profile /home/*/.zshrc /home/*/.ssh/environment
ldconfig -p only prints the cache; avoid plain ldconfig on a suspect host, since it rewrites the cache and the links.
Offline or from a collection
A mounted image is not under the rootkit's control, so /etc/ld.so.preload shows what it really contains. No Debian, Ubuntu or RHEL package ships that file: any content needs an owner, such as a known product (the Snoopy command logger installs itself there). For each library it names:
- ownership:
dpkg -S <path>orrpm -qf <path>, anddpkg -V <pkg>orrpm -Vf <path>for the packaged ones; - exported symbols:
readelf -W --dyn-syms lib.solisting libc names such asreaddir64,fopenorpam_authenticateis a hook; - times:
statthe library, the preload file and/etc/ld.so.conf.d/*(mtime and ctime), and look for other files created in the same minute.
Linux Persistence reads these files offline from an archive made with the collection command and lists every preload entry and unusual library folder.
What Linux Persistence flags
The parser reads /etc/ld.so.preload, /etc/ld.so.conf and /etc/ld.so.conf.d/*. Every library in the preload file becomes an entry; in the ld.so.conf files, include lines and glob patterns are skipped, and directories under the standard library folders (/lib, /usr/lib, /usr/local/lib, their lib32/lib64 and multiarch variants) or /opt are only listed when another flag hits.
- Library preloaded system-wide: every library listed in
/etc/ld.so.preload, always (high). An empty file shows as empty, with no entry. - Unusual library folder: a directory in
ld.so.conforld.so.conf.doutside the standard folders (medium). - Runs from a temporary folder: the path is under
/tmp,/var/tmp,/dev/shmor/run/user/<uid>(high). - Runs from a hidden folder: a dot-named file or folder in the path, other than the usual application folders in a home directory (medium).
- Sets LD_PRELOAD: an
LD_PRELOAD=orLD_AUDIT=assignment in a shell startup file,/etc/environment, a systemdEnvironment=line, a crontab variable line,~/.ssh/environmentor a forced command (high); these entries are listed under their own mechanism. - sudo keeps LD_PRELOAD, PermitUserEnvironment on, SSH environment file and Key sets environment variables report the sudo and SSH carry-overs.
- Not installed by any package: an unowned file in
/etc/ld.so.conf.d(low); Differs from package package: a packaged file such as/etc/ld.so.confwhose checksum changed (low). Both need/var/lib/dpkgin the collection; RPM databases are not read. Flags inside unmodified packaged files are shown as info only. - Changed date: the file changed within 7 days before the newest file in the collection (low).
Limits: the tool reads configuration, not libraries. It does not hash or disassemble the .so files, read /etc/ld.so.cache, /etc/environment.d or systemd's DefaultEnvironment=, or see what running processes have mapped. The pam_env.conf form LD_PRELOAD DEFAULT=… is matched too.
Cleaning up
- Preserve first. Capture memory if you can (the loaded rootkit lives there), then copy
/etc/ld.so.preload, the listed libraries and any plantedld.so.conf.dfile with their times (cp -a, ortarfrom rescue media) and recordsha256sumandstatoutput. - Check
lsattrfor the immutable bit; rootkits often set it. Clearing it and editing from rescue media or the mounted disk avoids a rootkit that hooksunlink()to protect its files. - Remove the line, or the whole file if nothing legitimate is listed, then delete the library. If
ld.someets a listed library that no longer exists it prints an error and carries on, so a stale line does not break the system. - Run
ldconfigafter removing a plantedld.so.conf.dfile, and restart or reboot: running processes keep the library mapped. - Look for the same library or a re-installer elsewhere: shell startup files, systemd drop-ins, cron, sudoers
env_keep,~/.ssh/environment, and PAM modules if the hooks targeted authentication. - Rotate what the hooks could see: passwords typed on the host, SSH keys and tokens used through sshd or sudo.
References
See also
Related techniques
On Linux Forensics
The artifacts behind this technique, in the Linux Forensics reference: