Apache2 添加 Set-Cookie HttpOnly Secure

背景:

网警告诉老板,我们的网站有漏洞,要在限时内处理

在被暴打一顿后准备辞职,但贫穷使我冷静,还是先来理一理思路吧。

要解决问题,首先是要定位问题,并且对于程序员来说大数情况下需要反复测试验证,所以第一步先找一个工具帮我们定位问题并可以验证问题的工具

Acunetix 扫描确认漏洞的存在,同时方便修复后验证

再查查自己的服务器版本

ubuntu 18.x + Apache2.4.7

可以开始百度了

1.Possible virtual host found

存在可能会漏隐私的虚拟机

 打开Apache2 http.conf 发现

解决方案:加个#注释掉

#ServerName localhost

2.OPTIONS method is enabled

需要对OPTIONS做限制,百度一翻

解决方案:在虚拟机中添加

     

         Order allow,deny

         Deny from all

     

3.Cookie(s) without HttpOnly flag set,Cookie(s) without Secure flag set

这两个问题足足花费了半天时间,各种百度无果,科学走起

比较有参考价值的文章:

https://stackoverflow.com/questions/24129201/add-secure-and-httponly-flags-to-every-set-cookie-response-in-apache-httpd

http://www.voidcn.com/article/p-baynbovc-btz.html

Header always edit Set-Cookie "(?i)^((?:(?!;\s?HttpOnly).)+)$" "$1; HttpOnly"

Header always edit Set-Cookie "(?i)^((?:(?!;\s?secure).)+)$" "$1; secure"

配置,重启,配置,重启,配置,重启,无果

初级程序员状态下无法解决,只能开启中级程序员状态

理解问题需要再深入再深入,我们真正需要解决的问题如下:

我们需要对传过来的所有Set-Cookie中的值,最后添加上 HttpOnly;Secure标识

中级程序员一般会直逼答案的核心,查apache2的官方文档

http://httpd.apache.org/docs/current/mod/mod_headers.html

Header always edit Set-Cookie "(?i)^((?:(?!;\s?HttpOnly).)+)$" "$1; HttpOnly"

Header always edit Set-Cookie "(?i)^((?:(?!;\s?secure).)+)$" "$1; secure"

#改为

Header always edit* Set-Cookie "(?i)^((?:(?!;\s?HttpOnly).)+)$" "$1; HttpOnly"

Header always edit* Set-Cookie "(?i)^((?:(?!;\s?secure).)+)$" "$1; secure"

保存,重启,再用Acunetix 测试一下,问题解决。

你可能感兴趣的:(Apache2 添加 Set-Cookie HttpOnly Secure)