因为做安全也有一段时间了,对安全这方面的问题会较为敏感,特别是在前些天因为自己的失误导致网站出现Nginx 502 gateway error的错误,让我觉得有必要在周末有空的时候对网站的一些配置进行全面的检查(我可不想再出什么岔子了,写blog本来就是为了记录一些内容,分享一些心得的,别弄到最后被黑了可就太不让人省心了,这种有违初衷的事情我可不能让它发生)。
1.PHP配置文件php.ini的检查
php.ini这个文件大部分做安全的人应该都会比较了解了,主要检查的也就那么几点:防注入检查magic_quotes_gpc(需要注意不要让magic_quotes_sybase的值覆盖了)、防跨站/跨目录检查open_basedir、还有版本显示/错误显示等设置(register_globals/disable_functions/log_errors/error_log/error_reporting/display_errors/allow_url_fopen/allow_url_include/expose_php/memory_limit/cgi.fix_pathinfo/session.cookie_httponly/disable_classes)需要根据自己的具体需求进行配置,安全有时候也是得需要给业务让步的。
手工一个个的查看太不科学,还容易出错,所以这里我使用刚发现的一个PHP配置检测工具PCC,在FreeBuf上有对应的介绍,这里我只说在使用的过程中碰到的一个小问题“Access denied. – Your IP is not cleared. Please set PCC_ALLOW_IP to a your IP address or a wildcard pattern, e.g. ‘SetEnv PCC_ALLOW_IP 10.0.0.*’”,在碰到这种问题的时候第一想法就是去github上找使用说明,于是就有:
Most of the time it is a good idea to keep security related issues such as your PHP configuration to yourself. The following safeguards have been implemented:
- mtime check: This script stops working in non-CLI mode after two days. Re-arming the check can be done by
touch phpconfigcheck.phpor by copying the script to your server again (e.g. via SCP). This check can be disabled by setting the environment variable:PCC_DISABLE_MTIME=1, e.g.SetEnv PCC_DISABLE_MTIME 1in apache’s.htaccess. - source IP check: By default only localhost (127.0.0.1 and ::1) can access this script. Other hosts may be added by setting
PCC_ALLOW_IPto a your IP address or a wildcard pattern, e.g.SetEnv PCC_ALLOW_IP 10.0.0.*in.htaccess. You may also choose to access your webserver via SSH Port forwarding, e.g.ssh -Dorssh -L.(默认情况下只有本地可以通过类似于http://localhost/phpconfigcheck.php?showall=1的方式进行访问,其它的主机如果想要直接通过该URL进行访问的话,需要设置访问权限:对于Apache来说就是设置.htaccess文件,添加指定的IP至白名单;或者使用SSH端口转发的方式也行)
这里我就直接使用CLI的方式检查了:php phpconfigcheck.php >check_php.ini.log,然后慢慢分析,有问题就改。
自己也可以写脚本进行自动化检查、修改,还可以练习自己的正则表达式,这里就不放自己的脚本了,写的太挫o(╯□╰)o
2.Nginx配置文件nginx.conf的检查
主要有:
- 配置nginx.conf 修改Nginx的运行账户为nobody
- 配置nginx.conf 对于上传目录无PHP的执行权限
- 配置nginx.conf 禁止访问的文件夹,如后台,或者限制访问IP
- 配置nginx.conf 禁止访问的文件类型,如一些txt日志文件
3.php-fpm的配置文件php-fpm.conf的检查
修改php-fpm的运行账户和组为nobody
《 “网站配置的安全检查” 》 有 9 条评论
利用 .htaccess 文件攻击Web Server
http://www.justanotherhacker.com/2011/05/htaccess-based-attacks.html
Self contained htaccess shells and attacks
https://github.com/wireghoul/htshells
Grep rough audit – source code auditing tool(用grep的方式进行源代码审计)
https://github.com/wireghoul/graudit
在Nginx中禁止对特定目录中的PHP文件的访问
https://bjornjohansen.no/block-access-to-php-files-with-nginx
`
location ~* /wp-includes/.*.php$ {
deny all;
# access_log off;
# log_not_found off;
}
location ~* /(?:uploads|files)/.*.php$ {
deny all;
# access_log off;
# log_not_found off;
}
location = /xmlrpc.php {
deny all;
# access_log off;
# log_not_found off;
}
`
How to Disable PHP Execution in Certain WordPress Directories (用.htaccess文件)
http://www.wpbeginner.com/wp-tutorials/how-to-disable-php-execution-in-certain-wordpress-directories/
http://stackoverflow.com/questions/1271899/disable-php-in-directory-including-all-sub-directories-with-htaccess
https://sleepycode.com/2010/11/disabling-php-files-in-wordpress-upload-when-using-nginx/
http://stackoverflow.com/questions/29234622/nginx-disallowing-execution-of-php-in-uploads-directory-with-magento
Gixy – Nginx 配置静态分析工具
https://github.com/yandex/gixy
三个案例看Nginx配置安全
https://www.leavesongs.com/PENETRATION/nginx-insecure-configuration.html
https://github.com/phith0n/vulhub/tree/master/nginx/insecure-configuration
security-txt – 用于定义网站安全策略的标准文件
https://github.com/securitytxt/security-txt
网站权限的安全配置
https://mp.weixin.qq.com/s/WutiIpXA3t4k8Oa64veaWw
`
0x02 网站部署架构
0x03 威胁点
通过上面这个架构,我们可以找出一些威胁点:
1.中间件apache服务权限
如果攻击者在获得网站的权限的话,如使用php漏洞进行命令执行,那么其权限就相当于apache服务的权限。如果apache是在管理管理员下直接启动的服务,那就当然是系统管理员的权限啦。
2.数据库mysql服务的权限
如果攻击者能在数据库进行命令执行的话,当然也会是以mysql服务的权限进行操作的。
3.数据库用户权限
第2点的服务权限,需要先得到数据库root权限。
0x04 配置权限防范点
从上面的威胁点,我们可以从下面几个点去做防范:
1.系统用户:创建一个非系统管理员帐户作为网站的管理帐户;
2.中间件服务apache:设置为非系统管理员帐户,设置对目录的读写;
3.数据库服务mysql:设置为非系统管理员帐户;
4.数据库用户权限:设置非root帐户,并控制读写权限;
5.php配置:限制访问目录,限制敏感函数;
`
娇弱的 PHP [ php-fpm.conf & php.ini 安全优化实践 ]
https://klionsec.github.io/2017/11/23/phpsec/
PHP环境安全加固配置
http://www.ttlsa.com/safe/php-environment-security-reinforcement-configuration/
`
一、启用 PHP 的安全模式
二、用户组安全
三、安全模式下执行程序主目录
四、安全模式下包含文件
五、控制 PHP 脚本能访问的目录
六、关闭危险函数
七、关闭 PHP 版本信息在 HTTP 头中的泄露
八、错误信息控制
九、错误日志
`
正确地配置一个安全的 https 网站 (nginx/Apache/Lighttpd)
https://imlonghao.com/2.html
https://paper.tuisec.win/detail/3c347387d6b7948
提高安全性的最佳 Nginx 配置
https://paper.tuisec.win/detail/e66553b79807d1a
https://laravel-china.org/articles/9485/the-best-nginx-configuration-for-improving-security