16 11 2021

1、屏蔽PHP错误输出


在/etc/php.ini(默认配置文件位置),将如下配置值改为Off

display_errors=Off

不要将错误堆栈信息直接输出到网页上,防止黑客加以利用相关信息。

正确的做法是:

把错误日志写到日志文件中,方便排查问题。


2、屏蔽PHP版本。


默认情况下PHP版本会被显示在返回头里,如:

Response Headers X-powered-by: PHP/7.2.0

将php.ini中如下的配置值改为Off

expose_php=Off


3、关闭全局变量。


如果开启全局变量会使一些表单提交的数据被自动注册为全局变量。代码如下:

<form action="/login" method="post"><input name="username" type="text"><input name="password" type="password"><input type="submit" value="submit" name="submit"></form>

如果开启了全局变量,则服务器端PHP脚本可以用$username和$password来获取到用户名和密码,这会造成极大的脚本注入危险。

开启方法是在php.ini中修改如下:

register_globals=On

建议关闭,参数如下:

register_globals=Off

当关闭后,就只能从$_POST、$_GET、$_REQUEST里面获取相关参数。


4、文件系统限制


可以通过open_basedir来限制PHP可以访问的系统目录。

如果不限制使用下面的脚本代码(hack.php)可以获取到系统密码。

<?php echo file_get_contents('/etc/passwd');

当设置了后则会报错,不再显示相关信息,让系统目录b不会被非法访问:


PHP Warning: file_get_contents(): open_basedir restriction in effect. File(/etc/passwd) is not within the allowed path(s): (/var/www) in /var/www/hack.php on line 3


Warning: file_get_contents(): open_basedir restriction in effect. File(/etc/passwd) is not within the allowed path(s): (/var/www) in /var/www/hack.php on line 3 PHP Warning: file_get_contents(/etc/passwd): failed to open stream: Operation not permitted in /var/www/hack.php on line 3


Warning: file_get_contents(/etc/passwd): failed to open stream: Operation not permitted in /var/www/hack.php on line 3


设置方法如下:

open_basedir=/var/www

 

5、禁止远程资源访问。

allow_url_fopen=Off allow_url_include=Off

其他第三方安全扩展


6、Suhosin


Suhosin是一个PHP程序的保护系统。它的设计初衷是为了保护服务器和用户,抵御PHP程序和PHP核心中已知或者未知的缺陷(感觉挺实用的,可以抵御一些小攻击)。Suhosin有两个独立的部分,使用时可以分开使用或者联合使用。

第一部分是一个用于PHP核心的补丁,它能抵御缓冲区溢出或者格式化串的弱点(这个必须的!);

第二部分是一个强大的 PHP扩展(扩展模式挺好的,安装方便…),包含其他所有的保护措施。


安装扩展

wget http://download.suhosin.org/suhosin-0.9.37.1.tar.gztar zxvf suhosin-0.9.37.1.tar.gzcd suhosin-0.9.37.1/phpize./configure  --with-php-config=/usr/local/bin/php-configmakemake install在php.ini下加入suhosin.so即可 extension=suhosin.so


特性

  • 模拟器保护模式

  • 增加两个函数sha256()和sha256_file()到PHP核心中

  • 所有平台,加入CRYPT_BLOWFISH到函数crypt()中

  • 打开对phpinfo()页的透明保护

  • SQL数据库用户保护(测试阶段)


运行时保护

  • 加密cookies

  • 防止不同种类的包含漏洞(不允许远程URL包含(黑/白名单);不允许包含已上传的文件;防止目录穿越攻击)

  • 允许禁止preg_replace() 

  • 允许禁止eval()函数

  • 通过配置一个最大执行深度,来防止无穷递归

  • 支持每个vhost配置黑白名单

  • 为代码执行提供分离的函数黑白名单

  • 防止HTTP响应拆分漏洞

  • 防止脚本控制memory_limit选项

  • 保护PHP的superglobals,如函数extract(),import_request_vars()

  • 防止mail()函数的新行攻击

  • 防止preg_replace()的攻击

Session 保护

  • 加密session数据

  • 防止session被劫持

  • 防止超长的session id

  • 防止恶意的session id

SESSION里的数据通常在服务器上的明文存放的。这里通过在服务端来加解密$_SESSION。这样将Session的句柄存放在Memcache或数据库时,就不会被轻易攻破,很多时候我们的session数据会存放一些敏感字段。


这个特性在缺省情况下是启用的,也可以通过php.ini来修改:

suhosin.session.encrypt = Onsuhosin.session.cryptkey = zuHywawAthLavJohyRilvyecyondOdjosuhosin.session.cryptua = Onsuhosin.session.cryptdocroot = On ;; IPv4 onlysuhosin.session.cryptraddr = 0suhosin.session.checkraddr = 0

Cookie加密

Cookie在客户端浏览器的传输的HTTP头也是明文的。通过加密cookie,您可以保护您的应用程序对众多的攻击,如

  • Cookie篡改:攻击者可能会尝试猜测其他合理的cookie值来攻击程序。

  • 跨应用程序使用Cookie:不正确配置的应用程序可能具有相同的会话存储,如所有会话默认存储在/tmp目录下,一个应用程序的cookie可能永远不会被重新用于另一应用,只要加密密钥不同。

Cookie加密在php.ini中的配置:

suhosin.cookie.encrypt = On ;; the cryptkey should be generated, e.g. with 'apg -m 32'suhosin.cookie.cryptkey = oykBicmyitApmireipsacsumhylWaps1suhosin.cookie.cryptua = Onsuhosin.cookie.cryptdocroot = On ;; whitelist/blacklist (use only one);suhosin.cookie.cryptlist = WALLET,IDEASsuhosin.cookie.plainlist = LANGUAGE ;; IPv4 onlysuhosin.cookie.cryptraddr = 0suhosin.cookie.checkraddr = 0Blocking Functions测试 ##默认PHP的Session保存在tmp路径下ll  -rt /tmp | grep sess##扩展未开启时查看某条sesson的数据cat  sess_ururh83qvkkhv0n51lg17r4aj6//记录是明文的##扩展开启后查看某条sesson 的数据cat  sess_ukkiiiheedupem8k4hheo0b0v4//记录是密文的可见加密对安全的重要性

阻断功能


白名单

##显式指定指定白名单列表suhosin.executor.func.whitelist = htmlentities,htmlspecialchars,base64_encodesuhosin.executor.eval.whitelist = htmlentities,htmlspecialchars,base64_encode <?phpecho htmlentities('<test>');eval('echo htmlentities("<test>");');


黑名单

##显式指定指定黑名单列表suhosin.executor.func.blacklist = assert,unserialize,exec,popen,proc_open,passthru,shell_exec,system,hail,parse_str,mt_srandsuhosin.executor.eval.whitelist = assert,unserialize,exec,popen,proc_open,passthru,shell_exec,system,hail,parse_str,mt_srand通过日志来查看非法调用黑白名单 suhosin.simulation = 1suhosin.log.file = 511suhosin.log.file.name = /tmp/suhosin-alert.log

其他配置项

suhosin.executor.include.max_traversal    扩目录的最大深度,可以屏蔽切换到非法路径suhosin.executor.include.whitelist        允许包含的URL,用逗号分隔suhosin.executor.include.blacklist        禁止包含的URL,用逗号分隔suhosin.executor.disable_eval = On        禁用eval函数 suhosin.upload.max_uploadssuhosin.upload.disallow_elfsuhosin.upload.disallow_binarysuhosin.upload.remove_binarysuhosin.upload.verification_script        上传文件检查脚本,可以来检测上传的内容是否包含webshell特征

使用Suhosin,你可以得到一些错误日志,你能把这些日志放到系统日志中,也可以同时写到其他任意的日志文件中去;它还可以为每一个虚拟主机创建黑名单和白名单;可以过滤GET和POST请求、文件上载和cookie;

你还能传送加密的会话和cookie,可以设置不能传送的存储上线等等;

它不像原始的PHP强化补丁,Suhosin是可以被像Zend Optimizer这样的第三方扩展软件所兼容的。

发表评论