If you’re at all knew to web development, it’s possible that you haven’t heard of XSS. Maybe you have heard of it, but you don’t know the technical details? This article will shed some light on the basics of coding against XSS in php, with vulnerable and patched code examples, as well as a proof of concept XSS for that code.

What does XSS look like? How can we determine if a sites has been attacked, or if it’s code is vulnerable? The exploid usually looks something like this:
view plaincopy to clipboardprint?

1.
<script>alert('XSS!');</script>

<script>alert('XSS!');</script>

A normal user should not be able to execute javascript on anyone elses client to the site. If input like that gets stored anywhere in the database and output to the user later, it could mean that their session gets stolen.
He’res a more specific example:

1.
<html>
2.
<head><title>This script is vulnerable to XSS</title></head>
3.
<body>
4.
<form action="vuln.php" method="POST">
5.
<input type="text" name="input" />
6.
<input type="submit" value="XSS" />
7.
</form>
8.
</body>
9.
</html>

An html form to test the vulnerable code.

1.
<?php
2.
include_once('sanitize.inc.php');
3.
$input = $_POST['input'];
4.
echo stripslashes($input); //stripslashes just in case magic quotes is on, for demonstration
5.
?>