Spring Apache Shiro 默认密钥致命令执行漏洞及解决方案

最近阿里云发了漏洞短信,需要在以后的老项目中修复漏洞,不同项目修复方式有所不同

漏洞检测工具
下载地址:https://xz.aliyun.com/forum/upload/affix/shiro_tool.zip
如图:

OK,检测下漏洞:


这样,我们看到了,检测到了使用默认的shiro密钥


解决方案:
每个项目的情况都可能不一样,所以有不同的解决办法。
现象:
使用的是springmvc,shiro1.2.4,spring 4.2.5

shiro默认的安全管理器使用了默认的shiro密钥,即使项目中开发者不配置,那么shiro也会加载。
既然开发人员没有配置,那么我们就配置下。

创建一个密钥生成的类


配置中使用


需要注意
在阿里云给的建议中,需要升级shiro版本1.7,但是此版本需要升级spring,这块可能会出现修改其他一些功能和代码问题。
所以,在此,我们不升级shiro,还是用老版本,只是增加密钥的管理方式,不再用默认密钥即可

修改后检测


至此,问题解决,希望对看见的开发者有帮助。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 Apache Shiro 加解密 Spring Boot 配置文件中的敏感信息的示例: 1. 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.7.1</version> </dependency> ``` 2. 在 Spring Boot 的配置文件中添加以下配置: ```yaml # 加密盐,可以自己定义 shiro.encryptSalt: mySalt # 需要加密的属性值 my.encrypted.property: ENC(7M9iu9qgMjG5+yMm5JLc3w==) ``` 3. 新建一个加解密工具类,例如: ```java import org.apache.shiro.codec.Base64; import org.apache.shiro.codec.Hex; import org.apache.shiro.crypto.AesCipherService; import org.apache.shiro.crypto.hash.Md5Hash; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.nio.charset.StandardCharsets; @Component public class ShiroEncryptUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ShiroEncryptUtils.class); @Value("${shiro.encryptSalt}") private String encryptSalt; private final AesCipherService aesCipherService; @Autowired public ShiroEncryptUtils(AesCipherService aesCipherService) { this.aesCipherService = aesCipherService; } /** * 加密 * * @param plaintext 明文 * @return 密文 */ public String encrypt(String plaintext) { // 加密密钥 byte[] key = new Md5Hash(encryptSalt.getBytes(StandardCharsets.UTF_8)).getBytes(); // 生成随机向量 byte[] iv = aesCipherService.generateNewInitializationVector(); // 加密 byte[] encrypted = aesCipherService.encrypt(plaintext.getBytes(StandardCharsets.UTF_8), key, iv); // 将向量和密文转换为十六进制字符串 String ivHex = Hex.encodeToString(iv); String encryptedHex = Hex.encodeToString(encrypted); return "ENC(" + ivHex + encryptedHex + ")"; } /** * 解密 * * @param ciphertext 密文 * @return 明文 */ public String decrypt(String ciphertext) { if (ciphertext == null || !ciphertext.startsWith("ENC(") || !ciphertext.endsWith(")")) { LOGGER.warn("Invalid ciphertext: {}", ciphertext); return ""; } String encryptedString = ciphertext.substring(4, ciphertext.length() - 1); // 解密密钥 byte[] key = new Md5Hash(encryptSalt.getBytes(StandardCharsets.UTF_8)).getBytes(); // 从密文中提取向量和密文 byte[] iv = Hex.decode(encryptedString.substring(0, 32)); byte[] encrypted = Hex.decode(encryptedString.substring(32)); // 解密 byte[] decrypted = aesCipherService.decrypt(encrypted, key, iv); return new String(decrypted, StandardCharsets.UTF_8); } /** * Base64 编码 * * @param plaintext 明文 * @return Base64 编码后的字符串 */ public String encodeBase64(String plaintext) { return Base64.encodeToString(plaintext.getBytes(StandardCharsets.UTF_8)); } /** * Base64 解码 * * @param ciphertext Base64 编码后的字符串 * @return 明文 */ public String decodeBase64(String ciphertext) { return new String(Base64.decode(ciphertext), StandardCharsets.UTF_8); } } ``` 4. 在需要解密的属性上添加 `@Value` 注解和 `@PostConstruct` 注解。例如: ```java @Component public class MyComponent { @Value("${my.encrypted.property}") private String encryptedProperty; @Autowired private ShiroEncryptUtils shiroEncryptUtils; private String decryptedProperty; @PostConstruct public void init() { decryptedProperty = shiroEncryptUtils.decrypt(encryptedProperty); } // ... } ``` 通过以上代码,我们可以使用 Apache Shiro 安全地加解密 Spring Boot 配置文件中的敏感信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值