「開発:セキュリティ:SQLインジェクション」の版間の差分
Mitsuhiro Yoshida (トーク | 投稿記録) |
Mitsuhiro Yoshida (トーク | 投稿記録) |
||
19行目: | 19行目: | ||
==どのようにして、Moodleはこの問題を回避するのですか?== | ==どのようにして、Moodleはこの問題を回避するのですか?== | ||
もう一度、これは、Moodle外部からの入力が非常に疑わしいという例です。上記例では、PARAM_INT から required_param に渡すことで、$idは明確にクリーニングされています。 | |||
以下、さらn手の込んだクエリーです: | |||
<code sql> | <code sql> | ||
UPDATE mdl_user SET lastname = '$lastname' WHERE id = $id; | UPDATE mdl_user SET lastname = '$lastname' WHERE id = $id; | ||
</code> | </code> | ||
$lastname が「O'Brian」の場合、どうなるでしょう? そうです、あなたは次のように「'」をエスケープする必要があります: O\'Brian | |||
In Moodle 1.9, addslashes is applied automatically to all input you get via required_param or optional_param. | In Moodle 1.9, addslashes is applied automatically to all input you get via required_param or optional_param. | ||
34行目: | 34行目: | ||
</code> | </code> | ||
and then we would pass an array of values array($lastname, $id) to the database along with the SQL. | and then we would pass an array of values array($lastname, $id) to the database along with the SQL. | ||
==What you need to do in your code== | ==What you need to do in your code== |
2010年1月30日 (土) 02:03時点における版
作成中です - Mitsuhiro Yoshida 2010年1月25日 (月) 16:11 (UTC)
このページは、Moodleセキュリティガイドラインの一部です。
何が危険ですか?
.../course/view.php?id=123 内にある、あなたのコードがURIから渡される「$id = 123」を使って、次のようなSQL文を実行すると想定しましょう。
SELECT FROM mdl_course WHERE id = $id;
そして、あなたのコードが、わざわざパラメータを適切にクリーニングしないと想定しましょう。
邪悪なハッカーいより、URLが次のように編集されます:
- .../course/view.php?id=123;DELETE+FROM+mdl_user
なぜ、これが非常に、非常に悪いことなのか、私があなたにお答えします。
もちろん、データベースクエリに応じて、悪意のある入力を適切に構築する必要がありますが、邪悪なハッカーにとって、単なる試行錯誤の問題でしかありません。
どのようにして、Moodleはこの問題を回避するのですか?
もう一度、これは、Moodle外部からの入力が非常に疑わしいという例です。上記例では、PARAM_INT から required_param に渡すことで、$idは明確にクリーニングされています。
以下、さらn手の込んだクエリーです:
UPDATE mdl_user SET lastname = '$lastname' WHERE id = $id;
$lastname が「O'Brian」の場合、どうなるでしょう? そうです、あなたは次のように「'」をエスケープする必要があります: O\'Brian
In Moodle 1.9, addslashes is applied automatically to all input you get via required_param or optional_param.
In Moodle 2.0 we completely avoid the dangerous process of building SQL by concatenating strings. In Moodle 2.0 the SQL would look like
UPDATE mdl_user SET lastname = ? WHERE id = ?;
and then we would pass an array of values array($lastname, $id) to the database along with the SQL.
What you need to do in your code
In Moodle 2.0
- Use higher level dmllib methods, like get_record, whenever possible, so you do not have to create SQL yourself.
- When you have to insert values into SQL statements, use place-holders to insert the values safely.
In Moodle 1.9
- Use higher level dmllib methods, like get_record, whenever possible, so you do not have to create SQL yourself.
- Data from required_param and optional_param have already had addslashes applied, ready to be used in database queries, but make sure you put single quotes round each value.
- If you have loaded some data from the database, and then want to re-insert it, then apply addslashes or addslashes_object to it first.
- Test your code by using a tool like sqlmap, or by manually trying tricky inputs like
< > & < > & ' \' 碁 \ \\
What you need to do as an administrator
- This is not something that administrators can do anything about (other than keeping your Moodle up-to-date).
関連情報
- http://sqlmap.sourceforge.net/ - a tool for automatically finding SQL injection vulnerabilities.
- 開発:セキュリティ
- 開発:コーディング