山东大学软件学院项目实训-创新实训-SDUMeeting(三)
山大会议项目web安全之文件注入防护
一、前言
山大会议的客户端加入了对用户自定义头像的支持,需要用户自行上传头像图片,这个过程存在文件注入漏洞,这篇文章记录解决文件注入漏洞的过程。
二、文件上传攻击防御方法
这里介绍了文件上传攻击防御方法,以dvwa为环境演示
1) 对上传文件的类型、大小做限制
漏洞:可以只改文件后缀,不改变文件内容,然后通过burpsuit抓包将文件再改回原来的后缀
难度(Medium)
审计代码
<?php
if( isset( $_POST[ 'Upload' ] ) ) {
// Where are we going to be writing to?
$target_path = DVWA_WEB_PAGE_TO_ROOT . "hackable/uploads/";
$target_path .= basename( $_FILES[ 'uploaded' ][ 'name' ] );
// File information
$uploaded_name = $_FILES[ 'uploaded' ][ 'name' ];
$uploaded_type = $_FILES[ 'uploaded' ][ 'type' ];
$uploaded_size = $_FILES[ 'uploaded' ][ 'size' ];
// Is it an image?
if( ( $uploaded_type == "image/jpeg" || $uploaded_type == "image/png" ) &&
( $uploaded_size < 100000 ) ) {
// Can we move the file to the upload folder?
if( !move_uploaded_file( $_FILES[ 'uploaded' ][ 'tmp_name' ], $target_path ) ) {
// No
echo '<pre>Your image was not uploaded.</pre>';
}
else {
// Yes!
echo "<pre>{
$target_path} succesfully uploaded!</pre>";
}