<a href="http://netkiller.github.com/article/phpsecurity.html">http://netkiller.github.com/article/phpsecurity.html</a>
版權 © 2011, 2012 http://netkiller.github.com
摘要
<a></a>
下面是我多年積累下來的經驗總結,整理成文檔供大家參考:
<a href="http://netkiller.github.com/architect/index.html" target="_top">netkiller architect 手劄</a>
<a href="http://netkiller.github.com/linux/index.html" target="_top">netkiller linux 手劄</a>
<a href="http://netkiller.github.com/developer/index.html" target="_top">netkiller developer 手劄</a>
<a href="http://netkiller.github.com/database/index.html" target="_top">netkiller database 手劄</a>
<a href="http://netkiller.github.com/debian/index.html" target="_top">netkiller debian 手劄</a>
<a href="http://netkiller.github.com/centos/index.html" target="_top">netkiller centos 手劄</a>
<a href="http://netkiller.github.com/freebsd/index.html" target="_top">netkiller freebsd 手劄</a>
<a href="http://netkiller.github.com/shell/index.html" target="_top">netkiller shell 手劄</a>
<a href="http://netkiller.github.com/www/index.html" target="_top">netkiller web 手劄</a>
<a href="http://netkiller.github.com/monitoring/index.html" target="_top">netkiller monitoring 手劄</a>
<a href="http://netkiller.github.com/storage/index.html" target="_top">netkiller storage 手劄</a>
<a href="http://netkiller.github.com/mail/index.html" target="_top">netkiller mail 手劄</a>
<a href="http://netkiller.github.com/security/index.html" target="_top">netkiller security 手劄</a>
<a href="http://netkiller.github.com/multimedia/index.html" target="_top">netkiller multimedia 手劄</a>
<a href="http://netkiller.github.com/docbook/index.html" target="_top">netkiller writer 手劄</a>
<a href="http://netkiller.github.com/version/index.html" target="_top">netkiller version 手劄</a>
<a href="http://netkiller.github.com/postgres/index.html" target="_top">netkiller postgresql 手劄</a>
<a href="http://netkiller.github.com/mysql/index.html" target="_top">netkiller mysql 手劄</a>
<a href="http://netkiller.github.com/cryptography/index.html" target="_top">netkiller cryptography 手劄</a>
<a href="http://netkiller.github.com/cisco/index.html" target="_top">netkiller cisco ios 手劄</a>
<a href="http://netkiller.github.com/ldap/index.html" target="_top">netkiller ldap 手劄</a>
<a href="http://netkiller.github.com/intranet/index.html" target="_top">netkiller intranet 手劄</a>
目錄
目錄權限安全
web server 啟動使用者不能于運作使用者為同一個使用者
web server 運作使用者與php程式不能為同一個使用者
夫程序
root 啟動 web server, 此時web server 父程序應該是 root,同時父程序監聽80端口
子程序
父程序派生許多子程序,同時使用setuid,setgid将子程序權限切換為非root
子程序使用者可以通過httpd.conf設定
nginx.conf
fastcgi 程序
php-fpm 于apache類似,都是root父程序,然後派生子程序,由于fastcgi 使用 9000 所有我們可以不使用root啟動php-fpm
現在我們開始講解安全配置問題
我們目的是避免使用者通過漏洞提升權限,或者由于權限配置不當産生漏洞
apache 案例
apache : root
apache 子程序 : nobody
htdocs 目錄 : /var/www
很多人會将/var/www使用者與組設定為 nobody:nogroup / nobody:nobody, 同時因為images會上傳檔案需要設定777, 很多書本于教程上面也是這樣講的, 這樣配置會有什麼問題呢?我們來分析一下:
我們假設,一個使用者上傳一個檔案到images目錄,會有幾種情況:
上傳一個.php檔案,我們可以通過程式禁止上傳.php檔案
内部開發人員偷偷将一個程式植入到系統中,這個做code review 可以避免
如何避免這樣問題出現,有一個辦法,我們建立一個使用者www, webserver 程序是nobody,程式目錄/var/www中的代碼是www使用者,nobody可能讀取但不能修改。/var/www/images 目錄所有者是nobody可以上傳圖檔
使所有可能目錄允許運作.php檔案,http://www.example.com/images/your.php 将被拒絕. include 也是同樣處理方式,隻允許使用include_once,require_one 包含,不允許http://www.example.com/include/your.php運作
nginx / lighttpd 案例分析
nginx / lighttpd : root
web server 子程序 : nobody
php-fpm : root
php-fpm 子程序 : nobody
fastcgi 遇到的問題與上面apache案例中遇到的問題類似,不同是的fastcgi把動态于靜态完全分開了,這樣更容易管理,我們可以這樣入手
php-fpm 子程序 : www
/var/www所有權限給nobody, images權限給www, 同時保證www使用者可以讀取/var/www下的程式檔案
/etc/php5/fpm/pool.d/www.conf
chroot可以徹底解決cd跳轉問題,單配置比較繁瑣
這樣當使用者試圖通過chdir跳轉到/var/www以外的目錄是,将被拒絕
這些函數應該盡量避免使用它們
運作結果
同時開啟error_log日志
選擇一個mvc開發架構,它們的目錄結構一般是這樣的:
然後放行index.php檔案,在url上不允許請求任何其他php檔案,并傳回404錯誤
session.save_path 預設session 存儲在/tmp, 并且一明文的方式将變量存儲在以sess_為字首的檔案中
http://www.example.com/session.php 我們重新整理幾次再看看sess_檔案中的變化
經過側記你可以看到session檔案中存儲的是明文資料,是以不要将敏感資料放到session中,如果必須這樣作。建議你加密存儲的資料
有一個辦法比較好,就是封裝一下session.不再采用$_session方式調用
cookie 也需要作同樣的處理,上面代碼僅供參考,未做過運作測試
sql 注入
shell 指令注入