{"id":213,"date":"2011-05-21T23:18:02","date_gmt":"2011-05-21T21:18:02","guid":{"rendered":"\/\/www.mcgill.org.za\/stuff\/?p=213"},"modified":"2011-06-15T12:49:13","modified_gmt":"2011-06-15T10:49:13","slug":"php-deobfuscation","status":"publish","type":"post","link":"https:\/\/www.mcgill.org.za\/stuff\/archives\/213","title":{"rendered":"PHP deobfuscation"},"content":{"rendered":"<p>One of the things that you see with depressing regularity when hosting crummy PHP scripts for others is this:<\/p>\n<blockquote><p><code>eval&28;base64_decode&28;'aWYgKCFlbXB0eSgkX1JFUVVFU1RbInRoZW<\/code><\/p><\/blockquote>\n<p>What&#8217;s that?  Well it says to decode that gobbledegook into a binary stream (the <code>base64_decode<\/code> part), and then interpret whatever it turns out to be as PHP code (the <code>eval)<\/code><\/p>\n<p>Now, if you have the time, you can decode this kind of thing yourself, and maybe find out whether it is evil or not.  This takes some time (which is why you need to have the time).  If you install the php xdebug extension, then you can hack in a bit of code to trace the execution of the process, and print out what it is that is being eval&#8217;d and see what it is.  That&#8217;s the right way to do it.  Alternatively you can do a crude search and replace and a few other hacks, and end up with the following abominable PHP code that decodes a stack of php obfuscation.  It&#8217;s time to tell the world:<br \/>\nHere&#8217;s the code: <a href=\"\/\/www.mcgill.org.za\/software\/deobfuscate\/deobfuscate.php?html\">deobfuscate.php<\/a> (this is new as of today &#8211; get your copy while it&#8217;s hot).<\/p>\n<p>One of the less evil reasons that PHP code may be obfuscated is that the writer of the code is trying to extract payment.  This usually means that they have embedded obnoxious advertising in the code (such as for gambling, drugs, SEO and other social ills).  Frequently they also add toothless legal threats against removing their obnoxious advertising.  <\/p>\n<p>This one of today takes the cake.  It combines all of that good stuff, like putting the copyright notice in the &#8220;uncopyable part&#8221;, a standard legal threat, but it adds that little something extra:<\/p>\n<blockquote><p><code><span style=\"color: #000000\"><span style=\"color:#0000BB\">&lt;?php<br \/>\n<\/span> <span style=\"color: #007700\">eval(<\/span><span style=\"color: #0000BB\">base64_decode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color:#DD0000\">'ZnVuY3Rpb24gZ2V0X2hlYWRzKCkgeyBpZiAoIWZpbGVf<br \/>\nZXhpc3RzKGRpcm5hbWUoX19maWxlX18pIC4gIi9mdW5jdGlvbnMucGhwIikgfHwg<\/span><br \/>\n<span style=\"color: #DD0000\">IWZ1bmN0aW9uX2V4aXN0cygidGhlbWVfdXNhZ2VfbWVzc2FnZSIpICkgeyBlY2hv<\/span><br \/>\n<span style=\"color: #DD0000\">ICgiVGhpcyB0aGVtZSBpcyBsaWNlbnNlZCB1bmRlciBDQzMuMCwgeW91IGFyZSBu<\/span><br \/>\n<span style=\"color: #DD0000\">b3QgYWxsb3dlZCB0byBtb2RpZnkvcmVtb3ZlIG91ciBsaW5rIHdpdGhvdXQgcGVy<\/span><br \/>\n<span style=\"color: #DD0000\">bWlzc2lvbi4gPGJyIC8+VGhhbmsgeW91IGZvciBzdXBwb3J0aW5nIHVzIG1ha2lu<\/span><br \/>\n<span style=\"color: #DD0000\">ZyBtb3JlIEZSRUUgY3JlYXRpdmUgdGhlbWVzLiIpOyBkaWU7IH0gfQ=='<\/span><span style=\"color: #007700\">));<br \/>\n<\/span><\/span><\/code>\n<\/p><\/blockquote>\n<p>What does it mean, do I hear you ask?  Well, it decodes to this:<\/p>\n<blockquote><p><code><span style=\"color: #000000\"><span style=\"color: #0000BB\">&lt;?php<\/span> <span style=\"color: #007700\">function&nbsp;<\/span><span style=\"color: #0000BB\">get_heads<\/span><span style=\"color: #007700\">()&nbsp;{&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">file_exists<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">dirname<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">__file__<\/span><span style=\"color: #007700\">)&nbsp;.<br \/>\n<\/span><span style=\"color: #DD0000\">\"\/functions.php\"<\/span><span style=\"color: #007700\">)&nbsp;||&nbsp;!<\/span><span style=\"color: #0000BB\">function_exists<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"theme_usage_message\"<\/span><span style=\"color: #007700\">)&nbsp;)&nbsp;{<br \/>\necho&nbsp;(<\/span><span style=\"color: #DD0000\">\"This&nbsp;theme&nbsp;is&nbsp;licensed&nbsp;under&nbsp;CC3.0,&nbsp;you&nbsp;are&nbsp;not&nbsp;allowed<br \/>\nto&nbsp;modify\/remove&nbsp;our&nbsp;link&nbsp;without&nbsp;permission.&nbsp;&lt;br&nbsp;\/&gt;Thank&nbsp;you<br \/>\nfor&nbsp;supporting&nbsp;us&nbsp;making&nbsp;more&nbsp;FREE&nbsp;creative&nbsp;themes.\"<\/span><span style=\"color: #007700\">);&nbsp;die;&nbsp;}&nbsp;}<\/span><\/span><\/code><\/p><\/blockquote>\n<p>Yep.  That&#8217;s right.  It checks that <code>functions.php<\/code> is still around.  It had better be.  It also checks whether there is a function <code>theme_usage_message<\/code> defined.  You must not take out the link. (What link?)  And it you don&#8217;t like that, then <code>die<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things that you see with depressing regularity when hosting crummy PHP scripts for others is this: eval&#038;28;base64_decode&#038;28;&#8217;aWYgKCFlbXB0eSgkX1JFUVVFU1RbInRoZW What&#8217;s that? Well it says to decode that gobbledegook into a binary stream (the base64_decode part), and then interpret whatever &hellip; <a href=\"https:\/\/www.mcgill.org.za\/stuff\/archives\/213\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[25,24,17,16,190,11],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-stuff","tag-code","tag-php","tag-rants","tag-security","tag-stuff","tag-stupidity"],"_links":{"self":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":27,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/posts\/213\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mcgill.org.za\/stuff\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}