dig & nslookup for DNS Enumeration
Use dig and nslookup for DNS enumeration in this Ethical Hacking tutorial — hands-on steps, troubleshooting, and what to study next.
Focus: use dig and nslookup for dns enumeration
Passive reconnaissance often starts with a single domain name. Before you scan a single port or write a line of exploit code, you need to map the target's digital footprint. The built-in dig and nslookup tools give you a powerful, zero-footprint way to enumerate DNS records, uncover subdomains, and identify mail servers — all from your Kali terminal. This lesson walks you through the core techniques of using dig and nslookup for DNS enumeration, turning a single domain into a treasure map of network assets.
The Problem This Lesson Solves
Imagine you're tasked with testing the security of example.com. You could run a port scan against the IP 93.184.216.34, but that only reveals one server. Most organizations run dozens of services — web portals, mail gateways, internal APIs, staging environments — each with its own hostname. Manually guessing subdomains is slow and noisy. You need a way to query the Domain Name System (DNS) itself, which acts as a public directory of the target's infrastructure. Using dig and nslookup for DNS enumeration lets you ask the DNS directly: "What mail servers do you have? What name servers manage your domain? What subdomains point to which IPs?" This is passive reconnaissance — you're not sending exploits, just standard DNS queries. For an ethical hacker, this is your first, safest, and most revealing step.
Without proper DNS enumeration, you risk: - Wasting time scanning IP ranges that aren't even in use. - Missing critical services like VPN gateways or admin panels that are hidden behind hard-to-guess subdomains. - Triggering intrusion detection systems with noisy brute-force scanning.
Core Concept / Mental Model
Think of DNS as the phonebook of the internet. When you type a hostname, your computer looks up the number (IP address) in this phonebook. dig and nslookup are your supercharged search tools for this phonebook. They let you browse entries, not just look up a single name. Instead of just asking "What is the IP of www.example.com?", you ask "Show me all records associated with example.com."
The key records you'll enumerate:
- A / AAAA — IPv4 / IPv6 addresses for a hostname.
- NS — Name servers that are authoritative for the domain. These often reveal the hosting provider.
- MX — Mail exchange servers. They often follow a pattern like mail.example.com and can expose the email security provider.
- TXT — Arbitrary text records. Often include SPF, DKIM, and DMARC policies, and can reveal third-party services like Google Workspace or proof-of-concept verification strings.
- CNAME — Aliases. They can expose internal hostnames or cloud services (e.g., cdn.example.com pointing to a CDN).
- SOA — Start of Authority. Contains the primary name server and the admin email, useful for social engineering.
- PTR — Reverse lookups. Given an IP, find the hostname. Helps map out IP ranges.
Here's a simple mental model for a DNS query flow:
You → Recursive Resolver → Root Server → TLD Server → Authoritative NS → Records
- Recursive Resolver (e.g., your ISP or
8.8.8.8) does the legwork. - Root servers direct to TLD servers (like
.com). - TLD servers point to the authoritative name servers.
- Authoritative NS holds the actual records.
dig and nslookup let you query any of these servers directly, or bypass caching by specifying a specific name server.
How It Works Step by Step
Both tools send DNS queries using the protocol defined in RFC 1035. nslookup is the older, simpler tool, available on most systems. dig (Domain Information Groper) is modern, verbose, and found on Linux/macOS by default. On Windows, nslookup is built-in, but you can install dig via the Windows Subsystem for Linux (WSL) or the BIND utilities.
Step 1: Understand the default output
- Both tools default to querying your system's configured resolver for an A record.
digshows the full response with timing information and the authoritative section.nslookupis more terse, showing just the answer.
Step 2: Specify query types
Use the -t flag in nslookup (e.g., -t MX) and the TYPE argument in dig (e.g., MX).
Step 3: Query specific name servers
- For
dig, append@8.8.8.8to bypass your local cache. - For
nslookup, use-server=8.8.8.8ornslookup -type=MX domain 8.8.8.8.
Step 4: Enumerate systematically
Work through the record types: NS → SOA → A/AAAA → MX → TXT → CNAME. For each, note the target's infrastructure.
Step 5: Perform reverse lookups
Once you have IPs, use dig -x <ip> for PTR records to discover additional hostnames.
Hands-On Walkthrough
Open your Kali Linux terminal and try these commands. We'll use example.com as a legal, safe target.
1. Basic A record lookup with dig
dig example.com
Expected output (simplified):
;; ANSWER SECTION:
example.com. 3600 IN A 93.184.216.34
2. Query mail servers with nslookup
nslookup -type=MX example.com
Expected output:
Non-authoritative answer:
example.com mail exchanger = 0 .
3. Enumerate name servers with dig
dig NS example.com
Expected output:
;; ANSWER SECTION:
example.com. 3600 IN NS a.iana-servers.net.
example.com. 3600 IN NS b.iana-servers.net.
4. Get TXT records (SPF, verification)
dig TXT example.com
5. Perform a reverse lookup
dig -x 93.184.216.34
This reveals the PTR record, which often reveals the hosting provider or internal naming conventions.
6. Automate with a simple loop (optional)
for sub in www mail ftp vpn portal; do
dig +short $sub.example.com
nslookup $sub.example.com 2>/dev/null
echo "---"
done
This brute-force style enumeration is often used after you've exhausted standard records.
Pro tip: Always use
+shortwithdigto get clean, parseable output for scripting. For example:dig +short MX example.comreturns just the mail servers, one per line.
Compare Options / When to Choose What
Both tools are essential, but they have different strengths. Here’s a comparison:
| Feature | dig |
nslookup |
|---|---|---|
| Output verbosity | Verbose, shows all sections | Terse, answer-focused |
| Query types | Supports all, easy to type (MX, TXT) |
Works, but syntax is -type=MX |
| Caching control | Bypass cache with @server |
Use -server flag or server command |
| Scripting | +short output is perfect for parsing |
More awkward for automation |
| Reverse lookups | -x flag is intuitive |
Requires set type=PTR then query IP |
| Platform | Default on Linux/macOS, not Windows | Built into Windows, Linux, macOS |
When to use dig:
- You're on Linux or macOS and need detailed, scriptable output.
- You want to query specific record types or bypass caching.
- You're doing advanced troubleshooting (e.g., trace the DNS path with +trace).
When to use nslookup:
- You're on a Windows machine without dig installed.
- You just need a quick answer and don't care about formatting.
- You're teaching someone and want the simplest possible example.
Other tools:
- host — a lightweight command that's faster to type for basic lookups.
- dnsrecon — a Python-based tool that automates DNS enumeration and even checks for zone transfers.
- Online tools like dnsdumpster.com — great for visual mapping, but always prefer your own terminal for privacy.
Troubleshooting & Edge Cases
nslookupis deprecated on macOS — it still works, but you'll see a warning. Usediginstead.- No answer section — The record type doesn't exist for that domain. This is common for
MXon domains that only use web services. - Non-authoritative answer — This means the response came from a cache, not the authoritative server. Use
destflags as described above to get the authoritative data by querying the NS directly. - DNS cache poisoning / stale records — Always query a well-known public resolver like
8.8.8.8or1.1.1.1for the most accurate results. - Timeouts — If a query times out, the target might be blocking DNS queries from your IP. Use a different resolver or a VPN.
- Wildcard records — Some domains have a
*A record that returns the same IP for every subdomain. This thwarts naive subdomain brute-forcing. Look for this by trying random subdomains; if they all resolve, the domain uses a wildcard. - Zone transfer (AXFR) — This is a critical vulnerability. If the name server is misconfigured, you can request a full copy of the zone. Try
dig AXFR @ns.example.com example.com. This is normally blocked, but always test it — it's a huge find.
Common mistake: Don't rely solely on your local DNS resolver. Use
@8.8.8.8or the authoritative server for the most complete picture.
What You Learned & What's Next
In this lesson, you learned how to use dig and nslookup for DNS enumeration. You can now:
- Enumerate A, NS, MX, TXT, and CNAME records to map a target's infrastructure.
- Perform reverse lookups to discover hostnames behind IPs.
- Compare and choose the right tool for the job.
- Troubleshoot common DNS issues and detect wildcard records.
Next, you'll move on to more advanced reconnaissance techniques, such as subdomain brute-forcing with dnsrecon or service discovery with nmap. These will build on the DNS map you create here, helping you identify live hosts and open ports. Keep practicing on legal targets like example.com or your own infrastructure, and remember: always stay within the legal boundaries of your engagement.
Common Mistakes
- Forgetting to check for wildcard records — If you don't test with a random subdomain, you might waste time on non-existent hosts.
- Not using the
-tflag correctly innslookup— Mixing up syntax likenslookup -type=ns domainvsnslookup -nscan cause confusion. - Ignoring the authoritative section of
digoutput — This tells you which server actually holds the records and can reveal external DNS providers. - Relying solely on online DNS tools — They may be blocked or can leak your queries. Use your terminal for maximum control.
Variations
- Use
hostcommand — A simpler alternative that works on many Unix systems:host -t MX example.com. - Use
dnsrecon— Automates DNS record enumeration, zone transfer attempts, and subdomain brute-forcing in one tool. - Query IPv6 records — Use
dig AAAA example.comto get IPv6 addresses, which are often missed by beginners.
Real-World Use Cases
- Passive recon in penetration testing — Enumerate MX and TXT records to guess the email security provider and spoofing defenses before an engagement.
- Subdomain discovery for asset mapping — Use
dig CNAMEto find cloud-hosted services (e.g.,api.example.compointing to an S3 bucket) and prioritize them for testing. - Detection of zone transfer vulnerabilities — Test
AXFRrequests during a security assessment to identify misconfigured authoritative name servers.
Key Takeaways
- DNS enumeration is the first stepping stone in any security assessment — it's passive and low-risk.
digis the go-to tool for scriptable, detailed DNS queries;nslookupis fine for quick checks.- Always query multiple record types (NS, MX, TXT, CNAME) and don't forget reverse lookups.
- Use public resolvers like
8.8.8.8to get accurate, uncached results. - Wildcard records and zone transfers are important edge cases that can change your approach.
- Practice on legal targets only — use
example.comor your own domain.
Practice Recap
Open your terminal and pick a domain you own (or example.com). Run a full DNS enumeration: dig NS, dig MX, dig TXT, dig CNAME, dig -x on the discovered IPs. Then, write a short bash loop that tries common subdomains (www, mail, ftp, vpn) and reports which resolve. Finally, attempt an AXFR query to see if the zone is misconfigured. Document your findings — this is exactly what a professional penetration tester would include in a recon report.
Practice recap
Open your terminal and pick a domain you own (or example.com). Run a full DNS enumeration: dig NS, dig MX, dig TXT, dig CNAME, dig -x on the discovered IPs. Then, write a short bash loop that tries common subdomains (www, mail, ftp, vpn) and reports which resolve. Finally, attempt an AXFR query to see if the zone is misconfigured. Document your findings — this is exactly what a professional penetration tester would include in a recon report.
Common mistakes
- Forgetting to check for wildcard records — if a random subdomain resolves, your brute-force results are meaningless.
- Using only the default resolver without specifying
@8.8.8.8— you may get stale cached data. - Confusing
nslookupsyntax — remember-type=MX, not-mx, and always specify the server as the last argument if needed. - Not testing for zone transfers (AXFR) — a misconfigured server can hand over the entire DNS zone.
Variations
- Use the
hostcommand for quick lookups:host -t MX example.com— simpler syntax, but less detail. - Use
dnsreconto automate enumeration: it can check for AXFR, brute-force subdomains, and output via JSON. - Query IPv6 records with
dig AAAA example.comto discover hidden infrastructure not visible with A records alone.
Real-world use cases
- Map a target's email infrastructure by enumerating MX and TXT records to assess SPF/DKIM spoofing risk before a phishing simulation.
- Discover cloud-hosted assets via CNAME records (e.g., pointing to S3 or Azure) to extend the attack surface for web app testing.
- Identify misconfigured authoritative name servers by testing AXFR zone transfers — a critical finding in any network security assessment.
Key takeaways
- DNS enumeration is a passive, low-risk technique that maps the target's infrastructure before any active scanning.
digis the preferred tool on Linux/macOS for its detailed, scriptable output;nslookupis fine for quick checks.- Always enumerate multiple record types: A, NS, MX, TXT, CNAME, and perform reverse lookups on discovered IPs.
- Query public resolvers like 8.8.8.8 to bypass local cache and ensure accurate responses.
- Watch for wildcard records and test for zone transfers — both significantly affect your enumeration results.
- Stick to legal targets like example.com or your own domain when practicing.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.