package LDAP_Injection; import javax.naming.*; import javax.naming.directory.*; import javax.servlet.http.*; import java.util.Hashtable; import java.io.IOException; import java.util.logging.Logger; public class LDAP_Injection { static final Logger log = Logger.getLogger("logger"); /* uses badsource and badsink */ public void bad(HttpServletRequest request, HttpServletResponse response) throws NamingException, IOException { String data = System.getProperty("data"); /* init data */ Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); DirContext ctx = new InitialDirContext(env); String search = "(cn=" + data + ")"; /* POTENTIAL FLAW: unsanitized data from untrusted source */ NamingEnumeration answer = ctx.search("", search, null); // bad LDAP注入 if (answer.hasMore()) { log.info("ok"); } } public void good(HttpServletRequest request, HttpServletResponse response) throws NamingException, IOException { String data = "foo"; Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389"); DirContext ctx = new InitialDirContext(env); String search = "(cn=" + data + ")"; /* POTENTIAL FLAW: unsanitized data from untrusted source */ NamingEnumeration answer = ctx.search("", search, null); // good LDAP注入 if (answer.hasMore()) { log.info("ok"); } } }