def __main(): t0 = time.time() # Login to the peer tracker and get the current client IPs clients = DNS.login(TRACKER) node = nodeThread.node() node.start() #starts listening # Peer and relay with OR1 messenger.sendPeerRequest(RELAYS[0]) messenger.sendRelayRequest(RELAYS[0]) # Relay with OR2 through OR1 messenger.onionRouteMessage([RELAYS[0], RELAYS[1]], message.RELAY_SYN) # Relay with OR3 through OR1,OR2 messenger.onionRouteMessage([RELAYS[0], RELAYS[1], RELAYS[2]], message.RELAY_SYN) # Register nickname with NP through OR1, OR2, OR3 messenger.onionRouteMessage([RELAYS[0], RELAYS[1], RELAYS[2], RELAYS[3]], message.PROXY_SYN + NICK) time.sleep(3) while (TARGET not in clients): t1 = time.time() if (t1-t0) >= TIMEOUT: clients = DNS.login(TRACKER) t0 = t1 # Send Bob a message messenger.onionRouteMessage([RELAYS[0], RELAYS[1], RELAYS[2], TARGET_NICKNAME_PROXY], message.TO_NICK + TARGET_NICK + message.SEPARATOR + "Testing") time.sleep(TIMEOUT)
def __main(): t0 = time.time() # Login to the peer tracker and get the current client IPs clients = DNS.login(TRACKER) node = nodeThread.node() node.start() #starts listening targetAddress = TARGET #Peer and relay with OR1 messenger.sendPeerRequest(RELAYS[NUM_OF_RELAYS-1]) messenger.sendRelayRequest(RELAYS[NUM_OF_RELAYS-1]) # Relay with OR2 through OR1 messenger.onionRouteMessage([RELAYS[NUM_OF_RELAYS-1], RELAYS[NUM_OF_RELAYS-2]], message.RELAY_SYN) # Relay with OR3 through OR1,OR2 messenger.onionRouteMessage([RELAYS[NUM_OF_RELAYS-1], RELAYS[NUM_OF_RELAYS-2], RELAYS[NUM_OF_RELAYS-3]], message.RELAY_SYN) # Register nickname with NP through OR1, OR2, OR3 messenger.onionRouteMessage([RELAYS[NUM_OF_RELAYS-1], RELAYS[NUM_OF_RELAYS-2], RELAYS[NUM_OF_RELAYS-3], RELAYS[NUM_OF_RELAYS-4]], message.PROXY_SYN + NICK) #Be logged in and keep nodeThread up while 1: t1 = time.time() if (t1-t0) >= TIMEOUT: clients = DNS.login(TRACKER) t0 = t1
def email_is_not_mit_mailing_list(email: str) -> None: """Prevent MIT mailing lists from signing up for Zulip""" if "@mit.edu" in email: username = email.rsplit("@", 1)[0] # Check whether the user exists and can get mail. try: DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT) except DNS.Base.ServerError as e: if e.rcode == DNS.Status.NXDOMAIN: raise ValidationError(mark_safe(MIT_VALIDATION_ERROR)) else: raise AssertionError("Unexpected DNS error")
def not_mit_mailing_list(value): # I don't want ec-discuss signed up for Zulip if "@mit.edu" in value: username = value.rsplit("@", 1)[0] # Check whether the user exists and can get mail. try: DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT) return True except DNS.Base.ServerError, e: if e.rcode == DNS.Status.NXDOMAIN: raise ValidationError(mark_safe(u'That user does not exist at MIT or is a <a href="https://ist.mit.edu/email-lists">mailing list</a>. If you want to sign up an alias for Zulip, <a href="mailto:[email protected]">contact us</a>.')) else: raise
def __main(): t0 = time.time() clients = DNS.login(TRACKER) #Be logged in while 1: t1 = time.time() if (t1-t0) >= TIMEOUT: clients = DNS.login(TRACKER) t0 = t1 for client in clients: if client not in PEERS: PEERS.append(client) messenger.sendPeerRequest(client)
def not_mit_mailing_list(value): # type: (str) -> bool """Prevent MIT mailing lists from signing up for Zulip""" if "@mit.edu" in value: username = value.rsplit("@", 1)[0] # Check whether the user exists and can get mail. try: DNS.dnslookup("%s.pobox.ns.athena.mit.edu" % username, DNS.Type.TXT) return True except DNS.Base.ServerError as e: if e.rcode == DNS.Status.NXDOMAIN: raise ValidationError(mark_safe(MIT_VALIDATION_ERROR)) else: raise return True
def check_dkim(domain, selector, folder): """Test the DKIM records for the given domain.""" correct_record = open(os.path.join(folder, domain, '%s.txt' % selector)).read().split('"')[1] txt_domain = '%s._domainkey.%s' % (selector, domain) results = { 'test': 'dkim', 'passed': False, 'records': [{'domain': txt_domain, 'type': 'TXT', 'value': correct_record}], 'messages': [] } try: actual_records = DNS.dnslookup(txt_domain, 'TXT') if len(actual_records) == 0: results['messages'].append("This test probably is yeilding a false negative.") except DNS.Base.ServerError: actual_records = [] results['messages'].append('No DKIM records found (for selector %s)' % selector) for record in actual_records: current_record = record[0].decode() if current_record == correct_record: if not results['passed']: results['passed'] = True results['messages'].append('Correct DKIM record found at %s' % txt_domain) else: results['messages'].append("%s found instead" % current_record) return results
def run(self): if not self.options.get('user', None): logging.error( 'Need to include --user to report who the submissions is from.') return domain = self.options['rcpt'].split('@')[-1] mx = DNS.mxlookup(domain) if not mx: logging.error("Can't get an MX for %s" % (domain,)) return server = smtplib.SMTP(mx[0][1]) text = email.mime.text.MIMEText('Package submission from %s for %s' % ( self.options['user'],self.project.metadata['name'])) btapp = email.mime.application.MIMEApplication( open(os.path.join('dist', '%s.btapp' % ( self.project.metadata['name'],)), 'rb').read(), 'zip', name='%s.btapp' % self.project.metadata['name']) msg = email.mime.base.MIMEBase('multipart', 'mixed') msg.add_header('Subject', 'Submission of %s for %s' % ( self.project.metadata['name'], self.options['user'])) msg.add_header('From', self.options['user']) msg.add_header('To', self.options['rcpt']) msg.attach(text) msg.attach(btapp) server.sendmail(self.options['user'], self.options['rcpt'], str(msg)) server.quit()
def validate_email(email, check_mx=False,verify=False): """Indicate whether the given string is a valid email address according to the 'addr-spec' portion of RFC 2822 (see section 3.4.1). Parts of the spec that are marked obsolete are *not* included in this test, and certain arcane constructions that depend on circular definitions in the spec may not pass, but in general this should correctly identify any email address likely to be in use as of 2011.""" try: assert re.match(VALID_ADDRESS_REGEXP, email) is not None check_mx |= verify if check_mx: if not DNS: raise Exception('For check the mx records or check if the email exists you must have installed pyDNS python package') DNS.DiscoverNameServers() hostname = email[email.find('@')+1:] mx_hosts = DNS.mxlookup(hostname) for mx in mx_hosts: try: smtp = smtplib.SMTP() smtp.connect(mx[1]) if not verify: return True status, _ = smtp.helo() if status != 250: continue smtp.mail('') status, _ = smtp.rcpt(email) if status != 250: return False break except smtplib.SMTPServerDisconnected: #Server not permits verify user break except smtplib.SMTPConnectError: continue except (AssertionError, ServerError): return False return True
def mxlookup(domain): try: if HAVE_DNSPYTHON: mxrecs = [] mxrequest = resolver.query(domain, 'MX') for rec in mxrequest: mxrecs.append(rec.to_text()) mxrecs.sort() #automatically sorts by priority return [x.split(None,1)[-1] for x in mxrecs] elif HAVE_PYDNS: mxrecs=[] mxrequest = DNS.mxlookup(domain) for dataset in mxrequest: if type(dataset) == tuple: mxrecs.append(dataset) mxrecs.sort() #automatically sorts by priority return [x[1] for x in mxrecs] except Exception: pass #TODO: other dns libraries? return None
def query_mx(domain): if domain not in mx_records: mx_hosts = DNS.mxlookup(domain) mx_records[domain] = mx_hosts if not mx_hosts: print "invalid domain: ", domain return mx_records[domain]
def get_mx_ip(hostname): try: return MX_DNS_CACHE[hostname] except KeyError: mx_hosts = DNS.mxlookup(hostname) MX_DNS_CACHE[hostname] = mx_hosts return mx_hosts
def validate_email(email, check_mx=False, verify=False, debug=False): """Indicate whether the given string is a valid email address according to the 'addr-spec' portion of RFC 2822 (see section 3.4.1). Parts of the spec that are marked obsolete are *not* included in this test, and certain arcane constructions that depend on circular definitions in the spec may not pass, but in general this should correctly identify any email address likely to be in use as of 2011.""" if debug: logger = logging.getLogger('validate_email') logger.setLevel(logging.DEBUG) else: logger = None try: assert re.match(VALID_ADDRESS_REGEXP, email) is not None check_mx |= verify if check_mx: if not DNS: raise Exception('For check the mx records or check if the email exists you must ' 'have installed pyDNS python package') DNS.DiscoverNameServers() hostname = email[email.find('@') + 1:] mx_hosts = DNS.mxlookup(hostname) for mx in mx_hosts: try: smtp = smtplib.SMTP() smtp.connect(mx[1]) if not verify: smtp.quit() return True status, _ = smtp.helo() if status != 250: smtp.quit() if debug: logger.debug(u'%s answer: %s - %s', mx[1], status, _) continue smtp.mail('') status, _ = smtp.rcpt(email) if status == 250: smtp.quit() return True if debug: logger.debug(u'%s answer: %s - %s', mx[1], status, _) smtp.quit() except smtplib.SMTPServerDisconnected: # Server not permits verify user if debug: logger.debug(u'%s disconected.', mx[1]) except smtplib.SMTPConnectError: if debug: logger.debug(u'Unable to connect to %s.', mx[1]) return None except AssertionError: return False except (ServerError, socket.error) as e: if debug: logger.debug('ServerError or socket.error exception raised (%s).', e) return None return True
def get_mx_for_domain(domain): try: mx_list = DNS.mxlookup(domain) best_mx_ip = sorted(mx_list)[0][1] except: best_mx_ip = None return best_mx_ip
def read(self,request,emailadress): emailadress=emailadress.split("@") DNS.DiscoverNameServers() mxhosts=DNS.mxlookup(emailadress[1]) if(mxhosts): return "dogru" else: return "yanlis"
def DNSMXQuery(hostname): r = DNS.DnsRequest(qtype="MX") mx_hosts = DNS.mxlookup(hostname) if len(mx_hosts) == 0: print "0 MX server found\n" else: for mx in mx_hosts: print mx[1] + "\t " + getIp(mx[1])
def get_mx_ip(hostname): if hostname not in MX_DNS_CACHE: try: MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname) except ServerError, e: if e.rcode == 3: # NXDOMAIN (Non-Existent Domain) MX_DNS_CACHE[hostname] = None else: raise
def testDnsRequestMX(self): dnsobj = DNS.DnsRequest('ietf.org') mx_response = dnsobj.qry(qtype='MX') self.assertTrue(mx_response.answers[0]) # is hard coding a remote address a good idea? # I think it's unavoidable. - sk self.assertEqual(mx_response.answers[0]['data'], (0, 'mail.ietf.org')) m = DNS.mxlookup('ietf.org') self.assertEqual(mx_response.answers[0]['data'], m[0])
def dns_txt(domain): try: resp = DNS.dnslookup(domain, 'TXT') except (DNS.ServerError, DNS.Base.TimeoutError) as err: print("{}: {}".format(domain, err.message), file=sys.stderr) return None response = [] for r in resp: response.append(''.join(r)) return response
def BruteForce(ParentDomain, verbose=False): """ Brute Force Forward DNS Lookups for some of the most common subdomain names. These subdomain prefixes are obtained from the file specified in the argument """ BruteForcePrefixes = open(str(utils.get_config()["brute_force_prefixes_file"]), "r") bruteforce_result_list = [] # Set of the domain names obtained by brute forcing for line in BruteForcePrefixes: CurrentName = ( line.replace("\n", "") + "." + ParentDomain ) # Append the subdoamin prefix to the parent domain name [e.g. abc + google.com = abc.google.com] Display = "Current Name is: " + CurrentName if verbose: print "-" * len(Display) print Display try: IP = DNS.dnslookup(unicode(CurrentName, "utf-8"), qtype="A")[ 0 ] # Do a DNS Lookup for the current host name. #The current name will be a combination of the brute force prefix and the parent domain. E.g. - Sub=abc and Parent=xyz.com. So, current=abc.xyz.com bruteforce_result_list.append(CurrentName) if verbose: print "SUCCESS! IP/CNAME = " + IP display_text = ( "WebServerStatus = ON" if utils.is_port_open(IP, 80) == True else "WebServerStatus = OFF" ) # Test whether the destination IP's WebServer is ON. If it isn't, this domain isn't of any interest to us. utils.pretty_print(display_text, len(Display)) else: print CurrentName # print " [Brute Force]" except DNS.Base.ServerError as e: display_text = ( "\nThe DNS Server is Refusing requests. \nPlease use 8.8.8.8 and try again." if "REFUSED" in e.message else "Non-Existent Domain" ) if verbose: utils.pretty_print(display_text, len(Display)) continue except DNS.Base.TimeoutError: # Handle the case where there's a DNS timeout if verbose: utils.pretty_print("Timeout", len(Display)) continue except IndexError: # This handles those (rare) cases where a valid DNS response is returned with no IP address (e.g. - 67.salesforce.com), because of which the variable index 0 of the array is non-existent and we thereforce cannot assign it to the variable 'IP'. if verbose: utils.pretty_print("Non-Existent Domain", len(Display)) continue return bruteforce_result_list
def lookup_domain(self, domain, lookup_record=None): """Looks up the DNS record for *domain* and returns: * None if it does not exist, * The IP address if looking up the "A" record, or * The list of hosts in the "MX" record. The return value, when treated as a boolean, says whether a domain exists. You can pass "a" or "mx" as the *lookup_record* parameter. Otherwise, the *lookup_dns* parameter from the constructor is used. "a" means verify that the domain exists. "mx" means verify that the domain exists and specifies mail servers. """ if lookup_record: lookup_record = lookup_record.lower() else: lookup_record = self._lookup_dns result = None if lookup_record == "a": try: answers = DNS.Request(domain).req().answers except NameError: print('To look up DNS records you must install pydns. Try:') print(' easy_install -UZ pydns') import sys sys.exit(1) except DNS.Lib.PackError: # A part of the domain name is longer than 63. return False # print(repr(answers)) if answers: result = answers[0]['data'] # This is an IP address if result in self.false_positive_ips: result = None # print("Domain '%s' not found" % domain) else: # print("Domain '%s' found with address: %s" \ # % (domain, result)) pass else: # print("Domain '%s' not found" % domain) pass elif lookup_record == "mx": result = DNS.mxlookup(domain) # this is a list of mx records # if result: # print("Domain '%s' has MX records: %s" % (domain, result)) # else: # print("Domain '%s' has no MX records." % domain) else: raise RuntimeError( "Not a valid lookup_record value: " + lookup_record) return result
def resolve_relay_host(self, To): import DNS address, target_host = To.split('@') mx_hosts = DNS.mxlookup(target_host) if not mx_hosts: logging.debug("Domain %r does not have an MX record, using %r instead.", target_host, target_host) return target_host else: logging.debug("Delivering to MX record %r for target %r", mx_hosts[0], target_host) return mx_hosts[0][1]
def get_mx_ip(self, hostname): if hostname not in self.MX_DNS_CACHE: try: self.MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname) except ServerError as e: if e.rcode == 3: # NXDOMAIN (Non-Existent Domain) self.MX_DNS_CACHE[hostname] = None else: raise return self.MX_DNS_CACHE[hostname]
def get_mx_ip(hostname): if hostname not in MX_DNS_CACHE: try: MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname) except ServerError as e: if e.rcode == 3 or e.rcode == 2: # NXDOMAIN (Non-Existent Domain) or SERVFAIL MX_DNS_CACHE[hostname] = None else: raise return MX_DNS_CACHE[hostname]
def testLazy(self): x = DNS.lazy.mxlookup('lathspell.de') self.assertEqual([(10, "mail3av.westend.com")], x) x = DNS.revlookup("212.117.79.2") self.assertEqual("www2.westend.com", x) reqobj=DNS.Request(server='dns1.netcologne.de') x = reqobj.req("lathspell.de", qtype=DNS.Type.A) self.assertEqual("NOERROR", x.header['status']) self.assertEqual(1, x.header['aa']) # authoritative answer self.assertEqual(1, len(x.answers)) self.assertEqual('212.117.79.2', x.answers[0]['data'])
def __main(): # Login to the peer tracker and get the current client IPs clients = DNS.login(TRACKER) # Pick an IP Address targetAddress = clients[0] # Testing operations (not what an actual client will eventually do) messenger.sendPeerRequest(targetAddress) messenger.sendRelayRequest(targetAddress) messenger.onionRouteMessage(targetAddress, targetAddress, targetAddress, targetAddress, message.PROXY_SYN + "Alice") time.sleep(3) messenger.onionRouteMessage(targetAddress, targetAddress, targetAddress, targetAddress, message.PROXY_SYN + "Bob") time.sleep(3) messenger.onionRouteMessage(targetAddress, targetAddress, targetAddress, targetAddress, message.TO_NICK + "Alice" + message.SEPARATOR + "Give Diretide")
def get_mx_hosts(self, mail): domain = mail.split('@')[1] if domain != self.domain or not self.mx_hosts: self.domain = domain try: self.mx_hosts = DNS.mxlookup(self.domain) except DNS.Base.ServerError: self.logger.error('It seems this domain has no MX entry: %s' % self.domain) sys.exit(1) self.logger.debug('MX hosts for domain %s:' % self.domain) for priority, mail_server in self.mx_hosts: self.logger.debug(" * %s %s" % (priority, mail_server)) return self.mx_hosts
def mxlookup(domain): if HAVE_DNSPYTHON: mxrecs=[] mxrequest = DNS.mxlookup(domain) for dataset in mxrequest: if type(dataset) == tuple: mxrecs.append(dataset) mxrecs.sort() #automatically sorts by priority return [x[1] for x in mxrecs] #TODO: other dns libraries? return None
def get_mx(): s = session() """ for contact in s.query(Contacts).filter(Contacts.email != None).all(): answers = dns.resolver.query(contact.email.split('@')[-1], 'MX') print str([rdata.exchange for rdata in answers]) """ # Получаем MX записи для каждого email DNS.DiscoverNameServers() for contact in s.query(Contacts).filter(Contacts.email != None).filter(Contacts.mx_records == None).all(): try: contact.mx_records = repr(DNS.mxlookup(contact.email.split('@')[-1])).encode('utf-8') logger.debug('id %s mx records: %s' % (contact.id, contact.mx_records.decode('utf-8'))) s.merge(contact) s.commit() except: logger.warning('id %s failed to get mx record' % contact.id.decode('utf-8')) # Получаем ip для каждого MX хоста, и его страну for contact in s.query(Contacts).filter(Contacts.mx_hosts == None).filter(Contacts.mx_records != None): # Имеем в базе mx записи типа [(8, 'mail.nmz-group.ru'), (9, 'mx.nmz-group.ru')] # print re.search(r'(\(.*?\))+', contact.mx_records).groups() #print list(ast.literal_eval(contact.mx_records))[0] g = Grab() try: iplist = [] countries = [] for rr in list(ast.literal_eval(contact.mx_records)): host = repr(rr).strip('()').split(', ')[1].strip("'") ip = gethostbyname(host) try: g.go('http://www.ip2nation.com/ip2nation') g.doc.set_input("ip", ip) g.doc.submit() country = g.doc.select('//div[@id="visitor-country"]').text().encode('utf-8') #logger.debug('get country by ip %s : %s' % (ip, country)) countries.append(country) except Exception as e: logger.critical('get country by ip failed %s: %s %s' % (ip, e.message, e.args)) iplist.append(ip) contact.mx_hosts = repr(iplist).encode('utf-8') contact.mx_countries = repr(countries).encode('utf-8') s.merge(contact) s.commit() logger.debug('id: %s get host by name : %s' % (contact.id, repr(iplist))) logger.debug('id: %s country : %s' % (contact.id, repr(countries))) except Exception as e: logger.critical('get host by name failed %s: %s %s' % (contact.id, e.message, e.args))
def lookup_domain(self, domain, lookup_record=None, **kw): """Looks up the DNS record for *domain* and returns: * None if it does not exist, * The IP address if looking up the "A" record, or * The list of hosts in the "MX" record. The return value, if treated as a boolean, says whether a domain exists. You can pass "a" or "mx" as the *lookup_record* parameter. Otherwise, the *lookup_dns* parameter from the constructor is used. "a" means verify that the domain exists. "mx" means verify that the domain exists and specifies mail servers. """ import DNS lookup_record = lookup_record.lower() if lookup_record else self._lookup_dns if lookup_record not in ('a', 'mx'): raise RuntimeError("Not a valid lookup_record value: " + lookup_record) if lookup_record == "a": request = DNS.Request(domain, **kw) try: answers = request.req().answers except (DNS.Lib.PackError, UnicodeError): # A part of the domain name is longer than 63. return False if not answers: return False result = answers[0]['data'] # This is an IP address if result in self.false_positive_ips: # pragma: no cover return False return result try: return DNS.mxlookup(domain) except UnicodeError: pass return False
import DNS DNS.AddNameServer('172.16.0.4') res = r.req('www.microsoft.com',qtype='A') print len(res.answers),'different A records' print map(lambda x:x['data'], res.answers)
def get_mx_ip(hostname): if hostname not in MX_DNS_CACHE: MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname) return MX_DNS_CACHE[hostname]
def __init__(self, *args, **kwargs): LookupHandler.__init__(self, *args, **kwargs) if not DNS: return DNS.ParseResolvConf() self.req = DNS.Request(qtype="TXT")
def nsname(ipaddrs): return DNS.revlookup(ipaddrs, 3)
# that there will be no such remuneration or payment rendered to them for any such contribution. # END OF LICENSE AGREEMENT ################################################################################################################################################################################## # ADDITIONAL NOTES: # 17) If a user finds a significant flaw or makes a significant improvement to this software, please feel free to notify the original developers so that we may also # include your user improvement in the next release; users are not obligated to do this, but we would enjoy this courtesy tremendously. # # 18) Sections 0) a) 0) b) and 1) a) are sufficient for use; however sections 1) b) through 1) h) are presented to clarify 1 a) and to enforce non-discrimination and non-exclusion of use. # For example some people may choose to redefine the meaning of the words "person" "human being" or "sentient individual" to exclude certain types of people. # This would be deemed unacceptable and is specifically rejected by the enumeration presented. If the wording presented is problematic please contact us and suggest a change, # and it will be taken into consideration. ################################################################################################################################################################################# ################ BEGINNING OF PROGRAM ################### import sys import DNS # routine to find your name servers DNS.DiscoverNameServers() # loop over shell input arguments and make dns requests to your nameservers for arg in sys.argv[1:]: print arg request = DNS.Request(arg) response = request.req(arg) # print the lookup results response.show() response.args quit() ################ END OF PROGRAM #######################
def find_host_from_ip(ip_addr): return DNS.revlookup(ip_addr)
large list of candidate domain names -- if no NS is present, the candidate likely does not have a domain registration. (C) mwatkins, a WebHostingTalk.com user. Use for any purpose you like provided attribution is provided. """ # install PyDNS: http://pydns.sourceforge.net # FreeBSD: /usr/ports/dns/py-dns import DNS import threading import time import Queue from optparse import OptionParser DNS.ParseResolvConf() WORKERS = 60 def has_ns(domain): try: query = DNS.Request(domain, qtype="NS").req() except: time.sleep(0.014) has_ns(domain) status = query.header['status'] return (domain, status) def get_domains_from_file(path):
#!/usr/bin/python import sys sys.path.insert(0, '..') import DNS # automatically load nameserver(s) from /etc/resolv.conf # (works on unix - on others, YMMV) DNS.ParseResolvConf() # lets do an all-in-one request # set up the request object r = DNS.DnsRequest(name='google.it', qtype='A') # do the request a = r.req() # and do a pretty-printed output a.show() # now lets setup a reusable request object r = DNS.DnsRequest(qtype='ANY') res = r.req("a.root-servers.nex", qtype='ANY') res.show() res = r.req("proxy.connect.com.au") res.show() # do a TCP reply r = DNS.DnsRequest("imsavscan.netvigator.com", qtype="A", server=['8.8.8.8'], protocol='tcp',
def gethttpcode(): conn = MySQLdb.connect(host='172.26.253.3', port=3306, user='******', passwd='platform') conn.set_character_set('utf8') cur = conn.cursor() cur.execute("SET NAMES utf8") cur.execute("use malicious_domain_sys;") conn.commit() i = 0 while 1 == 1: #ip_addr = '' ip_set = set() flag = 1 domain = '' if lock0.acquire(): try: # 取出一个域名 domain, ip_old, t_flag = result.pop() times = int(t_flag / 1000) % 10 if times == 0: times = 9 lock0.release() except: # 域名池为空 #print "return" lock0.release() cur.close() conn.commit() conn.close() return ip_old_set = set(ip_old.split(',')) #n = hash(domain)%len(dnslist) DNS.defaults['server'] = '223.6.6.6' #dnslist[n] # print domain + "\t" + DNS.defaults['server'][0] reqobj = DNS.Request() try: answerobj = reqobj.req(name=domain, qtype=DNS.Type.A) if not len(answerobj.answers): flag = 2 except: flag = 3 if flag == 1: for ip in answerobj.answers: # print ip['data'] ip_set.add(ip['data']) if times != 1: times -= 1 if not ip_set.issubset(ip_old_set): ip_set = ip_set | ip_old_set ip_addr = gen_ipstr(ip_set) t_flag = wei(wei(t_flag, 3, flag), 4, times) cur.execute( "UPDATE malicious_info SET IP = '%s', flag = %d, IP_detect_time= '%s' WHERE id = %d" % (ip_addr, t_flag, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), hash(domain))) #print domain + ' : ' + str(ip_old_set) + ' -> ' + str(ip_set) + ' <' + str(t_flag) + '>' conn.commit() else: t_flag = wei(wei(t_flag, 3, flag), 4, times) cur.execute( "UPDATE malicious_info SET flag = %d WHERE id = %d" % (t_flag, hash(domain))) conn.commit() else: print "==1" if not ip_set.issubset(ip_old_set): ip_addr = gen_ipstr(ip_set) t_flag = wei(wei(t_flag, 3, flag), 4, 9) cur.execute( "INSERT INTO ip_history1 (ID,IP,record_time) SELECT ID,SUBSTRING_INDEX(IP, ',', 1),IP_detect_time FROM malicious_info WHERE malicious_info.ID = %d" % hash(domain)) cur.execute( "UPDATE malicious_info SET IP = '%s', flag = %d, IP_detect_time= '%s' WHERE id = %d" % (ip_addr, t_flag, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), hash(domain))) print domain + ' : ' + str(ip_old_set) + ' -> ' + str( ip_set) + ' <' + str(t_flag) + '>' conn.commit()
#!/usr/bin/env python # Basoc DNS library example - Chapter 4 - DNS-basic.py import sys, DNS query = sys.argv[1] DNS.DiscoverNameServer() reqobj = DNS.Request() answerobj = reqobj.req(name=query, qtype=DNS.Type.ANY) if not len(answerobj.answers): print "Not found." for item in answerobj.answers: print "%-5s %s" % (item['typename'], item['data'])
server that returned 1 or more answers. If no server returned any answers, return [].""" for ns in nslist: reqobj = DNS.Request(sever=ns) try: answers = reqobj.req(name=qstring, qtype=qtype).answers if len(answers): return answers except DNS.Base.DNSError: pass return [] def nslookup(qstring, qtype, verbose=1): nslist = findnameservers(qstring) if nslist == None: raise (RuntimeError, "Couldn't find nameserver to use.") if verbose: print('using nameservers:', ", ".join(nslist)) return getrecordsfromnamesever(qstring, qtype, nslist) if __name__ == '__main__': query = sys.argv[1] DNS.DiscoverNameServers() answers = nslookup(query, DNS.Type.ANY) if not len(answers): print('Not found.') for item in answers: print('%-5s %s' % (item['typename'], item['data']))
def Collect(cursor): DNS.DiscoverNameServers() user_cache = {} remote_db = MySQLdb.connect(user=AUTH['username'], db=AUTH['database'], passwd=AUTH['password'], host=AUTH['hostname']) remote_cursor = remote_db.cursor(MySQLdb.cursors.DictCursor) now = datetime.datetime.now() one_minute = datetime.timedelta(minutes=1) five_minutes = datetime.timedelta(minutes=5) # We need to be five minutes behind so the summary db can keep up now -= five_minutes for i in range(24 * 60): # Internal flows aren't logged in the db, so we ignore zii here to # avoid double counting flows originating from zii statement = ( 'select internalip, sum(bytes) from flows_%04d%02d where ' 'time >= %s and time < %s and node="zii" group by internalip;' % (now.year, now.month, sql.FormatSqlValue( 'date', now - five_minutes), sql.FormatSqlValue('date', now))) remote_cursor.execute(statement) usage = {} epoch = time.mktime((now - five_minutes).timetuple()) for row in remote_cursor: ip = row['internalip'] print '%s %s %s' % (now - five_minutes, ip, row['sum(bytes)']) name = 'Netflow' if ip == '192.168.1.20': name = 'Gateway Netflow' cursor.execute('insert ignore into sensors ' '(epoch_seconds, sensor, value, hostname) ' 'values(%s, "%s", "%s", "%s");' % (epoch, name, row['sum(bytes)'], ip)) cursor.execute('commit;') # Determine the "user" for this IP if ip not in user_cache: try: ip_rev = ip.split('.') ip_rev.reverse() arpa = '%s.in-addr.arpa' % '.'.join(ip_rev) hostname = DNS.dnslookup(arpa, qtype='PTR')[0] owner = DNS.dnslookup(hostname, qtype='TXT')[0][0] print('%s Looking up the owner of %s gave %s' % (datetime.datetime.now(), ip, owner)) except Exception, e: print 'Owner lookup error for %s: %s' % (ip, e) owner = 'Unknown' user_cache[ip] = owner print '%s Owner of this IP is %s' % (datetime.datetime.now(), user_cache[ip]) usage.setdefault(user_cache[ip], 0) usage[user_cache[ip]] += row['sum(bytes)'] for owner in usage: cursor.execute('insert ignore into sensors ' '(epoch_seconds, sensor, value, hostname) ' 'values(%s, "User Netflow", "%s", "%s");' % (epoch, usage[owner], owner)) cursor.execute('commit;') now -= one_minute
local = ' '.join(spf) break else: return spftxt # No local policy adds for v=spf1 -all # Processing limits not applied to local policy. Suggest # inserting 'local' mechanism to handle this properly #MAX_LOOKUP = 100 return 'v=spf1 ' + local def _test(): import doctest, spf return doctest.testmod(spf) DNS.DiscoverNameServers() # Fails on Mac OS X? Add domain to /etc/resolv.conf if __name__ == '__main__': import sys if len(sys.argv) == 1: print USAGE _test() elif len(sys.argv) == 2: q = query(i='127.0.0.1', s='localhost', h='unknown', receiver=socket.gethostname()) print q.dns_spf(sys.argv[1]) elif len(sys.argv) == 4: print check(i=sys.argv[1], s=sys.argv[2],
def mainloop(self, unused_args): """HIP DNS proxy main loop.""" logging.info('Dns proxy for HIP started') self.parameter_defaults() # Default virtual interface and address for dnsproxy to # avoid problems with other dns forwarders (e.g. dnsmasq) os.system('ifconfig lo:53 %s' % (self.bind_ip, )) self.servsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.servsock.bind((self.bind_ip, self.bind_port)) except socket.error: logging.error('Port %d already in use. See HOWTO.', self.bind_port) return self.servsock.settimeout(self.app_timeout) self.hosts = [] if self.hostsnames: for hostsname in self.hostsnames: self.hosts.append(hosts.Hosts(hostsname)) else: if os.path.exists(self.hiphosts): self.hosts.append(hosts.Hosts(self.hiphosts)) if os.path.exists(DEFAULT_HOSTS): self.hosts.append(hosts.Hosts(DEFAULT_HOSTS)) self.write_local_hits_to_hosts() util.init_wantdown() # Initialize signal handler for shutdown util.init_hup() # Initialize signal handler for reload while not util.wantdown(): self.resolvconf.parse() if self.server_ip is None: self.server_ip = self.resolvconf.nameserver if util.wanthup(): logging.info('Received SIGHUP. Picking new upstream server') self.server_ip = self.resolvconf.nameserver util.wanthup(False) logging.info('Connecting to upstream DNS server %s ...', self.server_ip) if ':' not in self.server_ip: server_family = socket.AF_INET else: server_family = socket.AF_INET6 self.clisock = socket.socket(server_family, socket.SOCK_DGRAM) self.clisock.settimeout(self.dns_timeout) try: self.clisock.connect((self.server_ip, self.server_port)) self.connected = True logging.debug('... connected!') self.resolvconf.nameserver = self.bind_ip except socket.error: logging.error('Connecting to upstream DNS server failed!') time.sleep(3) self.connected = False while self.connected and (not util.wantdown()) and ( not util.wanthup()): try: self.hosts_recheck() if self.connected: rlist, _, _ = select.select( [self.servsock, self.clisock], [], [], 5.0) else: rlist, _, _ = select.select([self.servsock], [], [], 5.0) self.clean_queries() if self.servsock in rlist: payload, sender = self.servsock.recvfrom(2048) packet = DNS.DeSerialize(payload).get_dict() self.handle_query(packet, sender) if self.connected and self.clisock in rlist: payload, sender = self.clisock.recvfrom(2048) logging.info('Packet from DNS server %d bytes from %s', len(payload), sender) packet = DNS.DeSerialize(payload).get_dict() self.handle_response(packet, sender) except (select.error, OSError), exc: if exc[0] == errno.EINTR: pass else: logging.exception('Exception:') except socket.error, exc: logging.info('Connection to upstream DNS server lost') self.connected = False
import sys import DNS # Verify arguments if len(sys.argv) != 3: print 'Usage: query_dns_server.py <Hostname> <DNS Server>' sys.exit(3) hostname = sys.argv[1] dns_server = sys.argv[2] try: start = time.time() # Perform the DNS query s = DNS.Request(name=hostname, server=dns_server) resolve = s.req().answers end = time.time() except: print "Timeout from DNS server '%s'." % dns_server sys.exit(2) millisec = (end - start).microseconds / 1000 print "Hostname '%s' was%s found via DNS server '%s'. Latency: %sms|'Latency(MS)'=%ms;;;" % ( hostname, '' if resolve else ' not', dns_server, millisec, millisec, )
def verificationJob(self): try: verificationList = EmailLists.objects.get( listName=self.extraArgs['listName']) domain = verificationList.owner.domain if not os.path.exists('/home/cyberpanel/' + domain): os.mkdir('/home/cyberpanel/' + domain) tempStatusPath = '/home/cyberpanel/' + domain + "/" + self.extraArgs[ 'listName'] logging.CyberCPLogFileWriter.statusWriter( tempStatusPath, 'Starting verification job..') counter = 1 allEmailsInList = verificationList.emailsinlist_set.all() for items in allEmailsInList: if items.verificationStatus != 'Verified': try: email = items.email domainName = email.split('@')[1] records = DNS.dnslookup(domainName, 'MX') for mxRecord in records: # Get local server hostname host = socket.gethostname() server = smtplib.SMTP() server.set_debuglevel(0) # SMTP Conversation server.connect(mxRecord[1]) server.helo(host) server.mail('host' + "@" + host) code, message = server.rcpt(str(email)) server.quit() # Assume 250 as Success if code == 250: items.verificationStatus = 'Verified' items.save() break else: items.verificationStatus = 'Verification Failed' logging.CyberCPLogFileWriter.writeToFile( email + " verification failed with error: " + message) items.save() logging.CyberCPLogFileWriter.statusWriter( tempStatusPath, str(counter) + ' emails verified so far..') counter = counter + 1 except BaseException, msg: items.verificationStatus = 'Verification Failed' items.save() counter = counter + 1 logging.CyberCPLogFileWriter.writeToFile(str(msg)) logging.CyberCPLogFileWriter.statusWriter( tempStatusPath, str(counter) + ' emails successfully verified. [200]')
def ipaddr(hostname, rectype='A'): return DNS.dnslookup(hostname, rectype, 3)
def __init__(self): DNS.ParseResolvConf() self.req = DNS.Request(qtype="TXT")
def wait_for_dns(external_dns_server, fqdn, timeout=30, address=None, cname=None): """Waits for a DNS record to appear in all nameservers for the domain.""" # Input validation if external_dns_server is None or len(external_dns_server) == 0: raise ValueError("An 'external_dns_server' IP address must be specified") if address is None and cname is None: raise ValueError("A value for 'address' or 'cname' must be specified") # Split the FQDN in to each of it's domain parts split_fqdn = fqdn.split('.') # Further input validation if len(split_fqdn) <= 1: raise ValueError("'fqdn' must specify a fully-qualified domain name") # Search for NS records for each part recursively (e.g. for a.b.c.d, then b.c.d, then c.d, then d) for i in range(len(split_fqdn) - 1): # Rejoin the domain name ns_fqdn_search = '.'.join(split_fqdn[i:]) # Perform the DNS request r = DNS.DnsRequest(ns_fqdn_search, qtype='NS', server=[external_dns_server]) ns_res = r.req() # If we got NS servers, then stop searching. The explicit check for NS records is # required because doing an NS record search for a record that is a CNAME doesn't # do what you might expect. if ns_res.header['status'] == 'NOERROR' and len([answer for answer in ns_res.answers if answer['typename'] == 'NS']) > 0: break else: # We reached the end of the list, and didn't find a valid NS record, so raise exception raise Exception('Unable to find NS records for domain') # Extract the list of NS servers we need to check ns_list = [answer['data'] for answer in ns_res.answers if answer['typename'] == 'NS'] # Empty list of nameservers that contain the right result completed_name_servers = [] start_time = time.time() # Determine the query type if address is not None: # Determine if we're doing a v6 lookup if ':' in address: qtype = 'AAAA' # The data we get back from DNS is "packed" (byte representation) address = ipaddress.IPv6Address(str(address)).packed else: qtype = 'A' elif cname is not None: qtype = 'CNAME' # CNAME checks needs to be case-insensitive cname = cname.lower() # Loop whilst we're waiting for all the nameservers to pick up, but don't exceed our timeout while len(completed_name_servers) != len(ns_list) and time.time() - start_time < timeout: # Iterate over all nameservers for nameserver in ns_list: # Skip past nameservers we've already validated if nameserver in completed_name_servers: continue # Perform the DNS lookup r = DNS.DnsRequest(fqdn, qtype=qtype, server=[nameserver]) res = r.req() # If the query succeeded if res.header['status'] == 'NOERROR': # Iterate over the answers and make sure we have an A record for the address for answer in res.answers: if address is not None and answer['typename'] == qtype and answer['data'] == address: completed_name_servers.append(nameserver) elif cname is not None and answer['typename'] == qtype and answer['data'].lower() == cname: completed_name_servers.append(nameserver) # If we've not got all nameservers, sleep a little if len(completed_name_servers) != len(ns_list): time.sleep(1) # If we didn't ever succeed, raise an exception if len(completed_name_servers) != len(ns_list): raise Exception("Timeout whilst waiting for DNS records to update. Completed: " + str(completed_name_servers))
__doc__ = """Wrapper to pyDNS to retrieve all DNS results for a given hostname""" import DNS DNS.ParseResolvConf() resolver = DNS.DnsRequest(qtype='A') def lookup(hostname): global resolver res = resolver.req(hostname) if len(res.answers) == 0: return [] return [x['data'] for x in res.answers if x['typename'] == 'A']
statu = row[2] time = row[3] addrs.append(ip) status[ip] = statu except: print "Error: unable to fecth data" print addrs print status Domain = 'www.test.com' for addr in addrs: try: s = DNS.Request(name=Domain, server=addr, timeout=10) records = s.req().answers if not len(records): print "Not found" for i in records: print addr + ":[%s - %s]" % (i['typename'], i['data']) if status[addr] == 'false': now = datetime.datetime.now() now = str(now) now = now[:19] sql = "update server_list set `status` = 'true',`updatetime` = '" + now + "' where `ip` = '" + addr + "'" content = "【EflyDNS监控提醒】:缓存代理[" + addr + "]已重新恢复正常服务!" title = "【EflyDNS监控提醒】:缓存代理[" + addr + "]已重新恢复正常服务!"
def configure(conffile): """ Read configuration file into the global config dictionary and ensure sections are present """ # Perform a quick (but race vulnerable) config file existence and permission # check. try: # Open the config for reading (or fail) conffilefp = open(conffile, 'r') # Check the permissions on it if (os.stat(conffile).st_mode & stat.S_IWOTH): raise GeneralError( "Configuration file %s is world writable. Please remove the 'kick-me' sign with 'chmod og-wx %s'" % (conffile, conffile)) except (IOError, OSError) as err: raise GeneralError("Unable to read configuration file %s: %s" % (conffile, err)) # Read in the config and perish if nothing is read config.readfp(conffilefp) if not config.has_section('system'): raise GeneralError( "No [system] section in configuration file %s - EXITING" % conffile) # System config. Items commented out are features from the FreeBSD # code that need to be recoded into the LIO version. config['system']['loglevel'] = config.get('system', 'loglevel', fallback='info') config['system']['target_name'] = config.get( 'system', 'target_name', fallback='iqn.1997-03.com.citon:target0') config['system']['serial_attribute'] = config.get( 'system', 'serial_attribute', fallback='ID_SERIAL_SHORT') config['system']['czstc_pid'] = config.get( 'system', 'czstc_pid', fallback='/var/run/czs-targetcontrol.pid') # XXX Not yet implemented - Will need more sanity check code to prevent # swapping device LUNs on reboot #config['system']['save_changes'] = config.get('system', 'save_changes', fallback='true') # XXX Implemented for attach but not detach # # Set to "true" to normalize all devices to use the by-id path for access. # This is recommended for most uses. When enabled, the /dev/disk/by-id/ # path will automatically be selected regardless of how the device ID is # passed. # !!! DO NOT CHANGE THIS UNLESS ALL DEVICES ARE UNMAPPED !!! If some devices # are already mapped, you may be unable to add/remove devices accurately. #by_id = true config['system']['by_id'] = config.get('system', 'by_id', fallback='false') # Gone for now. May work back in for easier startup or updated FreeBSD port #config['system']['auth_group'] = config.get('system', 'auth_group', fallback='no-authentication') #config['system']['portal_group'] = config.get('system', 'portal_group', fallback='pg0') #config['system']['target_config'] = config.get('system', 'target_config', fallback='/etc/ctl.conf') #config['system']['dummy_lun_img'] = config.get('system', 'dummy_lun_img', fallback='/root/czs-targercontrol-dummy-lun0.img') # Sanity check loglevel if not re.match('debug|info|warning|error|critical', config['system']['loglevel']): raise GeneralError( "Invalid loglevel %s in %s - Must be debug, info, warning, error, or critical" % (config['system']['loglevel'], conffile)) # Sanity check device serial to name lookup settings if 'lookup' in config: config['lookup']['enable'] = config.get('lookup', 'enable', fallback='false') config['lookup']['domain'] = config.get('lookup', 'domain') # Prime the DNS system. This uses /etc/resolv.conf. DNS.DiscoverNameServers() else: config['lookup'] = {} config['lookup']['enable'] = 'false' # Sanity check log settings if 'syslog' in config: config['syslog']['enable'] = config.get('syslog', 'enable', fallback='false') # Check facility for proper code name if not re.match('user|daemon|syslog|local[0-7]', config.get('syslog', 'facility', fallback='local0')): raise GeneralError( "Invalid syslog facility %s in %s - Must be user, daemon, syslog, or local 0-7" % (config.get('syslog', 'facility'), conffile)) config['syslog']['syslog_server'] = config.get('syslog', 'syslog_server', fallback='/dev/log') # Replace with syslog facility config['syslog']['facility'] = 'LOG_' + config.get( 'syslog', 'facility', fallback='daemon').upper() else: config['syslog'] = {} config['syslog']['enable'] = 'false' # Mail config if ('mail' in config) and config.getboolean('mail', 'enable'): # Make sure everything is set. req = ['smtp_server', 'sender', 'recipients', 'subject_prefix'] for item in req: if not config.has_option('mail', item): raise GeneralError("Mail enabled but missing %s value" % item) # Break out the recipient list if item == 'recipients': config['mail']['recipients'] = config.get('mail', 'recipients') else: config['mail'] = {'enable': 'false'} return True
def handle_response(self, packet, sender): """Handle DNS response from upstream server.""" if packet['qdcount'] == 0: logging.warn('Bad response from upstream server: %s', pprint.pformat(packet)) return # Find original query query_id_o = packet['id'] query_o = self.find_query(sender[0], sender[1], query_id_o) if query_o and packet['qdcount'] > 0: qname = packet['questions'][0][0] qtype = packet['questions'][0][1] send_reply = True query_again = False hit_found = False packet_o = query_o[0] # Replace with the original query id packet['id'] = packet_o['id'] if qtype == DNS.Type.HIP and query_o[3] in (DNS.Type.A, DNS.Type.AAAA): # Restore qtype packet['questions'][0][1] = query_o[3] self.hip_lookup(packet) if packet['ancount'] > 0: hit_found = True if (not self.prefix or (hit_found and not (self.getaaaa(qname) or self.geta(qname)))): query_again = True send_reply = False elif self.prefix: hit_found = True packet['questions'][0][0] = (self.prefix + packet['questions'][0][0]) for answer in packet['answers']: answer[0] = self.prefix + answer[0] elif qtype in (DNS.Type.A, DNS.Type.AAAA): hit = self.getaaaa_hit(qname) ip6 = self.getaaaa(qname) ip4 = self.geta(qname) for answer in packet['answers']: if answer[1] in (DNS.Type.A, DNS.Type.AAAA): self.cache_name(qname, answer[4], answer[3]) if hit is not None: for answer in packet['answers']: if (answer[1] == DNS.Type.A or (answer[1] == DNS.Type.AAAA and not hosts.valid_hit(answer[4]))): add_hit_ip_map(hit[0], answer[4]) # Reply with HIT/LSI once it's been mapped # to an IP if ip6 is None and ip4 is None: if (packet_o['ancount'] == 0 and not self.prefix): # No LSI available. Return IPv4 tmp = packet['answers'] packet = packet_o packet['answers'] = tmp packet['ancount'] = len(packet['answers']) else: packet = packet_o if self.prefix: packet['questions'][0][0] = ( self.prefix + packet['questions'][0][0]) for answer in packet['answers']: answer[0] = (self.prefix + answer[0]) else: send_reply = False elif query_o[3] == 0: # Prefix is in use # IP was queried for cache only send_reply = False elif qtype == DNS.Type.PTR and isinstance(query_o[3], str): packet['questions'][0][0] = query_o[3] for answer in packet['answers']: answer[0] = query_o[3] if query_again: if hit_found: qtypes = [DNS.Type.AAAA, DNS.Type.A] pckt = copy.deepcopy(packet) else: qtypes = [query_o[3]] pckt = copy.copy(packet) pckt['qr'] = 0 pckt['answers'] = [] pckt['ancount'] = 0 pckt['nslist'] = [] pckt['nscount'] = 0 pckt['additional'] = [] pckt['arcount'] = 0 for qtype in qtypes: if self.prefix: query = (packet, query_o[1], query_o[2], 0) else: query = (packet, query_o[1], query_o[2], qtype) self.query_id = (self.query_id % 65535) + 1 pckt['id'] = self.query_id pckt['questions'][0][1] = qtype outbuf = DNS.Serialize(pckt).get_packet() self.clisock.sendto(outbuf, (self.server_ip, self.server_port)) self.add_query(self.server_ip, self.server_port, self.query_id, query) packet['questions'][0][1] = query_o[3] if send_reply: outbuf = DNS.Serialize(packet).get_packet() self.servsock.sendto(outbuf, (query_o[1], query_o[2]))
def test_example(self): request = DNS.Request() response = request.req(name='www.example.com', server='pdns') self.assertEqual(len(response.answers), 1) self.assertEqual(response.answers[0]['data'], '192.168.1.11')
def handle_query(self, packet, sender): """Handle DNS query from downstream client.""" qtype = packet['questions'][0][1] sent_answer = False if qtype in (DNS.Type.A, DNS.Type.AAAA, DNS.Type.PTR): if self.hip_cache_lookup(packet): try: outbuf = DNS.Serialize(packet).get_packet() self.servsock.sendto(outbuf, sender) sent_answer = True except socket.error: logging.exception('Exception:') elif (self.prefix and packet['questions'][0][0].startswith(self.prefix)): # Query with HIP prefix for unsupported RR type. # Send empty response. packet['qr'] = 1 try: outbuf = DNS.Serialize(packet).get_packet() self.servsock.sendto(outbuf, sender) sent_answer = True except socket.error: logging.exception('Exception:') if self.connected and not sent_answer: logging.info('Query type %d for %s from %s', qtype, packet['questions'][0][0], (self.server_ip, self.server_port)) query = (packet, sender[0], sender[1], qtype) # FIXME: Should randomize for security self.query_id = (self.query_id % 65535) + 1 pckt = copy.copy(packet) pckt['id'] = self.query_id if ((qtype == DNS.Type.AAAA or (qtype == DNS.Type.A and not self.disable_lsi)) and not is_reverse_hit_query(packet['questions'][0][0])): if not self.prefix: pckt['questions'][0][1] = DNS.Type.HIP if (self.prefix and pckt['questions'][0][0].startswith(self.prefix)): pckt['questions'][0][0] = pckt['questions'][0][0][ len(self.prefix):] pckt['questions'][0][1] = DNS.Type.HIP if qtype == DNS.Type.PTR and not self.disable_lsi: qname = packet['questions'][0][0] addr_str = hosts.ptr_to_addr(qname) if (addr_str is not None and hosts.valid_lsi(addr_str)): query = (packet, sender[0], sender[1], qname) hit_str = lsi_to_hit(addr_str) if hit_str is not None: pckt['questions'][0][0] = hosts.addr_to_ptr(hit_str) outbuf = DNS.Serialize(pckt).get_packet() self.clisock.sendto(outbuf, (self.server_ip, self.server_port)) self.add_query(self.server_ip, self.server_port, self.query_id, query)
import random import Queue import threading import time from log import * from pymongo import * import sys reload(sys) sys.setdefaultencoding('utf8') '''建立链接''' client = MongoClient('172.29.152.152', 27017) db = client.domain_icp_analysis collection = db.domain_ttl2 '''DNS请求''' req_obj = DNS.Request() '''相关参数''' timeout = 10 # 超时时间 server = '222.194.15.253' '''全局变量''' g_cnames = [] g_cnames_ttl = [] g_ips = [] g_ips_ttl = [] res_q = Queue.Queue() def get_ns(dm, ns_server): ''' 获取域名ns服务器
def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeout=10): """Indicate whether the given string is a valid email address according to the 'addr-spec' portion of RFC 2822 (see section 3.4.1). Parts of the spec that are marked obsolete are *not* included in this test, and certain arcane constructions that depend on circular definitions in the spec may not pass, but in general this should correctly identify any email address likely to be in use as of 2011.""" if debug: logger = logging.getLogger('validate_email') logger.setLevel(logging.DEBUG) else: logger = None try: assert re.match(VALID_ADDRESS_REGEXP, email) is not None check_mx |= verify if check_mx: if not DNS: raise Exception( 'For check the mx records or check if the email exists you must ' 'have installed pyDNS python package') DNS.DiscoverNameServers() hostname = email[email.find('@') + 1:] mx_hosts = get_mx_ip(hostname) for mx in mx_hosts: try: smtp = smtplib.SMTP(timeout=smtp_timeout) smtp.connect(mx[1]) if not verify: smtp.quit() return True status, _ = smtp.helo() if status != 250: smtp.quit() if debug: logger.debug(u'%s answer: %s - %s', mx[1], status, _) continue smtp.mail('') status, _ = smtp.rcpt(email) if status == 250: smtp.quit() return True if debug: logger.debug(u'%s answer: %s - %s', mx[1], status, _) smtp.quit() except smtplib.SMTPServerDisconnected: # Server not permits verify user if debug: logger.debug(u'%s disconected.', mx[1]) except smtplib.SMTPConnectError: if debug: logger.debug(u'Unable to connect to %s.', mx[1]) return None except AssertionError: return False except (ServerError, socket.error) as e: if debug: logger.debug('ServerError or socket.error exception raised (%s).', e) return None return True
#!/usr/bin/python import sys sys.path.insert(0, '..') import DNS # automatically load nameserver(s) from /etc/resolv.conf # (works on unix - on others, YMMV) DNS.ParseResolvConf() r = DNS.Request(qtype='srv') res = r.req('_ldap._tcp.openldap.org') res.show() print res.answers
def getNodesOfService(svc, port): # get A-records for current service q = DNS.dnslookup(svc, 'A') service_nodes = ["{0}:{1}".format(item, port) for item in q] return service_nodes
#!/usr/bin/python3 import sys sys.path.insert(0, '..') import DNS # automatically load nameserver(s) from /etc/resolv.conf # (works on unix - on others, YMMV) DNS.ParseResolvConf() # lets do an all-in-one request # set up the request object r = DNS.DnsRequest(name='munnari.oz.au', qtype='A') # do the request a = r.req() # and do a pretty-printed output a.show() # now lets setup a reusable request object r = DNS.DnsRequest(qtype='ANY') res = r.req("a.root-servers.nex", qtype='ANY') res.show() res = r.req("proxy.connect.com.au") res.show() # do a TCP reply r = DNS.DnsRequest("imsavscan.netvigator.com", qtype="A", server=['8.8.8.8'], protocol='tcp', timeout=300)