PHP安全优化

PHP安全优化

PHP Secure Configuration Checker,简称pcc,其目的是检测php配置文件中存在的安全漏洞,兼容 PHP >= 5.4, 或者 >= 5.0.
【详细说明参考】:https://github.com/sektioneins/pcc

一、安装
weget下载、解压
Pcc无需编译、安装。只需搭建好php环境,下载解压即可使用。

shell># wget https://codeload.github.com/sektioneins/pcc/zip/refs/heads/master
shell># unzip master

二、如何使用

  • 使用pcc时需要切换非root用户,不然会报错误
  • 支持命令行和网页执行(HTML)
shell># su - user
shell>$ php phpconfigcheck.php 

使用参数:-a 检测所有选项(包含隐藏),-j 输出格式为JSON,-h 输出格式为HTML

危险等级说明:
[high]:高危(深红色)
[medium]:中等(黄色)
[low]:低级别(绿色)
[maybe]:可选,根据实际情况修改(浅蓝)
[comment]:建议(蓝绿)
[ok]:正常(绿色)

三、安全修复

  • 3.1 allow_url_fopen
[high] php.ini / allow_url_fopen
  fopen() is allowed to open URLs.
  Deactivate, if possible. Allowing URLs in fopen() can be a suprising side-effect for unexperienced developers.
  Even if deactivated, it is still possible to receive content from URLs, e.g. with curl.

allow_url_fopen :默认为1,允许打开远程连接
本选项激活了 URL 形式的 fopen 封装协议使得可以访问 URL 对象例如文件。默认的封装协议提供用 ftp 和 http 协议来访问远程文件,一些扩展库例如 zlib 可能会注册更多的封装协议。

【详见官方手册】
https://www.php.net/manual/zh/filesystem.configuration.php

解决办法:编辑php.ini,增加选项

#禁止打开远程目录
allow_url_fopen = Off

==============================================================================

  • 3.2 enable_dl
[high] php.ini / enable_dl
PHP can load extensions during runtime
Deactivate this option to prevent arbitrary code to be loaded during runtime (see dl()).

enable_dl:默认为1 允许加载动态模块
该指令仅对 Apache 模块版本的 PHP 有效。 你可以针对每个虚拟机或每个目录开启或关闭 dl() 动态加载 PHP 模块。
【详见官方手册 】
https://www.php.net/manual/zh/info.configuration.php#ini.enable-dl

解决办法:编辑php.ini,增加选项:

# 自动加载扩展,对Apache模块有效
enable_dl = Off

==============================================================================

3.3 assert.active

[medium] php.ini / assert.active
  assert is active.
  assert() evaluates code just like eval(). Unless it is actually required in a live environment, 
  which is almost certainly not the case, this feature should be deactivated.

assert.active :断言评测.默认为1 激活 assert()
详见官方手册】:
https://www.php.net/manual/zh/info.configuration.php#ini.assert.active

解决办法:编辑php.ini,增加选项:

# 断言关闭
assert.active = Off

==============================================================================

3.4 disable_classes

[medium] php.ini / disable_classes
  no classes disabled.
  Potentially dangerous and unused classes should be deactivated.

disable_classes : 禁止某些潜在威胁的类,多个用,分隔

详见官方手册】:
https://www.php.net/manual/zh/ini.core.php#ini.disable-classes

解决办法:编辑php.ini,增加选项:

# 禁止某些危险类,根据实际情况选择
disable_classes = classes1,classes2,classes3

==============================================================================

3.5 disable_functions

[medium] php.ini / disable_functions
  no functions disabled.
  Potentially dangerous and unused functions should be deactivated, e.g. system().

disable_functions :禁止危险函数,仅对php内部函数有用,不影响用户自定义函数
详见官方手册:https://www.php.net/manual/zh/ini.core.php#ini.disable-functions

解决办法:编辑php.ini,增加选项

# 禁止的功能函数
disable_functions = dl,exec,system,passthru,popen,proc_open,pcntl_exec,shell_exec,mail,imap_open,imap_mail,putenv,apache_setenv,symlink,link,ini_set

==============================================================================

3.5 display_errors

[medium] php.ini / display_errors
  display_errors is on.
  Error messages can divulge information about the inner workings of an application and may include private information such as Session-ID, personal data, database structures, source code exerpts. It is recommended to log errors, but not to display them on live systems.

display_errors: 输出错误信息到用户屏幕,辅助开发功能,生产环境不建议使用。

【详见官方手册】:
https://www.php.net/manual/zh/errorfunc.configuration.php#ini.display-errors

解决办法:编辑php.ini,增加选项:

# 关闭php错误显示,生产服务器建议关闭。(开启以后出现错误,在用户显示屏输出Waring:/home/www/test/1.php 23)
display_errors = off

==============================================================================

3.6 max_execution_time

[medium] php.ini / max_execution_time
  Execution time is not limited.
  In order to prevent denial-of-service attacks where an attacker tries to keep your server's CPU busy, this value should be set to the lowest possible value, e.g. 30 (seconds).

max_execution_time 最大执行时间,默认设为 30
脚本被解析器中止之前允许的最大执行时间,单位秒。 这有助于防止写得不好的脚本占尽服务器资源。 默认设置为 30。 从命令行运行PHP时,没有限制。

【详见官方手册】
https://www.php.net/manual/zh/info.configuration.php#ini.max-execution-time

解决办法:编辑php.ini,增加选项:

# php脚本执行超时,30秒自动结束,上传大型文件时,可以改长时间
max_execution_time = 30

==============================================================================
3.7 session.use_strict_mode

[medium] php.ini / session.use_strict_mode
  strict mode not activated.
  If activated, PHP will regenerate unknown session IDs. This effectively counteracts session fixation attacks.

session.use_strict_mode :默认为0(禁用)
session.use_strict_mode指定模块是否将使用严格会话ID模式。如果启用此模式,则模块不接受未初始化的会话ID。如果从浏览器发送了未初始化的会话ID,则会将新的会话ID发送到浏览器。通过采用严格模式的会话,可以保护应用程序免于会话固定。默认为0(禁用)

详见官方手册】:
https://www.php.net/manual/zh/session.configuration.php#ini.session.use-strict-mode

解决办法:编辑php.ini,增加选项:

# 开启严格模式
session.use_strict_mode = On

==============================================================================

3.8 php.ini / zend.assertions

[medium] php.ini / zend.assertions
  assert is active.
  assert() in able to evaluate code. Please deactivate this feature for production environments by setting zend.assertions=-1.

zend.assertions : 断言等级配置,默认为1
断言等级配置。设置为1时,将生成并执行声明代码(开发模式)。设置为0时,将生成断言代码,但在运行时将跳过(不执行)断言代码。设置为-1时,将不会生成断言代码,从而使资产成本为零(生产模式)。默认配置为1
注意:
1.如果进程以生产方式启动,则zend.assertions无法在运行时更改,因为未生成断言代码。
2.如果进程以开发模式启动,则zend.assertions无法在运行时设置为-1

【详见官方手册】:
https://www.php.net/manual/zh/ini.core.php#ini.zend.assertions

解决办法:编辑php.ini,增加选项:

# 是否执行断言代码,1开发模式,生成并执行断言代码;
# 0生成断言代码,但执行时跳过。-1生产模式不生产断言代码
zend.assertions = -1

==============================================================================

3.9 open_basedir

[low] php.ini / open_basedir
  open_basedir not set.
  Usually it is a good idea to restrict file system access to directories related to the application, e.g. the document root.

open_basedir 限制目录权限
将PHP可以访问的文件限制为指定的目录树,包括文件本身。此指令不受安全模式打开或关闭的影响。默认为空.

【详见官方手册】:
https://www.php.net/manual/zh/ini.core.php#ini.open-basedir

解决办法:编辑php.ini,增加选项:

# 限制访问目录
open_basedir = /home/www/

==============================================================================

3.10 file_uploads

[maybe] php.ini / file_uploads
  file uploads are allowed.
  If an application does not require HTTP file uploads, this setting should be deactivated.

file_uploads
是否允许HTTP文件上传。另请参见upload_max_filesize,upload_tmp_dir和post_max_size指令。

【详见官方手册】
https://www.php.net/manual/zh/ini.core.php#ini.file-uploads

解决办法:编辑php.ini,增加选项:

# 开启HTTP上传功能
file_uploads = On

====================================================

3.11 max_input_time

[maybe] php.ini / max_input_time
  Input parsing time not limited.
  It may be useful to limit the time a script is allowed to parse input. This should be decided on a per application basis

max_input_time :脚本最大执行时间,单位秒,默认为30。
最大执行时间不会影响系统调用和系统操作等。更多细节参见 set_time_limit()。
其他 web 服务器也可以有其他超时设置,也有可能中断 PHP 的执行。 Apache 有一个 Timeout 指令,IIS 有一个 CGI 超时功能。 他们默认都是 300 秒。更多具体信息参见你的 web 服务器的文档。

【详见官方手册】
https://www.php.net/manual/zh/info.configuration.php#ini.max-input-time

解决办法:编辑php.ini,增加选项:

# php脚本执行超时,30秒自动结束,上传大型文件时,可以改长一些
max_execution_time = 30

==============================================================================

3.12 memory_limit

[maybe] php.ini / memory_limit
  Memory limit is 128M or more.
  A high memory limit may easily lead to resource exhaustion and thus make your application vulnerable to denial-of-service attacks. 
  This value should be set approximately 20% above an empirically gathered maximum memory requirement

memory_limit:单个脚本内存限制,默认为128M
设置了一个脚本允许分配的最大内存量,以字节(bytes)为单位。这有助于防止写得不好的脚本消耗掉服务器上所有可用的内存。请注意,如果不需要内存限制,请将此指令设置为 -1

详见官方手册】:
https://www.php.net/manual/zh/ini.core.php#ini.memory-limit

解决办法:编辑php.ini,增加选项:

# php每个脚本文件运行内存
memory_limit = 128M

==============================================================================

3.13 session.cookie_lifetime

[maybe] php.ini / session.cookie_lifetime
  no implicit lifetime for session cookie.
  Not limiting the cookie lifetime increases the chance for an attacker to be able to steal the session cookie. Depending on your application, this should be set to a reasonable value here or with session_set_cookie_params().

session.cookie_lifetime
浏览器中cookie 的生命周期。单位为妙,默认为 0, 关闭浏览器cookie才会失效。

详见官方手册】:
https://www.php.net/manual/zh/session.configuration.php#ini.session.cookie-lifetime

解决办法:编辑php.ini,增加选项:

# cookie生命周期,单位秒。0为关闭浏览器失效
session.cookie_lifetime = 0

=============================================================================
3.14 session.cookie_samesite

[maybe] php.ini / session.cookie_samesite
  SameSite is unset.
  Set SameSite to `Lax` or `Strict` to better protect against CSRF.

session.cookie_samesite : 此功能自php7.3以后支持
Cookie不应与跨站点请求一起发送。此断言允许用户代理减轻跨域信息泄漏的风险,并提供针对跨站点请求伪造攻击(CSRF)的某种保护。请注意,并非所有浏览器都支持此功能。
默认为空,空值表示将不会设置SameSite cookie属性.
session.cookie_samesite = Strict # 严格模式
表明这个 cookie 在任何情况下都不可能作为第三方 cookie,绝无例外;
session.cookie_samesite = Lax # 宽松模式
比 Strict 放宽了点限制:假如这个请求是我上面总结的那种同步请求(改变了当前页面或者打开了新页面)且同时是个 GET 请求(因为从语义上说 GET 是读取操作,比 POST 更安全),则这个 cookie 可以作为第三方 cookie。以上内容摘自:https://blog.csdn.net/mystonelxj/article/details/82349670

【详见官方手册】:
https://www.php.net/manual/zh/session.configuration.php#ini.session.cookie-samesite

解决办法:编辑php.ini,增加选项

# cookie跨站调用,Lax宽松模式,strict严格模式.请根据实际情况选择
session.cookie_samesite = Strict

==============================================================================

3.15 session.cookie_secure

[maybe] php.ini / session.cookie_secure
  no implicit secure-flag for session cookie.
  This option controls if cookies are tagged as secure and should therefore be sent over SSL encrypted connections only. It should either be activated here or in your application with session_set_cookie_params().

session.cookie_secure:指定是否仅通过安全连接(HTTPS)发送 cookie。默认为 off;

【详见官方手册】:
https://www.php.net/manual/zh/session.configuration.php#ini.session.cookie-secure

解决办法:编辑php.ini,增加选项:

# 是否允许通过HTTPS模式才能发送cookie。默认为off
session.cookie_secure = On

==============================================================================

3.16 session.referer_check

[comment] php.ini / session.referer_check
  referer check not activated.
  PHP can invalidate a session ID if the submitted HTTP Referer does not contain a configured substring. The Referer can be set by a custom client/browser or plugins (e.g. Flash, Java). However it may prevent some cases of CSRF attacks, where the attacker can not control the client's Referer.

session.referer_check :默认为空
包含有用来检查每个 HTTP Referer 的子串。如果客户端发送了 Referer 信息但是在其中并未找到该子串,则嵌入的会话 ID 会被标记为无效。默认为空。

【详见官方手册】:
https://www.php.net/manual/zh/session.configuration.php#ini.session.cookie-secure

解决办法:编辑php.ini,增加选项:

# 开启 HTTP Referer检测
session.referer_check = On

(完)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值