forked from google/oss-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBindAuthenticatorFuzzer.java
99 lines (86 loc) · 4.4 KB
/
BindAuthenticatorFuzzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.api.FuzzerSecurityIssueHigh;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.security.ldap.authentication.BindAuthenticator;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.ldap.core.AuthenticationSource;
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
import com.unboundid.ldap.listener.InMemoryListenerConfig;
import com.unboundid.ldap.sdk.DN;
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFReader;
import org.springframework.security.ldap.server.UnboundIdContainer;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.authentication.BadCredentialsException;
public class BindAuthenticatorFuzzer {
private static InMemoryDirectoryServer directoryServer;
public static void fuzzerTearDown() {
if (directoryServer instanceof InMemoryDirectoryServer) {
directoryServer.shutDown(true);
}
}
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
String username = data.consumeString(100);
String password = data.consumeRemainingAsString();
if (username.isEmpty() || password.isEmpty() || (username.equals("admin") && password.equals("secret"))) {
return;
}
if (directoryServer instanceof InMemoryDirectoryServer) {
directoryServer.shutDown(true);
}
createInMemoryLdapServer();
DefaultSpringSecurityContextSource context = new DefaultSpringSecurityContextSource("ldap://localhost:1234/dc=springframework,dc=org");
context.setUserDn("uid=admin,ou=system");
context.setPassword("secret");
context.afterPropertiesSet();
BindAuthenticator authenticator = new BindAuthenticator(context);
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
DirContextOperations user = null;
try {
user = authenticator.authenticate(token);
} catch (BadCredentialsException e) {
// BadCredentialsException is expected here
} finally {
if (user != null) {
throw new FuzzerSecurityIssueHigh("Invalid user `" + username + "` could authenticate");
}
}
}
private static void createInMemoryLdapServer() {
String defaultPartitionName = "dc=springframework,dc=org";
try {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionName);
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", 1234));
config.setEnforceSingleStructuralObjectClass(false);
config.setEnforceAttributeSyntaxCompliance(true);
Entry dc = new Entry(new DN("dc=springframework,dc=org"));
dc.addAttribute("objectClass", "top", "domain", "extensibleObject");
dc.addAttribute("dc", "springframework");
dc.addAttribute("ou", "people");
Entry ou = new Entry(new DN("ou=people,dc=springframework,dc=org"));
ou.addAttribute("objectClass", "organizationalUnit");
ou.addAttribute("ou", "people");
Entry cn = new Entry(new DN("uid=admin,ou=people,dc=springframework,dc=org"));
cn.addAttribute("objectClass", "person");
cn.addAttribute("objectClass", "inetOrgPerson");
cn.addAttribute("cn", "Adm");
cn.addAttribute("sn", "In");
cn.addAttribute("uid", "admin");
cn.addAttribute("userPassword", "secret");
directoryServer = new InMemoryDirectoryServer(config);
directoryServer.add(dc);
directoryServer.add(ou);
directoryServer.add(cn);
directoryServer.startListening();
} catch (LDAPException e) {
e.printStackTrace();
}
}
}