Obfuscated PHP hides what a file does, from the people reading it and from scanners that search for strings like base64_decode or system. Nearly all of it is built from the same small set of tricks, stacked on top of each other. Here is each one, what it looks like, and how to undo it.
At a glance
| Technique | Looks like | How to undo it |
|---|---|---|
| Encoding layers | eval(gzinflate(base64_decode('…'))) | Apply the decoding functions and print the result instead of running it |
| Escaped strings | "\x62\x61\x73\x65…" | Decode the escapes the way PHP does |
| Strings built from pieces | "ba" . "se64", chr(115), str_replace() | Work out the value |
| Lookup tables | $t[1].$t[0].$t[18], $GLOBALS['O0O0'][0] | Track what each element holds |
| Variable functions | $f($x), call_user_func() | Resolve the name, then read it as a direct call |
| Confusing names | $O0O0O0, $lIl1Il | Rename consistently |
| Functions that run code | assert(), create_function(), preg_replace('/…/e') | Treat them like eval() |
| goto flattening | goto a; … a: | Follow the jumps and rebuild if and while |
| Hidden branches | if (md5($_SERVER['HTTP_HOST']) == …) | Read both branches |
| Keyed payloads | openssl_decrypt(…, $_COOKIE['k'], …) | Needs the key; without it, the file is a loader |
| Compiled encoders | ionCube, SourceGuardian | Not decodable without the vendor’s loader |
1. Encoding and compression layers
eval(gzinflate(base64_decode('S03OyFdQz0jNyclXtwYA')));
eval(str_rot13(gzinflate(strrev(base64_decode('…')))));The payload is encoded, compressed or both, and eval() runs the result. Other functions used this way are gzuncompress(), gzdecode(), str_rot13(), strrev(), convert_uudecode(), hex2bin() and urldecode(). The output is usually another layer like it.
To undo it, apply the same functions without the eval(), innermost first, and repeat. How to decode eval(gzinflate(base64_decode())) PHP code covers this step by step, with a Python script.
2. Escaped strings
$f = "\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65"; // "base64_decode"
$g = "\142\x61se64_\u{64}ecode"; // octal, hex and Unicode escapesIn double-quoted strings, PHP turns \x62 (hex), \142 (octal) and \u{62} (Unicode) into characters. Writing every character this way hides function names from a text search.
To undo it, decode the escapes the way PHP does. Only double-quoted strings and heredocs have them; in single-quoted strings, \x62 stays four characters. UnPHP writes such strings out as plain text in its output wherever that leaves the code meaning the same.
3. Strings built from pieces
$a = "base" . "64_" . "decode";
$b = str_replace("q", "", "bqaqsqeq6q4q_qdqeqcqoqdqe");
$c = chr(115).chr(116).chr(114).chr(95).chr(114).chr(111).chr(116).chr(49).chr(51); // "str_rot13"
$d = strrev("edoced_46esab");
$e = implode("", ["base", "64_", "decode"]);
$f = pack("H*", "6261736536345f6465636f6465");All six lines produce a function name ($c is str_rot13, the rest base64_decode) without the name appearing anywhere in the file.
To undo it, work out the value. These expressions only use constants, so their result is fixed and can be computed without running anything else in the file.
4. Lookup tables
$t = "abcdefghijklmnopqrstuvwxyz_0123456789";
$f = $t[1].$t[0].$t[18].$t[4].$t[33].$t[31].$t[26].$t[3].$t[4].$t[2].$t[14].$t[3].$t[4]; // "base64_decode"
$GLOBALS["O0O0"] = ["base64_decode", "str_rot13"];
eval($GLOBALS["O0O0"][0]($payload));One long string or array holds the building blocks, and every name in the code is an index into it. Obfuscators often keep the table in $GLOBALS so that every function can reach it, and shuffle or encode the table itself.
To undo it, track what each variable and array element holds and substitute the values. By hand this is slow, and it is where an interpreter that follows the values saves the most time.
5. Variable functions and callbacks
$f = "sys" . "tem";
$f($_GET["c"]); // the same as system($_GET["c"])
call_user_func("str_rot13", $s);
array_map("assert", [$code]);PHP calls a function by the name stored in a string, so $f(), call_user_func(), call_user_func_array() and callbacks given to array_map(), array_filter() or usort() can all call anything. A scanner looking for system( finds nothing.
To undo it, resolve the name, then read the line as a direct call. Any call whose function comes from a variable deserves a close look.
6. Confusing names
$O0O0O0 = $OO0O00($O0OO00);
$lIl1Il = $l1lIlI[$Il1lIl];Names made of letters that look alike (O and 0, l, I and 1) are hard to tell apart. PHP also allows any byte from 0x80 to 0xff in a name, so names can show up as invisible or mangled characters in an editor.
To undo it, rename the variables consistently, for example to $v1, $v2 and so on. What matters is what each one holds, which the steps above reveal.
7. Functions that run code
eval() is the obvious one, but scanners know it. These also turn a string into running code:
assert()with a string argument ran it as PHP before PHP 8.create_function()builds a function from a string of code. It was removed in PHP 8.preg_replace()with the/emodifier ran the replacement as PHP. It was removed in PHP 7.includeorrequireof a file the script has just written, or of adata://stream whenallow_url_includeis on.
Plenty of hacked sites still run older PHP versions, so malware keeps using all of them.
To undo it, treat each of them as eval(): its string argument is the next layer. UnPHP decodes as if it were PHP 7.4, where all of these still work, and follows each one like an eval.
8. Control flow flattened with goto
goto b;
a: echo "second\n"; goto c;
b: echo "first\n"; goto a;
c: echo "third\n";Tools such as YAK Pro cut the code into pieces, shuffle them and chain them together with goto, and turn every if and loop into conditional jumps. The program behaves exactly as before, but the source no longer reads from top to bottom.
To undo it, follow the jumps from the start to put the statements back in order, then turn conditional jumps back into if and while. UnPHP does this in its output: the example above comes out as three echo lines in order. Where the flow can’t be written with if and while, a goto stays, so the result always means the same as the input.
9. Checks that hide the payload
if (md5($_SERVER["HTTP_HOST"]) == "5d41402abc4b2a76b9719d911017c592") {
// the payload only runs on one site
}
if (strpos($_SERVER["HTTP_USER_AGENT"], "Googlebot") !== false) {
// SEO spam only search engines see
}Malware often runs its payload only under conditions: on one host name, for search engine crawlers, after a date, or when a request carries the right parameter. Anyone who runs the file in another setting sees nothing happen.
To undo it, read both branches of every condition instead of relying on what one run prints. UnPHP runs the file a second time with the first few if conditions reversed and keeps whichever run decoded more. It also ignores exit and die, so a check that stops the script doesn’t stop the decode.
10. Payloads that need a key
eval(openssl_decrypt($blob, "aes-128-cbc", $_COOKIE["k"], 0, $iv));
$p = base64_decode($blob);
for ($i = 0; $i < strlen($p); $i++) {
$p[$i] = $p[$i] ^ $_POST["k"][$i % strlen($_POST["k"])];
}
eval($p);The payload is encrypted, with AES or a simple XOR, and the key arrives with each request.
To undo it, you need the key. If it is somewhere in the file, decrypt the payload the way the file would; UnPHP supports openssl_decrypt() for this. If the key only comes from the request, the file can’t be decoded on its own, and it doesn’t need to be: a file that runs encrypted code sent by a visitor is a backdoor, whatever the attacker sends it.
11. Compiled encoders
ionCube, SourceGuardian and the discontinued Zend Guard compile PHP to bytecode and ship it in an encrypted file with a small PHP header that checks for their loader extension. They are mostly used to protect commercial software, and they are a different problem: there is no PHP source in the file to recover. UnPHP doesn’t decode them.
Putting it together
Real samples stack these tricks. A lookup table builds the function names, variable functions call them, the payload sits under a dozen eval layers, and the decoded code is flattened with goto. Undo them from the outside in: resolve the names, decode the layers, then restore the control flow. Or paste the file into the UnPHP decoder, which follows all of this without running the code with PHP, and shows each function the file called.