You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LDAP_Injection.java 1.8 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package LDAP_Injection;
  2. import javax.naming.*;
  3. import javax.naming.directory.*;
  4. import javax.servlet.http.*;
  5. import java.util.Hashtable;
  6. import java.io.IOException;
  7. import java.util.logging.Logger;
  8. public class LDAP_Injection
  9. {
  10. static final Logger log = Logger.getLogger("logger");
  11. /* uses badsource and badsink */
  12. public void bad(HttpServletRequest request, HttpServletResponse response) throws NamingException, IOException
  13. {
  14. String data = System.getProperty("data"); /* init data */
  15. Hashtable<String, String> env = new Hashtable<String, String>();
  16. env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
  17. env.put(Context.PROVIDER_URL, "ldap://localhost:389");
  18. DirContext ctx = new InitialDirContext(env);
  19. String search = "(cn=" + data + ")"; /* POTENTIAL FLAW: unsanitized data from untrusted source */
  20. NamingEnumeration<SearchResult> answer = ctx.search("", search, null); // bad LDAP注入
  21. if (answer.hasMore())
  22. {
  23. log.info("ok");
  24. }
  25. }
  26. public void good(HttpServletRequest request, HttpServletResponse response) throws NamingException, IOException
  27. {
  28. String data = "foo";
  29. Hashtable<String, String> env = new Hashtable<String, String>();
  30. env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
  31. env.put(Context.PROVIDER_URL, "ldap://localhost:389");
  32. DirContext ctx = new InitialDirContext(env);
  33. String search = "(cn=" + data + ")"; /* POTENTIAL FLAW: unsanitized data from untrusted source */
  34. NamingEnumeration<SearchResult> answer = ctx.search("", search, null); // good LDAP注入
  35. if (answer.hasMore())
  36. {
  37. log.info("ok");
  38. }
  39. }
  40. }

No Description

Contributors (1)