예제 #1
0
    def _resolve(self, query: bytes, dest_addr):
        pp = protocol.PackageParser(query)
        query_pkg = pp.parsePackage()

        pb_res = protocol.PackageBuilder(query_pkg.identification)
        for question in query_pkg.questions:
            qtype = question.type
            domain_name = question.domain_name
            pb_res.add_question(domain_name, qtype)

            respond = self.get_from_cache(domain_name, qtype)
            if respond is None: continue
            if respond:
                for rdata, ttl in respond:
                    pb_res.add_answer(domain_name, qtype, ttl, rdata)
            else:
                try:
                    answ_pkg, _ = resolver.ask_serverok(
                        domain_name, qtype, self._ns_server)
                except socket.timeout:
                    continue
                self._update_cache(answ_pkg)
                respond = self.get_from_cache(domain_name, qtype)
                if respond:
                    for rdata, ttl in respond:
                        pb_res.add_answer(domain_name, qtype, ttl, rdata)
        if len(pb_res.answers) == 0:
            pb_res.set_flags(rcode=2)
        data = pb_res.build().encode()
        self._sock.sendto(data, dest_addr)
예제 #2
0
def test_3():
    data = str_to_bytes("db 42 01 00 00 01 00 01 00 00 00 00 03 77 77 77 "
                        "0c 6e 6f 72 74 68 65 61 73 74 65 72 6e 03 65 64 "
                        "75 00 00 01 00 01 c0 0c 00 01 00 01 00 00 02 58 "
                        "00 04 9b 21 11 44")
    pp = protocol.PackageParser(data)
    p = pp.parsePackage()
    [print(line) for line in p.hexdump()]
예제 #3
0
def test_8():
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(("192.168.43.15", 53))
    query_bytes, dest = sock.recvfrom(512)
    p_query = protocol.PackageParser(query_bytes).parsePackage()
    pb = protocol.PackageBuilder(p_query.identification)
    for record in p_query.questions:
        pb.add_question(record.domain_name, record.type)
    pb.add_answer("kidddy.man", protocol.Answer.QTYPE_A, 20, "192.168.43.15")
    sock.sendto(pb.build().encode(), dest)
예제 #4
0
def test_6():
    q1 = protocol.Question("yandex.ru", protocol.Question.QTYPE_A)
    q2 = protocol.Question("lol.kek.cheburek", protocol.Question.QTYPE_A)
    fl = protocol.Flags(protocol.Flags.QR_REQ, protocol.Flags.OPCODE_STD,
                        False, False, True, False, protocol.Flags.RCODE_OK)
    p = protocol.Package(5, fl, [q1, q2])
    data = p.encode()
    answ, _ = resolver.send_and_get(data)
    pp = protocol.PackageParser(answ)
    p = pp.parsePackage()
    [print(line) for line in p.pprint()]
예제 #5
0
def ask_serverok(name_question: str, qtype: int, ns_server):
    """Create package, send it to ns_server and return parsed answer"""
    pb = protocol.PackageBuilder(42)
    pb.set_flags(RA=True)
    pb.add_question(name_question, qtype, qclass=1)
    pkg = pb.build().encode()

    answ, _ = send_and_get(pkg, ns_server)
    pp = protocol.PackageParser(answ)

    return pp.parsePackage(), answ
예제 #6
0
def test_2():
    header = b"\x00\x03\x01\x00\x00\x03\x00\x00\x00\x00\x00\x00"
    namef = b"\x06yandex"
    names = b"\x02ru"
    endn = b"\x00"
    type_a = b"\x00\x01"
    class_q = b"\x00\x01"
    name_sh = b"\xc0\x0c"
    data = header + namef + names + endn + type_a + class_q + name_sh + b"\x00\x02" + class_q + b"\x03www\xc0\x0c" + type_a + class_q
    [print(line) for line in hexdump.dumpgen(data)]
    pp = protocol.PackageParser(data)
    p = pp.parsePackage()
    [print(line) for line in p.pprint()]
    return p
예제 #7
0
def test_1():
    q = protocol.Question("yandex.ru", protocol.Question.QTYPE_A,
                          protocol.Question.QCLASS_IN)
    a = protocol.Answer("yandex.ru", protocol.Answer.QTYPE_A,
                        protocol.Answer.QCLASS_IN, 42, "127.0.0.1")
    fl = protocol.Flags(protocol.Flags.QR_REQ, protocol.Flags.OPCODE_STD,
                        False, False, True, False, protocol.Flags.RCODE_OK)
    p = protocol.Package(5, fl, [q], [a])
    data = p.encode()
    [print(line) for line in p.hexdump()]
    pp = protocol.PackageParser(data)
    pac = pp.parsePackage()
    [print(line) for line in p.hexdump()]
    [print(line) for line in pac.pprint()]