Clickjacking: an invisible button under the cursor
Clickjacking is when a foreign page puts your site in a transparent iframe over a decoy, and the user clicks the real button without knowing it. Protection is a Content-Security-Policy header with frame-ancestors (the older X-Frame-Options), while a JavaScript window.top check is unreliable. A CSRF token does not help, because the user makes the request.
Pentest · Client side
A user clicked "Claim prize" and something changed in his account. What did he really click?
A page promises a prize and has a big button. But over it lies a transparent iframe of another site, in which a "Delete account" button ended up under the cursor. The user clicks where he looks, and the click reaches the hidden site. This is clickjacking (UI redress): the term was introduced by Jeremiah Grossman and Robert Hansen in 2008.
Two layers under one click
Mechanism. The attacker opens a foreign site in an iframe with the style opacity: 0 and positions it so that the needed button lands right over the decoy. The browser sends the click to the top layer, which is the iframe. Addresses like ?text=… with values that prefill a field let the action be prepared. For success the action in the iframe must also run as the user, that is the session cookie has to be sent. Cookies with SameSite=Lax or Strict are not sent in a cross-site iframe, so the framed page opens logged out; the risk remains where the cookie is SameSite=None and the browser does not partition third-party cookies (Chrome by default), or the embedding happens within the same site.
A real case. On 12 February 2009 a "Don’t Click" prank spread on Twitter: a page had a button with that label, and above it a transparent iframe with a Twitter form whose field was prefilled through the address. A click published a message with a link on behalf of the user, and it reached his followers. A script that stops a page from loading in an iframe ("frame-busting") is unreliable; later the headers described below replaced it. A similar "likejacking" scheme made people press "like" on Facebook.
Defence. The server forbids embedding the page with a header:
Content-Security-Policy: frame-ancestors 'none'
# legacy, still sent for old browsers
X-Frame-Options: DENY
frame-ancestors 'none'forbids embedding entirely,'self'allows only your own origin, a list of origins allows chosen partners. If both headers are sent,frame-ancestorstakes priority;X-Frame-Options(DENYorSAMEORIGIN) is older, described in RFC 7034, and its valueALLOW-FROMis obsolete;- JavaScript "frame busting" is unreliable (an iframe’s
sandboxturns it off), so it stays a fallback; - Sensitive actions (deleting, payment) additionally need a confirmation or a re-entered password, and cookies carry
SameSite.
How QA checks it. Looks at the headers of pages with actions (login, settings, payment, deletion) in DevTools or curl -I. On an authorized environment builds a local page with <iframe src="…">: if the target page loads, embedding is not forbidden. Exception: pages that partners deliberately embed (payment widgets) must have a closed list in frame-ancestors.
Common mistakes.
- Relying on JavaScript that checks
window.top; - Setting the header only on the main page and not on pages with actions;
- Mixing it up with CSRF: in clickjacking the user makes the click himself and a CSRF token does not help because the request is genuine.
Check yourself
A page has a CSRF token but answers without frame-ancestors and X-Frame-Options. Is it protected from clickjacking?
Answer
No. In clickjacking the user himself makes the request with the token by his click in the frame, so the token is valid. A ban on embedding (frame-ancestors) or a confirmation of the sensitive action is needed.
Why can a page open logged out in a cross-site iframe even if the user is logged in?
Answer
If the session cookie has SameSite=Lax or Strict, the browser does not attach it to requests of an iframe from another site. So the attack works with a SameSite=None cookie or embedding within one site.