当前位置: 代码网 > it编程>数据库>Redis > Nginx修复CORS漏洞的实现方法

Nginx修复CORS漏洞的实现方法

2024年11月16日 Redis 我要评论
漏洞描述cors 不安全配置漏洞指的是在跨域资源共享过程中,由于资源服务器的响应头 access-control-allow-origin 配置不当导致本应该受限访问的请求网站可以绕过访问控制策略读取

漏洞描述

cors 不安全配置漏洞指的是在跨域资源共享过程中,由于资源服务器的响应头 access-control-allow-origin 配置不当导致本应该受限访问的请求网站可以绕过访问控制策略读取资源服务器的数据,造成用户隐私泄露,信息窃取甚至账户劫持的危害。

漏洞细节

经过对以下目标进行扫描测试:https://xxx.com/external/

发现存在该漏洞。

发现 access-control-allow-origin 的值为 https://xxx.com.qa5bnet.cn

漏洞探测过程的请求流为
第 1 个请求为

get /external/ http/1.1
host: xxx.com
user-agent: mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/71.0.3578.98 safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-language: en
origin: https://xxx.com.qa5bnet.cn
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: none
sec-fetch-user: ?1
upgrade-insecure-requests: 1
accept-encoding: gzip

第 1 个响应为

http/1.1 401 
access-control-allow-methods: get,post,options,put,delete
access-control-allow-origin: https://xxx.com.qa5bnet.cn
connection: keep-alive
content-length: 0
date: mon, 13 nov 2023 02:07:00 gmt
www-authenticate: basic realm="application"

漏洞修复

        set $flag 0;

        if ($http_origin = ''){
            set $flag "${flag}1";
        }

        if ($http_origin !~* ^(http|https)://test\.test\.com$){
            set $flag "${flag}1";
        }

        if ($flag = "01"){
            return 403;
        }

        if ($http_origin ~* ^(http|https)://test\.test\.com$) {
            add_header access-control-allow-origin $http_origin;
            add_header access-control-allow-methods get,post;
            add_header access-control-allow-credentials true;
            add_header access-control-allow-headers dnt,keep-alive,user-agent,if-modified-since,cache-control,content-type;
}

具体配置如下:

  server {
        listen 80;
        server_name test.test.com;

        location / {
            set $flag 0;
    
            if ($http_origin = ''){
                set $flag "${flag}1";
            }
    
            if ($http_origin !~* ^(http|https)://test\.test\.com$){
                set $flag "${flag}1";
            }
    
            if ($flag = "01"){
                return 403;
            }
    
            if ($http_origin ~* ^(http|https)://test\.test\.com$) {
                add_header access-control-allow-origin $http_origin;
                add_header access-control-allow-methods get,post;
                add_header access-control-allow-credentials true;
                add_header access-control-allow-headers dnt,keep-alive,user-agent,if-modified-since,cache-control,content-type;
    				}
        
            #将ip和端口改为dataease服务器的访问地址和端口
            proxy_pass   http://192.168.110.251:81/;
            server_name_in_redirect off;

            # websocket 代理
            proxy_http_version      1.1;
            proxy_set_header        upgrade         $http_upgrade;
            proxy_set_header        connection "upgrade";

            proxy_set_header           host $host:$server_port;
            proxy_set_header           x-real-ip $remote_addr;
            proxy_set_header           x-forwarded-for $proxy_add_x_forwarded_for;
            proxy_set_header           x-forwarded-proto $scheme;

           
        }
  }

到此这篇关于nginx修复cors漏洞的实现方法的文章就介绍到这了,更多相关nginx修复cors漏洞内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

  • Redis过期键的删除策略分享

    redis过期键删除策略redis是内存型数据库,可对键设置过期时间,当键过期时时怎么淘汰这些键的呢?我们先来想一想,如果让我们设计,我们会想到哪些过期删除策略呢?定时器,创建一个…

    2024年11月08日 数据库
  • 深入理解Redis哈希槽

    深入理解Redis哈希槽

    1. 什么是 redis 哈希槽?redis cluster 是 redis 的分布式架构,它将数据分布在多个 redis 实例(节点)上。为了实现数据分片,r... [阅读全文]
  • redis缓存预热的实现示例

    redis缓存预热的实现示例

    一、缓存预热的必要性在一个高并发的系统中,如果缓存刚启动时是空的,所有的请求都会直接打到数据库,这可能会导致以下问题:高延迟:由于数据不在缓存中,所有请求都需要... [阅读全文]
  • Redis模拟延时队列实现日程提醒的方法

    Redis模拟延时队列实现日程提醒的方法

    使用redis模拟延时队列实际上通过mq实现延时队列更加方便,只是在实际业务中种种原因导致最终选择使用redis作为该业务实现的中间件,顺便记录一下。该业务是用... [阅读全文]
  • redis分布式锁实现示例

    redis分布式锁实现示例

    1.需求我们公司想实现一个简单的分布式锁,用于服务启动初始化执行init方法的时候,只执行一次,避免重复执行加载缓存规则的代码,还有预防高并发流程发起部分,产品... [阅读全文]
  • redis延时队列的项目实践

    redis延时队列的项目实践

    引入<redisson.version>3.15.5</redisson.version> <dependency> ... [阅读全文]

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com