敏感信息的明文传输¶
ID: cpp/cleartext-transmission
Kind: path-problem
Security severity: 7.5
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-319
- external/cwe/cwe-359
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);
}