界: Environment
本節包括原始程式碼之外的所有內容,但對於建立中產品的安全性仍至關重要。由於此領域所涵蓋的問題與原始程式碼沒有直接關係,因此我們將其與其他領域分開。
PHP Misconfiguration: file_uploads Enabled
Abstract
允許使用者上傳檔案可能會使攻擊者插入危險內容或執行惡意的程式碼
Explanation
若啟用
不論撰寫程式所使用的語言為何,毀壞性最大的攻擊通常都包含了遠端程式碼的執行,攻擊者可藉此在程式的上下文環境中成功的執行惡意程式碼。如果允許攻擊者上傳檔案到可由 Web 存取的目錄上,並將這些檔案傳送至 PHP 解譯器,他們便能使這些檔案中包含的惡意程式碼在伺服器中執行。
範例 1:以下程式碼會處理上傳的檔案,並將其移至 Web 根目錄下的目錄中。攻擊者可能上傳惡意的 PHP 來源檔到此程式中,且稍後從伺服器要求這些檔案,這樣會使 PHP 解譯器執行這些檔案。
即使程式將上傳的檔案儲存於無法從 Web 存取的目錄下,攻擊者仍然可能透過向伺服器環境引入惡意內容中來發動其他攻擊。如果程式容易出現 path manipulation、command injection 或 remote include 弱點,則攻擊者可能會上傳包含惡意內容的檔案,並利用另一個弱點使程式讀取或執行該檔案。
file_uploads
選項,會讓 PHP 使用者上傳任意的檔案到伺服器中。允許使用者上傳檔案這個功能本身並不代表一種安全性弱點。不過,這個功能可能導致許多的攻擊行為,因為它提供途徑給惡意的使用者將資料引入伺服器環境。 不論撰寫程式所使用的語言為何,毀壞性最大的攻擊通常都包含了遠端程式碼的執行,攻擊者可藉此在程式的上下文環境中成功的執行惡意程式碼。如果允許攻擊者上傳檔案到可由 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";
} ?>
即使程式將上傳的檔案儲存於無法從 Web 存取的目錄下,攻擊者仍然可能透過向伺服器環境引入惡意內容中來發動其他攻擊。如果程式容易出現 path manipulation、command injection 或 remote include 弱點,則攻擊者可能會上傳包含惡意內容的檔案,並利用另一個弱點使程式讀取或執行該檔案。
References
[1] M. Achour et al. PHP Manual
[2] Alla Bezroutchko Secure file upload in PHP web applications
[3] Standards Mapping - Common Weakness Enumeration CWE ID 434
[4] Standards Mapping - Common Weakness Enumeration Top 25 2019 [16] CWE ID 434
[5] Standards Mapping - Common Weakness Enumeration Top 25 2020 [15] CWE ID 434
[6] Standards Mapping - Common Weakness Enumeration Top 25 2021 [10] CWE ID 434
[7] Standards Mapping - Common Weakness Enumeration Top 25 2022 [10] CWE ID 434
[8] Standards Mapping - Common Weakness Enumeration Top 25 2023 [10] CWE ID 434
[9] Standards Mapping - Common Weakness Enumeration Top 25 2024 [10] CWE ID 434
[10] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001167
[11] Standards Mapping - FIPS200 CM
[12] Standards Mapping - General Data Protection Regulation (GDPR) Indirect Access to Sensitive Data
[13] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-18 Mobile Code (P2)
[14] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-18 Mobile Code
[15] Standards Mapping - OWASP API 2023 API8 Security Misconfiguration
[16] Standards Mapping - OWASP Application Security Verification Standard 4.0 12.2.1 File Integrity Requirements (L2 L3), 12.5.2 File Download Requirements (L1 L2 L3), 13.1.5 Generic Web Service Security Verification Requirements (L2 L3)
[17] Standards Mapping - OWASP Mobile 2014 M1 Weak Server Side Controls
[18] Standards Mapping - OWASP Top 10 2004 A10 Insecure Configuration Management
[19] Standards Mapping - OWASP Top 10 2007 A3 Malicious File Execution
[20] Standards Mapping - OWASP Top 10 2010 A6 Security Misconfiguration
[21] Standards Mapping - OWASP Top 10 2013 A5 Security Misconfiguration
[22] Standards Mapping - OWASP Top 10 2017 A6 Security Misconfiguration
[23] Standards Mapping - OWASP Top 10 2021 A05 Security Misconfiguration
[24] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 Requirement 6.5.10
[25] Standards Mapping - Payment Card Industry Data Security Standard Version 1.2 Requirement 6.5.3
[26] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.8
[27] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.8
[28] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.8
[29] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.8
[30] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0 Requirement 6.2.4
[31] Standards Mapping - Payment Card Industry Data Security Standard Version 4.0.1 Requirement 6.2.4
[32] Standards Mapping - Payment Card Industry Software Security Framework 1.0 Control Objective 5.4 - Authentication and Access Control
[33] Standards Mapping - Payment Card Industry Software Security Framework 1.1 Control Objective 5.4 - Authentication and Access Control
[34] Standards Mapping - Payment Card Industry Software Security Framework 1.2 Control Objective 5.4 - Authentication and Access Control, Control Objective C.2.3 - Web Software Access Controls, Control Objective C.3.4 - Web Software Attack Mitigation
[35] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-003300 CAT II
[36] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-003300 CAT II
[37] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-003300 CAT II
[38] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-003300 CAT II
[39] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-003300 CAT II
[40] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-003300 CAT II
[41] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-003300 CAT II
[42] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-003300 CAT II
[43] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-003300 CAT II
[44] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-003300 CAT II
[45] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-003300 CAT II
[46] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-003300 CAT II
[47] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-003300 CAT II
[48] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-003300 CAT II
[49] Standards Mapping - Security Technical Implementation Guide Version 6.1 APSC-DV-003300 CAT II
[50] Standards Mapping - Security Technical Implementation Guide Version 6.2 APSC-DV-003300 CAT II
[51] Standards Mapping - Web Application Security Consortium Version 2.00 Application Misconfiguration (WASC-15)
desc.structural.php.php_misconfiguration_file_uploads