Deny direct access to PHP files in wp-content/uploads/
The following PHP function secures your WordPress website by disabling the execution of PHP scripts in wp-content/uploads, on Windows Server IIS web servers. It creates a
web.config file for this purpose.
How to disable PHP in wp-content/uploads
The PHP execution in wp-content/uploads (aka WP_CONTENT_DIR/uploads) is disabled by writing a web.config file containing an accessPolicy for handlers. This accessPolicy tells IIS what a handler -such as PHP- can, and cannot do: execute, read, and/or write for example
Please note that this will only partially secure your WordPress website, but not fully! You must take other security measures as well. For example, you can use a .htaccess as a web application firewall (WAF), to block out some vulnerabilities based on their request characteristics. Or you can use a HTTP blacklist to block out IP addresses known to abuse websites.
web.config accessPolicy only works on IIS 7.0+ Windows Server web servers, consult your web hosting provider for more information.
Securing the WordPress uploads folder is important. In many hacked WordPress sites, a PHP backdoor is found within the
WP_CONTENT_DIR/uploads directory. Often because this is the location where uploads are placed automatically. From the backdoor within
wp-content/uploads other backdoors are uploaded to various locations, and scripts are injected with malware.
Disable the execution of PHP scripts in wp-content/uploads
As always, the PHP code is provided AS-IS.
The PHP code to write a web.config file comes partially from the WordPress functions iis7_add_rewrite_rule() and saveDomDocument(), both found in
It shouldn’t be too hard for you to wrap this in a WordPress plugin:
By disabling PHP in the uploads directory, your WordPress site is a little bit more secured. The, somewhat older, Smashing Magazine post Common WordPress Malware Infections gives great insight in common WordPress (core, theme, plugin) vulnerabilities and how they’re abused.
The function above creates the following web.config file in your wp-content/uploads directory:
This tells IIS that handlers (such as PHP) may only read, and not execute. If you don’t want to create a WordPress plugin, you can copy/paste the above code into a new file and save it as
web.config. Upload the file to your wp-content/uploads folder. Be careful not to upload it to a different folder.
Re-enable PHP script execution
If you want to re-enable PHP script execution, you can simply delete the web.config file from the wp-content/uploads directory. Or you can programmatic enable PHP by deleting the
<handlers accessPolicy="Read"/> line:
Example PHP backdoor found in wp-content/uploads
An example of a PHP backdoor you may find is the following one. You can use the PHP backdoor code for signatures in your automated backdoor scanning tools or grep regular expressions.
Preventing the execution of PHP in
wp-content/uploads prevents these exploits from spreading. But you still need to address the cause in your website.
Want to thank me?
If I’ve helped you out and you want to thank me, why not buy me a coffee?
This content was originally published here.