Hacking

How to exploit XSS with an image

Kondah Hamza
February 7, 2018 by
Kondah Hamza

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:

/deadly-consequences-XSS/

Alternatively, here:

/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.

FREE role-guided training plans

FREE role-guided training plans

Get 12 cybersecurity training plans — one for each of the most common roles requested by employers.

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?

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 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:

http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html

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

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 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:

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:

/beef-part-1/

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.

Become a Certified Ethical Hacker, guaranteed!

Become a Certified Ethical Hacker, guaranteed!

Get training from anywhere to earn your Certified Ethical Hacker (CEH) Certification — backed with an Exam Pass Guarantee.

Conclusion

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 

Kondah Hamza
Kondah Hamza

Kondah Hamza is an expert in it security and a Microsoft MVP in enterprise security. He is also involved with various organizations to help them in strengthening of their security. Today, he offers his services mainly as Consultant, Auditor/Pentester and Independent Trainer with Alphorm.com.