PHP擴展--Suhosin保護PHP應用系統


什么是Suhosin?

Suhosin是一個PHP程序的保護系統。它的設計初衷是為了保護服務器和用戶抵御PHP程序和PHP核心中,已知或者未知的缺陷。
Suhosin有兩個獨立的部分,使用時可以分開使用或者聯合使用。
第一部分是一個用於PHP核心的補丁,它能抵御緩沖區溢出或者格式化串的弱點;
第二部分是一個強大的PHP擴展,包含其他所有的保護措施。

下載安裝補丁

##高版本不需要,折中選擇是否打補丁
wget http://download.suhosin.org/suhosin-patch-5.3.3-0.9.10.patch.gz
gunzip suhosin-patch-5.3.3-0.9.10.patch.gz
cd php-5.3.3/
patch -p 1 -i ../suhosin-patch-5.3.3-0.9.10.patch
./configure  --with-php-config=/usr/local/bin/php-config
make
make install

安裝擴展

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

擴展應用

加密功能

Session加密

SESSION里的數據通常在服務器上的明文存放的。這里通過在服務端來加解密$_SESSION。這樣將Session的句柄存放在Memcache或數據庫時,就不會被輕易攻破,很多時候我們的session數據會存放一些敏感字段。

這個特性在缺省情況下是啟用的,也可以通過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在客戶端瀏覽器的傳輸的HTTP頭也是明文的。通過加密cookie,您可以保護您的應用程序對眾多的攻擊,如

  • Cookie篡改:攻擊者可能會嘗試猜測其他合理的cookie值來攻擊程序。
  • 跨應用程序使用Cookie:不正確配置的應用程序可能具有相同的會話存儲,如所有會話默認存儲在/tmp目錄下,一個應用程序的cookie可能永遠不會被重新用於另一應用,只要加密密鑰不同。

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的數據
cat  sess_ururh83qvkkhv0n51lg17r4aj6
//記錄是明文的
##擴展開啟后查看某條sesson 的數據
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

通過日志來查看非法調用黑白名單

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函數

suhosin.upload.max_uploads
suhosin.upload.disallow_elf
suhosin.upload.disallow_binary
suhosin.upload.remove_binary
suhosin.upload.verification_script        上傳文件檢查腳本,可以來檢測上傳的內容是否包含webshell特征

參考地址:http://suhosin.org/






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM