1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Escapeで、XSS対応します!!

Last updated at Posted at 2019-06-02

XSS(Cross-Site Scripting)

XSS…何かカッコイイなイメージですが、悪いやつです。
XSSは、Userが入力した情報を出力する時、Scriptを実行されるする攻撃技法です。

実行されるは、JavaScriptで、
Userのプライベートの情報(Cookie, session)などを攻撃者のサイトに転送したり、
或は、Userが意図しなかった行動(Userデータが消えたり、急に決済されたり)が起こされます。

XSSの例

sample.html

<html>
 <head>
 <title>sample</title>
 </head>
  <body>
   <form method="get" action="output.php">
   Koogle:
   <input type="text" name="member_id">
   <input type="submit" value="submit">
   </form>
 </body>
</html>
output.html

<?php echo $_GET['member_id']; ?>

一つの例で、
sample.htmlで入力したパラメータをoutput.htmlに伝えて、出力させる例です。
ここで、入力する内容でJavaScriptのコードを入れると、Scriptが実行します。

output_fix.html

<?php echo htmlspecialchars($_GET['page']); ?>

上記のように修正すると解決できると思います。
htmlspecialchars関数を利用して、HTML Entityに変換して出力します。

htmlspecialchars

htmlspecialcharsは、文字列から特定の特殊文字をHTML Entityに変換します。
https://www.php.net/manual/ja/function.htmlspecialchars.php

他の対応

  • ユーザーの入力値を制限

    • ユーザーの入力値を制限して、Scriptを挿入しないようにする。
  • ユーザーの入力値を置換

    • Scriptで主に使用される特殊文字を置換する。
  • XSSフィルタを使用

    • Javaのオープンソース、Lucy XSSなどを使用する。
  • HTTP Security Header使用

    • XSS攻撃を防ぐために、ブラウザで実装された機能を使用するためのHTTP Headerが存在
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?