2

現在開発中のシステム用に、非常に単純な XSS 検出システムを作成しようとしています。現在のシステムでは、ユーザーはメッセージ内に JavaScript を埋め込んで投稿を送信できます。ここに私が現在持っているものがあります:-

var checkFor = "<script>";
alert(checkFor.indexOf("<script>") !== -1);

これは実際にはまったくうまくいきません。検索する用語を含む配列を組み込んだコードを書く必要がある[e.g - "<script>","</script>","alert("]

JavaScript/jQuery を使用してこれを実現する方法についての提案。

これをチェックしていただきありがとうございます。どうもありがとう :)

4

3 に答える 3

2

文字の置換は、XSS を回避するための非常に脆弱な方法です。<(文字を入力せずにアクセスする方法は多数あります&#60; 。代わりに、データを HTML エンコードするなどです。私は次の関数を使用します。

var encode = function (data) {
    var result = data;
    if (data) {
        result = $("<div />").html(data).text();
    }
};
var decode = function (data) {
    var result = data;
    if (data) {
        result = $("<div />").text(data).html();
    }
};
于 2013-03-29T21:33:03.733 に答える
0

As Explosion Pills said, if you're looking for cross–site exploits, you're probably best to either find one that's already been written or someone who can write one for you.

Anyway, to answer the question, regular expressions are not appropriate for parsing markup. If you have an HTML parser (client side is easy, server a little more difficult) you could insert the text as the innerHTML of an new element, then see if there are any child elements:

function mightBeMarkup(s) {
  var d = document.createElement('div');
  d.innerHTML = s;
  return !!(d.getElementsByTagName('*').length);
}

Of course there still might be markup in the text, just that it's invalid so doesn't create elements. But combined with some other text, it might be valid markup.

于 2013-03-29T21:29:10.463 に答える
-1

xss 攻撃を防ぐ最も効果的な方法は、すべての <、>、および & 文字を 、、および に置き換える &lt;こと&gt;です&amp;

OWASP の JavaScript ライブラリがあります。私はまだそれを使っていないので、品質については何も言えません. リンクは次のとおりです: https://www.owasp.org/index.php/ESAPI_JavaScript_Readme

于 2013-03-29T21:28:07.533 に答える