Clickjacking là gì

Introduction¶

This cheat sheet is intended to provide guidance for developers on how to defend against Clickjacking, also known as UI redress attacks.

Bạn đang xem: Clickjacking là gì

There are three main mechanisms that can be used to defend against these attacks:

Implementing JavaScript code in the page to attempt to prevent it being loaded in a frame (known as a "frame-buster").

Note that these mechanisms are all independent of each other, and where possible more than one of them should be implemented in order to provide defense in depth.

Defending with Content Security Policy (CSP) frame-ancestors directive¶

The frame-ancestors directive can be used in a Content-Security-Policy HTTP response header to indicate whether or not a browser should be allowed to render a page in a or . Sites can use this to avoid Clickjacking attacks by ensuring that their content is not embedded into other sites.

frame-ancestors allows a site to authorize multiple domains using the normal Content Security Policy semantics.

Content-Security-Policy: frame-ancestors Examples¶

Common uses of CSP frame-ancestors:

Content-Security-Policy: frame-ancestors "none";This prevents any domain from framing the content. This setting is recommended unless a specific need has been identified for framing.Content-Security-Policy: frame-ancestors "self";This only allows the current site to frame the content.Content-Security-Policy: frame-ancestors "self" *.somesite.com https://myfriend.site.com;This allows the current site, as well as any page on somesite.com (using any protocol), and only the page myfriend.site.com, using HTTPS only on the default port (443).

Note that the single quotes are required around self and none, but may not occur around other source expressions.

See the following documentation for further details and more complex examples:

Limitations¶

Browser support: CSP frame-ancestors is not supported by all the major browsers yet.

Defending with X-Frame-Options Response Headers¶

The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a or . Sites can use this to avoid Clickjacking attacks, by ensuring that their content is not embedded into other sites. Set the X-Frame-Options header for all responses containing HTML content. The possible values are "DENY", "SAMEORIGIN", or "ALLOW-FROM uri"

X-Frame-Options Header Types¶

There are three possible values for the X-Frame-Options header:

DENY, which prevents any domain from framing the content. The "DENY" setting is recommended unless a specific need has been identified for framing.SAMEORIGIN, which only allows the current site to frame the content.ALLOW-FROM uri, which permits the specified "uri" to frame this page. (e.g., ALLOW-FROM http://www.example.com).Check limitations below because this will fail open if the browser does not support it.

Browser Support¶

The following browsers support X-Frame-Options headers.

References:

Implementation¶

To implement this protection, you need to add the X-Frame-Options HTTP Response header to any page that you want to protect from being clickjacked via framebusting. One way to do this is to add the HTTP Response Header manually to every page. A possibly simpler way is to implement a filter that automatically adds the header to every page or to add it at Web Application Firewall of Web/Application Server level.

Common Defense Mistakes¶

Meta-tags that attempt to apply the X-Frame-Options directive DO NOT WORK. For example, will not work. You must apply the X-FRAME-OPTIONS directive as HTTP Response Header as described above.

Xem thêm: Customs Procedures Là Gì - Phân Loại Thủ Tục Hải Quan

Limitations¶

Per-page policy specification: The policy needs to be specified for every page, which can complicate deployment. Providing the ability to enforce it for the entire site, at login time for instance, could simplify adoption.Problems with multi-domain sites: The current implementation does not allow the webmaster to provide a list of domains that are allowed to frame the page. While listing allowed domains can be dangerous, in some cases a webmaster might have no choice but to use more than one hostname.ALLOW-FROM browser support: The ALLOW-FROM option is a relatively recent addition (circa 2012) and may not be supported by all browsers yet. BE CAREFUL ABOUT DEPENDING ON ALLOW-FROM. If you apply it and the browser does not support it, then you will have NO clickjacking defense in place.Multiple options not supported: There is no way to allow the current site and a third-party site to frame the same response. Browsers only honour one X-Frame-Options header and only one value on that header.Nested Frames don"t work with SAMEORIGIN and ALLOW-FROM: In the following situation, the http://framed.invalid/child frame does not load because ALLOW-FROM applies to the top-level browsing context, not that of the immediate parent. The solution is to use ALLOW-FROM in both the parent and child frames (but this prevents the child frame loading if the //framed.invalid/parent page is loaded as the top level document).

*

X-Frame-Options Deprecated While the X-Frame-Options header is supported by the major browsers, it was never standardized and has been deprecated in favour of the frame-ancestors directive from the CSP Level 2 specification.Proxies Web proxies are notorious for adding and stripping headers. If a web proxy strips the X-Frame-Options header then the site loses its framing protection.

Defending with SameSite Cookies¶

The SameSite cookie attribute defined in RFC 6265bis is primarily intended to defend against cross-site request forgery (CSRF); however it can also provide protection against Clickjacking attacks.

Cookies with a SameSite attribute of either strict or lax will not be included in requests made to a page within an . This means that if the session cookies are marked as SameSite, any Clickjacking attack that requires the victim to be authenticated will not work, as the cookie will not be sent. An article on the Netsparker blog provides further details on which types of requests cookies are sent for with the different SameSite policies.

This approach is discussed on the JavaScript.info website.

Limitations¶

If the Clickjacking attack does not require the user to be authenticated, this attribute will not provide any protection.

Additionally, while SameSite attribute is supported by most modern browsers, there are still some users (approximately 6% as of November 2020) with browsers that do not support it.

The use of this attribute should be considered as part of a defence-in-depth approach, and it should not be relied upon as the sole protective measure against Clickjacking.

Best-for-now Legacy Browser Frame Breaking Script¶

One way to defend against clickjacking is to include a "frame-breaker" script in each page that should not be framed. The following methodology will prevent a webpage from being framed even in legacy browsers, that do not support the X-Frame-Options-Header.