网站配置的安全检查


因为做安全也有一段时间了,对安全这方面的问题会较为敏感,特别是在前些天因为自己的失误导致网站出现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.php or 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 1 in 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_IP to 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 -D or ssh -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 条评论

  1. 在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

  2. 网站权限的安全配置
    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配置:限制访问目录,限制敏感函数;
    `

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注