{"id":177,"date":"2009-04-11T13:36:46","date_gmt":"2009-04-11T05:36:46","guid":{"rendered":"http:\/\/www.computersolutions.cn\/blog\/?p=177"},"modified":"2009-04-11T21:57:14","modified_gmt":"2009-04-11T13:57:14","slug":"random-sysadmin-intrusion-stuff-and-pecl","status":"publish","type":"post","link":"https:\/\/www.computersolutions.cn\/blog\/2009\/04\/random-sysadmin-intrusion-stuff-and-pecl\/","title":{"rendered":"Random Sysadmin intrusion stuff and PECL"},"content":{"rendered":"<p>One of our clients was sending out spam unknowingly yesterday.  I spent most of my afternoon cleaning it up,  tracking down how the attackers were doing it.<\/p>\n<p>In this clients case, they have their own server (which we maintain), and they mostly write their own code.<br \/>\nMost of the common garden variety vulnerability scans don&#8217;t work on their server, because they write their own code, although in this case it didn&#8217;t save them from being exploited.<\/p>\n<p>In order to find out what was causing the spamming, I had to find out how the attackers got in.<br \/>\nUsually this means a check of the apache logs to check for anything untoward.<\/p>\n<p>In this case, although the logs had plenty of vulnerability scans (which were to files that don&#8217;t exist on their server), I couldn&#8217;t see anything in the logs that immediately stuck out as being the cause.<br \/>\n<!--more--><br \/>\nThinking that they might be sneaky, I decided to check if any files on the server had anything interesting in them.  PHP has a few functions that are a little dangerous, most of these are system related &#8211; you can execute other software on the system using them.  <\/p>\n<p>As pretty much all the code on this server is php based, I went a little deeper, and did a filesystem scan of some common php system calls to see if anything popped out.<\/p>\n<p>In unix, this is pretty easy<\/p>\n<p><code>grep shell_exec * -R<\/code><\/p>\n<p>A few seconds later, this popped up as a result.<\/p>\n<p><code>\/userpix\/1216256595.jpg<\/code><\/p>\n<p>Hmm, a jpg file containing shell_exec?<br \/>\nSeems strange.<\/p>\n<p>A quick check of the file in a text editor showed that it was actually a shell script that contained the c99shell.<br \/>\nI soon found another script in that folder with a .jpg extension that contained the r57shell.<\/p>\n<p>Both these scripts allowed for remote pseudo shell access to the server, as well as attempts to further hack the server using common vulnerabilities.   They are both quite comprehensive in what they do, and are worthy of further dissection at a later date.<\/p>\n<p>I removed the 2 files (after making copies for further analysis), and let the client know.<br \/>\nA check of the logs revealed that calls were indeed being made to those files.<\/p>\n<p>Sneaky!<\/p>\n<p>Turns out that the client was using a common upload library, and it appears that this had an upload  vulnerability.<br \/>\nI advised them to do better error checking, update that library, and installed some additional features for them in their server.<\/p>\n<p>PHP has an extension called FileInfo, which can check files to see if they&#8217;re actually what they say they are.<br \/>\nIts not perfect, but it does add a degree of safety for situations like this where things get abused.<\/p>\n<p>Even so, you still need to avoid using FileInfo for checking gif files, as it pretty much assumes any file starting in GIF is a gif.  <\/p>\n<p>Sample code to safely check for gif below.<\/p>\n<p><code>if (!$img = @imagecreatefromgif($uploadedfilename)) {<br \/>\n  trigger_error('Not a GIF image!',E_USER_WARNING);<br \/>\n  \/\/ do necessary stuff<br \/>\n}<\/code><\/p>\n<p>Back to FileInfo.  The typical way to install Pear extensions is <\/p>\n<p><code>pecl install <NAME><\/code><\/p>\n<p>In this case  &#8220;pecl install FileInfo&#8221; wasn&#8217;t working, because we set \/tmp as non executable for safety reasons.<\/p>\n<p><code><br \/>\npecl install Fileinfo<br \/>\n3 source files, building<br \/>\nrunning: phpize<br \/>\nConfiguring for:<br \/>\nPHP Api Version:         20041225<br \/>\nZend Module Api No:      20060613<br \/>\nZend Extension Api No:   220060519<br \/>\n\/usr\/bin\/phpize: \/tmp\/pear\/cache\/Fileinfo-1.0.4\/build\/shtool: \/bin\/sh: bad interpreter: Permission denied<br \/>\nCannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF<br \/>\nenvironment variable is set correctly and then rerun this script.<\/p>\n<p>ERROR: `phpize' failed<\/code><\/p>\n<p>So, we had to install it manually, sigh.<\/p>\n<p>Manual installation of pecl files is simple, although undocumented.<\/p>\n<p>Go to the download folder:<br \/>\n<code>cd \/tmp\/pear\/cache\/<\/code><\/p>\n<p>Move the file to a different folder with execute permissions<br \/>\n<code>cp Fileinfo-1.0.4.tgz \/downloads<br \/>\ncd \/downloads<\/code><\/p>\n<p>Extract, and install<\/p>\n<p><code>tar -zxf Fileinfo-1.0.4.tgz<br \/>\ncd Fileinfo-1.0.4<br \/>\nphpize<br \/>\n.\/configure<br \/>\nmake<br \/>\nmake install<\/code><\/p>\n<p>We also had to add extension=fileinfo.so to our \/etc\/php5\/apache2\/php.ini file.<\/p>\n<p>Conclusions for us &#8211; <\/p>\n<p>Luckily,  they didn&#8217;t manage to do any damage except for send mass email.<br \/>\nTripwire and Tiger (two IDS programs we use) showed that none of the system files on their server were compromised.  <\/p>\n<p>We&#8217;re planning to further tighten their system with a PHP Hardener called Suhosin, although this depends on their scripts being compatible.  We&#8217;re currently trialing a Suhosin install with logging enabled to see what may break when its fully enabled.<\/p>\n<p>We use Suhosin on a most of our servers already.<br \/>\nIronically we had been talking about it with that client when they got hacked!  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of our clients was sending out spam unknowingly yesterday. I spent most of my afternoon cleaning it up, tracking down how the attackers were doing it. In this clients case, they have their own server (which we maintain), and they mostly write their own code. Most of the common garden variety vulnerability scans don&#8217;t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[48,25],"tags":[528,50,49],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-exploits","category-technical-mumbo-jumbo","tag-exploits","tag-shell-scripts","tag-suhosin"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":2,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/posts\/177\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.computersolutions.cn\/blog\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}