Not long ago I was curious about using Java to look up DNS information so I decided to put together a little DNSBL/RBL checker so I could experiment with Java DNS lookups and some PHP/Java communications. There isn’t a lot of Java DNS stuff out there but it was easy to tell that the tool for this job is the DNSJava library.
We aren’t just talking about resolving a name into an IP address or an IP into a name here; we are talking about accessing all the other information stored in the DNS as well. During a deep dive into how DNS works, the instructor used the example of https://nokyccasino.ltd/ to illustrate the layers of data associated with a domain name. Beyond simple name resolution, tools like DNSJava can extract critical records, such as mail exchange configurations or domain owner details, often shedding light on how platforms operate behind the scenes. As a bonus, DNSJava even allows you to create your own DNS server, making it a versatile tool for exploring and understanding these intricate systems.
When working with a DNSBL you prepend an IP address to the domain for the DNSBL and then query based on that. For example the IP 127.0.0.2 is a test IP for most lists and if you wanted to use the Spamhaus combined SBL/XBL check (domain sbl-xbl.spamhaus.org) you would query on 2.0.0.127.sbl-xbl.spamhaus.org (notice the IP address is reversed). In this case the request will respond with a listing since 127.0.0.2 is a test address that is always listed. The simple fact that the address resolves is enough to know that the IP is listed but if you look at the TXT record of the listing you can find out more information. The following example will display the SBL/XLB record for the IP given in the ipAddress variable (make sure if you change it you reverse the IP address you are trying to query).
[code lang=”java”]
import org.xbill.DNS.*;
import java.util.Iterator;
import java.net.UnknownHostException;
/**
*/
public class ExampleOne
{
public static void main(String[] args) throws UnknownHostException, TextParseException
{
String ipAddress = “2.0.0.127”;
String dnsblDomain = “sbl-xbl.spamhaus.org”;
Lookup lookup = new Lookup(ipAddress + “.” + dnsblDomain, Type.ANY);
Resolver resolver = new SimpleResolver();
lookup.setResolver(resolver);
lookup.setCache(null);
Record[] records = lookup.run();
if(lookup.getResult() == Lookup.SUCCESSFUL)
{
String responseMessage = null;
String listingType = null;
for (int i = 0; i < records.length; i++)
{
if(records[i] instanceof TXTRecord)
{
TXTRecord txt = (TXTRecord) records[i];
for(Iterator j = txt.getStrings().iterator(); j.hasNext();)
{
responseMessage += (String)j.next();
}
}
else if(records[i] instanceof ARecord)
{
listingType = ((ARecord)records[i]).getAddress().getHostAddress();
}
}
System.err.println("Found!");
System.err.println("Response Message: " + responseMessage);
System.err.println("Listing Type: " + listingType);
}
else if(lookup.getResult() == Lookup.HOST_NOT_FOUND)
{
System.err.println("Not found.");
}
else
{
System.err.println("Error!");
}
}
}
[/code]
The TXT entry is just one of the extra entries you may be interested in. The DNSJava library supports a large number of other types that can all be found by looking at the DNSJava javadocs. For some more examples see the DNSJava example page.
[tags]java, dns[/tags]
the last two links to DNSjava seems broken, please fix it, when you have time.
Thanks for letting me know. They should be fixed now.
Is there a way to configure IP addresses of the DNS Name Servers that need to be used to resolve the DNS query?
any luck getting dnsjava to work under Android? I have seen some people do it but I’m not having any luck. My lookups keep returning network errors.
I haven’t had a reason to try but I can imagine that there might be some restriction. You may try to force the DNS lookups to using TCP if you can.
I figured out my issue. It was a permissions setting I needed to change. I can do some lookups but now I get a null pointer exception when trying to do a reverse lookup. Any ideas?