1. 漏洞简介

  • 本次复现使用的漏洞环境:Apache Shiro 3.0.0

  • 关键部署前置条件有三个:

    • 运行在 JDK 17 上,容器为 Tomcat 11(Servlet 6.1,shiro-web 3.0.0 的编译基线)

    • 在ini文件手动显式配置 Realm 为 org.apache.shiro.realm.ldap.DefaultLdapRealm,通过 userDnTemplate=uid={0},ou=people,dc=example,dc=com 与 contextFactory.url=ldap://127.0.0.1:11389 对接真实 LDAP 目录;显式开启认证缓存 authenticationCachingEnabled=true

    • 显式配置 MemoryConstrainedCacheManager(该开关默认关闭)。

  • 漏洞影响范围:Shiro >= 0, < 3.0.1,即官方公告所称 all Apache Shiro versions through 3.0.0

  • 该漏洞属于认证绕过:Shiro 3.0.0 的 DefaultLdapRealm 构造函数安装了恒真的 AllowAllCredentialsMatcher,认证缓存命中后不再执行 LDAP 绑定,攻击者只要知道一个曾登录过、未登出的用户名并提交任意口令,即可取得该用户的已认证会话

2. 漏洞复现

  1. 使用错误密码alice/wrongPassword登录,无法登录

    不能访问受保护页面/admin,会跳转到登录页面

  2. 模拟受害者用正确口令alice/alice123登录一次

  3. 用另一个浏览器模拟攻击者用错误密码alice/wrongPassword登录

    可以正常访问受保护页面

3. 代码分析

3.1 请求参数传入

3.2 参数处理(受害者使用正确的账号密码登录)

一直跟进到ModularRealmAuthenticator#doAuthenticate,realms集合中存放了在shiro.ini设置的org.apache.shiro.realm.ldap.DefaultLdapRealm,因为集合大小为1,直接取出DefaultLdapRealm,并调用doSingleRealmAuthentication()方法

跟进,调用 Realm.getAuthenticationInfo

实际执行的是 AuthenticatingRealm#getAuthenticationInfo(final 方法):

  1. 先调用this.getCachedAuthenticationInfo()

    缓存 key 由 this.getAuthenticationCacheKey() 给出,就是原始用户名字符串alice
    因为第一次登录,没有缓存的身份验证信息,所以返回的info为null

  2. info为null时走 DefaultLdapRealm#doGetAuthenticationInfo → DefaultLdapRealm#queryForAuthenticationInfo() → ldapContextFactory.getLdapContext()

  • 这里拿「用户 DN + 提交的口令」向 LDAP 目录发起一次 simple bind,由目录服务器判定这组凭据对不对,口令正确时返回一个已绑定身份的连接,口令错误时抛出javax.naming.AuthenticationException

  • bind 成功后 createAuthenticationInfo 返回 SimpleAuthenticationInfo 对象

  1. cacheAuthenticationInfoIfPossible 以用户名为 key 把它放进缓存——缓存条目里带着受害者登录时的正确口令

    缓存是否启用由 isAuthenticationCachingEnabled() 决定

    cachingEnabled 默认设置为true

    所以要在 shiro.ini 设置该 Realm 的 authenticationCachingEnabled 为 true 才能启用缓存

3.3 漏洞触发点(攻击者使用任意口令登录)

触发点有两层

第一层在 getAuthenticationInfo 的分支:因为传入的账号还是alice,匹配到key,缓存命中,不仅拿到缓存的账号密码,而且info != null,doGetAuthenticationInfo(token) 根本不会被调用,queryForAuthenticationInfo 与其中的 LDAP bind 也就整段跳过——而这个 Realm 设计上把口令校验完全交给了 LDAP bind

第二层在紧随其后的 assertCredentialsMatch:

继续跟进assertCredentialsMatchWithoutException

在DefaultLdapRealm 的构造函数中设置了CredentialsMatcher为AllowAllCredentialsMatcher,所以这里返回的是AllowAllCredentialsMatcher

然后调用AllowAllCredentialsMatcher#doCredentialsMatch(),恒真

导致assertCredentialsMatch 没有抛出凭据错误异常IncorrectCredentialsException ,最后直接返回的info中带有正确账号密码,然后得到已认证的会话

3.4 补充

如果受害者登出账号,会依次调用下面的方法清除缓存

CachingRealm#onLogout → clearCache → AuthenticatingRealm#doClearCache → clearCachedAuthenticationInfo → Cache.remove(getAuthenticationCacheKey(principals))

4. 漏洞修复

4.1 临时缓解

不改代码时,最直接的是关掉认证缓存,让每次登录都回到 LDAP bind:

ldapRealm.authenticationCachingEnabled=false

如果必须保留缓存,就给 Realm 换一个有实际比较逻辑的 CredentialsMatcher:org.apache.shiro.authc.credential.SimpleCredentialsMatcher

或在代码里对 Realm 调用 setCredentialsMatcher(new SimpleCredentialsMatcher())。前者消除”跳过 bind”的路径,后者让缓存命中后仍有真实口令比对

4.2 官方修复

修复版本 3.0.1

https://github.com/apache/shiro/commit/c3cfcf752d784444497aa60abd305aa135329c5b

改为使用 SimpleCredentialsMatcher ,缓存命中时 assertCredentialsMatch 用缓存条目里的 credentials(受害者登录时的正确口令)与提交值比较