WebSphere启用了TRACE,OPTIONS等不安全的HTTP方法漏洞修复

本文分享了在WebSphere服务器上修复五个HTTP安全漏洞的过程,包括禁用TRACE和OPTIONS方法,防止HTTP Host头攻击,配置X-Frame-Options,及拦截不安全的HTTP请求方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebSphere启用了TRACE,OPTIONS等不安全的HTTP方法

因甲方要求项目必须基于was服务器运行,在基于was服务器的安全扫描时发现漏洞,由于对was服务器的不熟悉,并且相关was问题搜索结果甚少,导致修复5个漏洞耗时4天才得以修复,此帖子记录修复方式和踩过的坑。

安全扫描出的漏洞中其主要漏洞为未关闭http的不安全请求方式,基于现有系统只有get和post请求,下面方法除get和post方式请求其余全部拦截,主要方式通过拦截去获取当前请求方式,判断是否允许访问。

扫描出来漏洞为下面五个,其中测试人员已提供解决方案,但有些并不足以修复漏洞,详情请继续往下看
1.检测到目标服务器启用了TRACE方法(高危)
在这里插入图片描述
解决方式:
由于项目使用was服务器运行,本人使用截图中解决办法并未奏效,因was并不基于Apache运行,所以改了之后毫无用处,并且was中并未找到有类似的配置文件设置关闭方法,所以只能通过应用中代码设置拦截。

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        //获取当前请求方式
        String str=((HttpServletRequest) req).getMethod();
        System.out.println("进入过滤器"+str);
        //判断当前请求方式是否为我允许访问的方式
        if (!str.equals(str.equals(HttpMethod.GET.toString()) && !str.equals(str.equals(HttpMethod.POST.toString())) {
            System.out.println("当前请求方式是"+str+"进行拦截,返回404");
            response.setStatus(404);
            return;
        }
        filterChain.doFilter(request, response);
    }

2.检测到目标服务器启用了OPTIONS方法(低危)
在这里插入图片描述
解决方式:
参考第1条漏洞

3.检测到目标URL存在http host头攻击漏洞(中危)
在这里插入图片描述
解决方式:
1.wes中自带请求头拦截功能,详情请见
https://wiki.smartbi.com.cn/pages/viewpage.action?pageId=47481406
2.通过拦截器方式配置白名单

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        // 头攻击检测  过滤主机名
        String requestHost = request.getHeader("host");
        if (requestHost != null && !checkBlankList(requestHost)) {
            response.setStatus(403);
            return;
        }
        filterChain.doFilter(request, response);
    }

    //判断主机是否存在白名单中
    private boolean checkBlankList(String host){
        //todo 未来改域名需要同步修改
        //添加允许访问的地址(项目运行地址,非本地请求地址)
        if(host.contains("127.0.0.1")
        || host.contains("localhost")){
            return true;
        }
        return false;
    }

4.点击劫持:X-Frame-Options未配置(低危)
在这里插入图片描述
解决方式:

@Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        //漏洞X-Frame-Options未配置
        response.addHeader("x-frame-options","SAMEORIGIN");
        filterChain.doFilter(request, response);
    }

5.检测到目标URL启用了不安全的HTTP方法(低危)
在这里插入图片描述
解决方式:
参考第1条漏洞

五个漏洞整合(项目中的代码)
项目中web.xml中添加如下代码:

<web-app version="2.5"
		 xmlns="http://java.sun.com/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>pms application</display-name>
	<session-config>
		<session-timeout>720</session-timeout>
	</session-config>
	<error-page>
		<error-code>404</error-code>
		<location>/error/404.html</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/error/500.html</location>
	</error-page>

	<!--头攻击过滤-->
	<filter>
		<filter-name>HttpHostFilter</filter-name>
		<filter-class>
			cn.linkey.ws.server.HttpHostFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpHostFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>

</web-app>

新建过滤器类代码为:

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("serial")
public class HttpHostFilter  extends HttpServlet implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        //漏洞X-Frame-Options未配置
        response.addHeader("x-frame-options","SAMEORIGIN");
        String str=((HttpServletRequest) req).getMethod();
        //判断当前请求方式是否为我允许访问的方式
        if (!str.equals(str.equals(HttpMethod.GET.toString()) && !str.equals(str.equals(HttpMethod.POST.toString())) {            System.out.println("当前方式是"+str+"进行拦截,返回404");
            response.setStatus(404);
            return;
        }
        // 头攻击检测  过滤主机名
        String requestHost = request.getHeader("host");
        if (requestHost != null && !checkBlankList(requestHost)) {
            response.setStatus(403);
            return;
        }
        filterChain.doFilter(request, response);
    }

    //判断主机是否存在白名单中
    private boolean checkBlankList(String host){
        //todo 未来改域名需要同步修改
        //添加允许访问的地址(项目运行地址,非本地请求地址)
        if(host.contains("127.0.0.1")
        || host.contains("localhost")
        ){
            return true;
        }
        return false;
    }
}

扩展
1、http的请求方式有
在这里插入图片描述
2.修复第一个漏洞如是apache服务器可执行如下操作,亲测本地可行:

linux具体操作如下: 找到服务器配置文件
/etc/httpd/conf/httpd.conf
在文件最后一行加上 TraceEnable off
如果不行的话在 vhost.conf 也加上以上的指令,重启apache
/etc/init.d/httpd restart
或是在httpd.conf里面每一个visual host里面加以下的module(RrwriteEngine需要compiler)
RewriteEngine on RewriteCond %{REQUEST_METHOD}^(TRACE|TRACK) RewriteRule .* – [F]
这一点比较复杂visual host都加上…重启
/etc/init.d/httpd restart 稍后生效

3.拦截http请求,修复第一、二、五漏洞,在web.xml中配置如下代码:
此方法本地测试和tomcat测试可行,was服务器中不生效,如用was建议使用拦截器方式解决。
此代码配置在应用中表示当前应用有效,配置在tomcat的web.xml中表示在tomcat下运行的项目都有效

<web-app version="2.5"
		 xmlns="http://java.sun.com/xml/ns/javaee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


	<!-- 关闭不安全的HTTP方法 -->
	<security-constraint>
		<web-resource-collection>
			<web-resource-name>test</web-resource-name>
			<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>
</web-app>

4.测试关闭http请求方式是否生效
telnet ip 端口
回车
TRACE / HTTP/1.1
两次回车会看到响应消息,如果不行多点几次或者根据情况查看原因
其他凡是请求可将头部换掉如
POST / HTTP/1.1
GET / HTTP/1.1
功能开启显示为
在这里插入图片描述

功能关闭显示为
在这里插入图片描述
本帖为个人备忘录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值