1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| import logging import socket from dns.resolver import Resolver from dnslib import DNSRecord, QTYPE, RR, A, CNAME, NS, MX, DNSHeader
dns_resolver = Resolver() dns_resolver.nameservers = ["8.8.8.8", "8.8.4.4"]
domain_ip_map = { "example.com": "192.0.2.1", "example.net": "203.0.113.2", }
domain_cname_map = { "www.example.com": "example.com", }
domain_ns_map = { "example.com": "ns1.example.com", }
domain_mx_map = { "example.com": [(10, "mail.example.com")], }
def get_ip_from_domain(domain): domain = domain.lower().strip() if domain in domain_ip_map: return domain_ip_map[domain] try: return dns_resolver.query(domain, 'A')[0].to_text() except: return None
def get_cname_from_domain(domain): domain = domain.lower().strip() if domain in domain_cname_map: return domain_cname_map[domain] try: return dns_resolver.query(domain, 'CNAME')[0].to_text() except: return None
def get_ns_from_domain(domain): domain = domain.lower().strip() if domain in domain_ns_map: return domain_ns_map[domain] try: return dns_resolver.query(domain, 'NS')[0].to_text() except: return None
def get_mx_from_domain(domain): domain = domain.lower().strip() if domain in domain_mx_map: return domain_mx_map[domain] try: response = dns_resolver.query(domain, 'MX') mx_records = [(r.preference, r.exchange.to_text()) for r in response] return mx_records except: return None
def reply_for_not_found(income_record): header = DNSHeader(id=income_record.header.id, bitmap=income_record.header.bitmap, qr=1) header.set_rcode(0) record = DNSRecord(header, q=income_record.q) return record
def reply_for_A(income_record, ip, ttl=None): r_data = A(ip) header = DNSHeader(id=income_record.header.id, bitmap=income_record.header.bitmap, qr=1) domain = income_record.q.qname query_type_int = QTYPE.reverse.get('A') or income_record.q.qtype record = DNSRecord(header, q=income_record.q, a=RR(domain, query_type_int, rdata=r_data, ttl=ttl)) return record
def reply_for_CNAME(income_record, cname, ttl=None): r_data = CNAME(cname) header = DNSHeader(id=income_record.header.id, bitmap=income_record.header.bitmap, qr=1) domain = income_record.q.qname query_type_int = QTYPE.reverse.get('CNAME') or income_record.q.qtype record = DNSRecord(header, q=income_record.q, a=RR(domain, query_type_int, rdata=r_data, ttl=ttl)) return record
def reply_for_NS(income_record, ns, ttl=None): r_data = NS(ns) header = DNSHeader(id=income_record.header.id, bitmap=income_record.header.bitmap, qr=1) domain = income_record.q.qname query_type_int = QTYPE.reverse.get('NS') or income_record.q.qtype record = DNSRecord(header, q=income_record.q, a=RR(domain, query_type_int, rdata=r_data, ttl=ttl)) return record
def reply_for_MX(income_record, mx_records, ttl=None): r_data = [MX(preference=preference, label=exchange) for preference, exchange in mx_records] header = DNSHeader(id=income_record.header.id, bitmap=income_record.header.bitmap, qr=1) domain = income_record.q.qname query_type_int = QTYPE.reverse.get('MX') or income_record.q.qtype record = DNSRecord(header, q=income_record.q) for mx in r_data: record.add_answer(RR(domain, QTYPE.MX, rdata=mx, ttl=ttl)) return record
def dns_handler(s, message, address): try: income_record = DNSRecord.parse(message) except: logging.error('from %s, parse error' % address) return
try: qtype = QTYPE.get(income_record.q.qtype) except: qtype = 'unknown'
domain = str(income_record.q.qname).strip('.') info = '%s -- %s, from %s' % (qtype, domain, address)
if qtype == 'A': ip = get_ip_from_domain(domain) if ip: response = reply_for_A(income_record, ip=ip, ttl=60) s.sendto(response.pack(), address) return logging.info(info)
if qtype == 'CNAME': cname = get_cname_from_domain(domain) if cname: response = reply_for_CNAME(income_record, cname=cname, ttl=60) s.sendto(response.pack(), address) return logging.info(info)
if qtype == 'NS': ns = get_ns_from_domain(domain) if ns: response = reply_for_NS(income_record, ns=ns, ttl=60) s.sendto(response.pack(), address) return logging.info(info)
if qtype == 'MX': mx = get_mx_from_domain(domain) if mx: response = reply_for_MX(income_record, mx, ttl=60) s.sendto(response.pack(), address) return logging.info(info)
response = reply_for_not_found(income_record) s.sendto(response.pack(), address) logging.info(info)
if __name__ == '__main__': udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp_sock.bind(('', 53)) logging.info('DNS server is started') while True: message, address = udp_sock.recvfrom(8192) dns_handler(udp_sock, message, address)
|