mini DNS server
本帖最后由 edelweiss92 于 2021-3-26 17:14 编辑功能,搭建一个简易dns服务器,将所有dns解析到指定ip
python3
https://attach.52pojie.cn//forum/202103/26/171408z88i8i73nh3dgzfi.png?l
import socket
class DNSQuery:
def __init__(self, data):
self.data = data
self.dominio = ''
tipo = ((data) >> 3) & 15# Opcode bits
print('tipo: ',tipo)
if tipo == 0:# Standard query
ini = 12
lon = (data)
print('lon: ',lon)
while lon != 0:
self.dominio += (data).decode('utf8') + '.'
ini += lon + 1
lon = (data)
def respuesta(self, ip):
packet = b''
if self.dominio:
#print(type(self.data[:2]))
#print(self.data[:2])
packet += self.data[:2] + b"\x81\x80"
packet += self.data + self.data + b'\x00\x00\x00\x00'# Questions and Answers Counts
packet += self.data# Original Domain Name Question
packet += b'\xc0\x0c'# Pointer to domain name
packet += b'\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'# Response type, ttl and resource data length -> 4 bytes
#packet += (str.join('', map(lambda x: chr(int(x)), ip.split('.')))).encode('utf8')# 4bytes of IP
packet += bytes(map(int, ip.split('.')))
return packet
if __name__ == '__main__':
ip = '192.168.6.2'
print('pyminifakeDNS:: dom.query. 60 IN A %s' % ip)
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('', 53))
try:
while 1:
data, addr = udps.recvfrom(1024)
print(data)
p = DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print('Respuesta: %s -> %s' % (p.dominio, ip))
except KeyboardInterrupt:
print('Finalizando')
udps.close() 可以的,666 特殊情况下还真可以有,感谢楼主分享 这个不错诶,可以做很多小的指向性链接 这个有用,看看我们开发工作中是不是能用上 这个不错诶,感谢楼主分享
页:
[1]