java代码审计-CSRF

发布时间 2023-03-29 00:52:30作者: Ne2o1

0x01 前言

CSRF跨站请求伪造(Cross-site request forgery),当某个接口没有设置CSRF验证,点击了别人恶意的链接,可能会造成对这个接口发送相应的数据,造成某个数据被更改。常发生在转帐、修改密码等敏感操作中。

0x02 GET型

利用十分简单,构造一个IMG标签,加载的时候即可发送一个恶意
get请求(可以和xss联合使用,也可以是有钓鱼,诱骗的方式让其
点击get请求链接)
<img src=https://xxx.cn/csrf?xx=11 />

0x03 POST型

controller/CSRF.java

@GetMapping("/")
    public String index() {
    return "form";
}

@PostMapping("/post")
    @ResponseBody
    public String post() {
    return "CSRF passed.";
}

前端提交数据页面(spring框架中调用_csrf.parameterName方法可以有效防止csrf)

<div>
  <!-- th:action with Spring 3.2+ and Thymeleaf 2.1+ can automatically force Thymeleaf to include the CSRF token as a hidden field -->
  <!-- <form name="f" th:action="@{/csrf/post}" method="post"> -->
  <form name="f" action="/csrf/post" method="post">
    <input type="text" name="input" />
    <input type="submit" value="Submit" />
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
  </form>
</div>

正常提交携带token值则能够通过请求
image.png

不提交token验证上面防护,这个字段就是用来表单提交,POST请求验证的csrf_token,后端生成的,提交后会和后端校验。如果我们直接通过POSTMAN或者其他post请求,缺少了csrf的token是无法完成的。如图
image.png

0x04 审计方法

审计前端html、jsp等前端页面,在提交表单时是否有token(隐藏属性)