Skip to content

Instantly share code, notes, and snippets.

@danielkec
Created August 22, 2013 16:07
Show Gist options
  • Save danielkec/6309279 to your computer and use it in GitHub Desktop.
Save danielkec/6309279 to your computer and use it in GitHub Desktop.
Querying DNS to find all ActiveDirectory it knows about
package cz.syntea.common.apmsvr.jtalk2ad;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class DCDiscovery {
public static void main(String args[]) {
String[] dnss = findDNSs();
for (int i = 0; i < dnss.length; i++) {
lookUpDC(dnss[i], "syntea.cz");
}
// lookUpDC("192.168.56.101", "kec.cz");
}
public static void lookUpDC(String dns,String hostname){
String domainController;
try {
DirContext dnsContext = dnsQuery(dns, hostname);
Attributes dnsQueryResult = dnsContext.getAttributes("_ldap._tcp.dc._msdcs",new String[]{"SRV"});
if (dnsQueryResult == null) {
System.out.println("Returned NULL");
}
for (NamingEnumeration dnsRR =
dnsQueryResult.getAll(); dnsRR.hasMoreElements();) {
Attribute rr = (Attribute) dnsRR.next();
//String attrId = rr.getID();
for (Enumeration vals = rr.getAll(); vals.hasMoreElements();) {
Object el = vals.nextElement();
domainController = el.toString().split(" ")[3];
System.out.println("DomainController: " + domainController+ " found with DNS: "+dns);
}
}
dnsContext.close();
} catch (Exception e) {
//System.err.println("Error performing DNS lookup for: " + "dns://"+dns+"/"+hostname + "\n" + e);
//e.printStackTrace();
}
}
public static String[] findDNSs() {
try {
Hashtable dnsEnv = new Hashtable();
dnsEnv.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
dnsEnv.put("java.naming.provider.url", "dns:///");
DirContext dnsContext = new InitialDirContext(dnsEnv);
Hashtable retEnv = dnsContext.getEnvironment();
Object obj = retEnv.get(Context.PROVIDER_URL);
Pattern ippattr = Pattern.compile("(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}");
Matcher matcher = ippattr.matcher(obj.toString());
ArrayList<String> iplist = new ArrayList();
while(matcher.find()){
iplist.add(matcher.group());
}
return iplist.toArray(new String[0]);
} catch (Exception e) {
}
return new String[0];
}
public static DirContext dnsQuery(String dns,String hostname) throws NamingException{
Hashtable dnsEnv = new Hashtable();
dnsEnv.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
// dnsEnv.put("java.naming.provider.url", "dns://" + dnsNameServer + "/" + aDDomain);
// dnsEnv.put("java.naming.provider.url", "dns:///_sites.dc._msdcs.syntea.cz");// defaultni dns
dnsEnv.put("java.naming.provider.url", "dns://"+dns+"/"+hostname);
return new InitialDirContext(dnsEnv);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment