26

How can I create logs of every DNS query that my computer makes along with the responses it gets?

6 Answers 6

18

You can have tcpdump log all port 53 UDP and TCP activity.

5
  • 7
    Any details on how?
    – e-sushi
    Feb 5, 2016 at 17:57
  • This is the best answer as we can't be sure the OP (or other readers) have access to the DNS server - only their local machine. To answer @e-sushi's question take a tcpdump using the utility (check out the man page or a good primer with examples). Your best bet is to dump to a file and then pull that data into wireshark for review & analysis. Apr 5, 2017 at 16:04
  • 1
    github.com/gamelinux/passivedns seems to be doing just that, see ./doc/How-it-works.txt
    – mxmlnkn
    Jan 12, 2018 at 21:10
  • 10
    tcpdump udp port 53
    – Brannon
    Mar 13, 2018 at 4:48
  • 3
    It may not select the outbound network interface by default, so you need a bit more: tcpdump --list-interfaces, tcpdump udp port 53 --interface (pickone). Also consider verbosity: -vv May 17, 2019 at 20:08
10

The easiest way is to install Bind locally. Most distros default install of Bind will be non-autoritative caching-only.

Simply add a logging {} config block (as described in the Bind 9 Configuration Reference) then set your system to use 127.0.0.1 or ::1 as the DNS resolver.

3
  • 2
    Given how large bind is and its lackluster security record, I think many people would hesitate to install something like that for the sole purpose of logging.
    – jw013
    Jul 23, 2012 at 1:16
  • doesn't bind have the issue that the nameservers in /etc/resolv.conf are not used but nameservers must be listed explicitly in the bind config?
    – Bananguin
    Jul 23, 2012 at 7:12
  • No. /etc/resolv.conf is the system resolver list. Bind's default configuration is to look up the authoritative name servers and ask them. You could forward all requests to a specific server (or set, such as your ISP, OpenDNS or Google Public DNS) but it's not required to do so in the config. I do this all the time. I can't even count the number of times I've set up caching only name servers.
    – bahamat
    Jul 23, 2012 at 15:19
9

To show and save to file all the A DNS requests, run this:

script -q -c "sudo tcpdump -l port 53 2>/dev/null | grep --line-buffered ' A? ' | cut -d' ' -f8" | tee dns.log

Example output:

google.com.
wikipedia.org.

2
  • 1
    This command worked for me, but I don't understand why script is needed. Could you explain?
    – Dagang
    Jul 16, 2021 at 17:39
  • Will print responses with implicit request info on a single line: sudo tcpdump -vvli eth0 port 53 | grep --line-buffered " q: A? " | cut -d' ' -f16-
    – Matteljay
    Aug 27, 2021 at 12:52
6

dnsmasq is far easier to configure as a DNS aggregator/caching daemon than BIND, and for that purpose, the performance might just be better. If you turn logging up to "debug", all the questions and answers show up in whatever syslog has configured for debug messages.

Dnsmasq also makes it easy to get rid of abusive advertisers and dirtbag privacy invading "analytic" creeps by aliasing entire domains to 127.0.0.1

0

If I recall correctly Snort can selectively monitor traffic based on user defined rules. However, Snort will not create logs for DNS requests when your computer, i.e. its resolver, can answer the question from its cache.

0

One easy way to filter the DNS , for the requests you are interested in is to grep the next row too grep -A 2 where -A is after and 2 rows after . If the server has a lot of DNS requests increase from 2 to 4-5

tcpdump -l port 53 |grep -A 2  redis

the second line will be the answer from DNS -> IP, CNAME ,none , other

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy