Cross Site Scripting is one of the most common and powerful vulnerabilities on the Web.
The OWASP organization has classified it in the third place in the top ten of the most critical web applications vulnerabilities
(OWASP Top 10 2013: https://www.owasp.org/index.php/Top_10_2013-Top_10 )
Figure 1: OWASP TOP 10 2013
The cross-site scripting attack is an attack on web applications which allow a hacker to inject malicious scripts to perform malicious actions. The malicious script is executed on the browser side, which makes this attack very powerful and critical.
You can have more information about the attack with some good articles here:
http://resources.infosecinstitute.com/deadly-consequences-XSS/
Alternatively, here:
http://resources.infosecinstitute.com/cross-site-scripting-XSS/
In this article, we will discuss how can we use an image to perform XSS attacks from simple to advanced exploitation.
Let’s go deeperLet’s pretend we want to inject a JS script directly, how can we do it?
You have to know that if the webmaster/administrator of a platform permits execution of JS scripts from the same domain. If so, we can exploit that!
The target just has to have a WYSIWYGeditor that permit writing HTML code and downloading images. This is sufficient for a hacker to create a script and inject it into an image, or create an image with an injected payload.
What’s a payload? Simply, It is a script that executes malicious actions.
Before going deeper into the exploitation, I advise you to read the articles related to these vulnerabilities that I shared with you at the beginning of the article
Now imagine that we can exploit XSS with an image. Can we insert it as a comment on an Article? A Blog? With a BEeF hookor another exploitation framework?
The results would be chaotic.
Now, How Can We Do It?For the exploitation, you will need this script to inject javascript into a gif:
http://pastebin.com/6yUbfGX5
and this one is for bmp ones:
http://pastebin.com/04y7ee3u
The two scripts are developed with python; the most important parts are:
The part that handles the opening of an open a valid GIF header in Python with \x2A\x2F (aka *\) .Figure 2: The part of the exploit that handles the creation of a valid GIF Header
If you open a gif image with a Hexadecimal editor, you can see in the metadata GIF89a , which verify the validity of our exploit.
Figure 3: Hexadecimal Editor
You can have more information here:
http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
Moreover, the part that handles the injecting of the payload:Figure 4: The part of the exploit that handles the injection of the payload
Finally, The Exploitation
Once you have downloaded the script, tape the following command that permits one to adapt the execution of the script into the linux environment (the environment used is Kali Linux)
Figure 5: Adaptation of the script into Unix environment
Then, let’s inject our test script into the image with the following command (we will do the test with a gif image so that we will use the gif injector script The first one). This is just a simple payload that will show you a JavaScript alert with the message “Learn XSS with gif,” but in a real scenario, an attacker will try to steal your cookie, inject hook (like BEeF one), redirect you to a malicious site such as:
Figure 6: Injection of the XSS payload into the gif image
The result image will change from [image_name].gif to [image_name]_malw.gif.
Now we have injected our script into our image; we can use it as an HTML page with the following script, that is automatically generated:
Figure 6: HTML Script to execute the image
Ethical Hacking Training Resources (InfoSec)
The image and script source is the output image.
Now let’s copy all the content of the directory, and put it into the /var/www directory, then run the apach2 service:
Figure 7: Starting APACH2 service
Then run the HTML page into your browser and SURPRISE:
Figure 8: Execution of the injected XSS payload
We can also exploit with a redirection like this:
Figure 9: Injection of redirection payload
We can also do it in C or ASM, not just in with Python; you can have more information there:
https://github.com/jklmnn/imagejs
https://gist.github.com/ajinabraham/f2a057fb1930f94886a3
We can also use it with BEeF, which allows to a hacker a golden door to your browser. You can see related article to this powerful framework here:
http://resources.infosecinstitute.com/beef-part-1/
How to prevent itThe prevention of this type of exploitation is very difficult, but you can provide it with the following points:
Always filter user input Use whitelist for the elements loaded, even if it is from the same domain Use high-level models: MVC, PEAR, SRUTS… Use a token based systemMoreover, always remember never to let the user write into HTML anywhere on your platform, it is the most important thing.
ConclusionThe XSS vulnerability is one of the most powerful vulnerabilities on the web, so never underestimate it and never forget that it can be exploited not just with a vulnerable URL, but also can be injected into content like images like we just saw.
In the next articles, we will discover how we can exploit XSS into applications and some advanced exploitations.
Always remember: Never trust user input