Guide · Obfuscated PHP
eval(base64_decode) in a WordPress PHP file: what it is and what to do
base64_decode is in thousands of legitimate plugins; eval() of what it decodes is almost never legitimate. You opened a theme’s functions.php, wp-config.php, index.php or a plugin file and found a line such as eval(base64_decode('…')), eval(gzinflate(base64_decode(…))) or a chain of str_rot13 and strrev wrapped around a long unreadable string — often on the first line, before the file’s own comments, and sometimes so wide that an editor shows nothing unless you scroll right. Or a scanner flagged it for you. This guide explains what the pattern does, why the function name alone does not decide anything, the other shapes the same trick takes, how to find every copy on the site without changing a file, how to read the hidden code without running it, and why the fix is replacing files rather than deleting a line.
Published Last reviewed
- What it does
- Decodes a hidden string at runtime and executes it as PHP
- Usual places
- Line 1 of functions.php, wp-config.php, index.php or a plugin’s main file
- Not proof alone
- base64_decode without eval is common in legitimate plugins
- Fix
- Replace every affected file from a clean source, then find the way in
What the pattern is
base64_decode is not the problem. eval() of its output is.
Base64 is an encoding, not encryption: it turns any bytes into letters, digits, plus signs and slashes so they survive being stored in text. Legitimate WordPress code uses base64_decode constantly — for inline SVG icons, image data from the media editor, licence keys, API responses, settings exports and the payloads of some page builders. A scanner or a grep that matches base64_decode on its own will light up a clean site.
eval() is different. It takes a string and runs it as PHP code. Wrapped around base64_decode, it means the file contains a program the author did not want anyone to read, and that program runs with the full permissions of the website every time the file is loaded: it can read wp-config.php and the database, write files, create administrators and fetch more code. Modern plugins and themes on WordPress.org are not permitted to ship obfuscated code, and there is no ordinary reason for a theme’s functions.php to execute an encoded blob. Treat eval() of decoded or decompressed data as malicious until you have read what it decodes to.
Attackers encode for two reasons: the payload does not contain words such as “shell_exec” or a domain name that a simple scan would match, and an owner skimming a file sees noise rather than an obvious backdoor. Layering gzinflate or str_rot13 on top changes the encoded string again, so the same payload looks different from site to site.
Figure 1 · Decoding is normal; running the result is not
base64_decode that hands back data is plugin code. eval() of what it decodes is a loader.
// an icon shipped inline as base64
$svg = base64_decode( $icon_b64 );
return 'data:image/svg+xml;base64,'
. base64_encode( $svg );
Decoded bytes are returned or saved as data. Nothing is executed.
<?php eval(gzinflate(base64_decode(
'7b0HYBxJliUmL23Kd...'))); ?>
<?php
/**
* Theme functions and definitions
Decoded text is run as PHP, above the theme’s own header, on every page load.
The shapes it takes
The other forms of the same trick, and what each one means
Search for the behaviour, not one spelling. The rows below are the constructions most often found in compromised WordPress files.
The last row is the most important. A one-line eval of request data is short, contains nothing encoded and is easy to miss among real code, yet it gives anyone who knows the parameter name full control of the site. It is frequently planted alongside a larger encoded loader so that removing the obvious block leaves a way back.
| What you see | What it does | How to read it |
|---|---|---|
| eval(base64_decode('…')) | Decodes a base64 string and runs it as PHP | Malicious unless decoding proves otherwise |
| eval(gzinflate(base64_decode('…'))), gzuncompress or gzdecode | Decodes, decompresses, then runs; often repeated several layers deep | Malicious unless decoding proves otherwise |
| str_rot13, strrev or chr() sequences inside the chain | Scrambles the string again so it matches no known signature | Extra layers only exist to hide the payload |
| $f = 'base' . '64_decode'; $f($x); | Builds the function name from pieces so the word never appears whole | Deliberate evasion; no legitimate reason |
| preg_replace with the /e modifier | Runs the replacement as PHP; removed in PHP 7.0, so it only works on very old PHP | Old malware, or an old file that must go anyway |
| create_function('', $decoded) | Builds and runs a function from a string; removed in PHP 8.0 | Same as eval; also breaks on current PHP |
| assert($string) | Older PHP evaluated a string passed to assert(); PHP 8 no longer does | Used as an eval substitute on old servers |
| @eval($_POST['x']), or $_REQUEST, $_COOKIE, $_SERVER | A one-line backdoor: runs whatever code a request sends | A confirmed backdoor; the attacker has a way back in |
Find every copy
Read-only checks across the whole site
Run these over SSH from the WordPress root, on a copy if you have one. None of them modify anything. On a hosting account with several sites, run the searches from the account home so neighbouring sites are covered: one infected site usually means its neighbours were reachable too.
- 01
Search for eval around a decoder
The core pattern and its compressed and scrambled variants, with line numbers:
grep -rnE --include='*.php' 'eval\s*\(\s*(base64_decode|gzinflate|gzuncompress|gzdecode|str_rot13|strrev)\s*\(' . | cut -c1-200The cut keeps a single-line payload from flooding the terminal. Every match needs reading; vendor libraries rarely match this pattern at all.
- 02
Search for request-driven backdoors
eval, assert or create_function fed directly from request data:
grep -rnE --include='*.php' '(eval|assert|create_function)\s*\(\s*(stripslashes\s*\(\s*)?\$_(POST|GET|REQUEST|COOKIE|SERVER)' . | cut -c1-200 - 03
Search for assembled function names and old evaluators
Function names built from fragments, and preg_replace with the /e modifier:
grep -rnE --include='*.php' "base['\"] *\. *['\"]64_decode|['\"]/[^'\"]*/e['\"] *," . | cut -c1-200The second half produces some noise in older libraries; read each hit in context.
- 04
List files whose first line is unusually long
Injected loaders are usually placed on line 1, before the file’s own code, and are one very long line:
find . -name '*.php' -exec awk 'FNR==1 { if (length($0) > 300) print FILENAME; nextfile }' {} + - 05
Verify core and WordPress.org plugins against checksums
Any core file or plugin file that differs from the published version:
wp core verify-checksums wp plugin verify-checksums --allPlugin checksums only exist for plugins hosted on WordPress.org; premium plugins report that checksums could not be retrieved, and themes are not covered at all. Compare those with a fresh download from the vendor.
- 06
List recently modified PHP files
What changed in the last two weeks, newest first, to date the injection and find companions:
find . -name '*.php' -mtime -14 -printf '%TY-%Tm-%Td %TH:%TM %p\n' | sort -r | head -n 50Attackers sometimes reset timestamps, so an old date does not clear a file. A cluster of files changed in the same minute usually marks the moment of compromise.
Read it safely
How to see what the encoded block does without running it
You do not need to know exactly what the payload does to remove it, but decoding it tells you what else to check: the domains it contacts, files it writes, accounts it creates.
- 01
Copy the block off the server as text
Copy the suspicious line into a plain text file on your own computer, or paste it into the decoder tool on this site, which decodes in the browser and never executes the code.
- 02
Peel one layer at a time
Work from the innermost function outwards: base64-decode the string, then inflate it if the next function is gzinflate, then apply str_rot13 or reverse it if those come next. Each step produces text, never execution. The result is often another eval layer; repeat until readable PHP appears.
- 03
Read the result for actions, not names
Look for file writes (file_put_contents, fwrite), remote requests (curl, file_get_contents with a URL, fsockopen), new users (wp_insert_user, wp_create_user), database writes, and anything taking input from $_POST, $_COOKIE or headers. Note every domain, path and option name it mentions.
- 04
Search the site for what it names
Grep for each file path, option name and domain from the decoded code. A loader usually has a partner — a payload in the database, a second file, or a scheduled task — and the decoded text is how you find it.
Remove it
Replace the files; do not just delete the line
Editing out the visible block leaves whatever else the same access added. The reliable order is to preserve, replace from clean sources, then close the way in.
- 01
Preserve first
Copy the infected files, with their timestamps, and a database export to somewhere outside the web root. The earliest modification time and the decoded payload are the evidence of how and when it happened.
- 02
Replace core, plugins and themes from official sources
Reinstall WordPress core, and every plugin and theme from WordPress.org or the vendor, rather than editing out the injected code. A file with one visible block may contain another further down, and the attacker may have touched files you have not opened.
- 03
Rebuild wp-config.php by hand
Create a fresh file from wp-config-sample.php and copy across only the database settings and any constants you recognise, then generate new security keys. Do not copy the old file and trim it.
- 04
Remove the files that belong to nothing
Anything left in wp-content, uploads or the root that is not part of core, an installed plugin or the theme — including PHP files in uploads — is removed after it has been preserved.
- 05
Rotate access
Change WordPress administrator, hosting panel, SFTP/SSH and database passwords, remove administrators and application passwords nobody can account for, and check for SSH keys you did not add.
- 06
Find how it got in
An outdated or nulled plugin, a reused password, a vulnerable theme, or a neighbouring site on the same account. Without closing the entry point, the same line returns.
Verify
How to know it is really gone
Re-run every search above after the cleanup and again after a few days of normal traffic. They should return nothing that you cannot attribute to a named library. Run wp core verify-checksums and wp plugin verify-checksums --all again until both are clean, and compare premium plugins and the theme with fresh vendor copies.
Then look at the site from the outside: as a logged-out visitor, on a phone, and arriving from a Google result, because many payloads hide from administrators and desktop browsers. If an encoded line reappears in the same file, something that runs automatically — a scheduled task, a PHP startup directive, a database payload or a file outside the site folder — survived, and the cleanup is not finished.
Common questions
Common questions about eval(base64_decode) in WordPress files
Is every base64_decode in WordPress malware?
No. Many legitimate plugins and themes decode base64 for images, icons, licence data and API responses. What matters is what happens to the decoded result: returned or stored as data is normal; passed to eval, assert, create_function or a variable function is not.
I removed the eval line and it came back. Why?
Something that runs automatically rewrote it: another infected file, a payload in the database, a malicious WP-Cron event or server cron job, a .user.ini auto_prepend_file directive, or the attacker still has access. Replacing all code from clean sources and rotating access usually stops it; if it does not, the source is outside the files you cleaned.
Can I just delete the encoded line and keep the file?
For core, plugins and themes, reinstall from the official source instead. You cannot be sure the line you found is the only change. For your own custom code, restore it from version control or a backup known to predate the infection.
My security plugin says the site is clean, but I can see the line. Who is right?
Trust what you can see and decode. Signature scanners miss variants that have been re-encoded, and some loaders hide from plugin scans. Decode the block as text and check what it does.
Does finding this mean customer data was taken?
It means someone could run code on the site, which includes reading the database. Whether data was actually taken depends on what the payload did and on the access logs. If the site stores customer or order data, preserve the evidence before cleaning and treat it as a possible breach until the investigation says otherwise.