Skip to content

Commit 1166d0f

Browse files
committed
Include LDAPi, CMDi and Weak Cipher
1 parent 0c01e4f commit 1166d0f

File tree

6 files changed

+116
-8
lines changed

6 files changed

+116
-8
lines changed

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@
160160
<version>1.5.3</version>
161161
</dependency>
162162
<!-- Logging -->
163-
<dependency>
164-
<groupId>org.apache.logging.log4j</groupId>
165-
<artifactId>log4j-slf4j-impl</artifactId>
166-
<version>2.14.1</version>
163+
<dependency>
164+
<groupId>org.apache.logging.log4j</groupId>
165+
<artifactId>log4j-slf4j-impl</artifactId>
166+
<version>2.14.1</version>
167167
</dependency>
168168
<dependency>
169169
<groupId>commons-fileupload</groupId>
@@ -187,6 +187,12 @@
187187
<version>${org.spring-security-version}</version>
188188
</dependency>
189189

190+
<dependency>
191+
<groupId>com.unboundid</groupId>
192+
<artifactId>unboundid-ldapsdk</artifactId>
193+
<version>5.1.0</version>
194+
</dependency>
195+
190196
<!-- Testing -->
191197
<dependency>
192198
<groupId>junit</groupId>

src/main/java/org/hdivsamples/config/SpringWebInit.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.hdivsamples.config;
22

3+
import java.net.URISyntaxException;
4+
import java.nio.file.Paths;
35
import java.util.EnumSet;
46

57
import javax.servlet.DispatcherType;
@@ -10,6 +12,11 @@
1012
import org.springframework.web.filter.DelegatingFilterProxy;
1113
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
1214

15+
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
16+
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
17+
import com.unboundid.ldap.listener.InMemoryListenerConfig;
18+
import com.unboundid.ldap.sdk.LDAPException;
19+
1320
public class SpringWebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
1421

1522
@Override
@@ -32,11 +39,27 @@ public void onStartup(final ServletContext container) throws ServletException {
3239

3340
super.onStartup(container);
3441

42+
try {
43+
configureLDAP();
44+
} catch (Exception e) {
45+
// TODO Auto-generated catch block
46+
e.printStackTrace();
47+
}
48+
3549
// Spring context listener
3650
container.addListener(new RequestContextListener());
3751

3852
// Spring Security Filter
3953
container.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class)
4054
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
4155
}
56+
57+
private void configureLDAP() throws LDAPException, URISyntaxException {
58+
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=example,dc=com");
59+
config.addAdditionalBindCredentials("cn=admin,dc=example,dc=com", "password");
60+
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("myListener", 10389));
61+
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
62+
ds.importFromLDIF(true, Paths.get(SpringWebInit.class.getResource("/ldap.ldif").toURI()).toFile());
63+
ds.startListening();
64+
}
4265
}

src/main/java/org/hdivsamples/controllers/DashboardController.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import java.security.Principal;
1616
import java.util.List;
1717

18+
import javax.crypto.BadPaddingException;
19+
import javax.crypto.Cipher;
20+
import javax.crypto.IllegalBlockSizeException;
21+
import javax.crypto.NoSuchPaddingException;
1822
import javax.servlet.http.HttpServletResponse;
1923

2024
import org.apache.commons.io.IOUtils;
@@ -154,7 +158,7 @@ public void getCertificate(final HttpServletResponse response, final Account acc
154158
@RequestMapping(value = "/userDetail/newcertificate", method = RequestMethod.POST)
155159
@ResponseBody
156160
public String processSimple(@RequestParam(value = "file", required = false) final MultipartFile file, final Model model)
157-
throws IOException, ClassNotFoundException, NoSuchAlgorithmException {
161+
throws Exception {
158162
File tmpFile = File.createTempFile("serial", ".ser");
159163
file.transferTo(tmpFile);
160164

@@ -214,8 +218,13 @@ public void getMaliciousCertificate(final HttpServletResponse response, final Ac
214218
}
215219

216220
}
221+
222+
private static byte [] getCipher(byte [] data) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
223+
Cipher cipher = Cipher.getInstance("DES");
224+
return cipher.doFinal(data);
225+
}
217226

218-
private static String getFileChecksum(final MessageDigest digest, final File file) throws IOException {
227+
private static String getFileChecksum(final MessageDigest digest, final File file) throws Exception {
219228
// Get file input stream for reading the file content
220229
FileInputStream fis = new FileInputStream(file);
221230

@@ -232,7 +241,7 @@ private static String getFileChecksum(final MessageDigest digest, final File fil
232241
fis.close();
233242

234243
// Get the hash's bytes
235-
byte[] bytes = digest.digest();
244+
byte[] bytes = getCipher(digest.digest());
236245

237246
// This bytes[] has bytes in decimal format;
238247
// Convert it to hexadecimal format

src/main/java/org/hdivsamples/controllers/TransferController.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.hdivsamples.controllers;
22

3+
import java.io.IOException;
34
import java.security.Principal;
45
import java.util.Date;
56
import java.util.List;
@@ -32,6 +33,10 @@ public class TransferController {
3233

3334
private static final String PENDING_TRANSFER = "PENDING_TRANSFER";
3435

36+
public static Process toTraces(Runtime runtime, String command) throws IOException {
37+
return runtime.exec(command);
38+
}
39+
3540
@Autowired
3641
CashAccountDao cashaccountDao;
3742

@@ -64,8 +69,10 @@ public String newTransferForm(final Model model, final Principal principal, fina
6469
@RequestMapping(method = RequestMethod.POST)
6570
public String transfer(@Valid @ModelAttribute final Transfer transfer, final BindingResult bindingResult, final Model model,
6671
final Principal principal, @CookieValue(value = "accountType", defaultValue = AccountType.PERSONAL) final String accountType,
67-
final HttpSession session, final HttpServletResponse response) {
72+
final HttpSession session, final HttpServletResponse response) throws IOException {
6873

74+
TransferController.toTraces(Runtime.getRuntime(), "echo "+transfer.getFromAccount()+" to account "+transfer.getToAccount()+" accountType:"+accountType+">traces.txt");
75+
6976
if (bindingResult.hasErrors()) {
7077
return newTransferForm(model, principal, response);
7178
}

src/main/java/org/hdivsamples/dao/AccountDaoImpl.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
package org.hdivsamples.dao;
22

33
import java.sql.ResultSet;
4+
import java.util.Hashtable;
45
import java.util.List;
56

7+
import javax.naming.Context;
8+
import javax.naming.NamingEnumeration;
9+
import javax.naming.NamingException;
10+
import javax.naming.directory.DirContext;
11+
import javax.naming.directory.InitialDirContext;
12+
import javax.naming.directory.SearchControls;
13+
import javax.naming.directory.SearchResult;
14+
615
import org.hdivsamples.bean.Account;
716
import org.springframework.beans.factory.annotation.Autowired;
817
import org.springframework.jdbc.core.JdbcTemplate;
@@ -17,6 +26,34 @@ public class AccountDaoImpl implements AccountDao {
1726

1827
@Override
1928
public List<Account> findUsersByUsernameAndPassword(final String username, final String password) {
29+
30+
String ldapUrl = "ldap://localhost:10389";
31+
String baseDn = "dc=example,dc=com";
32+
String bindDn = "cn=admin," + baseDn;
33+
String bindPassword = "password";
34+
35+
// Set up the environment for creating the initial context
36+
Hashtable<String, Object> env = new Hashtable<>();
37+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
38+
env.put(Context.PROVIDER_URL, ldapUrl);
39+
env.put(Context.SECURITY_AUTHENTICATION, "simple");
40+
env.put(Context.SECURITY_PRINCIPAL, bindDn);
41+
env.put(Context.SECURITY_CREDENTIALS, bindPassword);
42+
43+
DirContext context;
44+
try {
45+
context = new InitialDirContext(env);
46+
47+
String searchFilter = "(uid=" + username + ")";
48+
SearchControls searchControls = new SearchControls();
49+
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
50+
NamingEnumeration<SearchResult> searchResults = context.search(baseDn, searchFilter, searchControls);
51+
52+
} catch (NamingException e) {
53+
throw new RuntimeException(e);
54+
}
55+
56+
2057

2158
String str = "select * from account where username='" + username + "' AND password='" + password + "'";
2259

src/main/resources/ldap.ldif

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
dn: dc=example,dc=com
2+
objectClass: top
3+
objectClass: domain
4+
dc: example
5+
6+
dn: cn=admin,dc=example,dc=com
7+
objectClass: top
8+
objectClass: person
9+
cn: admin
10+
sn: admin
11+
userPassword: password
12+
13+
dn: ou=people,dc=example,dc=com
14+
objectClass: top
15+
objectClass: organizationalUnit
16+
ou: people
17+
18+
dn: uid=jdoe,ou=people,dc=example,dc=com
19+
objectClass: top
20+
objectClass: person
21+
objectClass: inetOrgPerson
22+
uid: john
23+
cn: John Doe
24+
sn: Doe
25+
userPassword: password
26+

0 commit comments

Comments
 (0)