web常见漏洞修复方法

负、检测工具

1、加密套件、协议兼容性、浏览器兼容性

1、https://www.ssllabs.com/ssltest/index.html
2、https://myssl.com/
通过域名检测

2、重大漏洞检测

http://0day.websaas.com.cn/

零、测试加密套件方法 (nmap)

安装方式:yum -y install nmap
可以告知你当前密码套件强度

nmap -sV --script ssl-enum-ciphers -p 443 www.example.com

[root@iZj6c2t7x4azg1onsgpzljZ nginxtest]# nmap -sV --script ssl-enum-ciphers -p 443 chatbot.hkpc.org

Starting Nmap 6.40 ( http://nmap.org ) at 2021-07-08 17:13 CST
Nmap scan report for www.example.com (xx.xxx.xx.xxx)
Host is up (0.0012s latency).
PORT    STATE SERVICE  VERSION
443/tcp open  ssl/http Jetty
| ssl-enum-ciphers: 
|   SSLv3: No supported ciphers found
|   TLSv1.2: 
|     ciphers: 
|       TLS_RSA_WITH_3DES_EDE_CBC_SHA - strong
|       TLS_RSA_WITH_AES_128_CBC_SHA - strong
|       TLS_RSA_WITH_AES_128_CBC_SHA256 - strong
|       TLS_RSA_WITH_AES_128_GCM_SHA256 - strong
|       TLS_RSA_WITH_AES_256_CBC_SHA - strong
|       TLS_RSA_WITH_AES_256_CBC_SHA256 - strong
|       TLS_RSA_WITH_AES_256_GCM_SHA384 - strong
|     compressors: 
|       NULL
|_  least strength: strong

一、cookie问题

1、httpOnly / Secure

Java 解决方案

let JSESSIONID = document.cookie.match("JSESSIONID");
if(JSESSIONID){
 console.log(JSESSIONID)
 let exp = new Date();
 exp.setTime(exp.getTime() + 7*24*60*60*1000);
  document.cookie = JSESSIONID[0]+"="+JSESSIONID['input']+";Secure;Path=/;expires="+exp.toGMTString()+";"
}

jetty解决方案

//配置文件 etc/webdefault.xml
// 可以根据注释:<-- Default session configuration -->下来定位
  <session-config>
    <session-timeout>30</session-timeout>
     <cookie-config>
     <secure>true</secure>
      <http-only>true</http-only>
    </cookie-config>
</session-config>

nginx 解决方案

//1、会直接设置当前的slb之类的cookies
proxy_cookie_path / "/; httponly; secure; SameSite=Lax";

//2、自定义设置cookie
//add_header Set-Cookie  <cookie-name>=<cookie-value>; Path=/; HttpOnly; Secure;SameSite=strict;
//例如针对jetty 的JSESSIONID,我们需要用到aws的slb
// $cookie_{keyname} 可以直接获取值
//$http_cookie 是获取整个cookie
add_header Set-Cookie "JSESSIONID=$cookie_AWSALB ; Path=/; HttpOnly; Secure; SameSite=strict;";

2、Cookies with missing, inconsistent or contradictory properties

java解决方案

通常建议在拦截器增加

public class HttpLeakInterceptor implements HandlerInterceptor {
...
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            Cookie cookie = cookies[0];
            if (cookie != null) {
                // serlvet 2.5 不支持在 Cookie 上直接设置 HttpOnly 属性.
                String value = cookie.getValue();

                StringBuilder builder = new StringBuilder();
                builder.append("JSESSIONID=" + value + "; ");
                builder.append("Secure; ");
                builder.append("SameSite=strict; "); //主要是这段
                builder.append("HttpOnly; ");
                builder.append("path=/; ");

                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.HOUR, 1);
                Date date = calendar.getTime();
                Locale locale = Locale.CHINA;

                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", locale);
                builder.append("Expires=" + sdf.format(date));
                httpServletResponse.setHeader("Set-Cookie", builder.toString());
            }

        }
    }
...
}

jetty 解决方案(9.4.22+版本)
严格模式:__SAME_SITE_STRICT__
不使用:__SAME_SITE_NONE__
lax模式:__SAME_SITE_LAX__

//配置文件 etc/webdefault.xml
// 可以根据注释:<-- Default session configuration -->下来定位
<session-config>
  <cookie-config>
    <comment>__SAME_SITE_NONE__</comment>
  </cookie-config>
</session-config>

二、 Clickjacking 问题

参考网站:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Security-Policy

1、X-Frame-Options header

1、DENY
表示该页面不允许在frame中展示,即便是在相同域名的页面中嵌套也不允许。
2、SAMEORIGIN
表示该页面可以在相同域名页面的frame中展示。
3、ALLOW-FROM uri
表示该页面可以在指定来源的frame中展示。

java 解决方案

//基于过滤器处理
//例如:我需要允许的网站:http://www.coffeeandice.cn
...
package cn.coffeeandice.filters
...
@Component
public class LeakFilter implements Filter {

	...
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletResponse resp = (HttpServletResponse) response;
		resp.addHeader("X-Frame-Options", "ALLOW-FROM http://www.coffeeandice.cn");
		chain.doFilter(request, resp);
	}
	...
}

若非springboot项目,还需要搭配web.xml配置过滤器

   <!-- clickJacking -->    
   <filter>        
   		<filter-name>LeakFilter</filter-name>        
 		<filter-class>cn.coffeeandice.filters.LeakFilter</filter-class>    
   </filter>    
   <filter-mapping>        
   		<filter-name>LeakFilter</filter-name>        
   		<url-pattern>/*</url-pattern>    
   </filter-mapping>

ngingx解决方案

//设:所需允许的网站为 https://www.coffeeandice.cn
add_header X-Frame-Options  "ALLOW-FROM https://www.coffeeandice.cn";

2、CSP frame-ancestors missing

nginx解决方案

通常我们可以通过配置 Content-Security-Policy处理

可以多个数据源,中间只需要空格分隔即可

Content-Security-Policy: frame-ancestors <source> <source>;

通常两个策略:

self: 设置一相信的域名,可以利用通配符 *

單個域名:
add_header Content-Security-Policy "frame-ancestors 'self' https://www.coffeeandice.cn";
多個域名: 利用空格分隔
add_header Content-Security-Policy "frame-ancestors 'self' https://www.coffeeandice.cn https://www.coffeeandice.cn"; 

none: 当该指令设置为none时,其作用类似于X-Frame-Options: DENY

add_header Content-Security-Policy "frame-ancestors 'none'";

三、TLS/SSL

1、LOGJAM attack

参考网站: https://weakdh.org/
总体建议是需要提升DHKey的长度,建议提升为2048

(Part A)jdk参数设置

jdk参数 -Djdk.tls.ephemeralDHKeySize = 2048
方式一: 直接设置jdk参数
方式二:设置jetty的启动参数

(Part B)jetty-ssl.xml文件配置

通常目录结构为{Jetty_Home}/etc/
前提条件:在start.ini中开启了这个模块配置 /etc/jetty.xml
TIps: 9.4.x之后迁移至jetty-ssl-context.xml

<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
...
<Set name="ExcludeCipherSuites">
    <Array type="String">
    ...
<!-- Disable cipher suites with Diffie-Hellman key exchange to prevent Logjam attack and avoid the ssl_error_weak_server_ephemeral_dh_key error in recent browsers -->
	    <Item>SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA</Item>
	    <Item>SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA</Item>
	    <Item>TLS_DHE_RSA_WITH_AES_256_CBC_SHA256</Item>
	    <Item>TLS_DHE_DSS_WITH_AES_256_CBC_SHA256</Item>
	    <Item>TLS_DHE_RSA_WITH_AES_256_CBC_SHA</Item>
	    <Item>TLS_DHE_DSS_WITH_AES_256_CBC_SHA</Item>
	    <Item>TLS_DHE_RSA_WITH_AES_128_CBC_SHA256</Item>
	    <Item>TLS_DHE_DSS_WITH_AES_128_CBC_SHA256</Item>
	    <Item>TLS_DHE_RSA_WITH_AES_128_CBC_SHA</Item>
	    <Item>TLS_DHE_DSS_WITH_AES_128_CBC_SHA</Item>
	...
    </Array>
</Set>
...

9.4.40后开启ssl启动时检查通用jks建议加上的排除套件如下

<Item>TLS_RSA_WITH_AES_256_GCM_SHA384</Item>
<Item>TLS_RSA_WITH_AES_128_GCM_SHA256</Item>
<Item>TLS_RSA_WITH_AES_256_CBC_SHA256</Item>
<Item>TLS_RSA_WITH_AES_128_CBC_SHA256</Item>
<Item>TLS_RSA_WITH_AES_256_CBC_SHA</Item>
<Item>TLS_RSA_WITH_AES_128_CBC_SHA</Item>

2023.4.10 更新记录

通过定期检测,发现需要额外增加以下内容使得通过安全检测

<Item>TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384</Item>
<Item>TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256</Item>
<Item>TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA</Item>
<Item>TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA</Item>

2、Sweet32 attack / Weak Cipher Suites

其实两个问题都是围绕 TLS_RSA_WITH_3DES_EDE_CBC_SHA 这个加密套件处理
Sweet32 attack :主要是围绕 64位加密不安全的情况出现
Weak Cipher Suites:主要是这个加密套件强度不够,建议关闭

阿里雲SLB解決方案
负载均衡 SLB/配置监听 大于图示选项即可
在这里插入图片描述

jetty解决方案

<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
...
<Set name="ExcludeCipherSuites">
    <Array type="String">
    ...
<!-- Disable cipher suites with Diffie-Hellman key exchange to prevent Logjam attack and avoid the ssl_error_weak_server_ephemeral_dh_key error in recent browsers -->
	    <Item>TLS_RSA_WITH_3DES_EDE_CBC_SHA</Item>
	...
    <Array type="String">
</Set>
...

nginx 解决方案

nginx编译时需要考虑到openSSl版本问题
参考网站: https://www.openssl.org/news/secadv/20160922.txt
OpenSSL 1.0.2 users should upgrade to 1.0.2i
OpenSSL 1.0.1 users should upgrade to 1.0.1u

   理论上nginx默认是: ssl_ciphers HIGH:!aNULL:!MD5;
   
   只需要在后加上一个!3DES 即可
   ssl_ciphers  AES128+EECDH:AES128+EDH:!aNULL:!3DES;

四、HSTS问题

具体而言就是 HTTP Strict Transport Security (HSTS) not implemented

jetty解决方案

方式一:变更jetty-rewrite.xml( 这个模式内,不需要证书挂载再jetty下)

//在start.ini修改以下两个参数为rewrite
//#Module: rewrite
//--module=rewrite
//
//直接放置<configure></configure> 节点内

在jetty-rewrite.xml添加
<Ref refid="Rewrite">
    <Call name="addRule">
        <Arg>
            <New class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                <Set name="pattern">*</Set>
                <Set name="name">Strict-Transport-Security</Set>
                <Set name="value">max-age=31536000;includeSubDomains</Set>
            </New>
        </Arg>
    </Call>
</Ref>

在这里插入图片描述

方式二: 变更 jetty-ssl.xml

通常为9.4+ version

初始默认值:

  <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
    <Arg><Ref refid="httpConfig"/></Arg>
    <Call name="addCustomizer">
      <Arg>
        <New class="org.eclipse.jetty.server.SecureRequestCustomizer">
          <Arg name="sniRequired" type="boolean"><Property name="jetty.ssl.sniRequired" default="false"/></Arg>
          <Arg name="sniHostCheck" type="boolean"><Property name="jetty.ssl.sniHostCheck" default="true"/></Arg>
          <Arg name="stsMaxAgeSeconds" type="int"><Property name="jetty.ssl.stsMaxAgeSeconds" default="-1"/></Arg>
          <Arg name="stsIncludeSubdomains" type="boolean"><Property name="jetty.ssl.stsIncludeSubdomains" default="false"/></Arg>
        </New>
      </Arg>
    </Call>
  </New>

适配解决HSTS值:

主要是:
stsMaxAgeSecondsstsIncludeSubdomains属性

  <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
    <Arg><Ref refid="httpConfig"/></Arg>
    <Call name="addCustomizer">
      <Arg>
        <New class="org.eclipse.jetty.server.SecureRequestCustomizer">
          <Arg name="sniRequired" type="boolean"><Property name="jetty.ssl.sniRequired" default="false"/></Arg>
          <Arg name="sniHostCheck" type="boolean"><Property name="jetty.ssl.sniHostCheck" default="true"/></Arg>
          <Arg name="stsMaxAgeSeconds" type="int"><Property name="jetty.ssl.stsMaxAgeSeconds" default="31536000"/></Arg>
          <Arg name="stsIncludeSubdomains" type="boolean"><Property name="jetty.ssl.stsIncludeSubdomains" default="true"/></Arg>
        </New>
      </Arg>
    </Call>
  </New>

nginx解决方案

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

五、TLS 1.0 enabled

阿里雲SLB解決方案
负载均衡 SLB/配置监听 大于图示选项即可

在这里插入图片描述

java解决方案

	理论上需要TLS1.1也需要禁用,内部通常有内容,往上添加即可
	
	路径: {JAVA_HOME}/jre/lib/security/java.security
	
	jdk.tls.disabledAlgorithms=TLSv1, TLSv1.1

nginx 解决方案

ssl_protocols TLSv1.2;

六、iframe问题

1、Insecure Inline Frame

主要是因为缺少sandbox属性,引入了外部资源

参数名称参数说明
allow-forms允许进行提交表单
allow-scripts运行执行脚本
allow-modals允许提示框,即允许执行window.alert()等会产生弹出提示框方法
allow-same-origin允许同域请求,比如ajax,storage
allow-top-navigation允许iframe能够主导window.top进行页面跳转
allow-popups允许iframe中弹出新窗口,比如,window.open,target=“_blank”
allow-popups-to-escape-sandbox允许弹出窗口不受沙箱的限制 ,针对谷歌和Edge拦截附件弹出网站奇效
allow-pointer-lock在iframe中可以锁定鼠标,主要和鼠标锁定有关
allow-downloads允许点击后下载文件 (83版本后新增)
allow-top-navigation允许嵌入的网页对顶级窗口进行导航
allow-top-navigation-by-user-activation允许嵌入的网页对顶级窗口进行导航,但必须由用户激活。
allow-downloads-without-user-activation允许在没有征求用户同意的情况下下载文件
allow-storage-access-by-user-activation允许在用户激动的情况下,嵌入的网页通过 Storage Access API 访问父窗口的储存

建议例子:

sandbox=‘allow-forms allow-popups allow-same-origin allow-scripts allow-downloads’

七、apache Log4j 远程漏洞执行问题

实际上是属于v2版本的漏洞问题:
受影响版本为v2系列,Apache Log4j 2.x <= 2.15.0-rc2
参考漏洞链接:
国家漏洞共享平台https://www.cnvd.org.cn/webinfo/show/7116
LunaSechttps://www.lunasec.io/docs/blog/log4j-zero-day

建议临时处置方式:

1)添加jvm启动参数-Dlog4j2.formatMsgNoLookups=true;
2)在应用classpath下添加log4j2.component.properties配置文件,文件内容为log4j2.formatMsgNoLookups=true;
3)JDK使用11.0.1、8u191、7u201、6u211及以上的高版本;
4)部署使用第三方防火墙产品进行安全防护。

八、CSRF 处理 (Broken Access Control)

这里通常是存在多种防御措施,验证Referer 是这里的处理方式。
其他建议方式(基于网络搜寻)
1、尽量Post request
这里是可以针对性侦测,处理风险。
2、验证码处理 & 针对性 csrf token
采用二次验证的方式处理,当然这里多少会影响负载能力
这里也可以采用lua脚本的方式校验
3、验证Referer
主要采用校验Referer 与 Host 的方式处理

1、单域名处理

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        # 检查请求的 Referer 头,这里已经正则匹配
        if ($http_referer !~* "^https://yourdomain\.com/") {
            return 403;
        }

        # 允许合法的请求继续到后端
        proxy_pass http://backend_server;
    }
}

2、多域名处理

可以采用map模块 去匹配
./configure --add-module=src/http/modules/ngx_http_map_module

http {
    map $http_referer $is_valid_referer {
        default         0;  # 默认情况下,禁止请求
        "~^https://(www\.)?domain1\.com"  1;
        "~^https://(www\.)?domain2\.com"  1;
        "~^https://(www\.)?domain3\.com"  1;
        # 添加其他受信任的域名
    }

    server {
        listen 80;
        server_name yourdomain.com;

        location / {
            if ($is_valid_referer = 0) {
                return 403;  # 拒绝无效 Referer 头的请求
            }
            # 处理请求
        }
    }
}

九、请求头相关问题

Tips: 如果下属内容需要页面嵌入到iframe中,需要将CSP & XFO 提前至顶部,避免内容被浏览器提前拦截
eg:

		add_header Content-Security-Policy "frame-ancestors 'self' https://www.coffeeandice.cn";
		add_header X-Frame-Options  "ALLOW-FROM https://www.coffeeandice.cn";
        if ($http_referer  !~ "https://[a-zA-Z0-9.-]+\.coffeeandice\.*$") {
                             return 403;
        }

1、Missing or insecure “X-Content-Type-Options” header

建议 nginx 增加请求头 X-Content-Type-Options 以避免对 MIME 类型 进行修改为可执行的内容

X-Content-Type-Options: nosniff

2、Security Misconfiguration (Access-Control-Allow-Origin issue)

主要是CORS问题,nginx 增加域名限制使得浏览器可以正常执行拦截

2.1、配置内容

#1 、$http_origin 这里根据环境替换内容域名(server级别支持)
add_header "Access-Control-Allow-Origin" $http_origin;


#2、补充隐藏应用的头部,避免应用滥用  Access-Control-Allow-Origin: * “
proxy_hide_header 'Access-Control-Allow-Origin';

2.2、校验方法

1、检查响应头是否与设置的源一致。
2、通过http客户端,自行设置 Referer header 看是否存在 * 滥用的情况,一般保持不变或不增加新行即可。

十、dom元素相关问题

1、Unsafe third-party link (target=“_blank”)

建议为每个a 标签中包含 target 的元素,添加 rel=“noopener noreferrer”

属性描述
noopener防止新打开的窗口能够通过 window.opener 访问到原始文档,从而提高安全性
noreferrer阻止浏览器发送原始文档的 Referer 头信息
<a href="https://example.com" target="_blank" rel="noopener noreferrer">打开新窗口</a>

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
web常见漏洞包括: 1. XSS(跨站脚本):攻击者通过注入恶意脚本代码来攻击用户浏览器,获取用户的敏感信息。 2. SQL注入:攻击者通过向Web应用程序的数据库查询中插入恶意的SQL代码来获取数据库的敏感信息。 3. CSRF(跨站请求伪造):攻击者利用用户在已认证的Web应用程序上执行未经授权的操作。 4. 文件包含:攻击者通过向Web应用程序的文件包含功能中插入恶意代码来执行任意命令。 5. 逻辑漏洞:通过操纵应用程序的逻辑流程来执行未经授权的操作。 针对这些常见漏洞,可以使用以下方法进行检测: 1. 漏洞扫描器:使用专门的扫描工具,对Web应用程序进行全面扫描,检测漏洞并给出相应的建议。 2. 安全审计:通过对应用程序代码和配置进行详细审查,识别潜在的漏洞点。 3. 条件测试:通过构造特殊的输入,观察应用程序的响应并判断是否存在漏洞。如在输入框中输入特殊字符,检测是否能成功执行恶意脚本。 4. 渗透测试:通过模拟攻击者实际进行渗透测试,评估应用程序的安全性,并发现漏洞。 在检测到漏洞后,需要及时采取相应的修复措施,如修复代码中的漏洞点、更新框架和组件、严格控制访问权限等,确保Web应用程序的安全性。此外,也需要定期进行漏洞检测和修复,以应对日益变化的网络安全威胁。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值