Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent frame injection (clickjacking) in java application?

How can we prevent frame injection in Java application?

Like in a Penetration testing, it is found that if a hacker drafts a demo html page, and inside that page he has used iframe, which has the URL of the working application, he/she can see the data through that URL/request(created in an iframe).

suppose this is the hackers file, test.html:

<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head><body>
<iframe id="inner" src="http://hostname:8080/Application_Name/ABC/DEF/SomePage.jsp?ABC=QWERTYL&XYZ=1&CDE=24" width="600" height="400" scrolling="yes">

</iframe>
</body>
</html>

And now the hacker is able to retrieve the data within the application. How to stop this?

like image 983
Hargovind Singh Avatar asked Apr 24 '15 10:04

Hargovind Singh


People also ask

How can we prevent clickjacking in Java?

Using the X-Frame-Options header A better approach to prevent clickjacking attacks is to ask the browser to block any attempt to load your website within an iframe. You can do it by sending the X-Frame-Options HTTP header.

How can clickjacking attack be prevented?

There are three main ways to prevent clickjacking: Sending the proper Content Security Policy (CSP) frame-ancestors directive response headers that instruct the browser to not allow framing from other domains. The older X-Frame-Options HTTP headers is used for graceful degradation and older browser compatibility.

What is iframe clickjacking?

Typically, clickjacking is performed by displaying an invisible page or HTML element, inside an iframe, on top of the page the user sees. The user believes they are clicking the visible page but in fact they are clicking an invisible element in the additional page transposed on top of it.

What header can be used to protect against clickjacking attacks?

The X-Frame-Options HTTP header can be used to indicate whether or not a browser should be allowed to render a page in a <frame> , <iframe> or <object> tag. It was designed specifically to help protect against clickjacking. The page cannot be displayed in a frame, regardless of the site attempting to do so.


1 Answers

This is clickjacking attack: https://www.owasp.org/index.php/Clickjacking The simpliest way to prevent it is to add header "X-Frame-Options" with value "DENY". This can be done using filter. Register it in your web.xml and use code like this:

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
            ServletException {
    HttpServletResponse response = (HttpServletResponse) resp;
    response.addHeader("X-Frame-Options", "DENY");    
    chain.doFilter(req, resp);
} 

All modern browsers support this header, but to protect users with legacy browsers you will need also defensive javascript in the UI. More details: https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet

like image 196
mies Avatar answered Sep 28 '22 05:09

mies