A line like this at the top of a PHP file hides the code that actually runs:
<?php eval(gzinflate(base64_decode('S03OyFdQz0jNyclXtwYA'))); ?>Read it from the inside out. base64_decode() turns the text back into bytes, gzinflate() decompresses them, and eval() runs the result as PHP. This example decodes to echo 'hello';. In files found on hacked sites, the result is usually another layer of the same thing, repeated dozens of times, with a web shell, a spam mailer or an SEO spam injector at the bottom.
Short answer: do the same steps yourself and print the result instead of running it. Base64-decode the string, inflate it as raw DEFLATE data, and repeat for each layer. The Python script below does this offline, or paste the file into the UnPHP decoder.
What each function in the chain does
| PHP function | What it does | Python equivalent |
|---|---|---|
base64_decode() | Turns Base64 text back into bytes. Skips characters that aren’t Base64, such as line breaks. | base64.b64decode() |
gzinflate() | Decompresses raw DEFLATE data (RFC 1951), the output of gzdeflate(). | zlib.decompress(data, -15) |
gzuncompress() | Decompresses zlib data (RFC 1950), the output of gzcompress(). | zlib.decompress(data) |
gzdecode() | Decompresses gzip data (RFC 1952), the output of gzencode(). | zlib.decompress(data, 31) |
str_rot13() | Shifts every letter 13 places. Applying it twice gives back the original. | codecs.encode(text, "rot13") |
strrev() | Reverses the string. | data[::-1] |
convert_uudecode() | Decodes uuencoded data. | binascii.a2b_uu(), line by line |
eval() | Runs the string as PHP code. The code starts in PHP mode, so it has no <?php tag; code that prints HTML starts with ?>. | None: this is the step to leave out |
Obfuscators mix these freely. One layer uses gzinflate(), the next gzuncompress(), the next adds str_rot13() or strrev(). Every layer is undone the same way: apply the inverse of each function, starting with the innermost one, and don’t run the result.
Why not just replace eval with echo?
The classic trick is to change eval to echo and run the file. That works for a single plain layer, but it means running an attacker’s file with PHP:
- Any code outside that
eval()call runs too. Droppers often write copies of themselves or contact a server before they reach the eval. - Many samples run code with something other than
eval():assert()with a string argument (before PHP 8),create_function(),preg_replace()with the/emodifier (before PHP 7), or a variable that holds one of those names. Swappingevalforechodoesn’t touch them. - Each layer has to be edited and run again, and some files have more than a hundred layers.
If you run it anyway, use a throwaway container with no network access, never the server where you found the file.
Decode it offline with Python
The common chains don’t need PHP at all. This script finds each eval(f1(f2(…('…')))) call, applies the functions from the innermost outwards, puts the result where the call was, and repeats until no layer is left. It never runs the PHP.
import base64
import codecs
import re
import sys
import zlib
def b64_clean(s):
# PHP skips characters that aren't base64 (line breaks, spaces) and
# doesn't need the = padding
s = re.sub(rb"[^A-Za-z0-9+/]", b"", s)
return s + b"=" * (-len(s) % 4)
# What each PHP function does, in Python
FUNCTIONS = {
"base64_decode": lambda s: base64.b64decode(b64_clean(s)),
"gzinflate": lambda s: zlib.decompress(s, -15), # raw DEFLATE
"gzuncompress": lambda s: zlib.decompress(s), # zlib header
"gzdecode": lambda s: zlib.decompress(s, 31), # gzip header
"str_rot13": lambda s: codecs.encode(s.decode("latin-1"), "rot13").encode(
"latin-1"
),
"strrev": lambda s: s[::-1],
}
# eval( f1( f2( ... ( 'literal' ) ... ) ) );
LAYER = re.compile(
rb"""eval\s*\(\s*((?:\w+\s*\(\s*)+)(['"])(.*?)\2\s*\)+\s*;""",
re.DOTALL | re.IGNORECASE,
)
def unwrap(code):
while True:
match = LAYER.search(code)
if not match:
return code
names = re.findall(rb"\w+", match.group(1))
data = match.group(3)
for name in reversed(names): # innermost function first
name = name.decode().lower() # PHP ignores the case
if name not in FUNCTIONS:
sys.exit(f"Add {name}() to FUNCTIONS")
data = FUNCTIONS[name](data)
# The decoded code takes the place of the eval() call
code = code[: match.start()] + data + code[match.end() :]
source = open(sys.argv[1], "rb").read()
sys.stdout.write(unwrap(source).decode("utf-8", "replace"))Save it as unwrap.py and run it on the file. On a sample with five nested layers made with gzinflate(), gzuncompress() and str_rot13(), it prints the code at the bottom:
$ python3 unwrap.py suspicious.php
<?php
echo "decoded!\n";If it stops with a message like Add bzdecompress() to FUNCTIONS, the chain uses a function the script doesn’t know yet. Add a line for it, for example bz2.decompress for bzdecompress() or binascii.unhexlify for hex2bin().
When the simple approach stops working
The script handles string literals passed straight through a chain of functions. Real samples often go further:
- The function names are hidden.
$f = "base" . "64_decode"; $f($s);, or a name picked out of a lookup string one character at a time. See PHP obfuscation techniques and how to reverse them. - The string is built up first from pieces, from
chr()calls, or withstr_replace()removing junk characters. - The payload comes from the request.
eval(base64_decode($_POST['x'])), or code decrypted with a key from a cookie. Without that request there is nothing to decode: the file is a loader that runs whatever the attacker sends, and that alone tells you it’s a backdoor. - The decoded code is flattened with
goto. Its statements are shuffled and chained with jumps, which is valid PHP but very hard to read. - It isn’t an eval chain at all. Files protected with ionCube, SourceGuardian or Zend Guard hold compiled bytecode and need the vendor’s loader extension. Neither this script nor UnPHP decodes them.
For everything except the last two, you need a tool that understands PHP instead of matching it with a regular expression.
Decode it with UnPHP
Paste the code or upload the file on the UnPHP decoder. UnPHP doesn’t run uploads with PHP. It reads them with its own PHP interpreter, written in Python, which follows every layer of the chain whatever does the evaluating, including assert(), create_function() and preg_replace() with /e. File writes, shell commands such as system() and exec(), network calls such as curl_exec() and fsockopen(), and mail() do nothing there; they only show up in the list of function calls.
The result page shows:
- the decoded code, with
goto-flattened code rebuilt intoifandwhileblocks, - every function the script called, with a count,
- the eval count, the number of times code was evaluated (one per layer),
- the variables the script set.
When an if stops the decode, such as a check on the host name, UnPHP runs the file a second time with the first few conditions reversed and keeps the run that decoded more. It ignores exit and die for the same reason.
To decode many files, use the free API:
$ curl -F api_key=YOUR_KEY -F [email protected] https://www.unphp.net/api/v2/post
The JSON response lists the function calls and the eval count, and links to the decoded file.
What you’ll usually find inside
- Web shells: a file manager and command runner in the browser, often behind a password.
- Backdoor loaders: a few lines that run code sent in a request, through
$_POST,$_COOKIEor a header. - Mailers: scripts that send spam or phishing through the site’s server.
- SEO spam: code that shows links or whole pages only to search engine crawlers.
- Redirects: code that sends visitors arriving from search results, or on phones, to another site.
- Nulled themes and plugins: commercial code with its license check removed, and often a backdoor added.
Any of these on a site means the site was compromised, and there is rarely only one file. Look for others with the same layout, check recently changed files, and fix the hole the attacker came in through before restoring anything.
Common questions
Is eval(gzinflate(base64_decode())) always malicious?
No. Some commercial scripts used it to hide license checks or branding. But on a WordPress or other CMS site, in a file you didn’t install, in an uploads folder, or added to the top of a file you know, it is almost always malware.
What’s the difference between gzinflate, gzuncompress and gzdecode?
They all decompress DEFLATE data but expect different framing: gzinflate() reads raw DEFLATE, gzuncompress() expects a zlib header and checksum, and gzdecode() a gzip header. With the wrong one, PHP warns data error and returns false.
Can I decode it without PHP installed?
Yes. The Python script above needs only Python 3, and UnPHP runs in the browser. Neither executes the PHP.
Why does decoding one layer give me another eval?
Obfuscators wrap their output again and again, often dozens of times, so that a single decoding pass reveals nothing useful. Keep going until the result no longer calls eval() or one of the functions that work like it.