文件中的敏感信息明文存储¶
ID: cpp/cleartext-storage-file
Kind: path-problem
Security severity: 7.5
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-260
- external/cwe/cwe-313
Query suites:
- cpp-code-scanning.qls
- cpp-security-extended.qls
- cpp-security-and-quality.qls
以明文形式存储的敏感信息可供获得存储访问权限的攻击者获取。
建议¶
确保敏感信息在存储到文件或通过网络传输之前始终进行加密。建议在将信息放入可能在内存中可读的缓冲区之前对其进行加密。
一般来说,仅在需要以明文形式使用敏感信息时才对其进行解密。
示例¶
以下示例展示了两种在文件中存储用户凭据的方法。在 ‘BAD’ 情况下,凭据直接以明文形式存储。在 ‘GOOD’ 情况下,凭据在存储之前先进行加密。
void writeCredentials() {
char *password = "cleartext password";
FILE* file = fopen("credentials.txt", "w");
// BAD: write password to disk in cleartext
fputs(password, file);
// GOOD: encrypt password first
char *encrypted = encrypt(password);
fputs(encrypted, file);
}