前言:最近有客户使用burp suite扫描出了一些安全问题,涉及到tomcat的配置一脸懵逼,搜到的文章也不太靠谱,记录此文希望对大家有帮助。
一、确认Tomcat版本
本文针对tomcat8,其他版本不保证是否可行,需要先确认版本是否OK。
cmd进入tomcat/bin,执行命令查看版本:catalina version,如下图所示版本号为8.5.23
二、配置https
1、生成密钥
cmd中输入(其中您的名字与姓氏是什么?需要填入主机域名,如本机测试可以写localhost):
keytool -genkey -alias tomcat -keyalg RSA -keystore D:\keystore 输入密钥库口令:cnblogs 再次输入新口令:cnblogs 您的名字与姓氏是什么? [Unknown]: cnblogs 您的组织单位名称是什么? [Unknown]: cnblogs 您的组织名称是什么? [Unknown]: cnblogs 您所在的城市或区域名称是什么? [Unknown]: beijing 您所在的省/市/自治区名称是什么? [Unknown]: beijing 该单位的双字母国家/地区代码是什么? [Unknown]: cn CN=cnblogs, OU=cnblogs, O=cnblogs, L=beijing, ST=beijing, C=cn是否正确? [否]: y 输入 <tomcat> 的密钥口令 (如果和密钥库口令相同, 按回车):
2、将生成的keystore放进tomcat的根目录
3、修改tomcat/conf/server.xml文件:
搜索到“8443”,找到如下内容:
修改如下,keystorePass为第一步输入的密钥口令
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="D:\cnblogs\tomcat\keystore" keystorePass="cnblogs" />
4、修改tomcat/webapps/projectName/WEB-INF/web.xml,在标签web-app中添加如下内容:
<security-constraint> <web-resource-collection > <web-resource-name >SSL</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>
三、安全配置
如上配置后,用burpsuite扫描会报两个漏洞:
1、SSL cookie without secure flag set
修改tomcat/conf/web.xml文件,在标签session-config下添加:
<session-config> <session-timeout>0</session-timeout> <cookie-config> <http-only>true</http-only> <secure>true</secure> </cookie-config> </session-config>
2、Strict transport security not enforced
修改tomcat/conf/web.xml文件,在标签web-app中添加:
<filter> <filter-name>httpHeaderSecurity</filter-name> <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class> <init-param> <param-name>hstsEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>hstsMaxAgeSeconds</param-name> <param-value>31536000</param-value> </init-param> <init-param> <param-name>antiClickJackingOption</param-name> <param-value>SAMEORIGIN</param-value> </init-param> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>httpHeaderSecurity</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
3、配置check,重启后,访问之前的http端口,可以看到返回302重定向从8085端口到8443端口(HSTS要求强制转换为https),且Set-Cookie中有了Secure和HttpOnly
重定向后https请求可以看到有了Strict-Transport-Security消息头