允許用戶上傳文件可能會讓攻擊者注入危險內容或惡意代碼,并在服務器上運行。
無論編寫程序所用的語言是什么,最具破壞性的攻擊通常都會涉及執行遠程代碼,攻擊者借此可在程序上下文中成功執行惡意代碼。如果允許攻擊者向某個可通過 Web 訪問的目錄上傳文件,并能夠將這些文件傳遞給 PHP 解釋器,他們就能促使這些文件中包含的惡意代碼在服務器上執行。
例 1:以下代碼會處理上傳的文件,并將它們移到 Web 根目錄下的一個目錄中。攻擊者可以將惡意的 PHP 源文件上傳到該程序中,并隨后從服務器中請求這些文件,這會促使 PHP 解析器執行這些文件。
<?php
$udir = 'upload/'; // Relative path under Web root
$ufile = $udir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $ufile)) {
echo "Valid upload received\n";
} else {
echo "Invalid upload rejected\n";
} ?>
[1] Standards Mapping - OWASP Top 10 2010 - (OWASP 2010) A1 Injection
[2] Standards Mapping - OWASP Top 10 2007 - (OWASP 2007) A3 Malicious File Execution
[3] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A6 Injection Flaws
[4] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP3510 CAT I
[5] Standards Mapping - Security Technical Implementation Guide Version 3.4 - (STIG 3.4) APP3510 CAT I
[6] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 434
[7] Standards Mapping - SANS Top 25 2010 - (SANS 2010) Insecure Interaction - CWE ID 434
[8] Standards Mapping - SANS Top 25 2011 - (SANS Top 25 2011) Insecure Interaction - CWE ID 434
[9] M. Achour et al. PHP Manual
[10] PHP Security Consortium PhpSecInfo Test Information
[11] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 - (PCI 1.2) Requirement 6.3.1.1, Requirement 6.5.3
[12] Standards Mapping - Payment Card Industry Data Security Standard Version 2.0 - (PCI 2.0) Requirement 6.5.1
[13] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.6
[14] Alla Bezroutchko Secure file upload in PHP web applications
[15] Standards Mapping - FIPS200 - (FISMA) SI