Overview
When you log in to a Linux system, the program prompting you for a password typically doesn't verify it on its own. Instead, programs such as login (console login), su (switch user), and graphical login managers delegate authentication to PAM (Pluggable Authentication Modules). PAM handles authentication through a configurable stack of modules, which is an ordered list defined for each service. As the password is passed to these modules during authentication, each module in the stack can read the password.

SSH public-key authentication is an exception. Because it doesn't require the user to enter a password, there is no password for a skimmer (a module that captures passwords) to collect. SSH password authentication and keyboard-interactive authentication, on the other hand, do involve user-entered credentials.
That makes PAM's module configuration an attractive target. With root access, an attacker can modify PAM configuration to introduce a malicious module into the authentication stack. Depending on how the module is implemented, it can capture passwords submitted during authentication or interfere with authentication decisions, allowing unauthorised access. From the user's perspective, authentication appears to work normally, and the authentication log file will have an ordinary login recorded.
This post covers two ways to tamper with the authentication process, both requiring root access.
- A rogue PAM module is a malicious library placed in a directory where PAM loads modules from and registered through an additional line in a PAM configuration file.
- A preload hook, by contrast, is a malicious library that Linux loads into the login program itself, ahead of PAM's own library, so it can intercept credential handling without modifying PAM's configuration.
PAM can be abused in other ways as well. Those cases are covered briefly in the section on other cases and are outside the scope of this post.
How PAM works
Why PAM exists
Before PAM, applications often implemented their own authentication logic or relied on application-specific mechanisms to verify credentials. login, su, FTP servers, and other services could each handle authentication differently. Changing the authentication mechanism often required application-specific changes.
V. Samar and R. Schemers of SunSoft proposed PAM in 1995 to remove that duplication, and Linux-PAM subsequently became the widely used implementation of PAM on Linux. PAM separates the application's authentication interface from the underlying authentication mechanisms. The application interacts with PAM through its API, while PAM uses a configured stack of modules to handle authentication and other associated tasks. This allows administrators to change authentication methods without having to modify each application (pam(8)).
That flexibility is also the weakness this post is about. Whoever can edit that list, or change a module on it, decides how logins on the machine are checked and can see the passwords that pass through.
The login path

The flowchart shows the path of a login. A login program, which is any program that needs to know who the user is (for example sshd, su or sudo), does not check the password itself. It hands the check to PAM and says which service it is acting for.
PAM then reads that service's file under /etc/pam.d/. There is one file per service, so su command has /etc/pam.d/su, and opening any of them shows a list of lines. Each line names one module, a small program that does one job, and says how to treat it. The pam_rootok manual gives two lines for su that let root run it without a password:
# su authentication. Root is granted access by default.
auth sufficient pam_rootok.so
auth required pam_unix.so
Each line has four parts: the type, the control, the module, and optional arguments, which these lines do not use. The type, auth, says the line belongs to the stage that checks who the user is. The module is the program PAM runs: pam_rootok.so checks whether the user is root, and pam_unix.so checks local passwords. The control says what PAM does with the answer, here sufficient and required.
When root runs su, pam_rootok.so recognises root and succeeds, and because the control is sufficient, PAM stops there and never asks for a password. For anyone else, it fails, PAM moves on to the next line, and pam_unix.so asks for the password and checks it. Because that line is required, a wrong password fails the login.
The same four parts appear in every line of every file. Besides auth, the type can be account, which checks that the account may log in, session, which sets up the login and cleans up when it ends, or password, which runs only when the user changes the password. PAM runs the stages in the order the flowchart shows, and a failure in auth or account ends in access denied. The control can also be requisite, which is like required but stops at the first failure, or optional, which normally does not change the outcome (pam.conf(5)).
The modules are .so files that sit in one directory, which depends on the distribution:
- Debian and Ubuntu:
/lib/x86_64-linux-gnu/security/on x86-64 and/lib/aarch64-linux-gnu/security/on arm64 - RHEL 9 and its rebuilds:
/usr/lib64/security/
A plain module name in a line is looked up in that directory, and a full path is used as given. Real systems combine many modules, such as pam_google_authenticator.so for one-time codes and pam_limits.so for resource limits.
A file can also include another file so that several services share the same lines. Two things therefore decide how a login is checked: the lines in the files under /etc/pam.d/ and the module files in the module directory.
Why PAM is exploitable
Modules can read the typed password
PAM does not protect the typed password from the modules in the list. The first module that needs it asks the user, and PAM keeps what was typed so that the next modules can read it too.
In the su example, pam_unix.so asks for the password and checks it. A third line naming another module would run in the same login, and that module could read the password the user just typed.
PAM does not check the modules it loads
PAM runs whatever module a line points to, by name or by full path, as long as it can find and load the file. It has no code signing or allowlist of its own, so it has no way of telling a genuine module from a replaced or added one. Kernel module signing does not help, because these are ordinary libraries, not kernel code. Any check has to come from outside PAM.
Rogue PAM module
A rogue module is a PAM module the attacker writes and installs. It takes two changes, both as root: the module file goes into the module directory, and one line naming it is added to the service file of the program being targeted, such as /etc/pam.d/su. PAM then runs it like any other module.
Figure 4. How a rogue module is installed in our test.
In our Ubuntu 22.04 lab we wrote a small C module, pam_demo.c (generated by Claude), that reads the user name and the typed password and writes them to a file, as the screenshot shows.

We compiled it, copied it into the module directory, and appended one line to the su file:
sudo cp /tmp/pam_demo.so /lib/x86_64-linux-gnu/security/pam_demo.so
echo "auth optional pam_demo.so" | sudo tee -a /etc/pam.d/su
The su file now ends with the added line, after the lines that pull in the shared files (Figure 5).

The new line is optional, so it does not change whether the login succeeds. We ran su as a normal user and typed the password. The login worked as usual, and /tmp/pam_demo_log held the host, the service, the user name, the password in plain text and a timestamp. Root would not reach the line, because pam_rootok.so is sufficient and ends the check first.
A real module would send the password to the attacker instead of a file. Nextron Systems analysed a PAM module backdoor of fewer than 100 lines that sends the user name, password, and host details to a command-and-control server and accepts a hardcoded password for any account.
Where the line goes decides how many logins it affects. Our test line went into the su file, so it only runs when someone uses su. Several services share the file common-auth, though. On our Ubuntu lab host sshd, sudo and login all include it, so a line added there would run for SSH, sudo and console logins too.
The position of the line matters as well, because PAM reads the lines from top to bottom. A line placed near the top with sufficient could approve a password chosen by the attacker and stop there, just as pam_rootok.so does for root in the su example, so the real check by pam_unix.so never runs.

Preload hook
A preload hook is a library the attacker writes so that Linux loads it into a login program, such as sshd or sudo, when the program starts. Linux lets a library that loads first replace functions in the libraries that load after it, so when the program asks PAM to check a password, the request reaches the hook first. The hook can read the password and then pass the request on unchanged, so the login still works.
The attacker lists the library in /etc/ld.so.preload, a file that applies to every program on the system and is normally empty or absent. That takes two changes, both as root: a new library file, and one added line naming it in that file. Nothing in PAM's configuration changes.
Figure 7. How a preload hook is prepared in our test.
From then on, every program that starts loads the library, including the login programs, so it can log typed passwords or tamper with the result of the login. A hook can also be loaded into a single service through LD_PRELOAD in that service's settings. We did not test that route.
Figure 8. A login with a preload hook. The library is loaded into the login program's process, here sshd, through a service setting or the preload file.
Trend Micro reported QLNX compiling two such PAM libraries on the victim host and installing both through the preload file.
Rootkits can also hide the file. The open-source vlany rootkit patches the loader to read a different, hidden file instead of /etc/ld.so.preload, and Intezer's analysis of OrBit, a fork of the Medusa rootkit, describes it as still in the wild nearly four years on, with a 2025 build that can approve or deny logins. A clean /etc/ld.so.preload is therefore not proof of a clean host.
Other cases
PAM can be abused in other ways too. These four are worth knowing about.
Approving every login
pam_permit.so is a module that always says yes, and its manual calls it "very dangerous". Listed first with sufficient, it makes PAM approve every login, the same way pam_rootok.so approves root in the su example.
Sending passwords to a script
pam_exec.so runs a script during a login. With its expose_authtok option, the script can read the typed password. An attacker's script can then collect the passwords. Group-IB documented pam_exec as a persistence route in September 2024.
Switching off the extra check
Deleting the line that calls a multi-factor authentication module turns that check off. MITRE tracks this as T1556.006, Modify Authentication Process: Multi-Factor Authentication, and its detection strategy names the removal of MFA hooks from /etc/pam.d/ configurations.
Replacing a system module
An attacker can overwrite pam_unix.so, the module that checks passwords, with a modified copy, which hides the backdoor inside a file that already belongs to the system. MITRE records Skidmap doing this, with a version of pam_unix.so that accepts a backdoor password for all users. Most of the variants in the Nextron Systems analysis had zero or one VirusTotal detections when they were found, so signature scanning alone is not enough.
MITRE ATT&CK mapping
The table maps each case in this post to the techniques MITRE lists for it.
| Tactic | Technique ID | Technique | How it appears here |
|---|---|---|---|
| Credential Access | T1556.003 | Modify Authentication Process: Pluggable Authentication Modules | A rogue module and its line in a service file, a replaced pam_unix.so, a module that approves every login, or one that sends passwords to a script. |
| Stealth | T1574.006 | Hijack Execution Flow: Dynamic Linker Hijacking | A library listed in /etc/ld.so.preload loads into every program ahead of PAM's own library. |
| Collection, Credential Access | T1056.004 | Input Capture: Credential API Hooking | The preload library sits in front of the login program's credential handling and captures the typed password. |
| Defense Impairment | T1556.006 | Modify Authentication Process: Multi-Factor Authentication | Deleting the line that calls a multi-factor authentication module removes the extra check. |
Detection with Guardsix SIEM
The sections below follow each case through the artifacts it leaves on the host. The rogue module leaves a file and a configuration line, and PAM's own audit records show whether it was loaded. The preload hook leaves a library file and a setting that loads it.
Required log sources
- auditd, forwarded as syslog and normalised as
Unix. It supplies the file records, the process records and the PAM records thatsshd,sudo,suandloginwrite. - Sysmon for Linux, normalised as
UnixSysmon. Optional. It adds file-creation events once rules for the PAM paths exist. - Authentication messages, the content of
auth.logon Ubuntu, received in syslog format. Supporting. They record module load failures.
Required telemetry configuration
auditd and Sysmon for Linux record none of the events below unless they are configured to. This is what each one needs.
Auditd
Watch writes to the PAM module directory, the PAM configuration files, the library directories, and /etc/ld.so.preload with auditd. These are the rules we loaded on an Ubuntu 22.04 x86-64 lab host, and the table shows which detection each one feeds.
| Detection | PATH |
|---|---|
| Detecting changes to PAM configuration | /etc/pam.d, /etc/pam.conf, /etc/security |
| Detecting the malicious PAM module | /usr/lib/x86_64-linux-gnu/security |
| Detecting the malicious library file | Everything under /usr, which includes the library directories |
| Detecting changes to Preload file | /etc/ld.so.preload |
-w /etc/pam.d -p wa -k pam_config_tamper
-w /etc/pam.conf -p wa -k pam_config_tamper
-w /etc/security -p wa -k pam_config_tamper
-a always,exit -F arch=b64 -S all -F dir=/usr/lib/x86_64-linux-gnu/security -F perm=wa -F key=pam_module_tamper
-a always,exit -F arch=b32 -S all -F dir=/usr/lib/x86_64-linux-gnu/security -F perm=wa -F key=pam_module_tamper
-a always,exit -F arch=b32 -S all -F dir=/usr -F perm=wa -F key=usr_writes
-a always,exit -F arch=b64 -S all -F dir=/usr -F perm=wa -F key=usr_writes
-w /etc/ld.so.preload -p wa -k systemwide_preloads
On RHEL, the module directory is /usr/lib64/security/. The usr_writes rule already records writes there because it watches everything under /usr, and the queries match that path. On arm64, replace the x86-64 module directory with the arm64 one in the pam_module_tamper rules and in the queries.
None of these rules cover /tmp, /var/tmp or /dev/shm, so a library first dropped to one of those directories leaves no record.
Sysmon for Linux
- Add file-creation rules for the PAM module directory,
/etc/pam.d/and the library directories. For new modules, the module directory is/usr/lib/x86_64-linux-gnu/securityon Debian and Ubuntu and/usr/lib64/security/on RHEL. - For file modifications and file access, prefer auditd over Sysmon for Linux. An auditd watch records writes and attribute changes to a path, and reads when the read permission is added. The rules above use it to see changes to files such as
/etc/pam.d/suand/etc/ld.so.preload.
Hunting a rogue PAM module
A rogue module needs two things on the host: the module file, usually in the folder PAM loads modules from, and a line in a PAM configuration file that tells PAM to load it. The hunt follows those two artifacts:
Detecting the malicious module
PAM loads modules from a default folder, /lib/x86_64-linux-gnu/security/ on Debian and Ubuntu, so an attacker usually puts the file there. A configuration line can also name a module by full path, so a module in another folder is possible, and this query would miss it. The configuration query below catches that case, because the line must exist either way. Auditd records a created file as a path record with the name type Create. This query lists the files created in the module directory.
((norm_id=UnixSysmon label=File label=Create) OR
(norm_id=Unix event_type=Path name_type=Create))
path IN ["*/lib/x86_64-linux-gnu/security/*","*/usr/lib64/security/*"]

Auditd splits one action across several record types sharing a record_id — a SYSCALL record (program and user), one or more PATH records (each file the syscall touched), a PROCTITLE record (the command line), and others such as CWD or EXECVE depending on the syscall. Your SIEM forwards each as a separate event.
A PATH record names the file but not the writer. One operation produces several records sharing a record_id — the PATH record carries the path, the SYSCALL record carries the program (command) and user. Joining them gives you the actor:
[norm_id=Unix event_type=Syscall
system_call IN ["open","openat","openat2","creat","rename","renameat","renameat2"]]
as s
join
[norm_id=Unix event_type=PATH
path IN ["*/lib/x86_64-linux-gnu/security/*","*/usr/lib64/security/*"]
name_type="CREATE"] as p
on s.record_id = p.record_id and s.host = p.host

EXECVE would show the launch of a program, but copying a file calls openat, not execve, so there is no EXECVE record for this event — PROCTITLE is where the command line lives.
[norm_id=Unix event_type=PROCTITLE ] as s
join
[norm_id=Unix event_type=PATH
path IN ["*/lib/x86_64-linux-gnu/security/*","*/usr/lib64/security/*"]
name_type="CREATE"] as p
on s.record_id = p.record_id and s.host = p.host
| chart count() by s.command, p.path

Detecting changes to PAM configuration
Dropping the .so is only half the install. PAM runs a module when a line in /etc/pam.d/ loads it, so the next thing to look for is a write to that directory around the same time.
[norm_id=Unix event_type=SYSCALL
/* system call for file writes and modifications */
system_call IN ["open","openat","openat2","creat","rename","renameat","renameat2"]
-command IN ["dpkg","apt","apt-get","rpm","yum","dnf","ldconfig","unattended-upgr"] /*FPS from package manager */] as s
join
[norm_id=Unix event_type=PATH path="/etc/pam.d/*" name_type IN ["NORMAL","CREATE"]] as p
on s.record_id = p.record_id
| chart count() by s.audit_user, s.system_call, s.command, p.path, p.name_type

To see the full command, join the PROCTITLE events with this query.
[norm_id=Unix event_type=PROCTITLE
-command IN ["dpkg","apt","apt-get","rpm","yum","dnf","ldconfig","unattended-upgr"] /*FPS from package manager */] as s
join
[norm_id=Unix event_type=PATH path="/etc/pam.d/*" name_type IN ["NORMAL","CREATE"]] as p
on s.record_id = p.record_id and s.host=p.host
Hunting a preload hook
A preload hook needs three things on the host: a library that holds the hook, a setting that makes a program load it, and a login program started after that setting was written. Nothing in /etc/pam.d/ changes, so these are the whole trail.
Detecting the malicious library file
The first step of a preload hook is creating the shared library, a .so file that holds the hook code. On Ubuntu and other Debian-based systems, shared libraries are installed under /usr/lib/x86_64-linux-gnu/, a directory named after the CPU architecture (see Debian's Multiarch/Tuples page), so a hook placed there looks like one more library. Auditd records file creation under that directory as path events, and Sysmon for Linux reports the same creation when a rule covers the directory. This query lists the files created there.
(norm_id=Unix* (label=File label=Create) OR (event_type=Path name_type=Create))
(path IN ["*/usr/lib/x86_64-linux-gnu/*.so","*/usr/lib64/security/*"] OR
file IN ["*/usr/lib/x86_64-linux-gnu/*.so","*/usr/lib64/security/*"])
It matches all .so files, so expect noise around apt and dpkg.

Detecting changes to Preload file
A library does nothing until a program is told to load it. The system-wide way is one line in /etc/ld.so.preload, a file that lists libraries every program loads. Adding the line is a write to the existing file (name type NORMAL), or a creation (CREATE) when the file is new. This query joins those file records to the process that made them and leaves out writes made by package managers.
[norm_id=Unix event_type=PATH path="/etc/ld.so.preload"
name_type IN ["NORMAL","CREATE"]] as p
join
[norm_id=Unix event_type=SYSCALL
system_call IN ["open","openat","openat2","creat","rename","renameat","renameat2"]
-command IN ["dpkg","apt","apt-get","rpm","yum","dnf","ldconfig"]] as s
on p.record_id = s.record_id
| chart count() by s.host, s.audit_user, s.command, s.system_call, p.path

Two new alerts cover the file changes in this post and ship in the next alert release:
- Linux PAM Configuration File Modification: changes to PAM configuration files
- Linux Dynamic Linker Preload File Modification: changes to /etc/ld.so.preload
They will be available in the next alert release and can be downloaded from here.