這篇文章給大家介紹文件上傳漏洞php代碼有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、井研網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為井研等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。漏洞描述
開發(fā)中文件上傳功能很常見,作為開發(fā)者,在完成功能的基礎(chǔ)上我們一般也要做好安全防護(hù)。
文件處理一般包含兩項(xiàng)功能,用戶上傳和展示文件,如上傳頭像。
文件上傳攻擊示例
upload.php
<?php $uploaddir = 'uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?>
upload.html
<form name="upload" action="upload1.php" method="POST" ENCTYPE="multipart/formdata"> Select the file to upload: <input type="file" name="userfile"> <input type="submit" name="upload" value="upload"> </form>
上述代碼未經(jīng)過任何驗(yàn)證,惡意用戶可以上傳php文件,代碼如下
<?php eval($_GET['command']);?>
惡意用戶可以通過訪問 如http://server/uploads/shell.php?command=phpinfo(); 來執(zhí)行遠(yuǎn)程命令
Content-type驗(yàn)證
upload.php
<?php if($_FILES['userfile']['type'] != "image/gif") {//獲取Http請(qǐng)求頭信息中ContentType echo "Sorry, we only allow uploading GIF images"; exit; } $uploaddir = 'uploads/'; $uploadfile = $uploaddir.basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?>
該方式是通過Http請(qǐng)求頭信息進(jìn)行驗(yàn)證,可通過修改Content-type ==> image/jpg繞過驗(yàn)證,可以通過腳本或BurpSuite、fiddle修改
如下
Content-Disposition: form-data; name="userfile"; filename="shell.php"
Content-Type: image/gif
圖片類型驗(yàn)證
該方法通過讀取文件頭中文件類型信息,獲取文件類型
備注:如JPEG/JPG文件頭標(biāo)識(shí)為FFD8
upload.php
<?php $imageinfo = getimagesize($_FILES['userfile']['tmp_name']); if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') { echo "Sorry, we only accept GIF and JPEG images\n"; exit; } $uploaddir = 'uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?>
可以通過圖片添加注釋來繞過此驗(yàn)證。
如添加注釋<?php phpinfo(); ?>,保存圖片后將其擴(kuò)展名改為php,則可成功上傳。
上傳成功后訪問該文件則可看到如下顯示
文件擴(kuò)展名驗(yàn)證
通過黑名單或白名單對(duì)文件擴(kuò)展名進(jìn)行過濾,如下代碼
upload.php
<?php $blacklist = array(".php", ".phtml", ".php3", ".php4"); foreach ($blacklist as $item) { if(preg_match("/$item\$/i", $_FILES['userfile']['name'])) { echo "We do not allow uploading PHP files\n"; exit; } } $uploaddir = 'uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)){ echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?>
當(dāng)黑名單不全,構(gòu)造特殊文件名可以繞過擴(kuò)展名驗(yàn)證
直接訪問上傳的文件
將上傳文件保存在非web root下其他文件夾下,可以防止用戶通過路徑直接訪問到文件。
upload.php
<?php $uploaddir = 'd:/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "File uploading failed.\n"; } ?>
用戶不可以直接通過http://localhost/uploads/ 來訪問文件,必須通過view.php來訪問
view.php
<?php $uploaddir = 'd:/uploads/'; $name = $_GET['name']; readfile($uploaddir.$name); ?>
查看文件代碼未驗(yàn)證文件名,用戶可以通過例如http://localhost/view.php?name=..//php/upload.php,查看指定的文件
解決漏洞示例
upload.php
<?php require_once 'DB.php'; $uploaddir = 'D:/uploads/'; $uploadfile = tempnam($uploaddir, "upload_"); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { $db =& DB::connect("mysql://username:password@localhost/database"); if(PEAR::isError($db)) { unlink($uploadfile); die "Error connecting to the database"; } $res = $db->query("INSERT INTO uploads SET name=?, original_name=?,mime_type=?", array(basename($uploadfile,basename($_FILES['userfile']['name']),$_FILES['userfile']['type'])); if(PEAR::isError($res)) { unlink($uploadfile); die "Error saving data to the database. The file was not uploaded"; } $id = $db->getOne('SELECT LAST_INSERT_ID() FROM uploads'); echo "File is valid, and was successfully uploaded. You can view it <a href=\"view.php?id=$id\">here</a>\n"; } else { echo "File uploading failed.\n"; } ?>
view.php
<?php require_once 'DB.php'; $uploaddir = 'D:/uploads/'; $id = $_GET['id']; if(!is_numeric($id)) { die("File id must be numeric"); } $db =& DB::connect("mysql://root@localhost/db"); if(PEAR::isError($db)) { die("Error connecting to the database"); } $file = $db->getRow('SELECT name, mime_type FROM uploads WHERE id=?',array($id), DB_FETCHMODE_ASSOC); if(PEAR::isError($file)) { die("Error fetching data from the database"); } if(is_null($file) || count($file)==0) { die("File not found"); } header("Content-Type: " . $file['mime_type']); readfile($uploaddir.$file['name']); ?>
上述代碼文件名隨機(jī)更改,文件被存儲(chǔ)在web root之外,用戶通過id在數(shù)據(jù)庫中查詢文件名,讀取文件,可以有效的阻止上述漏洞發(fā)生
通過以上示例分析,可總結(jié)一下幾點(diǎn)
1.文件名修改,不使用用戶上傳的文件名
2.用戶不可以通過上傳路徑直接訪問文件
3.文件查看采用數(shù)據(jù)庫獲取文件名,從而在相應(yīng)文件服務(wù)器讀取文件
4.文件上傳限制文件大小,個(gè)人上傳數(shù)量等
關(guān)于文件上傳漏洞php代碼有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:文件上傳漏洞php代碼有哪些-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://aaarwkj.com/article22/ccdjjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航、外貿(mào)建站、搜索引擎優(yōu)化、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容