所以,我注意到 XSS 主要依赖于未转义的尖括号来插入 html 标记。我这里有这段代码:
<!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>
本质上只是逃避了 <
和>
将文本插入网页之前的字符。在一个相关问题( Escaping <
Good Enough to Prevent XSS Attacks )中,我读到还必须声明字符集(无论如何人们都应该这样做)以防止 UTF-7 XSS,并且还应该转义与号、反斜杠以及单引号和双引号为了安全起见。
为了更好地理解“额外”字符(&、'、“)可能有害的方式和原因(并且因为学习起来也很有趣),我希望看到劫持我上面的代码使用这些字符的程序。
我知道这不完全是一个问题,更多的是对知识的追求,但我希望它没问题。
最佳答案
处理这个问题的正确方法是设置文本。
$('#abc').text('What <ever> you & want;');
那么,就不需要转义了。
关于javascript - 进一步的XSS字符擒纵解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24916287/