2-SpringSecurity:CSRF攻击

  • 再发POST /ok,403了。。

2020-12-08-SpringSecurityPOST.png

那么,问题来了,两个请求都是在登录状态下进行的,为什么GET成功,POST返回403了?

其实SpringSecurity默认就开启了CSRF防护,这在上一篇及官网中关于SpringBoot自动配置项那里可以看到。并且SpringSecurity默认忽略"GET", “HEAD”, “TRACE”, "OPTIONS"等请求,源码如下:

/**

  • Specify the {@link RequestMatcher} to use for determining when CSRF should be

  • applied. The default is to ignore GET, HEAD, TRACE, OPTIONS and process all other

  • requests.

  • @param requireCsrfProtectionMatcher the {@link RequestMatcher} to use

  • @return the {@link CsrfConfigurer} for further customizations

*/

public CsrfConfigurer requireCsrfProtectionMatcher(

RequestMatcher requireCsrfProtectionMatcher) {

Assert.notNull(requireCsrfProtectionMatcher,

“requireCsrfProtectionMatcher cannot be null”);

this.requireCsrfProtectionMatcher = requireCsrfProtectionMatcher;

return this;

}

private static final class DefaultRequiresCsrfMatcher implements RequestMatcher {

private final HashSet allowedMethods = new HashSet<>(

Arrays.asList(“GET”, “HEAD”, “TRACE”, “OPTIONS”));

/*

  • (non-Javadoc)

  • @see

  • org.springframework.security.web.util.matcher.RequestMatcher#matches(javax.

  • servlet.http.HttpServletRequest)

*/

@Override

public boolean matches(HttpServletRequest request) {

return !this.allowedMethods.contains(request.getMethod());

}

}

实验1:CSRF GET攻击

CSRF: Cross-Site Request Forgery 跨站请求伪造。一些网站比如知乎、简书中的外部链接,点击之后会有提示(免责声明),此操作有风险是否继续,这便与CSRF密切相关。

2020-12-08-SpringSecurityJump.png

结合上篇文章,新建一个SpringBoot项目,起名spring-security-csrf,核心依赖为WebThymeleaf,模拟一个钓鱼网站。

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-devtools

runtime

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

建好项目后,创建一个简单的HelloController.java,包含一个/GET请求,返回一个页面index.html:

  • 后端接口

@Controller

public class HelloController {

@RequestMapping(“/”)

public String hello(){

return “index”;

}

}

  • 前端模板
Title

Fishing:

Note:

  • 以下步骤在Firefox浏览器完成;

  • 修改springboot-security的后端接口,增加输出打印,方便后续确定请求是否进入后端;

@RestController

@Slf4j

public class HelloController {

@GetMapping(“/hello”)

public String hello(){

log.info("Hello ");

return “hello”;

}

@PostMapping(“/ok”)

public String ok(){

log.info(“ok”);

return “ok”;

}

}

  • 前提:为了模拟不同域名下的请求(即CSRF),我们在本地的hosts文件添加如下内容:

127.0.0.1 hello

127.0.0.1 world

实验步骤:

  1. 启动两个项目:springboot-security在8080端口、spring-security-csrf在8081端口;

  2. 打开浏览器,访问http://hello:8080,并完成登录,访问https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=http%3A%2F%2Fhello%3A8080%2Fhello&pos_id=img-g1cJRUXh-1737330358212)接口,观察项目springboot-security后台打印输出;

  3. 在同一个浏览器,访问http://world:8081,默认进入index.html,同时观察项目springboot-security后台打印输出;

实验结果:

  1. 在同一个浏览器(此处为Firefox,Chrome内核的浏览器未成功)的不同Tab下,在world域名下请求hello域名下的GET接口/hello:<img src="https://img-home.csdnimg.cn/images/20230724024159.png?origin_url=http%3A%2F%2Fhello%3A8080%2Fhello&pos_id=img-g1cJRUXh-1737330358212)">,请求成功到达源站后端,实现了CSRF:跨站请求伪造。

  2. 验证了SpringSecurity虽然默认开启CSRF防护,但是幂等请求诸如"GET", “HEAD”, “TRACE”, "OPTIONS"被忽略。

实验2:CSRF POST攻击

spring-security-csrf项目中模拟一个按钮操作,发起POST请求,这里采用原生JavaScript发起AjaxPOST请求:http://hello:8080/ok

Title

Fishing:

实验步骤:

  1. 启动两个项目:springboot-security在8080端口、spring-security-csrf在8081端口;

  2. 打开浏览器,访问http://hello:8080,并完成登录,PostMan访问http://hello:8080/ok接口,观察项目springboot-security后台打印输出;

  3. 在同一个浏览器,访问http://world:8081,默认进入index.html,同时观察项目springboot-security后台打印输出;

2020-12-08-SpringSecurityCSRF.png

实验结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值