-
Notifications
You must be signed in to change notification settings - Fork 1
/
DNSResolver.cs
320 lines (266 loc) · 9.99 KB
/
DNSResolver.cs
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
314
315
316
317
318
319
320
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace ndot
{
struct DnsFrame
{
}
public struct DnsQuestion
{
public byte Offset;
public string Hostname;
public ushort Type;
public ushort Class;
public override string ToString()
{
return Hostname;
}
}
public struct DnsAnswer
{
public int Offset;
public string Name;
public ushort Type;
public ushort Class;
public uint TTL;
public ushort Length;
public string Payload;
public byte[] Address;
internal ushort Preference;
public string IPString()
{
return _ipstring = _ipstring ?? Address.Aggregate("", (s, v) => s += v.ToString() + '.').TrimEnd('.');
}
string _ipstring;
public override string ToString()
{
return Payload ?? Address.Aggregate("", (s, v) => s += v.ToString() + '.').TrimEnd('.');
}
}
public struct DnsAuthority
{
public byte Offset;
public string Hostname;
public ushort Type;
public ushort Class;
public override string ToString()
{
return Hostname;
}
}
public struct DnsResponse
{
public bool isResponse;
public bool recursiveAvailable;
public List<DnsQuestion> Requests;
public List<DnsAnswer> Answers;
public List<DnsAuthority> Authorities;
public static DnsResponse FromRaw(byte[] raw)
{
DnsResponse respFrame = default;
var id = BitConverter.ToUInt16(new byte[2] { raw[1], raw[0] });
respFrame.isResponse = (raw[2] & 0b_1000_0000) == 128;
respFrame.recursiveAvailable = (raw[3] & 0b_1000_0000) == 128;
var QSCount = BitConverter.ToUInt16(new byte[2] { raw[5], raw[4] });
var ANCount = BitConverter.ToUInt16(new byte[2] { raw[7], raw[6] });
var NSCount = BitConverter.ToUInt16(new byte[2] { raw[9], raw[8] });
var ARCount = BitConverter.ToUInt16(new byte[2] { raw[11], raw[10] });
int i = 12;
var questions = new List<DnsQuestion>();
var buf = "";
byte qoffset = (byte)i;
if (QSCount > 0)
while (true)
{
var len = raw[i];
if (len == 0)
{
questions.Add(new DnsQuestion
{
Offset = qoffset,
Hostname = buf.Trim('.'),
Type = BitConverter.ToUInt16(new byte[2] { raw[i + 2], raw[i + 1] }),
Class = BitConverter.ToUInt16(new byte[2] { raw[i + 4], raw[i + 3] }),
});
buf = "";
i += 4;
qoffset = (byte)i;
if (questions.Count == QSCount)
break;
else continue;
}
for (int c = 0; c < len; c++)
{
i++;
buf += (char)(raw[i]);
}
buf += '.';
//i += len;
i++;
}
i++;
var dnsAnswers = new List<DnsAnswer>();
if (ANCount > 0)
while (true)
{
var isptr = (raw[i + 0] & 0b_1100_0000) == 192;
string host = null;
if (isptr)
{
var offset = raw[i + 1];
host = questions.FirstOrDefault(n => n.Offset == offset).Hostname;
if (host == null)
host = dnsAnswers.FirstOrDefault(n => n.Offset == offset).Payload;
}
ushort AName = BitConverter.ToUInt16(new byte[2] { raw[i + 1], raw[i + 0] });
i += 2;
ushort AType = BitConverter.ToUInt16(new byte[2] { raw[i + 1], raw[i + 0] });
i += 2;
ushort AClass = BitConverter.ToUInt16(new byte[2] { raw[i + 1], raw[i + 0] });
i += 2;
uint ATTL = BitConverter.ToUInt32(new byte[4] { raw[i + 3], raw[i + 2], raw[i + 1], raw[i + 0] });
i += 4;
ushort ALength = BitConverter.ToUInt16(new byte[2] { raw[i + 1], raw[i + 0] });
i += 2;
ushort APreference = 0;
var APayload = "";
byte len = 0;
var aoffset = i;
byte[] addrBuf = null;
DnsAnswer danswer = default;
if (AType == 1 && ALength == 4)
{
addrBuf = new byte[4];
for (int c = 0; c < ALength; c++)
{
addrBuf[c] = raw[i + c];
}
danswer.Address = addrBuf;
}
else
if (AType == 5 || AType == 15)
{
int c = 0;
int shift = 0;
if (AType == 15)
{
APreference = BitConverter.ToUInt16(new byte[2] { raw[i + 1], raw[i + 0] });
i += 2;
shift = 2;
}
for (; c < ALength - shift; c++)
{
var b = raw[i + c];
if ((b) == 192)
{
var idx = raw[i + c + 1];
APayload += '.' + extractbyoffset(raw, idx);
c += 1;
}
else
if (len == 0)
{
len = b;
if (APayload.Length > 0) APayload += '.';
}
else
{
APayload += (char)b;
len--;
}
}
}
else
if (AType == 16)
{
int c = 0;
int shift = 0;
//var textLength = raw[i + 0];
//i += 1;
shift = 0;
for (; c < ALength - shift; c++)
{
var b = raw[i + c];
if ((b) == 192)
{
var idx = raw[i + c + 1];
APayload += '.' + extractbyoffset(raw, idx);
c += 1;
}
else
if (len == 0)
{
len = b;
//if (APayload.Length > 0) APayload += '.';
}
else
{
APayload += (char)b;
len--;
}
}
}
APayload = APayload.Trim('.');
i += ALength - (AType == 15 ? 2 : 0);
danswer.Type = AType;
danswer.Name = host;
danswer.Type = AType;
danswer.Class = AClass;
danswer.TTL = ATTL;
danswer.Length = ALength;
danswer.Payload = string.IsNullOrEmpty(APayload) ? null : APayload;
danswer.Preference = APreference;
danswer.Offset = aoffset;
dnsAnswers.Add(danswer);
if (dnsAnswers.Count == ANCount)
break;
}
var authorities = new List<DnsAuthority>();
if (NSCount > 0)
{
}
respFrame.Requests = questions;
respFrame.Answers = dnsAnswers;
respFrame.Authorities = authorities;
return respFrame;
}
static string extractbyoffset(byte[] src, int from)
{
var buf = "";
var len = src[from];
var i = 1;
byte b = 0;
while (len >= 0 && len < 64)
{
b = src[i + from];
if (b == 192)
{
buf += '.' + extractbyoffset(src, src[i + from + 1]);
}
else
if (len == 0)
{
if (b > 0 && b < 64)
{
buf += '.';
len = (byte)(b + 1); // +1 will be substracted
}
else
break;
}
else
buf += (char)b;
len--;
i++;
}
return buf;
}
}
}