Пример #1
0
def dns_recurse(transaction_id, received_hostname, type_a_records):
	global tcp_enabled
	# tld_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	tld_ip_addr =type_a_records[0]['value'] #implement round robin
	print("tld ip"+tld_ip_addr)
	# tld_socket.connect((tld_ip_addr, 53))
	query = dns_query.dnsquery(transaction_id, received_hostname)
	# tld_socket.send(query) #build message
	# response = tld_socket.recv(100).decode('utf-8')
	response = dns_query.sendtoserver(tld_ip_addr,53,query, tcp_enabled)

	return response
Пример #2
0
import socket, random, sys
import dns_utility as dns_query

args = (sys.argv)
print(args)

domain_name = str(args[1])
tcp_enabled = 0
if args[2] == 'tcp':
    tcp_enabled = 1

print("domain_name", domain_name)
# sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
data = dns_query.dnsquery(
    random.randint(0, 65535).to_bytes(2, byteorder='big'), str(domain_name))
print("DATA", data)
# Simply set up a target address and port ...
# addr = ('127.0.0.1',53)
# ... and send data out to it!
# sk.sendto(data,addr)
response_data = dns_query.sendtoserver('127.0.0.1', 53, data, tcp_enabled)
print("DATA", response_data)
Пример #3
0
def createresponse(data):

    ##################### EXTRACT QUESTION DETAILS ##############################
    header_length = 12
    position = header_length
    question_len = data[position]
    domain_parts = []
    while question_len != 0:
        domain_parts.append(data[position + 1:position + question_len + 1])
        position += question_len + 1
        question_len = data[position]
        end_position = position
    domain_name = str(b'.'.join(domain_parts), encoding='UTF-8')
    question_type = data[position + 1:position + 3]
    if question_type == b'\x00\x01':
        record_type = 'a'
    elif question_type == b'\x00\x02':
        record_type = 'ns'
    elif question_type == b'\x00\x05':
        record_type = 'cname'
    elif question_type == b'\x00\xff':
        record_type = 'mx'

##################### Extract response from Cache ###############################
#cache_query = { "domainname" : domain_name}
    dns_records = {}
    cache_query = domain_name
    #print(cache_query)
    recFlag = str(int(data[2]) & 1)
    RDFlag = myAtoi(recFlag)
    #print("RecFlag: ")
    #print(recFlag)
    #print("\n")
    if (lru_dict.has_key(cache_query)):
        #print("Hellooooooo......cache hit")
        global cache_counter
        cache_counter += 1
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Cache Hit; Cache Counter : " + str(cache_counter)
        logging.info(logStr)
        dns_records = lru_dict[cache_query]
        print(dns_records, cache_query)
        ######################### Response Header #####################################
        ###Transaction ID
        ID = data[:2]
        RD = str(int(data[2]) & 1)
        ### FLAGS
        QR = '1'
        OPCODE = '0000'
        AA = '1'
        TC = '0'
        # RD = '0'
        RA = '1'
        Z = '000'
        RCODE = '0000'
        flags = int(
            QR + OPCODE + AA + TC + RD, 2).to_bytes(1, byteorder='big') + int(
                RA + Z + RCODE, 2).to_bytes(1, byteorder='big')

        ### QUESTION COUNT
        #q_count = (data[4] << 8) + data[5]
        #print('q_count',q_count)
        #QDCOUNT = q_count.to_bytes(2, byteorder='big') #b'\x00\x01'
        QDCOUNT = b'\x00\x01'  ### ASSUMPTION - only 1 question at a time
        #print('QDCOUNT',QDCOUNT)
        ### ANSWER COUNT
        ans_count = len(dns_records[record_type])  #fetch from DB
        ANCOUNT = ans_count.to_bytes(2, byteorder='big')
        #print('ANCOUNT', ANCOUNT)
        # Nameserver Count
        ns_count = 0
        NSCOUNT = ns_count.to_bytes(2, byteorder='big')
        #print('NSCOUNT', NSCOUNT)
        # Additonal Count
        ar_count = 0
        ARCOUNT = ar_count.to_bytes(2, byteorder='big')
        response_header = ID + flags + QDCOUNT + ANCOUNT + NSCOUNT + ARCOUNT
        print("Response Header", response_header)

        ######################### Response Question #################################
        response_question = data[header_length:end_position + 5]
        print("Response question........", response_question)
        ######################### Response Body #################################
        response_body = b''
        for rec in dns_records[record_type]:
            response_body += bytes([192]) + bytes(
                [12])  ## Name - compression applied
            response_body += question_type  ## record type
            response_body += b'\x00\x01'  ## record class
            ttl = int(rec['ttl']).to_bytes(
                4, byteorder='big')  #b'\x00\x00\x00\x04' ## 4 bytes
            response_body += ttl
            response_body += bytes([0]) + bytes([4])
            ipv4_addr = b''
            for ip_octet in rec['value'].split("."):
                ipv4_addr += bytes([int(ip_octet)])
            response_body += ipv4_addr
        print("response_body", response_body)
        return response_header + response_question + response_body
    elif RDFlag == 1:
        #print("I'm in recFlag section....")
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Rec:Sending to Root Server..."
        logging.info(logStr)
        response1 = dns_query.sendtoserver('35.196.214.148', 53, data,
                                           tcp_enabled)
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Rec:Receive from Root Server..." + str(response1)
        logging.info(logStr)
        jsonres = dns_query.json_response(response1)
        lru_dict[cache_query] = jsonres
        print(lru_dict[cache_query])
        return response1
    elif RDFlag == 0:
        #creating response for iterative
        #print("Here in not RD state \n\n")
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Itr:Sending to Root Server..."
        logging.info(logStr)
        rootResponse = dns_query.sendtoserver('35.196.214.148', 53, data,
                                              tcp_enabled)
        responseList = []
        responseList = dns_query.parseresponse(rootResponse)

        itrIp = responseList[1]
        authIp = itrIp[13:-1]
        #print(authIp)
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Itr:Receive from Root, Auth IP : " + authIp
        log.info(logStr)
        response1 = dns_query.sendtoserver(authIp, 53, data, tcp_enabled)
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        logStr = st + " - Itr:Receive from Auth Server..." + str(response1)
        logging.info(logStr)
        jsonres = dns_query.json_response(response1)
        lru_dict[cache_query] = jsonres
        print(lru_dict[cache_query])
        return response1
Пример #4
0
correctcnt = 0
data = pandas.read_csv('test.csv', sep=',')
cols = ['ID', 'Start Time', 'End Time', 'Response Time']
lst = []
for i in range(numreq):
    dnameid = random.randint(0, 57)
    dname = data.iloc[dnameid]["Domain Name"]
    tid = random.randint(10000, 65535).to_bytes(2, byteorder='big')
    query_data = dns_query.dnsquery(tid, str(dname))
    #print("DATA", query_data)
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    logStr = st + " - " + dname
    logging.info(logStr)
    start = time.time()
    response_data = dns_query.sendtoserver(HOST, PORT, query_data, tcp_enabled)
    end = time.time()
    print("DATA", response_data)
    ID = int.from_bytes(response_data[:2], byteorder='big')
    tidf = int.from_bytes(tid, byteorder='big')
    rec_lst = []
    if (str(tidf) == str(ID)):
        rec_lst = dns_query.parseresponse(response_data)
    print(rec_lst)
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    logStr = st + " - " + str(rec_lst)
    logging.info(logStr)
    if ((str(data.iloc[dnameid]["IP1"]) in rec_lst[1])
            and (str(data.iloc[dnameid]["IP2"]) in rec_lst[2])):
        correctcnt = correctcnt + 1