-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdns_client.py
More file actions
239 lines (219 loc) · 6.68 KB
/
dns_client.py
File metadata and controls
239 lines (219 loc) · 6.68 KB
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
import sys
import socket
import struct # For constructing and destructing the DNS packet.
# Construct the DNS packet of header + QNAME + QTYPE + QCLASS.
# Get the host name from the command line.
host_name_to_look_up = ""
T_A = 1 # Ipv4 address
T_CNAME = 5 # canonical name
address = sys.argv[2]
port = sys.argv[3]
query_type = int(sys.argv[4])
try:
host_name_to_look_up = sys.argv[1]
except IndexError:
print "No host name specified."
sys.exit(0)
# build the DNS packets to be sent with a single question
def DNSquery(host, type, Class):
packet = struct.pack("!H", 12049) # Query Ids (Just 1 for now)
packet += struct.pack("!H", 0) # Flags
packet += struct.pack("!H", 1) # Questions
packet += struct.pack("!HHH", 0, 0, 0) # Answers , Additional RR , info
for name in host: # add url to pack
packet += struct.pack("!B", len(name))
for byte in name:
packet += struct.pack("!c", byte.encode('utf-8'))
packet = packet + struct.pack("!bHH", 0, type, Class)
return packet
# check and merge 2 bytes to a single byte
def chk(chunk):
if len(chunk) == 2:
temp = ord(chunk[0])
temp1 = ord(chunk[1])
return temp * 256 + temp1
else:
return ord(chunk[0]) * 16777216 + ord(chunk[1]) * 65536 + ord(chunk[2]) * 256 + ord(chunk[3])
l = [None] * 6
def decode_A(data):
print "Non-authoritative answer:"
# decode header
for i in xrange(0, 12, 2):
# print ord(data[i]) , ord(data[i+1])
l[i / 2] = chk(data[i] + data[i + 1])
# print l[i/2]
ans_count = l[3]
question_count = l[2]
i = 0
name = []
# print l[3]
i += 12
# usign rfc1035
# get name of question
while ord(data[i]) > 0:
length = ord(data[i])
i += 1
x = []
for k in xrange(length):
# print ord(data[i+k])
x.append(chr(ord(data[i + k])))
name.append(''.join(x))
i += length
print_name = ".".join(name)
i += 1
# get query type of question
q_type = chk(data[i:i + 2])
i += 2
# get query class
q_class = chk(data[i:i + 2])
i += 2
prev = print_name
# for number of answers
for _ in xrange(ans_count):
while (ord(data[i]) > 0):
# print ord(data[i])
i += 1
a_type = chk(data[i:i + 2])
i += 2
# print a_type
# for type A
if a_type == 1:
# get each of class ttl datalen out
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
ip = []
# print a_datalen
for __ in xrange(a_datalen):
ip.append(str(ord(data[i + __])))
i += a_datalen
# get the ip address and print in the format
print "Name: ", prev
print "Address: ", ".".join(ip)
# for query type CNAME, SOA
elif a_type == 5 or a_type == 6:
# get class, ttl ,datalen
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
y = []
tt = i
length = ord(data[i])
# get the following address path of canonical name
while length > 0 and length <= 15:
i += 1
x = []
for _l in xrange(length):
x.append(chr(ord(data[i + _l])))
y.append("".join(x))
i += length
length = ord(data[i])
print prev, "\t canonical name = ", ".".join(y)
prev = ".".join(y)
# i+=a_datalen
i += 2
else:
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
i += a_datalen
# stop if the question query type = 1 or A
if q_type == 1:
return
# authoritative answers
print "\nAuthoritative answers can be found from:"
print prev
ky = 0
part = []
# authoritative answers same as non authoritative just addition of query type NS
for _ in xrange(l[4] + l[5]):
while ord(data[i]) > 0:
# print ord(data[i])
i += 1
a_type = chk(data[i:i + 2])
i += 2
# print a_type
if a_type == 5:
# type 5, 6 CNAME , SOA , done from above
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
y = []
tt = i
length = ord(data[i])
while 0 < length < 15:
i += 1
x = []
for _l in xrange(length):
x.append(chr(ord(data[i + _l])))
y.append("".join(x))
i += length
length = ord(data[i])
print prev, "\t address = ", ".".join(y)
prev = ".".join(y)
# i+=a_datalen
i += 2
elif a_type == 1:
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
ip = []
# print a_datalen
for __ in xrange(a_datalen):
ip.append(str(ord(data[i + __])))
i += a_datalen
print "Name: ", part[ky]
ky += 1
print "Address: ", ".".join(ip)
else:
a_class = chk(data[i:i + 2])
i += 2
a_ttl = chk(data[i:i + 4])
i += 4
a_datalen = chk(data[i:i + 2])
i += 2
i += a_datalen
# send packets to server , port taken in arguments
packet = DNSquery(host_name_to_look_up.split("."), query_type, 1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((address, int(port)))
sock.sendall(bytes(struct.pack('!H', len(packet)) + packet))
data = ""
try:
data = sock.recv(8192)
except Exception, e:
print "Timeout Occured"
sz = int(data[:2].encode('hex'), 16)
if sz < len(data) - 2:
raise Exception("Wrong size of TCP packet")
elif sz > len(data) - 2:
raise Exception("Too big TCP packet")
data = data[2:]
sock.close()
# print the server and address used
print "Server:\t\t", address
print "Address:\t", address + "#" + port
print "\n"
try:
# for i in data:
# print (ord(i))
decode_A(data)
except Exception, e:
print "Please check the url and dns address again"
sock.close()
sys.exit(0)