from scapy.all import *
from termcolor import colored

def 
(pkt):
   try:
      ip = pkt[IP]
   except IndexError:
      pass

   try:
      src = ip.src
      dst = ip.dst
   except UnboundLocalError:
      pass

   if pkt.haslayer(DNS):
      dns = pkt[DNS]
      query = dns[DNSQR]
      qtype = dnsqtypes.get(query.qtype)

      print("--------------------------------------------------------\n\n")
      print("                     .:{}:.                            ".format(colored('DNS','red')))
      print("                                                          ")
      print("  \033[1;36mSource IP:\033[00m {} \033[1;36mDestination IP:\033[00m {}".format(src, dst))
      print("  \033[1;36mDomain: \033[00m {}".format(query.qname))
      print("  \033[1;36mQuery Type \033[00m {}".format(qtype))
      print("  \033[1;36mId:\033[00m {}".format(dns.id))
      print("  \033[1;36mOpcode: \033[00m {}".format(dns.opcode))
      print("  \033[1;36mQuery Code: \033[00m {}".format(dns.qr))
      print("  \033[1;36mRcode \033[00m {}".format(dns.rcode))
      print("  \033[1;36mQuestion Count: \033[00m {}".format(dns.qdcount))
      print("  \033[1;36mAnswer Record Count:\033[00m {}".format(dns.ancount))
      print("  \033[1;36mAuthority Record Count:\033[00m {}".format(dns.nscount))
      print("  \033[1;36mAdditional Record Count:\033[00m {}".format(dns.arcount))
      rawLoad = pkt.getlayer(Raw)
      if rawLoad == None: pass
      else:
         print("  \033[1;36mRaw:\n\n\033[00m {}".format(colored(rawLoad, 'green')))
      

pkt = sniff(iface='wlan0' ,prn=pkthandler)
pkthandler