|
| 1 | +/** |
| 2 | +* OWASP Benchmark Project |
| 3 | +* |
| 4 | +* This file is part of the Open Web Application Security Project (OWASP) |
| 5 | +* Benchmark Project For details, please see |
| 6 | +* <a href="https://www.owasp.org/index.php/Benchmark">https://www.owasp.org/index.php/Benchmark</a>. |
| 7 | +* |
| 8 | +* The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms |
| 9 | +* of the GNU General Public License as published by the Free Software Foundation, version 2. |
| 10 | +* |
| 11 | +* The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 12 | +* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +* GNU General Public License for more details |
| 14 | +* |
| 15 | +* @author Juan Gama <a href="https://www.aspectsecurity.com">Aspect Security</a> |
| 16 | +* @created 2015 |
| 17 | +*/ |
| 18 | + |
| 19 | +package org.owasp.benchmark.helpers; |
| 20 | + |
| 21 | +import java.util.Hashtable; |
| 22 | + |
| 23 | +import javax.naming.Context; |
| 24 | +import javax.naming.NamingEnumeration; |
| 25 | +import javax.naming.NamingException; |
| 26 | +import javax.naming.directory.Attribute; |
| 27 | +import javax.naming.directory.Attributes; |
| 28 | +import javax.naming.directory.BasicAttribute; |
| 29 | +import javax.naming.directory.BasicAttributes; |
| 30 | +import javax.naming.directory.DirContext; |
| 31 | +import javax.naming.directory.InitialDirContext; |
| 32 | +import javax.naming.directory.ModificationItem; |
| 33 | +import javax.naming.directory.SearchControls; |
| 34 | +import javax.naming.directory.SearchResult; |
| 35 | + |
| 36 | +import org.apache.log4j.Logger; |
| 37 | + |
| 38 | +//import sun.misc.BASE64Encoder; |
| 39 | + |
| 40 | +public class LDAPHelper { |
| 41 | + private static Logger logger = Logger.getLogger(LDAPHelper.class); |
| 42 | + private static Hashtable<String, String> env = new Hashtable<String, String>(); |
| 43 | + private static DirContext dctx = null; |
| 44 | + |
| 45 | + public LDAPHelper() { |
| 46 | + try { |
| 47 | + /* This needs to be replaced with a real LDAP */ |
| 48 | + env.put(Context.INITIAL_CONTEXT_FACTORY, |
| 49 | + "com.sun.jndi.ldap.LdapCtxFactory"); |
| 50 | + env.put(Context.PROVIDER_URL, "ldap://localhost:10389"); |
| 51 | + env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"); |
| 52 | + env.put(Context.SECURITY_CREDENTIALS, "xxx"); |
| 53 | + } catch (Exception e) { |
| 54 | + logger.error(e, e); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public static InitialDirContext getInitialDirContext(){ |
| 60 | + return (InitialDirContext)getInitialDirContext(); |
| 61 | + } |
| 62 | + |
| 63 | + public static DirContext getDirContext(){ |
| 64 | + if(dctx == null){ |
| 65 | + try { |
| 66 | + dctx = new InitialDirContext(env); |
| 67 | + } catch (NamingException e) { |
| 68 | + System.out.println("Problem with LDAP init."); |
| 69 | + } |
| 70 | + } |
| 71 | + return dctx; |
| 72 | + } |
| 73 | + |
| 74 | + public static boolean insert(LDAPPerson person) { |
| 75 | + try { |
| 76 | + |
| 77 | + DirContext ctx = getDirContext(); |
| 78 | + Attributes matchAttrs = new BasicAttributes(true); |
| 79 | + matchAttrs.put(new BasicAttribute("uid", person.getName())); |
| 80 | + matchAttrs.put(new BasicAttribute("cn", person.getName())); |
| 81 | + matchAttrs.put(new BasicAttribute("street", person.getAddress())); |
| 82 | + matchAttrs.put(new BasicAttribute("sn", person.getName())); |
| 83 | + matchAttrs.put(new BasicAttribute("userpassword", |
| 84 | + encryptLdapPassword("SHA", person.getPassword()))); |
| 85 | + matchAttrs.put(new BasicAttribute("objectclass", "top")); |
| 86 | + matchAttrs.put(new BasicAttribute("objectclass", "person")); |
| 87 | + matchAttrs.put(new BasicAttribute("objectclass", |
| 88 | + "organizationalPerson")); |
| 89 | + matchAttrs.put(new BasicAttribute("objectclass", "inetorgperson")); |
| 90 | + String name = "uid=" + person.getName() + ",ou=users,ou=system"; |
| 91 | + InitialDirContext iniDirContext = (InitialDirContext) ctx; |
| 92 | + iniDirContext.bind(name, ctx, matchAttrs); |
| 93 | + |
| 94 | + logger.debug("success inserting " + person.getName()); |
| 95 | + return true; |
| 96 | + } catch (Exception e) { |
| 97 | + logger.error(e, e); |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static boolean edit(LDAPPerson person) { |
| 103 | + try { |
| 104 | + |
| 105 | + DirContext ctx = getDirContext(); |
| 106 | + ModificationItem[] mods = new ModificationItem[2]; |
| 107 | + Attribute mod0 = new BasicAttribute("street", person.getAddress()); |
| 108 | + Attribute mod1 = new BasicAttribute("userpassword", |
| 109 | + encryptLdapPassword("SHA", person.getPassword())); |
| 110 | + mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0); |
| 111 | + mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod1); |
| 112 | + |
| 113 | + ctx.modifyAttributes("uid=" + person.getName() |
| 114 | + + ",ou=users,ou=system", mods); |
| 115 | + |
| 116 | + logger.debug("success editing " + person.getName()); |
| 117 | + return true; |
| 118 | + } catch (Exception e) { |
| 119 | + logger.error(e, e); |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static boolean delete(LDAPPerson person) { |
| 125 | + try { |
| 126 | + |
| 127 | + DirContext ctx = getDirContext(); |
| 128 | + ctx.destroySubcontext("uid=" + person.getName() |
| 129 | + + ",ou=users,ou=system"); |
| 130 | + |
| 131 | + logger.debug("success deleting " + person.getName()); |
| 132 | + return true; |
| 133 | + } catch (Exception e) { |
| 134 | + logger.error(e, e); |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static boolean search(LDAPPerson person) { |
| 140 | + try { |
| 141 | + |
| 142 | + DirContext ctx = getDirContext(); |
| 143 | + String base = "ou=users,ou=system"; |
| 144 | + |
| 145 | + SearchControls sc = new SearchControls(); |
| 146 | + sc.setSearchScope(SearchControls.SUBTREE_SCOPE); |
| 147 | + |
| 148 | + String filter = "(&(objectclass=person)(uid=" + person.getName() |
| 149 | + + "))"; |
| 150 | + |
| 151 | + NamingEnumeration<SearchResult> results = ctx.search(base, filter, sc); |
| 152 | + |
| 153 | + while (results.hasMore()) { |
| 154 | + SearchResult sr = (SearchResult) results.next(); |
| 155 | + Attributes attrs = sr.getAttributes(); |
| 156 | + |
| 157 | + Attribute attr = attrs.get("uid"); |
| 158 | + if (attr != null) |
| 159 | + logger.debug("record found " + attr.get()); |
| 160 | + } |
| 161 | + ctx.close(); |
| 162 | + |
| 163 | + return true; |
| 164 | + } catch (Exception e) { |
| 165 | + logger.error(e, e); |
| 166 | + return false; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static String encryptLdapPassword(String algorithm, String _password) { |
| 171 | + /* |
| 172 | + * Removed until adding BouncyCastle |
| 173 | + String sEncrypted = _password; |
| 174 | + if ((_password != null) && (_password.length() > 0)) { |
| 175 | + boolean bMD5 = algorithm.equalsIgnoreCase("MD5"); |
| 176 | + boolean bSHA = algorithm.equalsIgnoreCase("SHA") |
| 177 | + || algorithm.equalsIgnoreCase("SHA1") |
| 178 | + || algorithm.equalsIgnoreCase("SHA-1"); |
| 179 | + if (bSHA || bMD5) { |
| 180 | + String sAlgorithm = "MD5"; |
| 181 | + if (bSHA) { |
| 182 | + sAlgorithm = "SHA"; |
| 183 | + } |
| 184 | + try { |
| 185 | + MessageDigest md = MessageDigest.getInstance(sAlgorithm); |
| 186 | + md.update(_password.getBytes("UTF-8")); |
| 187 | + sEncrypted = "{" + sAlgorithm + "}" |
| 188 | + + (new BASE64Encoder()).encode(md.digest()); |
| 189 | + } catch (Exception e) { |
| 190 | + sEncrypted = null; |
| 191 | + logger.error(e, e); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + return sEncrypted; |
| 196 | + */ |
| 197 | + return _password; |
| 198 | + } |
| 199 | +} |
0 commit comments