十大PHP最佳安全實踐

來源:互聯網
上載者:User

 

  PHP被廣泛用於各種Web開發。而當伺服器端指令碼配置錯誤時會出現各種問題。現今,大部分Web伺服器是基於Linux環境下運行(比如:Ubuntu,Debian等)。本文例舉了十大PHP最佳安全實踐方式,能夠讓您輕鬆、安全配置PHP.

  PHP安全性設定提示:

  DocumentRoot: /var/www/ Default Web server: Apache

  Default PHP configuration file: /etc/php.ini

  Default PHP extensions config directory: /etc/php.d/

  Our sample php security config file: /etc/php.d/security.ini (you need to create this file using a text editor)

  Operating systems: Ubuntu (the instructions should work with any other Linux distributions such as RHEL / CentOS / Fedora or other Unix like operating systems such as OpenBSD/FreeBSD/HP-UX)。

  1. 減少PHP內建模組

  為了增強效能和安全性,強烈建議,減少PHP中的模組。來看看下面這個被執行命令安裝的模組。

  # php –m你將會得到類似的結果:

  [PHP Modules] apc bcmath bz2 calendar Core ctype curl date dom ereg exif fileinfo filter ftp gd gettext gmp hash iconv imap json libxml mbstring memcache mysql mysqli openssl pcntl pcre PDO pdo_mysql pdo_sqlite Phar readline Reflection session shmop SimpleXML sockets SPL sqlite3 standard suhosin tokenizer wddx xml xmlreader xmlrpc xmlwriter xsl zip zlib [Zend Modules] Suhosin刪除一個模組,並執行此命令。例如:刪除模組sqlite3

  # rm /etc/php.d/sqlite3.ini

  或者

  # mv /etc/php.d/sqlite3.ini /etc/php.d/sqlite3.disableRestrict 2. 使PHP資訊泄露最小化

  在預設PHP時在HTTP抬頭處會產生一條線介於每個響應中,(比如X-Powered-By: PHP/5.2.10)。而這個在系統資訊中為攻擊者建立了一個非常有價值的資訊。

  HTTP樣本:

  HTTP/1.1 200 OK X-Powered-By: PHP/5.2.10 Content-type: text/html; charset=UTF-8 Vary: Accept-Encoding, Cookie X-Vary-Options: Accept-Encoding;list-contains=gzip,Cookie;string-contains=wikiToken;string-contains=wikiLoggedOut;string-contains=wiki_session Last-Modified: Thu, 03 Nov 2011 22:32:55 GMT……

  因此,我們強烈建議,禁止PHP資訊泄露,想要要禁止它,我們要編輯/etc/php.d/secutity.ini,並設定以下指令:

  expose_php=Off

  3. 使PHP載入模組最小化

  在預設情況下,RHEL載入的所有模組可以在/etc/php.d/目錄中找到。要禁用或啟用一個特定的模組,只需要在設定檔/etc/php.d/目錄中中注釋下模組名稱。而為了最佳化PHP效能和安全性,當你的應用程式需要時,我們強烈建議建議啟用擴充功能。舉個例子:當禁用GD擴充時,鍵入以下命令:

  # cd /etc/php.d/

  # mv gd.{ini,disable}

  # /etc/init.d/apache2 restart為了擴充PGP GD模組,然後鍵入以下命令:

  # mv gd.{disable,ini}

  # /sbin/service httpd restart 4. 記錄PHP錯誤資訊

  為了提高系統和Web應用程式的安全,PHP錯誤資訊不能被暴露出。要做到這一點,需要編輯/etc/php.d/security.ini 檔案,並設定以下指令:

  display_errors=Off為了便於開發人員Bug修複,所有PHP的錯誤資訊都應該記錄在日誌中。

  log_errors=On

  error_log=/var/log/httpd/php_scripts_error.log

  5. 禁用遠程執行代碼

  如果遠程執行代碼,允許PHP代碼從遠程檢索資料功能,如FTP或Web通過PHP來執行構建功能。比如:file_get_contents()。

  很多程式員使用這些功能,從遠程通過FTP或是HTTP協議而獲得資料。然而,此法在基於PHP應用程式中會造成一個很大的漏洞。由於大部分程式員在傳遞使用者提供的資料時沒有做到適當的過濾功能,開啟安全性漏洞並且建立了代碼時注入了漏洞。要解決此問題,需要禁用_url_fopen in /etc/php.d/security.ini,並設定以下命令:

  allow_url_fopen=Off除了這個,我還建議禁用_url_include以提高系統的安全性。

  allow_url_include=Off 6. 禁用PHP中的危險函數

  PHP中有很多危險的內建功能,如果使用不當,它可能使你的系統崩潰。你可以建立一個PHP內建功能列表通過編輯/etc/php.d/security.ini來禁用它。

  disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source 7. 資源控制

  為了提高系統的穩定性,強烈建議設定每個指令碼解析請求資料所花費的時間和指令碼可能消耗的最大記憶體量。正確的配置這些參數可以防止PHP任何指令碼消耗太多的資源或是記憶體,從而避免系統不安全或降低安全係數。

  # set in seconds

  max_execution_time = 30

  max_input_time = 30

  memory_limit = 40M

  8. 限制PHP訪問檔案系統

  該open_basedir指令指定的目錄是允許PHP訪問使用fopen()等功能。如果任何指令碼試圖訪問超出open_basdir定義的路徑檔案,PHP將拒絕開啟。值得注意的是,你不能使用一個符號連結作為一種變通方法。

  ; Limits the PHP process from accessing files outside; of specifically designated directories such as /var/www/html/ open_basedir="/var/www/html/"; ——; Multiple dirs example; open_basedir="/home/httpd/vhost/cyberciti.biz/html/:/home/httpd/vhost/nixcraft.com/html/:/home/httpd/vhost/theos.in/html/"; ——9.限制檔案/目錄訪問

  進行適當的安全設定:確保Apache作為非root使用者運行,比如www-data或www.對於檔案和目錄在基於/var/www/下同樣屬於非root使用者。想要更改所有者,執行以下命令:

  # chown -R apache:apache /var/www/ 10.編譯保護Apache,PHP和MySQL的設定檔

  使用charrt命令編譯保護設定檔

  # chattr +i /etc/php.ini

  # chattr +i /etc/php.d/*

  # chattr +i /etc/my.ini

  # chattr +i /etc/httpd/conf/httpd.conf

  # chattr +i /etc/使用charrt命令可以編譯保護PHP檔案或者是檔案中的/var/www/html的目錄:

  # chattr +i /var/www/html/file1.php

  # chattr +i /var/www/html/



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.