主页 > 网络知识 > Java代码审计入门篇:WebGoat 8(初见)(3)

Java代码审计入门篇:WebGoat 8(初见)(3)

我们需要重点关注的代码块是:

@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { //auth.userDetailsService(userDetailsService)根据userDetailsService对象,添加身份认证 auth.userDetailsService(userDetailsService); //.passwordEncoder(bCryptPasswordEncoder()); }

翻阅AuthenticationManagerBuilder相关资料:
AuthenticationManagerBuilder用于创建AuthenticationManager。 允许轻松构建内存身份验证,LDAP身份验证,基于JDBC的身份验证,添加UserDetailsService以及添加AuthenticationProvider。

基于内存身份认证:

我们可以看到,用户名密码直接写死在代码中然后运行时进入内存,当结合任意文件读取,代码泄漏等漏洞时,可获取明文密码,这种做法是不安全的。

public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").roles("USER").and() .withUser("admin").password("password").roles("USER", "ADMIN"); }

基于JDBC认证:

@Autowiredprivate DataSource dataSource; @Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSource) .withDefaultSchema() .withUser("user").password("password").roles("USER").and() .withUser("admin").password("password").roles("USER", "ADMIN"); }

基于LDAP的认证:

@Autowired private DataSource dataSource; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0},ou=people") .groupSearchBase("ou=groups"); }

基于自定义UserDetailsService认证:

由于WebGoat8就是基于自定义UserDetailsService认证,所以接下来重点关注一下这个方法。

//根据传入的自定义UserDetailsService添加身份验证。然后返回DaoAuthenticationConfigurer以允许自定义身份验证。 //此方法还确保UserDetailsService可用于getDefaultUserDetailsService()方法。 请注意,其他UserDetailsService可能会覆盖此UserDetailsService作为默认值。 public <T extends UserDetailsService> DaoAuthenticationConfigurer<AuthenticationManagerBuilder, T> userDetailsService( T userDetailsService) throws Exception { this.defaultUserDetailsService = userDetailsService; return apply(new DaoAuthenticationConfigurer<>( userDetailsService)); }

然后我们追踪userDetailsService,如下图,即追踪源码中的UserService:

 

Java代码审计入门篇:WebGoat 8(初见)

 

UserService.java:

package org.owasp.webgoat.users; import lombok.AllArgsConstructor; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import java.util.List; /** * @author nbaars * @since 3/19/17. */ @Service @AllArgsConstructor public class UserService implements UserDetailsService { private final UserRepository userRepository; private final UserTrackerRepository userTrackerRepository; @Override public WebGoatUser loadUserByUsername(String username) throws UsernameNotFoundException { WebGoatUser webGoatUser = userRepository.findByUsername(username); if (webGoatUser == null) { throw new UsernameNotFoundException("User not found"); } else { webGoatUser.createUser(); } return webGoatUser; } public void addUser(String username, String password) { userRepository.save(new WebGoatUser(username, password)); userTrackerRepository.save(new UserTracker(username)); } public void addUser(String username, String password, String role) { userRepository.save(new WebGoatUser(username,password,role)); userTrackerRepository.save(new UserTracker(username)); } public List<WebGoatUser> getAllUsers () { return userRepository.findAll(); } }
说点什么吧
  • 全部评论(0
    还没有评论,快来抢沙发吧!