Improved DNS python script, made it PEP8 compliant

This commit is contained in:
Tobias Manske 2018-04-05 10:53:30 +02:00
parent 57cfb63ff3
commit 347bab9fce
No known key found for this signature in database
GPG Key ID: 978D99F12D4E041F
1 changed files with 38 additions and 33 deletions

View File

@ -711,48 +711,53 @@ index-file.names = (
# Create a DNS service with python, forwarding all traffic to gateway.
echo "\
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import socket
class DNSQuery:
def __init__(self, data):
self.data=data
self.dominio=''
def __init__(self, data):
self.data=data
self.dominio=''
tipo = (ord(data[2]) >> 3) & 15
if tipo == 0:
ini=12
lon=ord(data[ini])
while lon != 0:
self.dominio+=data[ini+1:ini+lon+1]+'.'
ini+=lon+1
lon=ord(data[ini])
tipo = (ord(data[2]) >> 3) & 15
if tipo == 0:
ini=12
lon=ord(data[ini])
while lon != 0:
self.dominio+=data[ini + 1:ini + lon + 1] + '.'
ini += lon + 1
lon=ord(data[ini])
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
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
if __name__ == '__main__':
ip='$CaptivePortalGatewayAddress'
print 'pyminifakeDwebconfNS:: dom.query. 60 IN A %s' % ip
ip='$CaptivePortalGatewayAddress'
print 'pyminifakeDwebconfNS:: dom.query. 60 IN A %s' % ip
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind((ip, 53))
try:
while 1:
data, addr = udps.recvfrom(1024)
p=DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print 'Request: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
print 'Finalizando'
try:
while True:
data, addr = udps.recvfrom(1024)
p=DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print 'Request: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
print 'Finalizando'
udps.close()\
" >"$FLUXIONWorkspacePath/fluxion_captive_portal_dns.py"