Solution:
Root Cause: Malicious Server Rewrite Rules
Malware actors tamper with Apache
.htaccess or Nginx configuration blocks to inspect incoming
HTTP_REFERER and
HTTP_USER_AGENT headers. When the server detects a request originating from search engine results (e.g., Google, Yahoo, Bing) or mobile user agents, it returns an HTTP 301/302 location header pointing to spam or phishing landing pages, while bypassing direct requests to evade administrator detection.
# Diagnostic Verification:
1. Connect to your web server via SSH and inspect your
.htaccess file:
bash
cat /var/www/html/.htaccess
2. Look for suspicious
RewriteCond %{HTTP_REFERER} or
RewriteCond %{HTTP_USER_AGENT} rules matching keywords like
q=,
google,
bing,
android, or encoded base64 strings.
3. Test conditional response using
curl with a spoofed referrer:
bash
curl -I -A "Mozilla/5.0" -e "[https://www.google.com](https://www.google.com)" [https://yourdomain.com/](https://yourdomain.com/)
# Step-by-Step Fix:
1.
Quarantine and Reset .htaccess File:
Backup the existing configuration and reset .htaccess to standard WordPress rules:bash
mv /var/www/html/.htaccess /var/www/html/.htaccess.bak
cat << 'EOF' > /var/www/html/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EOF
2.
Lock File Permissions:
Restrict permission to prevent web server processes from modifying .htaccess:bash
chmod 644 /var/www/html/.htaccess
chown www-data:www-data /var/www/html/.htaccess
3.
Flush Server Caches:
Restart web server and purge object caches (Nginx/LiteSpeed/Varnish).# Prevention & Long-Term Monitoring:
Implement file integrity monitoring using tools like Fail2ban or Tripwire to alert on root directory file modifications.