def to_bytes(self): name_bytes = return_hex(domain_to_str_bytes(self.name)) type_bytes = return_bytes_form_str(self.type) ttl_bytes = int_to_hex(self.ttl, 8) length_bytes = int_to_hex(self.length, 4) if self.type == 'NS': data_bytes = return_hex(domain_to_str_bytes(self.data)) elif self.type == 'A': data_bytes = return_hex('{:02X}{:02X}{:02X}{:02X}'.format( *map(int, self.data.split(".")))) else: if type(self.data) == str: data_bytes = return_hex(self.data) else: data_bytes = self.data return name_bytes + type_bytes + b'\x00\x01' + ttl_bytes + length_bytes + data_bytes
def find_any_record_on_ns(response, domain): req = Request(Header('aaaa', Flag(BitArray(b'\x01\x00')), 1, 0, 0, 0), [Question(domain, 'ANY')], return_hex(any)) req_bytes = req.to_bytes() with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.sendto(req_bytes, (response.response[0].data, 53)) data, sender = s.recvfrom(512) response = parse_answers(data) return response
def parse_int_to_bytes(ints): normal_data = '' for j in ints.split(' '): if j == '\n': continue hex_char = hex(int(j))[2:] if len(hex_char) == 1: hex_char = '0' + hex_char normal_data += hex_char + ' ' return return_hex(normal_data)
def find_ip_for_ns(self, response): domain = response.get_all_info()[0].data request = Request( Header('aaaa', Flag(BitArray(b'\x01\x00')), 1, 0, 0, 0), [Question(domain, 'A')], return_hex(a)) req_bytes = request.to_bytes() self.socket_resolver.sendto(req_bytes, (self.yandex_dns, self.port)) data, sender = self.socket_resolver.recvfrom(512) data = parse_answers(data) return data
def __init__(self, q_name, q_type): if q_type not in ['A', 'MX', 'TXT']: raise ValueError("Invalid type value") self.data = return_hex(insert_info_into_request(q_name, q_type)) self.address = '127.0.0.1' self.port = 53 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: s.sendto(self.data, (self.address, self.port)) data, sender = s.recvfrom(256) answer = parse_answers(data) print(answer)
def get_error_response(request): return return_hex(f'{request.header.id}' + err)
def to_bytes(self): return return_hex( domain_to_str_bytes(self.domain) + ' 00 ' + return_hex_str_form_str(self.type)) + b'\x00\x01'
def to_bytes(self): id_bytes = return_hex(self.id) flags_bytes = self.flags.to_bytes() return id_bytes + flags_bytes + int_to_hex( self.qd_count, 4) + int_to_hex(self.an_count, 4) + int_to_hex( self.ns_count, 4) + int_to_hex(self.ar_count, 4)
def to_bytes(self): return return_hex(self.flags.hex)