Rewrote mini DNS server to support python 2 & 3.

This commit is contained in:
Matias Barcenas 2018-04-30 19:58:20 -05:00
parent 3a6b9ecae7
commit d571cc835c
1 changed files with 69 additions and 31 deletions

View File

@ -744,49 +744,87 @@ index-file.names = (
# Create a DNS service with python, forwarding all traffic to gateway. # Create a DNS service with python, forwarding all traffic to gateway.
echo "\ echo "\
import socket import sys, traceback, socket
# NOTICE: This DNS server works with python 2 and python 3
class DNSQuery: class DNSQuery:
def __init__(self, data): def __init__(self, data):
self.data=data self.data = data
self.dominio='' self.domain = ''
tipo = (ord(data[2]) >> 3) & 15 queryType = (ord(data[2]) >> 3) & 15
if tipo == 0:
ini=12 # Only handle basic requests.
lon=ord(data[ini]) if queryType != 0:
while lon != 0: print('Ignoring Query: Non-spoofed type.')
self.dominio+=data[ini+1:ini+lon+1]+'.' return
ini+=lon+1
lon=ord(data[ini]) domainStart = 13 # Skip length byte and start at domain.
domainLength = ord(data[domainStart - 1]) # Evaluate length byte.
while domainLength != 0:
self.domain += data[domainStart : domainStart + domainLength] + '.'
domainStart += domainLength + 1 # Skip the length byte & start at domain.
domainLength = ord(data[domainStart - 1]) # Evaluate length byte.
def response(self, ipv4):
if not self.domain: return ''
packet = ''
packet += self.data[ :2] + '\x81\x80'
packet += self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'
packet += self.data[12:]
packet += '\xc0\x0c'
packet += '\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'
# Convert string IPv4 quads to binary values (bytes).
packet += str.join('', map(lambda s: chr(int(s)), ipv4.split('.')))
def respuesta(self, ip):
packet=''
if self.dominio:
packet+=self.data[:2] + \"\x81\x80\"
packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'
packet+=self.data[12:]
packet+='\xc0\x0c'
packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'
packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.')))
return packet return packet
if __name__ == '__main__': if __name__ == '__main__':
ip='$CaptivePortalGatewayAddress' targetIPv4 = '$CaptivePortalGatewayAddress'
print 'pyminifakeDwebconfNS:: dom.query. 60 IN A %s' % ip
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) print('Mini DNS Spoofer:: dom.query. 60 IN A %s' % targetIPv4)
udps.bind(('',53))
link = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
link.bind(('',53))
try: try:
while 1: while True:
data, addr = udps.recvfrom(1024) clientData, clientIPv4 = link.recvfrom(1024)
p=DNSQuery(data)
udps.sendto(p.respuesta(ip), addr) queryData = clientData if sys.version_info < (3, 0) else clientData.decode('unicode_escape')
print 'Request: %s -> %s' % (p.dominio, ip)
query = DNSQuery(queryData)
response = query.response(targetIPv4)
if sys.version_info > (3, 0):
# Someone that knows more about python and how it does byte-handling,
# please fix the following shitfest and make it a bit more elegant.
# Do what? A raw conversion of the \"response\" string to bytes.
responseHex = ''
for xx in response:
responseHex += \"%x%x\" % ((ord(xx) >> 4) & 0b1111, ord(xx) & 0b1111)
response = bytearray.fromhex(responseHex)
link.sendto(response, clientIPv4)
print('Request: %s -> %s' % (query.domain, targetIPv4))
except KeyboardInterrupt: except KeyboardInterrupt:
print 'Finalizando' print('INTERRUPT: Stopping.')
udps.close()\ link.close()
except Exception as error:
print('EXCEPTION: Stopping!')
print(error)
print(traceback.format_exc())
link.close()
" >"$FLUXIONWorkspacePath/fluxion_captive_portal_dns.py" " >"$FLUXIONWorkspacePath/fluxion_captive_portal_dns.py"
chmod +x "$FLUXIONWorkspacePath/fluxion_captive_portal_dns.py" chmod +x "$FLUXIONWorkspacePath/fluxion_captive_portal_dns.py"