Created
November 5, 2015 08:11
-
-
Save ts0818/61a9df6d6b43796eb9b2 to your computer and use it in GitHub Desktop.
パーフェクトPHP ファイルアップロード攻撃対応方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// アップロードディレクトリは、ドキュメントルートの外側 | |
$upload_dir = '/var/www/uploads/'; | |
if (empty($_FILES) === false && empty($_FILES['upfile']) === false) { | |
if (is_uploaded_file($_FILES['upfile']['tmp_name']) === true) { | |
move_uploaded_file( | |
$_FILES['upfile']['tmp_name'], | |
$upload_dir . date('YmdHis') . '_' | |
. md5(microtime() . $_FILES['upfile']['tmp_name']) | |
); | |
// 元のファイル名と保存したファイル名をデータベースなどに保存する。 | |
// 処理自体は省略。 | |
// アップロード成功画面を出力 | |
echo <<<EOF | |
<html> | |
<head> | |
<title>アップロード成功</title> | |
<meta http-equiv="refresh" content="3; url=./upload_attack_02.php" /> | |
</head> | |
<body> | |
ファイルのアップロードに成功しました | |
</body> | |
</html> | |
EOF; | |
} else { | |
echo 'ファイルアップロード攻撃を受けた可能性があります。'; | |
echo 'ファイル名:' | |
. htmlspecialchars($_FILES['upfile']['name'], ENT_QUOTES, 'UTF-8') | |
. ' / '; | |
} | |
} | |
// ファイルアップロードフォームを出力 | |
echo <<<EOF | |
<html> | |
<head> | |
<title>ファイルアップロード</title> | |
</head> | |
<body> | |
<form action="" method="post" enctype="multipart/form-data"> | |
<!-- 最大サイズ --> | |
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" /> | |
<input type="file" name="upfile" /> | |
<input type="submit" value="アップロード" /> | |
</form> | |
</body> | |
</html> | |
EOF; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment