Today, I will show you one of the most common tricks that are used to force use to click on something we do not want. I developed a simple jQuery plugin to make it simpler.
What is clickjacking?
It’s described very on Wikipedia but in a very simple sentence: users click on something different than they think they do. Example? Do you remember pop-ups with an X and when you click on it and another website shows up? Facebook is protected against this attack but imagine: you are on an interesting website, you click a “Read more” button and… you liked a Facebook profile. Then, you click one more time (because you think something did not work) and you go to the page you wanted. In very many cases you would not realize you added a Like somewhere. Sounds interesting?
How does it work?
The idea behind it is very simple: add an iframe with the website (in your example Facebook’s profile page) resize the iframe to the sizes of the Like button, add transparency to 100% and set position absolute to the iframe (and it will follow the mouse). When the user clicks on the button (actually button in the iframe) the iframe is going to the background. It allows triggering the click by the page in the iframe and the user now can use the website he expects. Simple, isn’t it?
Need an example?
Some time ago I developed a simple jQuery plugin called clickjacking. Using it is extremely simple. Below you have a simple usage of the plugin. To make it easier to understand I set opacity to 50% to let you see how it works.
<html lang=" en"="">
<!--[if lt IE 9]>
<iframe id="clickjacking" src="https://iamcode.guru"></iframe>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
How to protect agains this attack?
There are few ways of doing it. I can recommend the easiest one -add the X-Frame-Options header to your responses.
There are three possible values for the X-Frame-Options header: DENY, SAMEORIGIN and ALLOW-FROM.
- X-Frame-Options: DENY – it disallows embedding the page in iframes at all. It means you cannot add the page into an iframe even from the same domain.
- X-Frame-Options: SAMEORIGIN – same as above but it allows embedding only from the same domain.
- X-Frame-Options: ALLOW-FROM – it allows using iframes only from specified URIs.
Facebook uses the first header – it disallows doing it at all!