Click here to Skip to main content
15,302,185 members
Articles / Web Development / HTML
Posted 6 Dec 2010


45 bookmarked

HTML and JavaScript Injection

Rate me:
Please Sign up or sign in to vote.
4.86/5 (14 votes)
6 Dec 2010CPOL6 min read
HTML and JavaScript code injection techniques.


This article is about HTML and JavaScript injection techniques used to exploit web site vulnerabilities. Nowadays, it's not usual to find a completely vulnerable site to this type of attacks, but only one is enough to exploit it. I'll make a compilation of these techniques all together, in order to facilitate the reading and to make it entertaining. HTML injection is a type of attack focused upon the way HTML content is generated and interpreted by browsers at client side. Otherwise, JavaScript is a widely used technology in dynamic web sites, so the use of techniques based on this, like injection, complements the nomenclature of 'code injection'.

Code Injection

This type of attack is possible by the way the client browser has the ability to interpret scripts embedded within HTML content enabled by default, so if an attacker embeds script tags such <SCRIPT>, <OBJECT>, <APPLET>, or <EMBED> into a web site, the web browser's JavaScript engine will execute it. Typical targets of this type of injection are forums, guestbooks, or whatever section where the administrator allows the insertion of text comments; if the design of the web site isn't parsing the comments inserted, and takes '<' or '>' as real chars, a malicious user could type:

I like this site because <script>alert('Injected!');</script> teaches me a lot

If it works and you can see the message box, the door is opened to the attacker's imagination limits! A common code insertion used to drive navigation to another website is something like this:

<H1> Vulnerability test </H1> 
<META HTTP-EQUIV="refresh" CONTENT="1;url=">

Same within a <FK> or <LI> tag:

<FK STYLE="behavior: url(http://<<Other website>>;">

Other tags used to execute malicious JavaScript code are, for example, <BR>, <DIV>, even background-image:

<BR SIZE="&{alert('Injected')}"> 
<DIV STYLE="background-image: url(javascript:alert('Injected'))">

The <TITLE> tag is a common weak point if it's generated dynamically. For example, suppose this situation:

<TITLE><?php echo $_GET['titulo']; ?>

If you build title as 'example </title></head><body><img src=http://myImage.png>', HTML resulting would insert the 'myImage.png' image first of all:

<TITLE>example</title></head><body><img src=http://myImage.png></TITLE>

There is another dangerous HTML tag that could exploit a web browser's frames support characteristic: <IFRAME>. This tag allows (within Sandbox security layer) cross-scripting exploiting using web browser elements (address bar or bookmarks, for example), but this theme is outside the scope of this article.

Otherwise, there is a common technique widely known as "in-line" code injection. This technique exploits the JavaScript functions "alert" and "void". Testing it is very easy, just navigate to whatever site, and type in the web browser's address bar:


This is not a harmful script, as you can see, but suppose you want to get information about the site, for example, if it is using cookies or not, you could type something like this :


If the website is not using cookies, no problem, but else, you could read values like the server session ID, or any user data stored in cookies by the application.

Suppose now that we use the void() JavaScript function instead of alert(). This function returns a null value to the web browser, so no recharging page action is executed. We could change the DOM values inside this function and no navigation change state would occur. Imagine you've found a site that stores the PHP session ID in the common cookie 'PHPSESSID'; if we start a new navigation to the same website in another web browser instance, we'll get a new 'PHPSESSID'; we could change the session IDs in both instances by typing:

javascript: void(document.cookie="PHPSESSID = <<Any other session ID>>"); 

(The code above was wrapped to avoid scrolling)

You will see in the message box the new session ID assigned to the actual one. This example also shows the possibility of concatenating more than one action in the same line of execution.

By only taking a look at site cookies, you could find some very descriptive ones implementing security features; for example, if you find a site cookie like "logged=no", probably you could go into the logged area simply by changing that cookie value:

javascript: void(document.cookie="logged=yes");

Following this line, it's possible to modify any DOM object using JavaScript and inject it using the previous techniques. Analyzing the source code of a web page, you may find it uses forms (<FORM>) for different purposes; in this case, you could change any form field value using the void() function, too. Imagine a shopping portal with a shopping cart; if the site designer didn't take care of this type of injection, you could fill the cart and pay for it only $1:


These other techniques are named indirect code injection; not only cookies or forms modification are exploited by this technique, any DOM component or HTTP header is exposed.

So, it's very important to keep in mind these code injection techniques when developing web applications to make it a more safe application.

Preventing Code Injection

When developing web applications, it's very recommendable to follow the next considerations to prevent possible code injection:

  • Do not rely on client-side JavaScript validation whenever possible; as shown before, this is easily deceived using "in-line" injection. For example, suppose you have a shopping portal where you rely the price of each item at the client side.
  • Suppose you have only one form to store the shopping chart; attackers could modify your bill, simply by changing the price as seen before:


    A solution to this situation is just maintaining the shopping chart actions on the server side, and getting the client side refreshed via AJAX, for example.

  • Don't store sensible data into cookies, because they can be easily modified by an attacker, as seen before. If you need to store data in cookies, store them with a hash signature generated with a server side key.
  • Never use hidden boxes to hold items because they can be hard coded into the code. Otherwise, you should always validate the fields at server side using a secure algorithm, with data received from the client as input:
  • signatures.png

  • Like the <TITLE> example above, you better not use dynamic DOM element generation.
  • Take care about dynamic evaluation vulnerabilities (like the <TITLE> example above). Imagine this piece of code in a PHP page:
  • PHP
    $dato = $_GET['formAge'];
    eval('$edad = ' . $dato . ';');

    eval() parameters will be processed, so if "formAge" is set to "5; system('/bin/rm -rf *')", additional code will be executed on the server and will remove all the files. Dangerous, don't you think so?

  • If you are developing a web site that allows the user to upload content (forum, guestbook, "contact me", etc.), you may split special HTML chars, so injected tags will be maintained in the website, but will not be executed; you can get this with the strip_tags() PHP function, htmlentities(), urlencode(), or htmlspecialchars(), for example.
  • Use an SSL certificate for sensible operations; this doesn't avoid JavaScript injection, but avoids sensible data from being read by anyone else.

Ultimately, the best defense to code injection attacks resides on "Best practices" while programming.

Points of Interest

Implementing security features in web applications sometimes takes no care by programmers, and exposes the project to attackers.


  • 1.0 - 05/12/2010 - Original version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Spain Spain
I am a software developer grown with programming paradigm evolution. Think that C is God's Programming Language.

Comments and Discussions

QuestionMessage Closed Pin
4-Jan-22 17:09
MemberMember 154891334-Jan-22 17:09 
GeneralMy vote of 5 Pin
joey1011117-Jul-19 23:57
Memberjoey1011117-Jul-19 23:57 
Questionadf Pin
Member 1400333830-Sep-18 22:37
MemberMember 1400333830-Sep-18 22:37 
Answersadasd Pin
Member 1034526019-May-15 4:15
MemberMember 1034526019-May-15 4:15 
GeneralMy vote of 5 Pin
Member 1094430318-Nov-14 15:50
MemberMember 1094430318-Nov-14 15:50 
Generalthanks for sharing Pin
Pranay Rana21-Dec-10 19:36
professionalPranay Rana21-Dec-10 19:36 
GeneralRe: thanks for sharing Pin
Skynet_Code24-Dec-10 21:42
MemberSkynet_Code24-Dec-10 21:42 
GeneralMy vote of 5 Pin
ruipalhinhas14-Dec-10 3:27
Memberruipalhinhas14-Dec-10 3:27 
GeneralRe: My vote of 5 Pin
Skynet_Code14-Dec-10 6:33
MemberSkynet_Code14-Dec-10 6:33 
GeneralGood to remember that ! Pin
Manuel Vilar13-Dec-10 4:35
MemberManuel Vilar13-Dec-10 4:35 
GeneralRe: Good to remember that ! Pin
Skynet_Code14-Dec-10 6:31
MemberSkynet_Code14-Dec-10 6:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.