PHP安全配置優(yōu)化詳解
由于腳本語言和早期版本設(shè)計的諸多原因,php項目存在不少安全隱患。從配置選項來看,可以做如下的優(yōu)化。
1.屏蔽PHP錯誤輸出。
在/etc/php.ini
(默認配置文件位置),將如下配置值改為Off
display_errors=Off
不要將錯誤堆棧信息直接輸出到網(wǎng)頁上,防止黑客加以利用相關(guān)信息。
正確的做法是:
把錯誤日志寫到日志文件中,方便排查問題。
2.屏蔽PHP版本。
默認情況下PHP版本會被顯示在返回頭里,如:
Response Headers X-powered-by: PHP/7.2.0
將php.ini中如下的配置值改為Off
expose_php=Off
3.關(guān)閉全局變量。
如果開啟全局變量會使一些表單提交的數(shù)據(jù)被自動注冊為全局變量。代碼如下:
<form action="/login" method="post"> <input name="username" type="text"> <input name="password" type="password"> <input type="submit" value="submit" name="submit"></form>
開啟方法是在php.ini中修改如下:
register_globals=On
建議關(guān)閉,參數(shù)如下:
register_globals=Off
當(dāng)關(guān)閉后,就只能從POST、_GET、$_REQUEST里面獲取相關(guān)參數(shù)。
4.文件系統(tǒng)限制
可以通過open_basedir來限制PHP可以訪問的系統(tǒng)目錄。
如果不限制使用下面的腳本代碼(hack.php)可以獲取到系統(tǒng)密碼。
<?php echo file_get_contents('/etc/passwd');
當(dāng)設(shè)置了后則會報錯,不再顯示相關(guān)信息,讓系統(tǒng)目錄b不會被非法訪問:
PHP Warning: file_get_contents(): open_basedir restriction in effect. File(/etc/passwd) is not within the allowed path(s): (/var/www) in /var/www/hack.php on line 3
Warning: file_get_contents(): open_basedir restriction in effect. File(/etc/passwd) is not within the allowed path(s): (/var/www) in /var/www/hack.php on line 3 PHP Warning: file_get_contents(/etc/passwd): failed to open stream: Operation not permitted in /var/www/hack.php on line 3 Warning: file_get_contents(/etc/passwd): failed to open stream: Operation not permitted in /var/www/hack.php on line 3
設(shè)置方法如下:
open_basedir=/var/www
5.禁止遠程資源訪問。
allow_url_fopen=Off allow_url_include=Off
其他第三方安全擴展
6.Suhosin。
Suhosin
是一個PHP程序的保護系統(tǒng)。它的設(shè)計初衷是為了保護服務(wù)器和用戶,抵御PHP程序和PHP核心中已知或者未知的缺陷(感覺挺實用的,可以抵御一些小攻擊)。Suhosin有兩個獨立的部分,使用時可以分開使用或者聯(lián)合使用。
第一部分是一個用于PHP核心的補丁,它能抵御緩沖區(qū)溢出或者格式化串的弱點(這個必須的!);
第二部分是一個強大的 PHP擴展(擴展模式挺好的,安裝方便…),包含其他所有的保護措施。
安裝擴展
wget http://download.suhosin.org/suhosin-0.9.37.1.tar.gz tar zxvf suhosin-0.9.37.1.tar.gz cd suhosin-0.9.37.1/ phpize ./configure --with-php-config=/usr/local/bin/php-config make make install 在php.ini下加入suhosin.so即可 extension=suhosin.so
特性
- 模擬器保護模式
- 增加兩個函數(shù)sha256()和sha256_file()到PHP核心中
- 所有平臺,加入CRYPT_BLOWFISH到函數(shù)crypt()中
- 打開對phpinfo()頁的透明保護
- SQL數(shù)據(jù)庫用戶保護
運行時保護
加密cookies
防止不同種類的包含漏洞(不允許遠程URL包含(黑/白名單);不允許包含已上傳的文件;防止目錄穿越攻擊)
允許禁止preg_replace()
允許禁止eval()函數(shù)
通過配置一個最大執(zhí)行深度,來防止無窮遞歸
支持每個vhost配置黑白名單
為代碼執(zhí)行提供分離的函數(shù)黑白名單
防止HTTP響應(yīng)拆分漏洞
防止腳本控制memory_limit選項
保護PHP的superglobals,如函數(shù)extract(),import_request_vars()
防止mail()函數(shù)的新行攻擊
防止preg_replace()的攻擊、
Session 保護
加密session數(shù)據(jù)
防止session被劫持
防止超長的session id
防止惡意的session id
SESSION
里的數(shù)據(jù)通常在服務(wù)器上的明文存放的。這里通過在服務(wù)端來加解密$_SESSION。這樣將Session的句柄存放在Memcache或數(shù)據(jù)庫時,就不會被輕易攻破,很多時候我們的session數(shù)據(jù)會存放一些敏感字段。
這個特性在缺省情況下是啟用的,也可以通過php.ini來修改:
suhosin.session.encrypt = On suhosin.session.cryptkey = zuHywawAthLavJohyRilvyecyondOdjo suhosin.session.cryptua = On suhosin.session.cryptdocroot = On ;; IPv4 only suhosin.session.cryptraddr = 0 suhosin.session.checkraddr = 0
Cookie加密
Cookie在客戶端瀏覽器的傳輸?shù)腍TTP頭也是明文的。通過加密cookie,您可以保護您的應(yīng)用程序?qū)Ρ姸嗟墓簦?/p>
Cookie篡改:攻擊者可能會嘗試猜測其他合理的cookie值來攻擊程序。
跨應(yīng)用程序使用Cookie:不正確配置的應(yīng)用程序可能具有相同的會話存儲,如所有會話默認存儲在/tmp目錄下,一個應(yīng)用程序的cookie可能永遠不會被重新用于另一應(yīng)用,只要加密密鑰不同。
Cookie加密在php.ini中的配置:
suhosin.cookie.encrypt = On ;; the cryptkey should be generated, e.g. with 'apg -m 32' suhosin.cookie.cryptkey = oykBicmyitApmireipsacsumhylWaps1 suhosin.cookie.cryptua = On suhosin.cookie.cryptdocroot = On ;; whitelist/blacklist (use only one) ;suhosin.cookie.cryptlist = WALLET,IDEAS suhosin.cookie.plainlist = LANGUAGE ;; IPv4 only suhosin.cookie.cryptraddr = 0 suhosin.cookie.checkraddr = 0 Blocking Functions 測試 ##默認PHP的Session保存在tmp路徑下 ll -rt /tmp | grep sess ##擴展未開啟時查看某條sesson的數(shù)據(jù) cat sess_ururh83qvkkhv0n51lg17r4aj6 //記錄是明文的 ##擴展開啟后查看某條sesson 的數(shù)據(jù) cat sess_ukkiiiheedupem8k4hheo0b0v4 //記錄是密文的 可見加密對安全的重要性
阻斷功能
白名單
##顯式指定指定白名單列表 suhosin.executor.func.whitelist = htmlentities,htmlspecialchars,base64_encode suhosin.executor.eval.whitelist = htmlentities,htmlspecialchars,base64_encode <?php echo htmlentities('<test>'); eval('echo htmlentities("<test>");');
黑名單
##顯式指定指定黑名單列表 suhosin.executor.func.blacklist = assert,unserialize,exec,popen,proc_open,passthru,shell_exec,system,hail,parse_str,mt_srand suhosin.executor.eval.whitelist = assert,unserialize,exec,popen,proc_open,passthru,shell_exec,system,hail,parse_str,mt_srand 通過日志來查看非法調(diào)用黑白名單 suhosin.simulation = 1 suhosin.log.file = 511 suhosin.log.file.name = /tmp/suhosin-alert.log
其他配置項
suhosin.executor.include.max_traversal 擴目錄的最大深度,可以屏蔽切換到非法路徑 suhosin.executor.include.whitelist 允許包含的URL,用逗號分隔 suhosin.executor.include.blacklist 禁止包含的URL,用逗號分隔 suhosin.executor.disable_eval = On 禁用eval函數(shù) suhosin.upload.max_uploads suhosin.upload.disallow_elf suhosin.upload.disallow_binary suhosin.upload.remove_binary suhosin.upload.verification_script 上傳文件檢查腳本,可以來檢測上傳的內(nèi)容是否包含webshell特征
使用Suhosin,你可以得到一些錯誤日志,你能把這些日志放到系統(tǒng)日志中,也可以同時寫到其他任意的日志文件中去;
它還可以為每一個虛擬主機創(chuàng)建黑名單和白名單;
可以過濾GET和POST請求、文件上載和cookie;
你還能傳送加密的會話和cookie,可以設(shè)置不能傳送的存儲上線等等;
它不像原始的PHP強化補丁,Suhosin是可以被像Zend Optimizer這樣的第三方擴展軟件所兼容的。
到此這篇關(guān)于PHP安全配置優(yōu)化詳解的文章就介紹到這了,更多相關(guān)PHP安全配置優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php中debug_backtrace、debug_print_backtrace和匿名函數(shù)用法實例
這篇文章主要介紹了php中debug_backtrace、debug_print_backtrace和匿名函數(shù)用法,以實例形式分析了debug_backtrace和debug_print_backtrace函數(shù)在調(diào)試過程中的作用,并分析了PHP5.3新增匿名函數(shù)的用法,需要的朋友可以參考下2014-12-12PHP隨機數(shù)函數(shù)rand()與mt_rand()的講解
今天小編就為大家分享一篇關(guān)于PHP隨機數(shù)函數(shù)rand()與mt_rand()的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03如何使用Linux的Crontab定時執(zhí)行PHP腳本的方法
我們的PHP程序有時候需要定時執(zhí)行,我們可以使用ignore_user_abort函數(shù)或是在頁面放置js讓用戶幫我們實現(xiàn)。但這兩種方法都不太可靠,不穩(wěn)定。我們可以借助Linux的Crontab工具來穩(wěn)定可靠地觸發(fā)PHP執(zhí)行任務(wù)2011-12-12php使用pclzip類實現(xiàn)文件壓縮的方法(附pclzip類下載地址)
這篇文章主要介紹了php使用pclzip類實現(xiàn)文件壓縮的方法,分析了使用pclzip類的具體步驟與實現(xiàn)文件壓縮的相關(guān)技巧,并附帶pclzip類文件的下載地址,需要的朋友可以參考下2016-04-04