Magento’s pub/media and pub/static folders should contain only images, videos, CSS, JavaScript and fonts. A PHP file in either one is almost always a web shell: a backdoor an attacker uploaded so they can run commands on your server. They use it to read your configuration, create admin users and inject card skimmers.

Look for them

find pub/media pub/static -type f \( -name '*.php*' -o -name '*.phtml' -o -name '*.phar' \)

On a clean store this returns nothing, apart from a few known Magento files such as pub/static/ placeholders on some versions. Treat anything else as hostile until proven otherwise.

Also check the web root for PHP files you don’t recognise:

ls -la pub/*.php

A normal Magento pub/ folder contains only a few PHP files, such as index.php, get.php, static.php and health_check.php.

What to do with them

  1. Do not open them in a browser. That runs them.
  2. Copy them somewhere outside the web folder as evidence, then delete them.
  3. Note each file’s modification time and check the web server access logs around that time to see how it was uploaded.

Make sure PHP can never run there

Magento’s nginx.conf.sample only passes a short list of entry points to PHP, so uploaded PHP files are not executed. If your config was customised, check it. For Nginx, add a rule like:

location ~* ^/(media|static)/.*\.(php|phtml|phar)$ {
    deny all;
}

On Apache, keep Magento’s pub/media/.htaccess, which turns off the PHP engine in that folder.

Assume the store is compromised

A backdoor means someone already had access. After removing it:

  • Apply all missing security patches and update vulnerable extensions.
  • Rotate admin passwords, the database password and the encryption key.
  • Look for new admin users and for injected scripts (see finding a card skimmer).
  • Run a full malware scan.

Catch it automatically

MageMonitor checks for this every few minutes as part of its Security Center checks for unexpected scripts, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.

Related fixes