-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
313 lines (269 loc) · 8.96 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import binascii
import codecs
import socket
GOOGLE_IP = "198.41.0.4"
GOOGLE_PORT = 53
currOctet = 0
references = {}
servers = []
class DNSMessage:
def __init__(self,header,question) -> None:
self.header = header
self.question = question
class Header:
def __init__(self,id,qr,opcode,aa,tc,rd,ra,rcode,qdcount,ancount,nscount,arcount) -> None:
self.id = [id,16]
self.qr = [qr,1]
self.opcode = [opcode,4]
self.aa = [aa,1]
self.tc = [tc,1]
self.rd = [rd,1]
self.ra = [ra,1]
self.z = [0,3]
self.rcode = [rcode,4]
self.qdcount = [qdcount,16]
self.ancount = [ancount,16]
self.nscount = [nscount,16]
self.arcount = [arcount,16]
class Question:
def __init__(self,qname,qtype,qclass) -> None:
self.qname = qname
self.qtype = qtype
self.qclass = qclass
def parseHeader(resp):
responseHeader = Header(0,0,0,0,0,0,0,0,0,0,0,0)
start = 0
values = []
for item in responseHeader.__dict__.items():
values.append(convertToNumber(resp[start:start+item[1][1]]))
start+=item[1][1]
idx = 0
for key in responseHeader.__dict__.keys():
setattr(responseHeader,key,values[idx])
idx+=1
if(responseHeader.ancount>=1):
print("resolved the address")
print(responseHeader.id,"id")
print(responseHeader.ancount,"ancount")
print(responseHeader.arcount,"arcount")
print(responseHeader.nscount,"nscount")
return responseHeader
def parseQuestion(header,data):
global currOctet
respQuestion = Question(0,0,0)
domainName = parseDomainName(data,0)
respQuestion.qname = domainName.get("domainName")
start = domainName.get("start")
values = []
print(data[start:start+8])
for item in respQuestion.__dict__.items():
if item[0] == "qname":
values.append(0)
continue
else:
values.append(int(data[start:start+4],16))
start+=4
currOctet+=4
idx = 0
for key in respQuestion.__dict__.keys():
if(idx==0):
idx+=1
continue
setattr(respQuestion,key,values[idx])
idx+=1
print(respQuestion.qname)
print(respQuestion.qtype)
print(respQuestion.qclass)
parseRR((data)[start:])
def parseDomainName(data,start):
global servers
global currOctet
initial = start
start = start
domainName = ""
length = 0
currDomainPart = ""
startingOctect = currOctet
while start<len(data):
curr = data[start:start+4]
if(HexToBinary(curr)[0:2]=="11"):
print(currOctet)
referenceAddress = int(HexToBinary(curr)[3:],2) + 1
print(referenceAddress,">>>>>>>>>>>")
domainName+=f".{references[referenceAddress]}"
servers.append(domainName)
idx = len(domainName) - 1 - len(f".{references[referenceAddress]}")
totalLength = len(domainName) - len(f".{references[referenceAddress]}")
while idx>=0:
if(domainName[idx]=="."):
references[currOctet-(totalLength-idx-1)]=domainName[idx+1:]
idx-=1
references[currOctet-(totalLength)] = domainName
print(references)
currOctet+=2
start+=4
break
# print(data[start:start+2])
if(data[start:start+2]=="00"):
servers.append(domainName)
idx = len(domainName) - 1
totalLength = len(domainName)
while idx>=0:
if(domainName[idx]=="."):
references[currOctet-(totalLength-idx-1)]=domainName[idx+1:]
idx-=1
references[currOctet-(totalLength-1)] = domainName
print(references)
start+=2
currOctet+=1
break
if(length==0):
# two cases
# case1: I am at the start of the domain name so the currDomainPart would be empty so do nothing
# case2: I have parsed a part of domain name so put it into map and refersh the variables
if len(currDomainPart)!=0:
references[startingOctect] = currDomainPart
startingOctect = currOctet+1
currDomainPart = ""
if(start!=initial):
domainName+="."
length=int(data[start:start+2],16)
else:
domainName+=chr(int(data[start:start+2],16))
currDomainPart+=chr(int(data[start:start+2],16))
length-=1
start+=2
currOctet+=1
print("diff ", start - initial)
return {"domainName":domainName,"start":start}
def parseRR(data):
global currOctet
global servers
start = 0
name = data[start:start+4]
print(name,"name")
start = start+4
currOctet+=2
type = int(data[start:start+4],16)
print(type," RR type")
start+=4
classCode = int(data[start:start+4],16)
print(classCode," RR class code")
start+=4
ttl = int(data[start:start+8],16)
print(ttl," TTL")
start+=8
idx=0
rdlength = int(data[start:start+4],16)
print(rdlength," octets")
start+=4
currOctet+=10
if(type==1):
idx = 0
ip = ""
while idx<4:
ip+=str(int(data[start:start+2],16))
if(idx!=3):
ip+="."
idx+=1
start+=2
print(ip," ip")
GOOGLE_IP = ip
currOctet = 0
# servers = []
print(servers,".................")
servers = []
global message
if responseHeader.ancount==0:
resp = sock.sendto(message,(GOOGLE_IP,GOOGLE_PORT))
print("contacted ",ip)
return
if(type==2):
domainName = parseDomainName(data,start)
# message2 = getMessage(domainName.get("domainName"))
# GOOGLE_IP = "198.41.0.4"
# message2 = bytes.fromhex(message2)
# resp = sock.sendto(message2,(GOOGLE_IP,GOOGLE_PORT))
print("contacted ",domainName.get("domainName"))
# start = domainName.get("start")
if(len(data[start + (rdlength*2):])==0 or responseHeader.ancount==1):
return
else:
print(data[start + (rdlength*2):])
parseRR(data[start + (rdlength*2):])
return
def convertToBits(number,numberOfBits):
bits = bin(number)[2:].zfill(numberOfBits)
return bits
def convertToNumber(binary):
return int(binary,2)
def HexToBinary(resp):
hex_dict = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', '4': '0100', '5': '0101', '6': '0110', '7': '0111', '8': '1000', '9': '1001', 'a': '1010', 'b': '1011', 'c': '1100', 'd': '1101', 'e': '1110', 'f': '1111'}
binary = ''
for digit in resp:
binary += hex_dict[digit]
return binary
def getURL(url):
result = ""
strs = list(url.split("."))
for parts in strs:
result+=str(len(parts))
result+=parts
result+="0"
print(result)
return result
def binary_to_hex(binary_string):
"""
Convert a binary string to a hex string.
Args:
- binary_string (str): Binary string to be converted.
Returns:
- str: Hexadecimal representation of the binary string.
"""
# Convert binary string to bytes
binary_bytes = int(binary_string, 2).to_bytes((len(binary_string) + 7) // 8, byteorder='big')
# Convert bytes to hex string
hex_string = binascii.hexlify(binary_bytes).decode('utf-8')
return hex_string
def getMessage(url):
header1 = Header(22,0,0,0,0,1,0,0,1,0,0,0)
url = getURL(url)
question = Question(url,1,1)
hexString = ""
binaryString=""
for item in header1.__dict__.items():
binaryString+=convertToBits(item[1][0],item[1][1])
hexString+=binary_to_hex(binaryString)
binaryString = ""
for item in question.__dict__.items():
domainName = ""
if item[0] == "qname":
start = 0
for idx in range(len(item[1])):
if item[1][idx].isalpha()==False:
binaryString=convertToBits(int(item[1][idx]),8)
hexString+=(binary_to_hex(binaryString))+domainName
else:
binaryString=format(ord(item[1][idx]),'08b')
hexString+=binary_to_hex(binaryString)
else:
binaryString=convertToBits(item[1],16)
hexString+=binary_to_hex(binaryString)
return hexString
UDP_IP = "127.0.0.1"
PORT = 5005
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
message = getMessage("dns.google.com")
print(message)
message = bytes.fromhex(message)
resp = sock.sendto(message,(GOOGLE_IP,GOOGLE_PORT))
print(resp)
while True:
data,addr = sock.recvfrom(2048)
print(data)
header = HexToBinary(bytes.hex(data))
responseHeader = parseHeader(header)
# header has 12 octects
currOctet+=12
parseQuestion(responseHeader,bytes.hex(data)[24:])