Linux Auditd for WordPress Security: Tracking File and Process Events

auditd for WordPress: who changed wp-config.php, and which process ran it

After an incident, file timestamps say that wp-config.php changed at 03:12, but not who changed it or how: an SFTP user, a compromised plugin through PHP, a cron job. The Linux audit system records such events in the kernel, with the user, the process and the command, and it cannot be bypassed by the web application. A handful of rules covers the files and actions that matter on a WordPress server.

Problem: A site was defaced twice. Each time, a PHP file in wp-content was replaced, and the logs of the web server show nothing unusual. Nobody knows whether the change came through PHP, an FTP account or SSH.

Solution: Install auditd, add watch rules for wp-config.php, .htaccess, the plugins and mu-plugins folders and the PHP-FPM pool configuration, a rule that logs programs executed by the web server user, tag each rule with a key, search the log with ausearch -k, and keep the rules persistent in /etc/audit/rules.d/.

Install and add rules

sudo apt install auditd
sudo systemctl enable --now auditd
# /etc/audit/rules.d/wordpress.rules
# writes and attribute changes on critical files
-w /var/www/example.com/public/wp-config.php -p wa -k wp_config
-w /var/www/example.com/public/.htaccess -p wa -k wp_htaccess
-w /var/www/example.com/public/wp-content/mu-plugins/ -p wa -k wp_muplugins
-w /var/www/example.com/public/wp-content/plugins/ -p wa -k wp_plugins
-w /etc/php/8.3/fpm/pool.d/ -p wa -k php_pool

# every program started by the web server user (uid 33 = www-data)
-a always,exit -F arch=b64 -S execve -F uid=33 -k www_exec
sudo augenrules --load
sudo auditctl -l

-w path -p wa watches a file or directory recursively for writes and attribute changes. The execve rule records every program the web server user starts. PHP itself does not run programs during normal WordPress requests, so entries here (a shell, wget, curl, base64) are a strong sign of a web shell. Plugins that legitimately run commands (image optimizers calling cwebp, for example) show up too and can be recognized.

Search the log

sudo ausearch -k wp_config -i --start today
sudo ausearch -k www_exec -i --start this-week | grep -E 'exe=|proctitle='
sudo aureport -f -i --summary            # summary of file events

-i resolves user IDs and syscalls into names. A record shows the time, the syscall, the file, the user (auid, the login user, even after sudo), the process (exe, comm) and its command line. A change of a plugin file by exe=/usr/sbin/php-fpm8.3 came through PHP, by exe=/usr/lib/openssh/sftp-server through an SFTP account.

Noise and volume

Watching the whole wp-content folder would record every upload and cache file. Watch only code paths, and expect events during normal plugin updates from the admin (those are writes by PHP-FPM too). WP-CLI updates run as the shell user and are clearly distinguishable. Limit the log size in /etc/audit/auditd.conf (max_log_file, num_logs) and send it to a remote log server if possible, because an attacker with root can delete local logs.

Combine with integrity checks

auditd tells who and how; a hash baseline tells what changed. Together they answer both questions after an incident: file integrity checks for WordPress. Restricting what PHP can write in the first place reduces the events to look at: harden file permissions.

NOTE: auditd needs root and a kernel with audit support, which most VPS and dedicated servers have; on shared hosting it is not available. In containers, audit rules belong on the host, not in the container.