Detect Hacking attempts with Google Analytics
If someone was attempting to break into YOUR site, use YOUR bandwidth, or even use YOUR site to launch attacks against OTHER sites, would you know? How would you know? When would you know?
Would you be able to detect the attack and stop it before it caused any damage? Or would you be stuck trying to cleanup after the attack was finished?
Recently at SEER interactive while examining some unusual traffic to a client’s website, we discovered that Google Analytics was picking up an attack against the site as legitimate traffic. With a little digging we found several key indicators which can help you determine if the traffic to your site is actually traffic, or if some of it is an attack against your site. Also included in this post, is a recommendation on how to handle an attack once discovered, and the end of this post is an Alert you can setup in Google Analytics that should email you if someone starts to launch attacks against your site.
The site we were examining recently had a dramatic increase in direct traffic without an outside event to explain the increase such as a newsletter, TV appearance or marketing campaign. Since direct traffic gives us very little information, the best place to start is the Visitor report. Once inside the Visitor report, we can take a look at the map overlay, drilling down to the city level to see which cities are responsible for sending the most traffic to your site.
Carefully examine the top cities in this report, do the top cities seem appropriate to be your top cities? Does the amount of traffic from these seem to be much greater compared to the rest of cities sending you traffic? This is the first sign that you are suffering from an attack, as these top cities can be the launching point for these attacks.
You will often see a sudden sharp increase in traffic, starting on the day the attack was launched.
(Image no longer available)
Hmmm… Does this graph look strange? It should!
Simply seeing an increase in traffic from a city is not in itself enough evidence to say that you’re site is being attacked. If you drill down into the city report itself there may be a couple more indications that you’re site is being attacked. These factors are also useful if the attack has been sustained for a long period of time you and there is no sudden sharp increase of traffic to alert you of the attack.
(Image no longer available)
- Pages / Visit will be closer to 0 than site average.
- Time on Site will be closer to 0 seconds than site average.
- % Of New Visitors will be closer to 100% than site average.
- Bounce Rate for your site will be closer to 100% than site average.
Why do these numbers indicate that your site is suffering from some kind of an attack?
Most Bots do maintain sessions.
Since bots do not maintain sessions, each time the bot queries your site it appears as a new visitor, who immediately leaves, resulting in a bounced visit. This is also why the attack shows up as direct traffic in your reports. If you receive a significant number of these visits your numbers will be skewed to look like the results listed above.
It’s important to note that depending on the amount of traffic your site gets, and the nature of your site, you may not see all of these trends. However if you see a significant difference in these stats compared to other referring cities there is a good chance that your site is being attacked.
What Next?
You have determined that you’re site is suffering from an attack, or you suspect that it might be what are your next steps?
Since Google Analytics is just a reporting system, and it cannot collect the IP addresses of visitors, Google Analytics cannot do anything besides alert you that your site is suffering from an attack.
Since you can’t use Google Analytics for this, the best idea is to contact your hosting company. If you give your hosting company the cities from which you believe the attack is originating from they should be able to determine what IP addresses the attacks are coming from and block them, thus ending the attack.
Google Analytics Alert
If you would like to setup an Alert in Google Intelligence to email you if any of this behavior is detected follow these steps:
- Login to the Google Analytics profile you wish to setup the alert on.
- Select “Intelligence” (beta) from the left navigation.
- Select “Create Custom Alert”.
- Enter an Alert name such as “Hacking Monitor”.
- Select Period -> Day
- Check Receive Email Alert
Now for the fun part, the alert itself! Do not include quotes when entering these values in Google Analytics.
- Select this applies to -> “City”
- Select Condition -> “Matches Regular Expression”
- Enter Value -> “.*”
- Select Alert me when -> “Visits”
- Condition -> “% Increases by More than”
Example Alert:
(Image no longer available)
The final two values of this alert will depend on your preferences and your website. If your website doesn’t receive much traffic you are probably safe putting a high value here (500%+ increase) as any attack will likely result in this increase in percentage. However if you run a larger website, you’ll need to decrease this number, since the attack will be a smaller percentage of traffic from that city. The last value is if you want to compare to the previous day, or to the same day the previous week, this will depend on the traffic patterns of your website.
The end result of this alert will be Whenever any city sends you a dramatic increase in traffic, the primary indicator of an attack on your site, send an email alert.
The last thing to keep in mind is that this alert will only let you know of hacking attacks that run Javascript. If the attacks do not run Javascript then the Google Analytics code snippet will not trigger and the attack will not be recorded as a visit.
Have you noticed this kind of activity on your site before? Do you make use of any other Google Analytics alerts to protect against foul play?
Comments and Questions welcome!
Posted: 07.26.10

Jose Uzcategui:
Thanks Andrew for the great tip.
To my knowledge, we haven’t experienced any serious security threats or attacks. Either way, I think it’s a good alert to have just in case :)
Brian Clifton:
Nice lateral use of GA here :)
One comment – if a bot doesn’t set cookies, Google Analytics will not be able to track/report on the visit – it needs *both* JavaScript and cookies in order to receive data.
Julien Coquet:
Nice post Andrew!
Of course, you could complement this method with server-side GA tracking (with mobile tracking for instance) ;-)
Cheers,
Julien
Rat Mort:
Thanks for sharing this tips, but I don’t think analytics is the best tool to do such monitoring. It nice to understand how “Intelligence” works in Analytics, but from my opinion, hacking detection should be linked to a specific action to be efficient, like banning the ip, or redirecting the hacker to a temporary page for a while to limit bandwith. Analytics is not designed for that at all.
When do you recieve the email? In real time, or once the data are processed, few ours to 24 h later?
Jason Smith:
Great advice and the useful email alert. Forewarned is to be forearmed. Thanks
Andrew Burke:
@Jose
Glad to be of help! Hopefully you won’t need it but as you said good to have just in case.
@Brian
Thanks for the catch, Ive updated the post to reflect this.
@Julien
You certainly could, this would help you detect non-JavaScript attacks as well.
@Rat
You are right; analytics isn’t the best tool to use to monitor hacking. However often times web analysts are consultants who either do not have direct access to a client’s server, or do not know how to the setup anti-hacking techniques you mentioned. This is just a way for a web analyst to detect an attack, further measures must be taken to stop it. You should receive the email alert sometime the next day once Google Analytics has finished processing the previous days data.
@Jason
Youre quite welcome!
SEO Brighton:
great article – I suspected one of my sites had been hacked and followed the Google analytics alert suggestion and it worked – thank you so much :)
Andrew Burke:
@SEO Brighton
Glad to hear it was of help!
maurice:
well this is only going to detect trafic on port 80 – say a brute force attack to guess passwords or a DDOS.
its not going to replace a proper ICE system
Iyabo:
This is so good to know. I have experienced some unusual spikes on some of my sites which i really cant explain. This makes it all easier Andrew thanks.
I will have to do this and see how it works out.
Thank you for this insightful post.
Hal1:
Hacking on site caused endless tech headaches but hijacked pages did not cache.
beware the single client sign on. Turns out Drupal Open Source really is open source unless you change the setting.