PHPとXSSスパン攻撃の防止


実はこの話題はずっと前から言いたいのですが、国内のPHPサイトにはXSSホールがあります。今日は偶然PHP 5のXSSホールを見ました。ここでまとめます。ちなみに、PHP 5を使う友達はパッチを打ったり、アップグレードしたりしたほうがいいです。もしXSSが分からないなら、ここを見てもいいです。あるいはここを見てもいいです。国内の多くのフォーラムでは、例えば、ここで脚本の穴が開いています。  Googleがあります Hack+XSSの攻撃例は、Discuzに対してです。 4.0.0RC 3です。海外でもこのような例が多く、Googleさえも出てきましたが、12月初めに修正しました。スパン攻撃は構造が簡単で、非常に隠しています。(通常は情報を盗むとすぐに元のページにジャンプします。)どのように攻撃するかは、ここでは説明しません。まず、サイドスクリプト攻撃は、ユーザーの入力を厳格にフィルタリングしていないため、私たちはすべてのデータを私たちのウェブサイトとデータベースに入力する前に危険をブロックしなければなりません。不正なHTMLコードに対して単双引用符などが含まれています。httmlentities()が使えます。 。 bold“/” Outputs: A '「te」 is <bt>ボールド<bt>echo httmlentities($str)// Outputs: A '「萝」 is <bt>ボールド<bt>echo httmlentities($str) ENT_QUOTES);これは違法なスクリプトを無効にすることができます。ただし、httmlentities()はデフォルトでは ISO-859-1は、もしあなたの不法なスクリプトが他のコードとして符号化されたら、フィルタリングできないかもしれません。同時にブラウザが識別して実行できます。この問題はまずいくつかのサイトを探してテストしてからにします。ここでは不正なスクリプトをフィルタする関数を提供します。 RemoveXSS($val) {     // remove all. non-printable characters. CR(0 a) and LF(0 b) and TAB(9) エリア allowed     // this prevents そして私 character re-spacing such as      // ノート that あなた すばらしい ト ハンドル スプリット with , \r, and \t later シンプル they *are* allowed in そして私 input     $val = preg_replace('/(\\x 00-\x 08)[\x 0 b-\x 0 c]/', '', $val;     // stright replaccements the user ショルダー never need these シンプル they normal characters     // this prevents like      $search = 'abcdefghijklmnopqrstuvxyz;     $search .= 'ABCDEFGHIJKLM NOPQRSTVWXYZ'     $search .= '12345567890!@菗莑^&*';     $search .= '~`";:?+/={}[]-_|\'\\';     for ($i = 0; $i < streen($search) $i++) {        // ;? matches the ;, which is optional        // 0{0,7} matches any padded ゼロス、 which エリア optional and go upする。 ト 8 chars        // &#x 0040 @ search for the hex values        $val = preg_replace('/(&獠X){0,8}'.dechex(ord($search[$i])).';)/)/)/?)/i $search[$i] $val; // with a. ;        // @ @ 0{0,7} matches '0。 ゼロ ト セブン times        $val = preg_replace('/(&铉0{0,8}'.ord($search[$i])/', $search[$i] $val; // with a. ;     }     // now the only remaning ホワイトパス atacks エリア \t,t, , and \r     $1を引く = Aray('javascript'は、 'vbscript' 'expression'は、 'apple 'メタ 'xml 'blink'は、 'link 'スタイル 'スクリプト 'embed' 'object 'iframe 'frame' 'frame eset 'ilayer 'layer 'bgsound' 'title 'ベース     $2を引く = Aray('onaboot') 'onactivate 'onafterprint' 'onafterupldate' 'onbeforeactivate' 'onbeforecopy'は、 'onbeforecut' 'onbeforedictivate' 'onbeforeeditfocus' 'onbeforepaste' 'onbeforeprint'は、 'onbeforeunload'は、 'onbeforeudate' 'onblur 'オンブンス 'オンセルchange 'onchange 'onclick 'oncontextmenu 'oncontrollselect 'oncopy 'oncut 'ondataavailable 'ondatasetch anged' 'ondataset complete' 'ondblclick 'ondeactivate' 'ondrag 'ondragend'は、 'ondragenter' 'ondragleave 'ondragover'は、 'ondragstart' 'ondrop 'オーロラ 'オーロラpdate 'onfilterchange' 'onfinish 'onfocus 'onfocusin 'onfocusout' 'onhelp' 'onkeydown 'onkeyress 'onkeyup'は、 'onlayout complete' 'onload 'onlosecapture'は、 'onmousedown' 'onmouseenter' 'onmouseleave' 'onmousemove'は、 'onmouseout' 'onmouseover'は、 'onmouseup; 'onmousewheel 'onmove 'moveend 'onmovestart 'onpaste 'onpropertychange 'onreadystatechange' 'オンレス 'onresize 'onresizend 'onresizerstart 'rowenter 'rowexit 'ローソンdelete' 'onrows inserted' 'onscroll 'onselect 'onselection change' 'onselectstart' 'onstart 'onstop 'onsubmit 'onunload;;     $ラ = アラリmerge($ra 1) $ラ2)     $found = true; // keep replace as long as the previous round replacced something     while ($found == true {        $val_before = $val        for ($i = 0; $i < sizeof($ra) $i++) {           $pattern = '/';           for ($j = 0; $j < streen($ra[$i]); $j++) {              if ($j > 0) {                 $pattern .= '(';                 $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?';                 $pattern .= '|(&#0{0,8}([9][10][13]]]])?';                 $pattern .= ')?';              }              $pattern .= $ラ[$i][$j];           }           $pattern .= '/i;           $replace ment = substr($ra[$i])は、 0, 2)''.substr($ra[$i] 2) // add in <> ト ner the タグ           $val = preg_replace($pattern) $replaccement、 $val; // filter out the hex tags           if ($val_before == $val {              // no。 replaccements were made、 そ exit the loop              $found = false;           }        }     }  }