前言
在测试的时候,遇到了任意文件读取,或者拿到了网站服务器的权限,进一步做信息收集的时候,我们往往会查看配置文件。因为配置文件当中很有可能存放redis、mysql、mmsql等数据库的ip、账号和密码,阿里云的aksk,或者springboot的账号密码,方便我们更进一步扩大战果。攻防演练中,数据分也是很大的占比。
但是,随着网络安全逐渐深入人心,公司也在政策的要求下,不断改变着。前几年,在application.properties当中保存的密码还是明文的,现在遇到的,几乎都是加密后的,如果找不到密钥,竹篮打水一场空。
不知道大家有没有在工作中遇到过 ENC(xbtRi4G2Ax2Gk+jh7BYs2g==)
,接下来文章就来说一下这个是什么,如何解密,以及爆破的方式。
声明:文中所涉及的技术、思路和工具仅供以安全为目的的学习交流使用,任何人不得将其用于非法用途给予盈利等目的,否则后果自行承担!
前言
jasypt
Jasypt加密场景
搭建环境
其他办法
enc解密工具
解密以及批量枚举
参考资料
前言
在测试的时候,遇到了任意文件读取,或者拿到了网站服务器的权限,进一步做信息收集的时候,我们往往会查看配置文件。因为配置文件当中很有可能存放redis、mysql、mmsql等数据库的ip、账号和密码,阿里云的aksk,或者springboot的账号密码,方便我们更进一步扩大战果。攻防演练中,数据分也是很大的占比。
但是,随着网络安全逐渐深入人心,公司也在政策的要求下,不断改变着。前几年,在application.properties当中保存的密码还是明文的,现在遇到的,几乎都是加密后的,如果找不到密钥,竹篮打水一场空。
不知道大家有没有在工作中遇到过 ENC(xbtRi4G2Ax2Gk+jh7BYs2g==)
,接下来文章就来说一下这个是什么,如何解密,以及爆破的方式。
jasypt
enc()是jasypt 默认的加密方式。
项目地址:https://github.com/ulisesbocchio/jasypt-spring-boot
Jasypt加密场景
-
System Property 系统变量
-
Envirnment Property 环境变量
-
Command Line argument 命令行参数
-
Application.properties 应用配置文件
-
Yaml properties 应用配置文件
-
other custom property sources 其它配置文件
搭建环境
maven的pom.xml中添加依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
修改配置文件
yml
jasypt:
encryptor:
# 加密的秘钥
password: key
# 加密算法
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
property:
# 算法识别的前后缀,默认ENC(),包含在前后缀的加密信息,会使用指定算法解密
prefix: Enc(
suffix: )
yaml
# jasypt
jasypt.encryptor.password=key
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.property.prefix=Enc(
jasypt.encryptor.property.suffix=)
通过类生成加密数据
代码如下
package org.example.test;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
public class JasyptUtils {
public static void main(String[] args) {
String info = encrypt("root");
System.out.println(info);
}
/**
* 加密
*
* @param plaintext 明文
* @return
*/
public static String encrypt(String plaintext) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
// 指定算法
config.setAlgorithm("PBEWithMD5AndDES");
// 指定秘钥,和yml配置文件中保持一致
config.setPassword("key");
encryptor.setConfig(config);
// 生成加密数据
return encryptor.encrypt(plaintext);
}
/**
* 解密
*
* @param data 加密后数据
* @return
*/
public static String decrypt(String data) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
config.setPassword("key");
encryptor.setConfig(config);
// 解密数据
return encryptor.decrypt(data);
}
}
把加密后的字符串复制出来 3nYis+VTpR+GQbMzwiUGrw==
因为之前的环境没有可以演示的数据库,这里用ruoyi的系统演示,若依使用的是yml文件。
替换yml中数据库的密码:Enc(3nYis+VTpR+GQbMzwiUGrw==)
很奇怪,没解密
搜了一下,发现了原因
https://cloud.tencent.com/developer/article/1040111
每个微服务都要引入?
众所周知,若依有多个pom.xml,我导入依赖是在最外面的,然后我在ruo-admin下的pom.xml中也引入依赖
若依启动成功
并且系统可以正常使用
其他办法
直接将将密钥放在了yml配置里面,安全性较低。
可以参考下面的几种方式
方法1
在启动java进程的时候,附加jasypt相关参数如下所示:
java -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=password
--jasypt.encryptor.password=密钥
--jasypt.encryptor.algorithm=PBEWithMD5AndDES
--jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator
--jasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator
IDEA可以在这里配置
方法2
采用环境变量,将密钥相关信息存储在环境变量中,然后去调用
export JASYPT_PASSWORD=你的秘钥 # 设置环境变量(仅用于演示,实际应已事先设置好)
java -jar your-spring-boot-app.jar
--jasypt.encryptor.password=${JASYPT_PASSWORD}
--jasypt.encryptor.algorithm=${JASYPT_ALGORITHM:-PBEWithMD5AndDES}
--jasypt.encryptor.iv-generator-classname=${JASYPT_IV_GENERATOR:-org.jasypt.iv.RandomIvGenerator}
--jasypt.encryptor.salt-generator-classname=${JASYPT_SALT_GENERATOR:-org.jasypt.salt.RandomSaltGenerator}
enc解密工具
DecryptTools综合解密 https://github.com/wafinfo/DecryptTools
其中有一项 springenc
解密以及批量枚举
从配置文件中找到jasypt的配置,然后根据配置写解密函数
参考环境搭建
中的代码:
package org.example.test;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JasyptUtils {
public static void main(String[] args) {
// 需要解密的数据
String info = "8NJAQh5F7zQYzaVlVA0lcw==";
// 枚举密钥
String filePath = "D:\try_test\web_test\password.txt"; // 替换为你的文件路径
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] columns = line.split(","); // 假设列是用逗号分隔的
for (String column : columns) {
lines.add(column.trim()); // 将每一列添加到列表中
}
}
} catch (IOException e) {
e.printStackTrace();
}
// 打印读取的内容
for (String line : lines) {
try {
String info_de = decrypt(info, line);
System.out.println("解密后结果: " + info_de);
System.out.println("密钥: " + line);
} catch (org.jasypt.exceptions.EncryptionOperationNotPossibleException e){
;
}
}
}
/**
* 解密
*
* @param data 加密后数据
* @return
*/
public static String decrypt(String data, String password) {
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
config.setPassword(password);
encryptor.setConfig(config);
// 解密数据
return encryptor.decrypt(data);
}
}
可能的password
salt
jasypt
password
123456
key
其他的可以参考常用密码
参考资料
SpringBoot 配置文件/属性ENC加密 https://www.cnblogs.com/ruhuanxingyun/p/12152579.html
攻防演练-实战中的外网突破 https://cloud.tencent.com/developer/article/2352206
Spring Boot集成jasypt快速入门Demo https://mp.weixin.qq.com/s/MtMTK7zaCtkrZAoMkWHU9g
敏感数据的保护伞——SpringBoot集成jasypt https://mp.weixin.qq.com/s/lMAlE3zgASRiaqz0E591bw
SpringBoot实战:jasypt实现配置文件信息加密 https://mp.weixin.qq.com/s/yG9veZQyHtB4JK0DxH08Jw
Jasypt加解密配置项 https://mp.weixin.qq.com/s/qWgDcBcNGd30e9tRP2h0rw
原文始发于微信公众号(进击的HACK):SpringBoot jasypt 配置文件/属性ENC加密
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论