def import_module(name): with _import_lock: mod = __import__(name) components = name.split('.') for comp in components[1:]: mod = getattr(mod, comp) return mod
def domain_search(request): name = request.GET.get('d', '') url_re = re.compile(r'^\s*(https?://)?(%s)/?\s*$' % urls.dns_name) name = url_re.sub(r'\2', name) ipv4_re = re.compile( r'^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$' ) if ipv4_re.match(name): octets = name.split('.') octets.reverse() name = '.'.join(octets) + '.in-addr.arpa' #TODO similarly detect IPv6 address name_valid = True try: name = dns.name.from_unicode(name) name = util.name_url_encode(name) except: name_valid = False # even an valid name might not fit our (current) URL criteria name_re = re.compile(r'^(%s)$' % urls.dns_name) if name_re.match(urllib.unquote(name)) is None: name_valid = False if not name_valid: return render_to_response('search.html', { 'domain_name': name, 'title': 'Search' }, context_instance=RequestContext(request)) return HttpResponseRedirect('../d/%s/' % name)
def decode_query(data: bytes): query_dns_pkt = dns.message.from_wire(data) name = str(query_dns_pkt.question[0].name) name = name[:-20] ip_data = ''.join(name.split('.')) write_to_tun_data = base64.urlsafe_b64decode(ip_data) return write_to_tun_data, query_dns_pkt
def recever(self): data, addr = self._socket.recvfrom(65523) pack_from_client = dns.message.from_wire(data) name = str(pack_from_client.question[0].name) name = name[:-20] data_recv = ''.join(name.split('.')) tun_bus_data = b64.b64decode(data_recv) self.data_queue.put((pack_from_client, addr)) return tun_bus_data
def _recv_from_socket(self): data, addr = self._socket.recvfrom(65523) query_msg = dns.message.from_wire(data) name = str(query_msg.question[0].name) name = name[:-20] ip_data = ''.join(name.split('.')) data_to_tun = coder.b64decode(ip_data) self.data_queue.put((query_msg, addr)) return data_to_tun
def assertNoReverseMatch( # pylint: disable=invalid-name self, name: str, args: typing.Optional[typing.Sequence[typing.Any]] = None, kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None, ) -> None: """Context manager asserting that the given URL pattern is **not** found.""" urlname = name if ":" in name: _namespace, urlname = name.split(":", 1) msg = f"Reverse for '{urlname}' not found. '{urlname}' is not a valid view function or pattern name." with self.assertRaisesRegex(NoReverseMatch, msg): reverse(name, args=args, kwargs=kwargs)
def splitFQDN(name): hostname = name.split('.')[0] domain = name.partition('.')[2] return hostname,domain
def myDig(ipAddress, fullName, iteration, first): #print("\nGOT TO THE FRONT") #first = 0 #I split up the name to be used for the resolver part lengthWeb2 = 0 partsOfName = web.split(".") for i in partsOfName: lengthWeb2 = lengthWeb2 + 1 i = i + "." #print(i) wwwPresent = 0 if (partsOfName[0] == "www"): #print("\nGOT STUCK HERE") wwwPresent = 1 # i get the information from the server answer = dns.message.make_query(fullName, queryType) response = dns.query.udp(answer, ipAddress) # types of responses additionalSection = response.additional answerSection = response.answer authoritativeSection = response.authority # counts answerSection_count = 0 additionalSection_count = 0 authoritativeSection_count = 0 # I count the lines in all the sections for lines in additionalSection: additionalSection_count = additionalSection_count + 1 for lines in answerSection: answerSection_count = answerSection_count + 1 # lines = lines.to_text().split(" ") currentCount = len(authoritativeSection) if (currentCount != 0): authSection = authoritativeSection[0].to_text().splitlines() for lines in authSection: authoritativeSection_count = authoritativeSection_count + 1 # lines = lines.to_text().split() name = "" # if there is something in the answer section if (answerSection_count > 0): #print("\nIN ANSWERS") for x in answerSection: x = x.to_text().split(" ") # name = "" if x[3] == "CNAME": #found the cname name = x[4] #I find out the iteration to start with for the new name temp = name #print("\nNAME") #print(name) temp = temp.split(".") count = 0 for i in temp: count = count + 1 iteration = count #print("\nITERATION VAL") #print(count) resloveAns = resolver(temp[count - 2], rootServer, queryType) if (resloveAns["success"] == False): exit() #DO I NEED TO CALL RESOLVER myDig(rootServer, name, iteration, first) break if x[3] == "A": testingCount = 1 # we found the answer --> Verification was successful #NEED TO CHECK KSK verificationSuccessful() break elif (additionalSection_count > 0): #print("\nIN ADDITIONAL") for lines in additionalSection: lines = lines.to_text().split(" ") if (first == 0): #we have yet to validate anything, we need to check with the root #NEED TO HAVE A WAY OF CHECKING THIS valRoot = validRoot(ipAddress) if (valRoot["success"] == False): exit() if (first > 0): #oldName = "" newName = "" #firstGo = 0 countForIt = 0 #I take parts of my name to be used in the resovler for blah in partsOfName: if ((blah == "www") or (countForIt < (iteration))): #continue print("") else: newName = newName + blah + "." countForIt = countForIt + 1 #I call on my resolver function to see if we can validate this part resolveAns = resolver(newName, ipAddress, queryType) if (resolveAns["success"] == False): exit() first = 1 iteration = iteration - 1 if (lines[3] == "A"): ipAddress = lines[4] #print("\nGOT TO A") #print(ipAddress) myDig(ipAddress, fullName, iteration, first) break elif ((answerSection_count == 0) and (additionalSection_count == 0)): #print("\nIN OTHER") # if there is nothing in the answer section or the additional section, I check the authority section # print authSection for lines in authSection: lines = lines.split() name = lines[4] #print("\nNAME") #print(name) temp = name.split(".") countTemp2 = 0 for parts in temp: #print (parts) countTemp2 = countTemp2 + 1 #print ("\n\nCOUNT TEMP") #print (countTemp2) #print("\nPRINTING VALUE") #print (temp[countTemp2-2]) resolveAns = resolver(temp[countTemp2 - 2], rootServer, queryType) if (resolveAns["success"] == False): exit() myDig(rootServer, name, iteration, first)
def resolve(self, domain, record, origdomain=None): # domain = 'gmail.com' name = dns.name.from_text(domain) print "***************************************************\n\n" print "COMMAND: resolve " + domain + " " + record for i in range(len(name), 0, -1): s = name.split(i) if self.cache.has_key(s[1].to_text()): startKey = s[1].to_text() break origT = time.time() if not self.answerCache.has_key(domain): self.answerCache[domain] = [] elif self.answerCache.has_key(domain): for i in range(len(self.answerCache[domain])): if self.answerCache[domain][i].has_key(record): endT = time.time() print "*** QUERY " + domain + " for RRType " + record print "*** Answer found in Cache" print "*** FINAL RESPONSE found with latency " + str((endT - origT)) + "\n" print self.answerCache[domain][i][record] return print "*** NS records fetched from cache: " + str(self.cache[startKey]["NS"]) nameServer = self.cache[startKey]["NS"][0] ips = self.cache[nameServer]["A"] print "*** Nameserver " + nameServer + " has IP addresses " + str(ips) print "*** QUERY name server " + "'" + nameServer + "'" + " at " + ips[0] + " for " + "'" + str( name ) + "'" + " " + "'" + record + "'" nameServerList = [] # LOOP HERE depth = 1 root = False while not root: s = name.split(depth) subdomain = s[1] response = None if depth == 1: nameserver = ips[0] # Keeping this intact for the first iteration startT = time.time() response = self.tryQueryingNameServer(nameserver, domain, record) endT = time.time() print "*** Response received with latency: " + str((endT - startT)) print response else: print "*** Start next iteration with domain " + "'" + str(subdomain) + "'" + " nameservers " + str( self.cache[subdomain.to_text()]["NS"] ) nameServer = self.cache[subdomain.to_text()]["NS"] print "*** Nameserver " + "'" + nameServer[0] + "'" + " has IP addresses " + str( self.cache[nameServer[0]]["A"] ) for i in range(len(nameServer)): # temporary hotfix below: select a random server. refactor to recurse nameServerList length if response is empty # nameserver = nameServerList[random.randint(0,len(nameServerList)-1)] print "*** QUERY name server " + "'" + nameServer[i] + "'" + " at " + str( self.cache[nameServer[i]]["A"][0] ) + " for " + "'" + str(name) + "'" + " " + "'" + record + "'" nameserver = self.cache[nameServer[i]]["A"][0] ST = time.time() response = self.tryQueryingNameServer(nameserver, domain, record) endT = time.time() print "*** Response received with latency: " + str((endT - ST)) if response is not None: print response break if response is None: print "All of the queries on the following servers have failed, with blank responses:" for k in range(len(nameServerList)): print nameServerList[k] return if response.rcode() != 0: key = domain key1 = record value = response.to_text() d = {key1: value} self.putSomethingIntoCache2(d, key) return if len(response.answer) != 0: print "_____________________________________________________" print "*** QUERY " + str(name) + " for RRType " + record print "*** FINAL RESPONSE found with latency " + str((endT - origT)) + "\n\n" print response print "_____________________________________________________" root = True d1 = {} if response.authority and response.authority[0].rdtype == 6: key = domain key1 = record value = response.to_text() d = {key1: value} self.putSomethingIntoCache2(d, key) return response if len(response.answer) != 0: if response.authority and response.authority[0].rdtype == 6 and origdomain == None: key = domain key1 = record value = response.to_text() d = {key1: value} self.putSomethingIntoCache2(d, key) return response for i in range(len(response.answer)): s = response.answer[i].to_text() s = s.replace("\n", " ").split(" ") line = s[0:5] if response.answer[i].rdtype == 5: res = self.resolve(s[4], record, domain) if origdomain == None: for r in res.answer: response.answer.append(r) d1 = {record: response.to_text()} self.putSomethingIntoCache2(d1, name) # self.resolveCNAME(domain, nameServerList, record , line[4], response) return response # if (self.cnameFlag): # key = # continue elif origdomain == None: key = s[0].rstrip(".") key1 = s[3].rstrip(".") value = response.to_text() d1 = {key1: value} self.putSomethingIntoCache2(d1, key) # print "FIRST ANSWER = " + str(s[0:5]) # y = 10 # z = 5 # for j in range(len(response.answer)): # y = 10 # z = 5 # for k in range(len(response.answer[j])-1): # print "ANSWER = " + str(s[z:y]) # v = s[y-1] # d1[key][key1].append(v) # z += 5 # y += 5 # self.answerCache.update(d1) break # answer section is filled so the iterations are complete else: # print response j = 10 i = 5 s = response.authority[0].to_text() s = s.replace("\n", " ").split(" ") if s[0].rstrip(".") == domain.strip("www."): key = s[0].rstrip(".") else: key = s[0] key = s[0] key1 = s[3] value = [s[4]] # first nameserver response # Get authority section d1 = {} d2 = {} for k in range(len(response.authority)): lines = response.authority[k].to_text().split("\n") for line in lines: line = line.split(" ") key = line[0] key1 = line[3] if key1 == "DS" or key1 == "RRSIG" or key1 == "NSEC3": v = " ".join(line[4:]) d1.setdefault(key, {}) d2.setdefault(key1, []) d2[key1].append(v) d1[key].update(d2) self.putSomethingIntoCache(d1) else: v = line[4] d1.setdefault(key, {}) d2.setdefault(key1, []) d2[key1].append(v) d1[key].update(d2) i += 5 j += 5 self.putSomethingIntoCache(d1) # initialize keys d2 = {} nameServerList = [] for k in range(len(response.additional)): line = response.additional[k].to_text() line = line.split(" ") nameServerList.append(line[4]) # raw nameserver list key = line[0] d2[key] = {} for k in range(len(response.additional)): line = response.additional[k].to_text() line = line.split(" ") key = line[0] key1 = line[3] d3 = {key1: []} d3[key1].append(line[4]) d2[key].update(d3) self.putSomethingIntoCache(d2) depth += 1 return response
def import_module(name): mod = __import__(name) components = name.split(".") for comp in components[1:]: mod = getattr(mod, comp) return mod
def to_dns_name(cls, name): return dns.name.Name(name.split('.'))