|
| 1 | +/** |
| 2 | + * @name Cleartext Credentials in Properties File |
| 3 | + * @description Finds cleartext credentials in Java properties files. |
| 4 | + * @kind problem |
| 5 | + * @id java/credentials-in-properties |
| 6 | + * @tags security |
| 7 | + * external/cwe/cwe-555 |
| 8 | + * external/cwe/cwe-256 |
| 9 | + * external/cwe/cwe-260 |
| 10 | + */ |
| 11 | + |
| 12 | +/* |
| 13 | + * Note this query requires properties files to be indexed before it can produce results. |
| 14 | + * If creating your own database with the CodeQL CLI, you should run |
| 15 | + * `codeql database index-files --language=properties ...` |
| 16 | + * If using lgtm.com, you should add `properties_files: true` to the index block of your |
| 17 | + * lgtm.yml file (see https://lgtm.com/help/lgtm/java-extraction) |
| 18 | + */ |
| 19 | + |
| 20 | +import java |
| 21 | +import semmle.code.configfiles.ConfigFiles |
| 22 | + |
| 23 | +private string possibleSecretName() { |
| 24 | + result = "%password%" or |
| 25 | + result = "%passwd%" or |
| 26 | + result = "%account%" or |
| 27 | + result = "%accnt%" or |
| 28 | + result = "%credential%" or |
| 29 | + result = "%token%" or |
| 30 | + result = "%secret%" or |
| 31 | + result = "%access%key%" |
| 32 | +} |
| 33 | + |
| 34 | +private string possibleEncryptedSecretName() { |
| 35 | + result = "%hashed%" or |
| 36 | + result = "%encrypted%" or |
| 37 | + result = "%crypt%" |
| 38 | +} |
| 39 | + |
| 40 | +/** Holds if the value is not cleartext credentials. */ |
| 41 | +bindingset[value] |
| 42 | +predicate isNotCleartextCredentials(string value) { |
| 43 | + value = "" // Empty string |
| 44 | + or |
| 45 | + value.length() < 7 // Typical credentials are no less than 6 characters |
| 46 | + or |
| 47 | + value.matches("% %") // Sentences containing spaces |
| 48 | + or |
| 49 | + value.regexpMatch(".*[^a-zA-Z\\d]{3,}.*") // Contain repeated non-alphanumeric characters such as a fake password pass**** or ???? |
| 50 | + or |
| 51 | + value.matches("@%") // Starts with the "@" sign |
| 52 | + or |
| 53 | + value.regexpMatch("\\$\\{.*\\}") // Variable placeholder ${credentials} |
| 54 | + or |
| 55 | + value.matches("%=") // A basic check of encrypted credentials ending with padding characters |
| 56 | + or |
| 57 | + value.matches("ENC(%)") // Encrypted value |
| 58 | + or |
| 59 | + // Could be a message property for UI display or fake passwords, e.g. login.password_expired=Your current password has expired. |
| 60 | + value.toLowerCase().matches(possibleSecretName()) |
| 61 | +} |
| 62 | + |
| 63 | +/** The properties file with configuration key/value pairs. */ |
| 64 | +class ConfigProperties extends ConfigPair { |
| 65 | + ConfigProperties() { this.getFile().getBaseName().toLowerCase().matches("%.properties") } |
| 66 | +} |
| 67 | + |
| 68 | +/** The credentials configuration property. */ |
| 69 | +class CredentialsConfig extends ConfigProperties { |
| 70 | + CredentialsConfig() { |
| 71 | + this.getNameElement().getName().trim().toLowerCase().matches(possibleSecretName()) and |
| 72 | + not this.getNameElement().getName().trim().toLowerCase().matches(possibleEncryptedSecretName()) |
| 73 | + } |
| 74 | + |
| 75 | + string getName() { result = this.getNameElement().getName().trim() } |
| 76 | + |
| 77 | + string getValue() { result = this.getValueElement().getValue().trim() } |
| 78 | +} |
| 79 | + |
| 80 | +from CredentialsConfig cc |
| 81 | +where not isNotCleartextCredentials(cc.getValue()) |
| 82 | +select cc, |
| 83 | + "Plaintext credentials " + cc.getName() + " have cleartext value " + cc.getValue() + |
| 84 | + " in properties file." |
0 commit comments