在服务器漏扫中经常遇到"远端www服务支持TRACE请求"漏洞,绿盟扫描器所提供修复建议有不适用的情况。对已经处理过的不同应用禁用TRACE请求做一总结记录。
首先漏洞验证:
模拟trace请求,假设报漏洞的端口是8081:
curl -v -X TRACE -I localhost:8081
如果回显为:
< HTTP/1.1 200 OK HTTP/1.1 200 OK < Content-Type: message/http Content-Type: message/http
则该端口服务支持trace请求,漏洞存在。
如果回显为:
< HTTP/1.1 403 Forbidden < Content-Type: text/html; charset=iso-8859-1 或者回显为 < HTTP/1.1 405 Method Not Allowed < Content-Type: text/html; charset=iso-8859-1
则该漏洞不存在。
漏洞修复:
1.对于apache:
对于2.0.55以上版本的apache服务器,
在httpd.conf尾部添加如下指令后重启apache即可:
TraceEnable off
其它版本的Apache服务器可编辑httpd.conf文件:
激活rewrite模块(去掉符号 # ):
LoadModule rewrite_module modules/mod_rewrite.so
在各虚拟主机的配置文件里添加如下语句:
# 启用 Rewrite 引擎
RewriteEngine On
# 对Request中的Method字段进行匹配:^TRACE 即以TRACE字符串开头
RewriteCond %{REQUEST_METHOD} ^TRACE
# 定义规则:对于所有格式的来源请求,均返回[F]-Forbidden响应
RewriteRule .* - [F]
注:可以在httpd.conf里搜索VirtualHost确定虚拟主机的配置文件。
2.对于非内嵌tomcat:
直接修改tomcat根目录conf目录下的web.xml,
在文件末尾(</web-app>之前)添加如下代码:
<security-constraint> <web-resource-collection> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> </web-resource-collection> <auth-constraint> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config>
注:在tomcat
的在server.xml
中先允许TRACE
请求,再在web.xml中
禁用TRACE
,以此禁用TRACE请求.
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" allowTrace="true" redirectPort="8443" />
3.对于spring boot内嵌tomcat:
配置TomcatConfig.java
1 import org.apache.catalina.Context; 2 import org.apache.tomcat.util.descriptor.web.SecurityCollection; 3 import org.apache.tomcat.util.descriptor.web.SecurityConstraint; 4 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; 5 import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer; 6 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration 11 public class TomcatConfig { 12 13 @Bean 14 public EmbeddedServletContainerFactory servletContainer() { 15 TomcatEmbeddedServletContainerFactory tomcatServletContainerFactory = new TomcatEmbeddedServletContainerFactory(); 16 tomcatServletContainerFactory.addContextCustomizers(new TomcatContextCustomizer(){ 17 @Override 18 public void customize(Context context) { 19 SecurityConstraint securityConstraint = new SecurityConstraint(); 20 securityConstraint.setUserConstraint("CONFIDENTIAL"); 21 SecurityCollection collection = new SecurityCollection(); 22 23 collection.addPattern("/*"); 24 collection.addMethod("HEAD"); 25 collection.addMethod("PUT"); 26 collection.addMethod("DELETE"); 27 collection.addMethod("OPTIONS"); 28 collection.addMethod("TRACE"); 29 collection.addMethod("COPY"); 30 collection.addMethod("SEARCH"); 31 collection.addMethod("PROPFIND"); 32 securityConstraint .addCollection(collection); 33 context.addConstraint(securityConstraint ); 34 } 35 }); 36 37 //禁用TRACE请求 38 tomcatServletContainerFactory.addConnectorCustomizers(connector -> { 39 connector.setAllowTrace(true); 40 }); 41 return tomcatServletContainerFactory; 42 } 43 }
4.对于非内嵌式Jetty:
在jetty.xml中增加配置:
1 <security-constraint> 2 <web-resource-collection> 3 <web-resource-name>NoTrace</web-resource-name> 4 <url-pattern>/*</url-pattern> 5 <http-method>TRACE</http-method> 6 </web-resource-collection> 7 <auth-constraint></auth-constraint> 8 </security-constraint>
5.对于Springboot内嵌式Jetty:
由于这种情况没有实际操作过,代码参考其他博主。采用拦截器来过滤所有的trace请求->启动类增加配置来实现,或者和内嵌式tomcat一样直接添加Jetty配置类来实现也可以。
参考链接:https://blog.csdn.net/qq_33479841/article/details/109769790