<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 命令注入