cplus
cplus
fromZone(kv_pairs)
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=mydata))
else:
# dnslib doesn't like trailing dots
if fake_record[-1] == ".": fake_record = fake_record[:-1]
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](fake_record)))
response = response.pack()
response = DNSRecord(DNSHeader(id=d.header.id,
bitmap=d.header.bitmap,qr=1, aa=1, ra=1), q=d.q)
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](mname,rname,times)))
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](order,preference,flags,service,regexp,replacement)))
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](priority, weight, port, target) ))
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](flags, protocol, algorithm, key) ))
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](covered, algorithm, labels,orig_ttl, sig_exp, sig_inc, key_tag,
name, sig) ))
else:
# dnslib doesn't like trailing dots
if fake_record[-1] == ".": fake_record =
fake_record[:-1]
response.add_answer(RR(qname, getattr(QTYPE,qtype),
rdata=RDMAP[qtype](fake_record)))
response = response.pack()
nameserver_tuple =
random.choice(self.server.nameservers).split('#')
response = self.proxyrequest(data, *nameserver_tuple)
return response
# Find appropriate ip address to use for a queried name. The function can
def findnametodns(self,qname,nametodns):
if protocol == "udp":
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
elif protocol == "tcp":
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
if protocol == "udp":
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif protocol == "tcp":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3.0)
if protocol == "udp":
sock.sendto(request, (host, int(port)))
reply = sock.recv(1024)
sock.close()
sock.close()
except Exception as e:
log.error(f"[!] Could not proxy request: {e}")
else:
return reply
def handle(self):
(data, socket) = self.request
response = self.parse(data)
if response:
socket.sendto(response, self.client_address)
def handle(self):
data = self.request.recv(1024)
if response:
# Calculate and add the additional "length" parameter
# used in TCP DNS protocol
length = binascii.unhexlify("%04x" % len(response))
self.request.sendall(length + response)
if logfile:
fh = logging.FileHandler(logfile, encoding='UTF-8')
fh.setLevel(logging.INFO)
fh.setFormatter(DNSChefFormatter(datefmt="%d/%b/%Y:%H:%M:%S %z"))
log.addHandler(fh)
log.info("DNSChef is active.")
if tcp:
log.info("DNSChef is running in TCP mode")
server = ThreadedTCPServer((interface, int(port)), TCPHandler,
nametodns, nameservers, ipv6, log)
else:
server = ThreadedUDPServer((interface, int(port)), UDPHandler,
nametodns, nameservers, ipv6, log)
# Start a thread with the server -- that thread will then start
# more threads for each request
server_thread = threading.Thread(target=server.serve_forever)
except Exception as e:
log.error(f"Failed to start the server: {e}")
if __name__ == "__main__":
mexclusivegroup = parser.add_mutually_exclusive_group()
mexclusivegroup.add_argument('--fakedomains',
metavar="thesprawl.org,google.com", help='A comma separated list of domain names
which will be resolved to FAKE values specified in the the above parameters. All
other domain names will be resolved to their true values.')
mexclusivegroup.add_argument('--truedomains',
metavar="thesprawl.org,google.com", help='A comma separated list of domain names
which will be resolved to their TRUE values. All other domain names will be
resolved to fake values specified in the above parameters.')
options = parser.parse_args()
if options.nameservers == "8.8.8.8":
options.nameservers = "2001:4860:4860::8888"
if section in nametodns:
for domain, record in config.items(section):
nametodns[section][domain] = record
log.info(f"Cooking {section} replies for domain {domain} with
'{record}'")
else:
log.warning(f"DNS Record '{section}' is not supported. Ignoring
section contents.")
if options.fakedomains:
for domain in options.fakedomains.split(','):
if fakeip:
nametodns["A"][domain] = fakeip
log.info(f"Cooking A replies to point to {options.fakeip}
matching: {domain}")
if fakeipv6:
nametodns["AAAA"][domain] = fakeipv6
log.info(f"Cooking AAAA replies to point to {options.fakeipv6}
matching: {domain}")
if fakemail:
nametodns["MX"][domain] = fakemail
log.info(f"Cooking MX replies to point to {options.fakemail}
matching: {domain}")
if fakealias:
nametodns["CNAME"][domain] = fakealias
log.info(f"Cooking CNAME replies to point to
{options.fakealias} matching: {domain}")
if fakens:
nametodns["NS"][domain] = fakens
log.info(f"Cooking NS replies to point to {options.fakens}
matching: {domain}")
elif options.truedomains:
for domain in options.truedomains.split(','):
if fakeip:
nametodns["A"][domain] = False
log.info(f"Cooking A replies to point to {options.fakeip} not
matching: {domain}")
nametodns["A"]['*.*.*.*.*.*.*.*.*.*'] = fakeip
if fakeipv6:
nametodns["AAAA"][domain] = False
log.info(f"Cooking AAAA replies to point to {options.fakeipv6}
not matching: {domain}")
nametodns["AAAA"]['*.*.*.*.*.*.*.*.*.*'] = fakeipv6
if fakemail:
nametodns["MX"][domain] = False
log.info(f"Cooking MX replies to point to {options.fakemail}
not matching: {domain}")
nametodns["MX"]['*.*.*.*.*.*.*.*.*.*'] = fakemail
if fakealias:
nametodns["CNAME"][domain] = False
log.info(f"Cooking CNAME replies to point to
{options.fakealias} not matching: {domain}")
nametodns["CNAME"]['*.*.*.*.*.*.*.*.*.*'] = fakealias
if fakens:
nametodns["NS"][domain] = False
log.info(f"Cooking NS replies to point to {options.fakens} not
matching: {domain}")
nametodns["NS"]['*.*.*.*.*.*.*.*.*.*'] = fakealias
else:
if fakeip:
nametodns["A"]['*.*.*.*.*.*.*.*.*.*'] = fakeip
log.info(f"Cooking all A replies to point to {fakeip}")
if fakeipv6:
nametodns["AAAA"]['*.*.*.*.*.*.*.*.*.*'] = fakeipv6
log.info(f"Cooking all AAAA replies to point to {fakeipv6}")
if fakemail:
nametodns["MX"]['*.*.*.*.*.*.*.*.*.*'] = fakemail
log.info(f"Cooking all MX replies to point to {fakemail}")
if fakealias:
nametodns["CNAME"]['*.*.*.*.*.*.*.*.*.*'] = fakealias
log.info(f"Cooking all CNAME replies to point to {fakealias}")
if fakens:
nametodns["NS"]['*.*.*.*.*.*.*.*.*.*'] = fakens
log.info(f"Cooking all NS replies to point to {fakens}")
# Launch DNSChef
start_cooking(interface=options.interface, nametodns=nametodns,
nameservers=nameservers, tcp=options.tcp, ipv6=options.ipv6, port=options.port,
logfile=options.logfile)