使用“属性”类明文存储敏感信息¶
ID: java/cleartext-storage-in-properties
Kind: problem
Security severity: 7.5
Severity: warning
Precision: medium
Tags:
- security
- external/cwe/cwe-313
Query suites:
- java-security-extended.qls
- java-security-and-quality.qls
未加密存储的敏感信息可供获得存储访问权限的攻击者使用。
建议¶
确保敏感信息在存储前始终加密。在将信息放入堆数据结构(例如 Java.util.Properties
)之前加密信息可能是明智之举,该数据结构稍后可能会写入磁盘。可序列化或可编组的对象也应始终包含加密信息,除非你确定它们永远不会被序列化。
通常,仅在必须以明文形式使用敏感信息时对其进行解密。
示例¶
以下示例展示了两种在 cookie 中存储用户凭据的方法。在“错误”情况下,凭据仅以明文形式存储。在“正确”情况下,凭据在存储之前经过哈希处理。
public static void main(String[] args) {
{
String data;
PasswordAuthentication credentials =
new PasswordAuthentication("user", "BP@ssw0rd".toCharArray());
data = credentials.getUserName() + ":" + new String(credentials.getPassword());
// BAD: store data in a cookie in cleartext form
response.addCookie(new Cookie("auth", data));
}
{
String data;
PasswordAuthentication credentials =
new PasswordAuthentication("user", "GP@ssw0rd".toCharArray());
String salt = "ThisIsMySalt";
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.reset();
String credentialsToHash =
credentials.getUserName() + ":" + credentials.getPassword();
byte[] hashedCredsAsBytes =
messageDigest.digest((salt+credentialsToHash).getBytes("UTF-8"));
data = bytesToString(hashedCredsAsBytes);
// GOOD: store data in a cookie in encrypted form
response.addCookie(new Cookie("auth", data));
}
}
参考¶
SEI CERT Oracle Java 编码标准:SER03-J. 不要序列化未加密的敏感数据。
M. Dowd、J. McDonald 和 J. Schuhm,软件安全评估的艺术,第 1 版,第 2 章 - “加密的常见漏洞”,第 43 页。艾迪生韦斯利,2006 年。
M. Howard 和 D. LeBlanc,编写安全代码,第 2 版,第 9 章 - “保护机密数据”,第 299 页。微软,2002 年。
常见弱点枚举:CWE-313。