So, I've noticed that XSS primarily relies on unescaped angle braces to insert html markup. I have this code here:
<!doctype html>
<html lang = 'en'>
<head>
<meta charset = 'utf-8' />
<title>XSS Blocker</title>
<style>
textarea { width: 400px; height: 300px; margin-left: auto; margin-right: auto }
#abc { background: white; }
div { background: blue; }
</style>
</head>
<body>
<div id = 'abc'>
</div>
<br>
<textarea id = 'noXSS'> </textarea>
<button type = 'button' id = 'insert'>Insert into page</button>
<button type = 'button' id = 'toggle'>Disable XSS</button>
</body>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function() {
var noXSS = false;
function cleanInput(input) {
return input = input.replace('<','<').replace('>','>');
}
$('#toggle').mousedown(function() {
noXSS = !noXSS;
if (noXSS) {
$(this).html('Enable XSS');
}
else {
$(this).html('Disable XSS');
}
});
$('#insert').mousedown(function() {
var text = $('#noXSS').val();
if (noXSS) { text = cleanInput(text) }
$('#abc').append(text);
$('#noXSS').val('');
});
});
</script>
</html>
Which in essence simply escapes the <
and >
characters before inserting text into the webpage. In an related question (Escaping <
Good Enough to Prevent XSS Attacks), I read that one also has to declare the charset (which people should be doing anyway) to prevent UTF-7 XSS, and should also escape the ampersand, backslash and both the single and double quote characters for safety.
To gain a better understanding of how and why the 'extra' characters (&, ', ") could possibly be harmful (and because it will also be fun to learn), I would like to see code that hijacks my above program using those characters.
I know that this isn't quite a question, more of a quest for knowledge, but I hope it is fine.