...
   Back   Home   Programming   Blog  


Javascript Injection (Part 1: The Basics)


This is a simple program that allows you to choose the background color of a webpage. For instance, type in red, purple, or green. You will be greeted with a page that displays the color of your choice.




It's also a basic example of how Javascript injection works. The above program fails to sanitize the user input. If you look at the source code of the new page, you will notice that your color choice is being placed directly into the <body> tag.



So let's try to inject some Javascript.

Now, instead of just entering a color, let's try something like:

%22red%22%20onLoad=%22alert(%27Javascript%20Injection%27)%22

After entering the Javascript code above, look at the source code once again and you will notice that our Javascript has also been placed into the <body> tag, making the page do something the author didn't intend.



You can play around with various ideas. For instance you can replace onLoad with onMouseOver, then try to click the link. There is a lot that can be tried here. Maybe even something like:

%22red%22%20onLoad=%22location.replace(%27http://www.example.com%27)%22





The below program is the same as the one above, however it sanitizes the user input so it doesn't allow Scripting Injection. If you try to inject any code, the code won't run.





Sign up for our newsletter and get a free eBook!
 
Twitter Get the latest, subscribe via Twitter!



Namecheap


Copyright © ...