Website malware can compromise your files, steal user information, redirect visitors to suspicious websites, inject spam links, or give attackers unauthorized access to your server. If your PHP, Laravel, or WordPress website suddenly displays suspicious content, redirects visitors, or triggers security warnings, it may be infected.
This malware removal guide explains how I approach website malware detection, cleanup, and recovery. The safest approach is to isolate the affected website, identify the entry point, remove malicious code, restore trusted files, and close the security vulnerability before bringing the website fully back online.
Common signs of website malware infection
Malware does not always produce an obvious error. Some infections operate quietly in the background while using your website to distribute spam, steal information, or redirect search engine traffic.
Look for these warning signs:
- Unexpected redirects to gambling, pharmaceutical, or suspicious websites.
- Unknown PHP files appearing in application directories.
- Strange JavaScript code injected into website pages.
- Google Safe Browsing or hosting provider security warnings.
- Unexpected administrator accounts or modified user permissions.
- Unusual server CPU usage, outgoing traffic, or scheduled tasks.
- Search engine results displaying spam titles or unfamiliar pages.
- Files changing even after you restore a backup.
Common causes of website malware
1. Vulnerable plugins, themes, or dependencies
Outdated plugins, themes, PHP packages, and content management systems can contain security vulnerabilities. Attackers may exploit these weaknesses to upload malicious files or execute unauthorized code.
For Laravel applications, review outdated Composer dependencies and investigate packages that are no longer maintained.
2. Compromised FTP, hosting, or administrator credentials
Weak passwords, reused credentials, exposed deployment keys, and compromised hosting accounts can allow attackers to modify website files directly. Even a technically secure application can be infected if an attacker gains access to its hosting account.
3. Insecure file uploads
File upload functionality is another common entry point. If an application accepts executable files or fails to validate uploaded content properly, attackers may upload PHP scripts and execute them through the web server.
4. Malicious scripts and vulnerable server configurations
Incorrect file permissions, exposed environment files, insecure server configurations, and unauthorized scheduled tasks can help malware survive or spread across an account.
Step-by-step malware removal guide
Step 1: Isolate the infected website
Start by preventing further damage. If the website is actively redirecting visitors, distributing malware, or exposing sensitive information, place it behind a maintenance page or restrict public access while investigating.
Contact your hosting provider if you suspect the entire hosting account or server is compromised. Ask them to check server-level processes, access logs, and other websites sharing the same account.
Before deleting anything, create a copy of the infected files, database, relevant logs, and server configuration for investigation. Store this evidence outside the publicly accessible web directory. Do not restore the infected backup directly to production.
Step 2: Scan website files for suspicious code
Use your hosting provider's malware scanner or a reputable security tool to scan the complete website, including hidden files, upload directories, and files outside the document root that belong to the affected application.
On Linux servers, you can start by listing recently modified PHP files:
find /path/to/website -type f -name "*.php" -mtime -14 -print
This command lists PHP files modified during the last 14 days. Recent modification does not automatically mean infection, so compare the results with deployment records and known application files.
You can also search for potentially suspicious PHP functions:
grep -RInE 'base64_decode|gzinflate|str_rot13|eval|shell_exec|passthru' /path/to/website --include='*.php'
These functions have legitimate uses, so their presence alone does not prove malware. Investigate unfamiliar combinations, encoded payloads, unexpected file locations, and code that executes content received from external requests.
Step 3: Identify and remove malicious files
Compare suspicious files against a clean copy of your application or the original release from its trusted source. Pay particular attention to unexpected PHP files inside upload directories, unfamiliar files in public folders, modified entry points, and files with unusual names.
For Laravel applications, inspect these locations carefully:
public/for unexpected PHP scripts or modified JavaScript files.storage/for suspicious executable files or unexpected content.bootstrap/for unauthorized changes to application startup files.routes/andapp/for unfamiliar code or unauthorized modifications.vendor/for altered dependencies or unexpected package files.
Do not delete files simply because their names look unfamiliar. Verify their purpose first. If the application files have been compromised, restoring a clean version from a trusted source is generally safer than manually editing individual infected files.
Step 4: Check the database for malicious injections
Malware can also be stored in the database. Attackers may inject spam links, malicious JavaScript, unauthorized administrator accounts, or unwanted content into database records.
Inspect content management tables, user accounts, configuration records, and recently modified content. Search for unfamiliar external domains, suspicious iframe elements, unexpected scripts, and unauthorized administrator privileges.
Before modifying database records, export a backup. Remove confirmed malicious content carefully while preserving legitimate application data.
Step 5: Restore clean application files
For Laravel, the safest recovery method is usually to deploy a verified clean version of the application from source control or a trusted release. Do not copy potentially infected application files back from the compromised server.
Install dependencies from the project's trusted lock file:
composer install --no-dev --prefer-dist --optimize-autoloader
Review Composer scripts and package sources before running installation commands. Never use an untrusted package or dependency simply to replace a suspicious file.
Restore only verified user uploads and required data. Keep executable PHP files out of directories intended for user-uploaded content.
Step 6: Secure Laravel environment and file permissions
Check your .env file for unauthorized modifications and ensure it is not publicly accessible. Laravel's environment configuration should remain outside the public document root. Never expose application secrets through a public repository or downloadable backup.
Set restrictive permissions appropriate to your server's user and group configuration. A common Linux starting point for a Laravel application is:
find /path/to/laravel -type d -exec chmod 755 {} \;
find /path/to/laravel -type f -exec chmod 644 {} \;
Ensure the web server or PHP-FPM process can write to Laravel's storage/ and bootstrap/cache/ directories without making the entire application writable. Avoid setting permissions to 777.
Step 7: Change credentials and remove unauthorized access
Removing malicious code is not enough if an attacker still has valid credentials. Rotate credentials that may have been exposed, including:
- Hosting control panel and server SSH passwords or keys.
- FTP and SFTP credentials.
- Database usernames and passwords.
- Laravel application secrets and exposed API keys.
- Administrator and other privileged user passwords.
For Laravel, generate a new application key if the existing key was compromised. Understand that changing APP_KEY can invalidate encrypted data and existing encrypted sessions or cookies. Plan the change carefully, especially if the application stores encrypted database values.
Remove unauthorized accounts, review SSH authorized keys, inspect scheduled jobs, and revoke unknown access tokens and sessions.
Step 8: Check for persistence and verify the cleanup
Attackers may install backdoors, cron jobs, modified server configuration, or malicious scripts that recreate deleted files. Inspect scheduled tasks, startup processes, web server configuration, PHP configuration, and hosting account files.
After cleanup, clear Laravel's cached configuration and application caches using the appropriate deployment procedure:
php artisan optimize:clear
php artisan config:cache
Run a fresh malware scan and check application logs, server access logs, file modifications, and outgoing network activity. Test authentication, uploads, forms, APIs, and important website functionality.
How to prevent future malware infections
Once the website is clean, focus on preventing the same attack from happening again.
- Keep PHP, Laravel, Composer dependencies, plugins, and server software updated.
- Use strong, unique passwords and multi-factor authentication where available.
- Validate file uploads on the server and store uploads outside executable locations.
- Restrict file permissions and avoid unnecessary write access.
- Keep regular off-server backups and periodically test restoration.
- Use HTTPS and secure cookies for sensitive application sessions.
- Monitor file changes, authentication activity, and suspicious server requests.
- Review third-party packages and remove unused dependencies.
If your website also displays connection or availability problems after an infection, investigate the underlying issue separately. You may find these guides useful: How to Fix Error Establishing a Database Connection and How to Fix Laravel 500 Internal Server Error.
Frequently asked questions
Can I remove website malware manually?
Yes, if you can identify the malicious code and understand the affected application. However, manual deletion without investigating the entry point and persistence mechanisms can leave hidden backdoors behind.
Will restoring a backup remove malware?
Only if the backup was created before the infection and is verified clean. If attackers compromised the hosting account or credentials, the restored website may become infected again.
Can Laravel websites get infected with malware?
Yes. Laravel applications can be compromised through vulnerable dependencies, exposed credentials, insecure uploads, server misconfiguration, or compromised hosting accounts. Laravel itself does not automatically protect an application from every server-level or application-level attack.
How do I know if malware has been completely removed?
Run multiple security scans, compare application files against trusted source versions, inspect database records, review scheduled tasks and server logs, and monitor the website for unexpected changes. No single scan can guarantee that a compromised environment is completely clean.
Final thoughts
Website malware removal requires more than deleting suspicious files. You need to identify how the attacker gained access, clean both application files and database content, remove persistence mechanisms, rotate compromised credentials, and verify that the server is secure.
For PHP and Laravel websites, I recommend rebuilding compromised application files from trusted source code rather than trusting files that were already present on an infected server. If you suspect a server-level compromise, involve your hosting provider or a security specialist before returning the website to normal operation.
Comments
No comments yet. Did this work for you, or did you hit something different? Ask below.
Leave a comment
Comments are reviewed before they appear. Your email is never shown.