The dns doesnt work if i do this it gives this error and idk whats the matter it just gives me this error:
Traceback (most recent call last):
File "c:\Users\engin\Downloads\All files\dns.py", line 160, in <module>
r = buildresponse(data)
File "c:\Users\engin\Downloads\All files\dns.py", line 155, in buildresponse
dnsbody += rectobytes(domainname, rectype, record["ttl"], record["value"])
TypeError: string indices must be integers
and i did write this code for my DNS Server:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((ip, port))
def load_zone():
jsonzone = {}
zonefiles = glob.glob('zones/*.zone')
for zone in zonefiles:
with open(zone) as zonedata:
data = json.load(zonedata)
zonename = data["$origin"]
jsonzone[zonename] = data
return jsonzone
zonedata = load_zone()
def getFlags(flags):
byte1 = bytes(flags[:1])
byte2 = bytes(flags[1:2])
rflags = ''
QR = '1'
OPCODE = ''
for bit in range(1,5):
OPCODE += str(ord(byte1) & (1<<bit))
AA = '1'
TC = '0'
RD = '0'
RA = '0'
Z= '000'
RCODE = '0000'
return(int(QR + OPCODE + AA + TC + RD, 2).to_bytes(1, byteorder='big') + int(RA + Z + RCODE, 2).to_bytes(1, byteorder='big'))
def getquestiondomain(data):
state = 0
expectedlenght = 0
domainsting = ''
domainparts = []
x = 0
y = 0
for byte in data:
if state == 1:
if byte != 0:
domainsting += chr(byte)
x += 1
if x == expectedlenght:
domainparts.append(domainsting)
domainsting = ''
state = 0
x = 0
if byte == 0:
domainparts.append(domainsting)
break
else:
state = 1
expectedlenght = byte
# x += 1
y += 1
questiontype = data[y + 1 : y + 3]
return(domainparts, questiontype)
def getzone(domain):
global zonedata
zone_name = '.'.join(domain)
return zonedata[zone_name]
def getrecs(data):
domain, questiontype = getquestiondomain(data)
qt = ''
if questiontype == b'\x00\x01':
qt = 'a'
zone = getzone(domain)
return (zone, qt, domain)
def rectobytes(domainname, rectype, recttl, recval):
rbytes = b'\xc0\x0c'
if rectype == 'a':
rbytes = rbytes + bytes([0]) + bytes([1])
rbytes = rbytes + bytes([0]) + bytes([1])
rbytes += int(recttl).to_bytes(4, byteorder='big')
if rectype == 'a':
rbytes = rbytes + bytes([0]) + bytes([4])
for part in recval.split('.'):
rbytes += bytes([int(part)])
return rbytes
def buildquestion(domainname, rectype):
qbytes = b''
for part in domainname:
lenght = len (part)
qbytes += bytes([lenght])
for char in part:
qbytes += ord(char).to_bytes(1, byteorder='big')
if rectype == 'a':
qbytes += (1).to_bytes(2, byteorder='big')
qbytes += (1).to_bytes(2, byteorder='big')
return qbytes
def buildresponse(data):
TransactionID = data[:2]
# TID = ''
# for byte in TransactionID:
# TID += hex(byte)
Flags = getFlags(data[2:4])
QDCOUNT = b'\x00\x01'
# getquestiondomain(data[12:])
ANCOUNT = len(getrecs(data[12:])[0]).to_bytes(2, byteorder='big')
NSCOUNT = (0).to_bytes(2, byteorder='big')
ARCOUNT = (0).to_bytes(2, byteorder='big')
dnsheader = TransactionID + Flags + QDCOUNT + ANCOUNT + NSCOUNT + ARCOUNT
dnsbody = b''
records, rectype, domainname = getrecs(data[12:])
dnsquestion = buildquestion(domainname, rectype)
for record in records:
dnsbody += rectobytes(domainname, rectype, record["ttl"], record["value"])
return dnsheader + dnsquestion + dnsbody
while 1:
data, addr = sock.recvfrom(512)
r = buildresponse(data)
sock.sendto(r, addr)
and this value record in the for at the very bottom of buildresponse it just prints out @origin
Idk whats the problem or so and then i created this Post and pls help.
pls help!