一、spring security 简介
spring security 的核心功能主要包括:
-
认证 (你是谁) -
授权 (你能干什么) -
攻击防护 (防止伪造身份)
.antMatchers("/admin").hasRole("ADMIN")
:仅允许ADMIN
角色的用户访问/admin
。.antMatchers("/**").permitAll()
:允许所有用户访问应用中的所有其他路径。
@EnableWebSecurity
@Configuration
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 配置从内存中加载认证信息
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("admin")
.password(passwordEncoder.encode("123456")) // 使用加密后的密码
.roles("ADMIN")
.and()
.withUser("user")
.password(passwordEncoder.encode("123456")) // 使用加密后的密码
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin").hasRole("ADMIN")
.antMatchers("/user").hasRole("USER")
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin() // 使用 Spring Security 默认的登录页面
.and()
.httpBasic();
}
}
全部接口分为下面四个:
/admin
/admin/work
/user
/user/wrok
实战绕过
/admin 需要登录
/admin? 绕过失败-需要登录
/admin/ 绕过成功
/admin/work 绕过成功
/admin/??? 绕过成功
其他路径测试
对于/user/和/user/work也可以直接访问的,因为本身也做了路径匹配。
但是对于没有做路径匹配的,也可以直接访问!
修复方案:
mvcMatchers("/admin").access("hasRole('ADMIN')")
或者使用
antMatchers("/admin/**").access("hasRole('ADMIN')")
写法防止认证绕过。
.regexMatchers("/admin").access("hasRole('ADMIN')")
-
- 这条规则要求访问
/admin
路径的请求必须具有ADMIN
角色。
- 这条规则要求访问
.antMatchers("/**").access("anonymous")
-
- 这条规则允许所有请求通过,不需要身份验证,即匿名用户可以访问所有路径。
anonymous
允许未登录的用户访问所有路径,这意味着没有进行身份验证的用户也能访问所有的请求。
@EnableWebSecurity
@Configuration
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 配置从内存中加载认证信息
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("admin")
.password(passwordEncoder.encode("123456")) // 使用加密后的密码
.roles("ADMIN")
.and()
.withUser("user")
.password(passwordEncoder.encode("123456")) // 使用加密后的密码
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.regexMatchers("/admin").access("hasRole('ADMIN')")
.antMatchers("/**").access("anonymous")
.and()
.formLogin() // 使用 Spring Security 默认的登录页面
.and()
.httpBasic();
}
}
全部接口分为下面四个:
/admin
/admin/work
/user
/user/wrok
绕过测试
/admin 需要登录
/admin; 报错
/admin;css 报错
/admin/ 直接绕过鉴权
/admin? 直接绕过鉴权
非设置路径,可以直接全部访问
如果不存的话会报错
总结
修复方案
Matchers没使用类似/test.*
的方式,在传入/test?
时候,正则会匹配不上,不会命中/test
的规则。
.regexMatchers("/test.*?").access("hasRole('ADMIN')")
useSuffixPatternMatch 绕过
这个是一个spring-webmvc低版本造成的漏洞,在低版本中
useSuffixPatternMatch
配置默认值为true
,表示使用后缀匹配模式匹配路径。
如/path/abc
路由也会允许/path/abc.xxx
、/path/abc.asas
等增加.xxx
后缀形式的路径匹配成功。
需要满足条件:springboot <=1.5.22.RELEASE
对应的mvc版本,即为<=4.3.25
实战测试
修改springboot的版本为1.5.22.RELEASE,这样对应的spring-web的mvc版本就为4.3.25, 是存在这个漏洞的。
这里注意,修改springboot的版本,相关是spring组件版本都会进行修改,这里就是降低,并且springboot 1.x 只能低版本jdk运行,记得修改为jdk8!
全部接口分为下面四个:
/admin
/admin/work
/user
/user/wrok
regexMatchers无漏洞写法配置测试-绕过失败
就是这种配置呢,就是本身不存在漏洞的,就是正确写法,但是因为Spring MVC版本在<=4.3.25导致存在了漏洞,下面进行验证猜测。
/admin/和/admin?和/index/../admin/ 都无法绕过了!!!
但是/admin.sdasd也不能绕过。。。
服了,还是得添加上那个,其他路径可以通过的代码。
结果还是无法绕过!!!
/admin/和/admin?和/index/../admin/ 都无法绕过了!!!
/admin.dasda失败!
antMatchers无漏洞写法配置测试-绕过成功
这里改写成使用antMatchers进行权限的判定,当然,下面的配置也是正确写法。
/admin/ /admin? /admin/work 全部失败
唯独!!!
http://127.0.0.1:8082/admin.dasda绕过成功 !!!(利用漏洞成功,在后面添加任意后缀进行绕过)
总结
如果Spring MVC版本在<=4.3.25,这种情况下,如果后端是使用的antMatchers进行匹配,无论是写的防护多好,都会存在漏洞!如果是regexMatchers无漏洞写法,就没法绕过!!
版本问题,可以直接看Spring MVC版本,也可也看springboot的版本springboot <= 1.5.22.RELEASE也行!
原文始发于微信公众号(进击的HACK):SpringSecurity权限绕过漏洞-好玩
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论