How to exploit XSS with an image
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
Figure 1: OWASP TOP 10 – 2013
The cross-site scripting attack is an attack on web applications that 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:
In this article, we will discuss how can we use an image to perform XSS attacks from simple to advanced exploitation.
Let’s go deeper
Let’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 WYSIWYG editor 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 hook or another exploitation framework?
The results would be chaotic.
Now, How Can We Do It?
and this one is for bmp ones:
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 x2Ax2F (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:
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 4: Adaptation of the script into Unix environment
Figure 5: 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
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:
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:
How to prevent it
The 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 system
Moreover, always remember never to let the user write into HTML anywhere on your platform, it is the most important thing.
The 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 ☺