Google Webmaster Central Blog - Official news on crawling and indexing sites for the Google index

Best practices against hacking

Friday, February 20, 2009 at 6:46 AM

These days, the majority of websites are built around applications to provide good services to their users. In particular, are widely used to create, edit and administrate content. Due to the interactive nature of these systems, where the input of users is fundamental, it's important to think about security in order to avoid exploits by malicious third parties and to ensure the best user experience.

Some types of hacking attempts and how to prevent them

There are many different types of attacks hackers can conduct in order to take partial or total control of a website. In general, the most common and dangerous ones are SQL injection and cross-site scripting (XSS).

SQL injection is a technique to inject a piece of malicious code in a web application, exploiting a security vulnerability at the database level to change its behavior. It is a really powerful technique, considering that it can manipulate URLs (query string) or any form (search, login, email registration) to inject malicious code. You can find some examples of SQL injection at the Web Application Security Consortium.

There are definitely some precautions that can be taken to avoid this kind of attack. For example, it's a good practice to add a layer between a form on the front end and the database in the back end. In PHP, the PDO extension is often used to work with parameters (sometimes called placeholders or bind variables) instead of embedding user input in the statement. Another really easy technique is character escaping, where all the dangerous characters that can have a direct effect on the database structure are escaped. For instance, every occurrence of a single quote ['] in a parameter must be replaced by two single quotes [''] to form a valid SQL string literal. These are only two of the most common actions you can take to improve the security of a site and avoid SQL injections. Online you can find many other specific resources that can fit your needs (programming languages, specific web applications ...).

The other technique that we're going to introduce here is cross-site scripting (XSS). XSS is a technique used to inject malicious code in a webpage, exploiting security vulnerabilities of web applications. This kind of attack is possible where the web application is processing data obtained through user input and without any further check or validation before returning it to the final user. You can find some examples of cross-site scripting at the Web Application Security Consortium.

There are many ways of securing a web application against this technique. Some easy actions that can be taken include:
  • Stripping the input that can be inserted in a form (for example, see the strip tags function in PHP);
  • Using data encoding to avoid direct injection of potentially malicious characters (for example, see the htmlspecialchars function in PHP);
  • Creating a layer between data input and the back end to avoid direct injection of code in the application.
Some resources about CMSs security

SQL injection and cross-site scripting are only two of the many techniques used by hackers to attack and exploit innocent sites. As a general security guideline, it's important to always stay updated on security issues and, in particular when using third party software, to make sure you've installed the latest available version. Many web applications are built around big communities, offering constant support and updates.
To give a few examples, four of the biggest communities of Open Source content management systems—Joomla, WordPress, PHP-Nuke, and Drupal—offer useful guidelines on security on their websites and host big community-driven forums where users can escalate issues and ask for support. For instance, in the Hardening WordPress section of its website, WordPress offers comprehensive documentation on how to strengthen the security of its CMS. Joomla offers many resources regarding security, in particular a Security Checklist with a comprehensive list of actions webmasters should take to improve the security of a website based on Joomla. On Drupal's site, you can access information about security issues by going to their Security section. You can also subscribe to their security mailing list to be constantly updated on ongoing issues. PHP-Nuke offers some documentation about Security in chapter 23 of their How to section, dedicated to the system management of this CMS platform. They also have a section called Hacked - Now what? that offers guidelines to solve issues related to hacking.

Some ways to identify the hacking of your site

As mentioned above, there are many different types of attacks hackers can perform on a site, and there are different methods of exploiting an innocent site. When hackers are able to take complete control of a site, they can deface it (changing the homepage), erase all the content (dropping the tables of your database), or insert malware or cookie stealers. They can also exploit a site for spamming, such as by hiding links pointing to spammy resources or creating pages that redirect to malware sites. When these changes in your application are evident (like defacing), you can easily spot the hacking activity; but for other types of exploits, in particular those with spammy intent, it won't be so obvious. Google, through some of its products, offers webmasters some ways of spotting if a site has been hacked or modified by a third party without permission. For example, by using Google Search you can spot typical keywords added by hackers to your website and identify the pages that have been compromised. Just open google.com and run a site: search query on your website, looking for commercial keywords that hackers commonly use for spammy purposes (such as viagra, porn, mp3, gambling, etc.):

[site:example.com viagra]

If you're not already familiar with the site: search operator, it's a way to query Google by restricting your search to a specific site. For example, the search site:googleblog.blogspot.com will only return results from the Official Google Blog. When adding spammy keywords to this type of query, Google will return all the indexed pages of your website that contain those spammy keywords and that are, with high probability, hacked. To check these suspicious pages, just open the cached version proposed by Google and you will be able to spot the hacked behavior, if any. You could then clean up your compromised pages and also check for any anomalies in the configuration files of your server (for example on Apache web servers: .htaccess and httpd.conf).
If your site doesn't show up in Google's search results anymore, it could mean that Google has already spotted bad practices on your site as a result of the hacking and may have temporarily removed it from our index, due to infringement of our webmaster quality guidelines.

In order to constantly keep an eye on the presence of suspicious keywords on your website, you could also use Google Alerts to monitor queries like:

site:example.com viagra OR casino OR porn OR ringtones

You will receive an email alert whenever these keywords are found in the content of your site.

You can also use Google's Webmaster Tools to spot any hacking activity on your site. Webmaster Tools provide statistics about top search queries for your site. This data will help you to monitor if your site is ranking for suspicious unrelated spammy keywords. The 'What Googlebot sees' data is also useful, since you'll see whether Google is detecting any unusual keywords on your site, regardless of whether you're ranking for them or not.

If you have a Webmaster Tools account and Google believes that your site has been hacked, often you will be notified according to the type of exploit on your site:
  • If a malicious third party is using your site for spammy behaviors (such as hiding links or creating spammy pages) and it has been detected by our crawler, often you will be notified in the Message Center with detailed information (a sample of hacked URLs or anchor text of the hidden links);
  • If your site is exploited to place malicious software such as malware, you will see a malware warning on the 'Overview' page of your Webmaster Tools account.
Hacked behavior removed, now what?

Your site has been hacked or is serving malware? First, clean up the malware mess and then do one of the following:
  • If your site was hacked for spammy purpose, please visit our reconsideration request page through Webmaster Tools to request reconsideration of your site;
  • If your site was serving malware to users, please submit a malware review request on the 'Overview' page of Webmaster Tools.
We hope that you'll find these tips helpful. If you'd like to share your own advice or experience, we encourage you to leave a comment to this blog post. Thanks!

The comments you read here belong only to the person who posted them. We do, however, reserve the right to remove off-topic comments.

20 comments:

Marjory said...

That is really great information. Thanks so much.

Public Speaking Training Expert, David Portney said...

Question: are static HTML sites less vulnerable to hacking than PHP or other more complexly coded sites?

Sankar Datti said...

Nice information. Every webmaster should have a look at this article. Most of the webmasters are suffering with hacks. They all need to follow tips to protect their sites.

Tarun said...

Thanks for the information very informative

noam said...

i built recently a website and the company installed in their website a web application firewall - software solution, that provides protection to the 7 layer- HTTP, real time. is someone can give me feedback on this solution? http://www.applicure.com

Anoop Engineer said...

a similar article on preventing GQL injection and XSS in google app engine would be quite nice.

Fear My Design said...

If you a Blogger Blog with google, is there a chance that this could get hacked as well? and loose all the information or articles you have written?

Paolo Petrolini and Iris Mariano, Search Quality Team said...

Hi guys, thank you very much for your feedback!
We would like to underline the fact that the techniques and precautions that we described in our blog post are only common examples.
There are different ways of securing a web application starting from the security of passwords, permission management and many others.
As we mentioned, what is really important is to constantly stay updated about security according to your specific needs.
Thanks a gain and keep posting your questions and comments!
Paolo and Iris

Smartdomainers said...

Now I fully understand why my site got probem last month.
Thanks a lot

The Mighty Will said...

Tsk, tsk... I am not surprised to see that DotNetNuke is once again shunned in the open source CMS category. Especially considering the topic at hand. DNN has a great Security Team and formal security policy. http://security.dotnetnuke.com

Raj said...

Paolo Petrolini and Iris Mariano,

Can I have your email address as I would like to discuss on adopting the said best practice.

Thank you

Please email to raj@cybersecurity.my

hbw said...

Google appears to be over-punishing the victims. We had our site hacked but now understand it will be some weeks before Google "considers" putting back our pages on its list. Currently, no-one can find us. This is punishing a whole community as we operate an community website. This is the first time we have been hacked since we started in 1995 and we have taken all steps to prevent its recurrence. While appreciating the work Google does against the hackers, we feel we are being punished for being victims.

la nena said...

Hi,
great information, however, I noticed that the search "site:example.com porn" (without quotes, returns results based on the cached pages rather than the actual content of the site. This results in confusion ...
A little bit off topic: I notice something similar on the crawling result problems reported in the webmaster tools, for example, after correcting missing title errors, I still see the same pages appear reported with the same error and the confusing thing is that the report says something like 'last review: 14.3.2009' (that would be today, even though the titles were corrected about a week ago).
I think this kind of tools should work with actual information, not with cached pages. My site had been spammed and I would use the reconsideration tool, however, since I still see appearing many pages when checking for site:example.com porn, I'm not sure if it's the right moment.

Buzzlord said...

I have a question. When you say that we can test if our site was hacked by putting "spammy" keywords, what keywords would those be specifically?

Thanks for the info!

MakeSafePassword said...

I created a randomization tool that might help less-technical people avoid simple password guessing / stealing: http://makesafepassword.com

The default is set to only use numbers and letters, but there's an option for punctuation too.

RoadTrucker said...

How about a little help here Google? How hard would it be to allow webmasters to add negative keywords, that should not appear on their website. When Google indexes that website it could very easily scan the negative word list and if any are found, trigger an email to the webmaster, that action is needed. Lets face it.

Amarjit Singh said...

Interesting post. A well planned hacker follows these steps before performing any attack..Hope these links will be helpful.

5 steps mentioned below.

Hacking Phase 1 - Reconnaissance

http://www.freehacking.net/2009/03/hacking-phase-1-reconnaissance.html

Hacking Phase 2 - Scanning

http://www.freehacking.net/2009/03/hacking-phase-2-scanning.html

Hacking Phase 3 - Gaining Access - The hacker exploits the system

http://www.freehacking.net/2009/03/hacking-phase-3-gaining-access-hacker.html

Hacking Phase 4 - Maintaining Access

http://www.freehacking.net/2009/03/hacking-phase-4-maintaining-access.html

Hacking Phase 5 - Covering Tracks

http://www.freehacking.net/2009/03/hacking-phase-5-covering-tracks.html

Athlon Maurer said...

Thanks for this info, it has helped me to make my php/mysql-project something more secure.

Paresh Shrimali said...

Today many websites and online services hacked by hackers. also we face problems through virus also. we need exact solution towards hacking.

Thank Ypu
Paresh Shrimali

Google Webmaster Central said...

Hi everyone,

Since over a year has passed since we published this post, we're closing the comments to help us focus on the work ahead. If you still have a question or comment you'd like to discuss, free to visit and/or post your topic in our Webmaster Central Help Forum.

Thanks and take care,
The Webmaster Central Team