WordPress · Drupal · Site Maintenance · Technical SEO · Editorial Policy

How to Find and Remove Injected Code on a Drupal Site

Injected code on a Drupal site usually shows up as a redirect that only some visitors see, spammy links hidden in the page, or a third-party loader you never added. Finding it means comparing what is on the server with what should be there, then checking the places Drupal stores content outside of files. This guide covers both, and what to do afterward.

Before you change anything

Take a full backup of the files and the database first. It preserves evidence, and it lets you undo a cleanup step that turns out to be wrong. If the site is live and visibly redirecting visitors, put it into maintenance mode at Configuration > Development > Maintenance mode while you work.

Keep in mind how the code got there. Injected code is a symptom, not the cause. A cleaned site with the same weak entry point will be infected again, so the investigation and the credential steps later in this guide matter as much as the cleanup itself.

Where injected code hides in Drupal

Check these places, roughly in order of how often they are involved:

  • Theme files: Twig templates (.html.twig), the theme’s .theme file, .libraries.yml and any bundled asset files.
  • Custom modules: especially hook implementations that add attachments to every page or alter responses.
  • Settings files: settings.php, settings.local.php and any file they include.
  • Entry files: index.php and .htaccess in the web root, where redirect rules can be added.
  • The files directory: PHP files should not exist in sites/default/files. Any that do are suspicious.
  • Database content: custom blocks, node bodies and other fields using a text format that allows raw HTML.
  • Configuration: theme settings, tag or analytics fields and anything that accepts a header or footer snippet.

Compare files against a known-good copy

The fastest reliable method is a comparison, not a manual read-through. How you do it depends on how the site is built.

If you use Composer and version control

Most modern Drupal projects are managed with Composer, and the project code lives in a Git repository. On the server, run git status and git diff. Any file that differs from the last known-good commit and was not changed by your team is a lead. Untracked files in the theme, modules or web root deserve the same scrutiny.

Core and contributed modules installed by Composer do not need to be inspected line by line. Delete the core and module directories and reinstall them from your composer.lock file, which restores clean copies. Then diff again to see what remains.

If you have no repository

Download a fresh copy of the same core release from drupal.org and compare it to the server with a diff tool. For contributed modules, do the same using the version listed on the Extend page. Custom themes and modules have no official copy, so compare them with the most recent backup taken before the problem appeared, or read them manually.

If your site is on managed Drupal hosting

You may not have shell access. The host’s dashboard may offer file restore points, a Git-based deployment or a scan. Features differ by host and plan, so check what yours provides. Whatever the platform, the principle is the same: restore from a clean source rather than editing infected files in place.

Read custom code and templates for these patterns

When a file differs, or when you have nothing to compare it with, look for code that does not belong in a theme or module:

  • Functions that decode or unpack long encoded strings and then run the result, such as eval, base64_decode, gzinflate or str_rot13 combined with execution.
  • Outbound requests to domains you do not recognise, made from PHP or from a loaded asset file.
  • Conditions that only trigger for certain visitors, for example search engine crawlers, first-time visitors or mobile browsers. This is how hidden links and conditional redirects avoid detection by the site owner.
  • Extra attachments added to every page request in a hook that has no obvious purpose.
  • Link lists, often with unrelated anchor text, placed in a footer template or rendered through a hidden element.

A useful test: view the site as a logged-out visitor in a private window, then fetch the page with a command-line tool and compare the HTML. Differences between the two can point to code that behaves differently depending on who is asking.

Review embeds and database content

Not all injected code is in files. Drupal lets editors place HTML in body fields, custom blocks and similar content, depending on the text formats configured. If a role can use a format that allows unrestricted HTML, code placed there is stored in the database and survives a clean file reinstall.

  1. Open Structure > Block layout > Custom block library and read each block, switching to the source view for HTML blocks.
  2. Check text formats at Configuration > Content authoring > Text formats and editors. Confirm that unrestricted formats are limited to trusted roles only.
  3. Search the database for suspicious external domains. The Drush command drush sql:cli opens a database prompt; search the body and block tables for terms you identified in the files.
  4. Review any fields or settings added for tracking snippets, and compare them with what your marketing team actually uses.

Also review the user list at People. Look for administrator accounts nobody created, and for recent logins at odd times in Reports > Recent log messages.

Remove it and close the way in

Work through this checklist in order:

  1. Restore or delete the modified files, using clean copies from version control, Composer or a verified backup.
  2. Remove injected content from blocks and fields, and tighten text formats.
  3. Delete unknown PHP files from the files directory and unknown accounts from the user list.
  4. Clear caches with drush cr and test again as a logged-out visitor and on a phone.
  5. Update core and every module. Use composer audit and the Security advisories listed on drupal.org to see what applies to your site.
  6. Rotate every credential: all Drupal user passwords for administrators and editors, the database password, hosting and SFTP or SSH logins, API keys in settings, and any Git deploy keys. Regenerate the hash salt in settings.php if you suspect session theft, which also logs everyone out.
  7. Remove modules and themes you do not use, rather than only disabling them.

If you cannot tell how the attacker got in, treat it as unresolved. Check the web server access logs around the first modified file’s timestamp, and ask your host for logs if you cannot reach them yourself.

Prevent it from coming back

  • Manage the whole codebase in Git and deploy from it, so any change made directly on the server is visible as a difference.
  • Make the codebase read-only to the web server user wherever your hosting allows it, apart from the files directory.
  • Prevent PHP execution in the files directory through your web server configuration.
  • Apply security updates promptly. The Drupal Security Team publishes advisories for core and contributed modules, and you can subscribe to them.
  • Use unique passwords with a password manager, and turn on two-factor authentication for administrators if you add a module for it.
  • Keep the administrator list short and review it regularly.
  • Consider the Security Review contributed module as an extra check of file permissions and text formats.

Frequently asked questions

How do I know whether the injection is in the theme or somewhere else?

Switch temporarily to a default core theme such as Olivero. If the problem disappears, it is in your theme or its templates. If it persists, look in custom modules, settings files, the web root and database content.

Is deleting the suspicious lines enough?

Usually not. Attackers often leave a second backdoor or a spare administrator account. Restore from a clean source, review users and content, update everything and rotate credentials as well.

Does this apply to an old Drupal 7 site?

The steps for finding and removing code apply, but Drupal 7 reached end of life on 5 January 2025, so it no longer receives security fixes from the Drupal Security Team. Plan an upgrade to a supported version. See the official Drupal security pages and the Drupal documentation, and the OWASP Top Ten for general web risks.

We use essential cookies to make this site work and remember your preferences. We do not use advertising or analytics cookies. Privacy Policy. Cookie Policy.