注:框架是Struts2+Spring+ibatis
漏洞代码
jdbc.url=jdbc:db2://172.17.33.118:50000/qjxt
jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
jdbc.username=db2admin
jdbc.password=db2admin
漏洞说明
在配置文件中存储明文密码。
解决方案
密码模糊化,并把模糊化资源分散到系统各处。
采用加密算法AES级别以上。
自定义密码机制。
将jdbc.password的值使用AES算法加密
public class SecurityUtil {
/**
* 加密算法名称AES
*/
private static final String ENCRYPTION_ALGORITHM_NAME = "AES";
/**
* 字符编码
*/
private static final String CHARACTER_ENCODING = "UTF-8";
public static void main(String[] args) {
String str = "admin";
System.out.println("明文:" + str);
// 生成**
String keyStr = "jsepc01!";
System.out.println("**:" + keyStr);
SecretKey key = SecurityUtil.getKey(keyStr);
// 加密
String miwen = SecurityUtil.encryption(str, key);
System.out.println("密文:" + miwen);
// 解密
String returnStr = SecurityUtil.decryption("3k5KAxN2D7k9QvYUu80kIA==", key);
System.out.println("解密后:" + returnStr);
}
private static SecretKeySpec getKey(String key){
try {
byte[] arrTemp = key.getBytes(CHARACTER_ENCODING);
byte[] arr = new byte[16];
for (int i = 0; i < arrTemp.length; i++) {
arr[i] = arrTemp[i];
}
SecretKeySpec keySpec = new SecretKeySpec(arr, ENCRYPTION_ALGORITHM_NAME);
return keySpec;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 加密
*
* @param str
* 明文
* @param key
* 秘钥
* @return 密文
*/
private static String encryption(String str, SecretKey key) {
try {
// 创建密码器
Cipher cp = Cipher.getInstance(ENCRYPTION_ALGORITHM_NAME);
// 设置加密模式
cp.init(Cipher.ENCRYPT_MODE, key);
byte[] strs = str.getBytes(CHARACTER_ENCODING);
// 加密
byte[] cipherStrs = cp.doFinal(strs);
String cipherStr = new BASE64Encoder().encode(cipherStrs);
return cipherStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
1.用密文替换原有的明文(properties文件中jdbc.password的value值)
2.创建MyPropertiesPersist类,给password解密,如图
3.在applicationcontext中配置信息 具体如下
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
<property name="propertiesPersister">
<bean class="coml.MyPropertiesPersist"></bean>
</property>
<property name ="ignoreResourceNotFound" value="true"/>
</bean>