|
| 1 | +/** Provides classes to reason about insecure LDAP authentication. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.dataflow.DataFlow |
| 5 | +private import semmle.code.java.frameworks.Networking |
| 6 | +private import semmle.code.java.frameworks.Jndi |
| 7 | + |
| 8 | +/** |
| 9 | + * An expression that represents an insecure (non-SSL, non-private) LDAP URL. |
| 10 | + */ |
| 11 | +class InsecureLdapUrl extends Expr { |
| 12 | + InsecureLdapUrl() { |
| 13 | + this instanceof InsecureLdapUrlLiteral |
| 14 | + or |
| 15 | + // Concatentation of insecure protcol and non-private host: |
| 16 | + // protocol + host + ... |
| 17 | + exists(AddExpr e, CompileTimeConstantExpr protocol, Expr rest, Expr host | |
| 18 | + e = this and |
| 19 | + protocol = e.getLeftOperand() and |
| 20 | + rest = e.getRightOperand() and |
| 21 | + if rest instanceof AddExpr then host = rest.(AddExpr).getLeftOperand() else host = rest |
| 22 | + | |
| 23 | + protocol.getStringValue() = "ldap://" and |
| 24 | + not exists(string hostString | hostString = getHostname(host) | |
| 25 | + hostString.length() = 0 or // Empty host is loopback address |
| 26 | + hostString instanceof PrivateHostName |
| 27 | + ) |
| 28 | + ) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * A sink representing the construction of a `DirContextEnvironment`. |
| 34 | + */ |
| 35 | +class InsecureLdapUrlSink extends DataFlow::Node { |
| 36 | + InsecureLdapUrlSink() { |
| 37 | + exists(ConstructorCall cc | |
| 38 | + cc.getConstructedType().getAnAncestor() instanceof TypeDirContext and |
| 39 | + this.asExpr() = cc.getArgument(0) |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Holds if `ma` sets `java.naming.security.authentication` (also known as `Context.SECURITY_AUTHENTICATION`) to `simple` in some `Hashtable`. |
| 46 | + */ |
| 47 | +predicate isBasicAuthEnv(MethodAccess ma) { |
| 48 | + hasFieldValueEnv(ma, "java.naming.security.authentication", "simple") or |
| 49 | + hasFieldNameEnv(ma, "SECURITY_AUTHENTICATION", "simple") |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Holds if `ma` sets `java.naming.security.protocol` (also known as `Context.SECURITY_PROTOCOL`) to `ssl` in some `Hashtable`. |
| 54 | + */ |
| 55 | +predicate isSslEnv(MethodAccess ma) { |
| 56 | + hasFieldValueEnv(ma, "java.naming.security.protocol", "ssl") or |
| 57 | + hasFieldNameEnv(ma, "SECURITY_PROTOCOL", "ssl") |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Holds if `ma` writes the `java.naming.provider.url` (also known as `Context.PROVIDER_URL`) key of a `Hashtable`. |
| 62 | + */ |
| 63 | +predicate isProviderUrlSetter(MethodAccess ma) { |
| 64 | + ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and |
| 65 | + ma.getMethod().hasName(["put", "setProperty"]) and |
| 66 | + ( |
| 67 | + ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "java.naming.provider.url" |
| 68 | + or |
| 69 | + exists(Field f | |
| 70 | + ma.getArgument(0) = f.getAnAccess() and |
| 71 | + f.hasName("PROVIDER_URL") and |
| 72 | + f.getDeclaringType() instanceof TypeNamingContext |
| 73 | + ) |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * An insecure (non-SSL, non-private) LDAP URL string literal. |
| 79 | + */ |
| 80 | +private class InsecureLdapUrlLiteral extends StringLiteral { |
| 81 | + InsecureLdapUrlLiteral() { |
| 82 | + // Match connection strings with the LDAP protocol and without private IP addresses to reduce false positives. |
| 83 | + exists(string s | this.getValue() = s | |
| 84 | + s.regexpMatch("(?i)ldap://[\\[a-zA-Z0-9].*") and |
| 85 | + not s.substring(7, s.length()) instanceof PrivateHostName |
| 86 | + ) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** The class `java.util.Hashtable`. */ |
| 91 | +private class TypeHashtable extends Class { |
| 92 | + TypeHashtable() { this.getSourceDeclaration().hasQualifiedName("java.util", "Hashtable") } |
| 93 | +} |
| 94 | + |
| 95 | +/** Get the string value of an expression representing a hostname. */ |
| 96 | +private string getHostname(Expr expr) { |
| 97 | + result = expr.(CompileTimeConstantExpr).getStringValue() or |
| 98 | + result = |
| 99 | + expr.(VarAccess).getVariable().getAnAssignedValue().(CompileTimeConstantExpr).getStringValue() |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Holds if `ma` sets `fieldValue` to `envValue` in some `Hashtable`. |
| 104 | + */ |
| 105 | +bindingset[fieldValue, envValue] |
| 106 | +private predicate hasFieldValueEnv(MethodAccess ma, string fieldValue, string envValue) { |
| 107 | + // environment.put("java.naming.security.authentication", "simple") |
| 108 | + ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and |
| 109 | + ma.getMethod().hasName(["put", "setProperty"]) and |
| 110 | + ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = fieldValue and |
| 111 | + ma.getArgument(1).(CompileTimeConstantExpr).getStringValue() = envValue |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Holds if `ma` sets attribute name `fieldName` to `envValue` in some `Hashtable`. |
| 116 | + */ |
| 117 | +bindingset[fieldName, envValue] |
| 118 | +private predicate hasFieldNameEnv(MethodAccess ma, string fieldName, string envValue) { |
| 119 | + // environment.put(Context.SECURITY_AUTHENTICATION, "simple") |
| 120 | + ma.getMethod().getDeclaringType().getAnAncestor() instanceof TypeHashtable and |
| 121 | + ma.getMethod().hasName(["put", "setProperty"]) and |
| 122 | + exists(Field f | |
| 123 | + ma.getArgument(0) = f.getAnAccess() and |
| 124 | + f.hasName(fieldName) and |
| 125 | + f.getDeclaringType() instanceof TypeNamingContext |
| 126 | + ) and |
| 127 | + ma.getArgument(1).(CompileTimeConstantExpr).getStringValue() = envValue |
| 128 | +} |
0 commit comments