-
Notifications
You must be signed in to change notification settings - Fork 1
/
DnsClient.cs
208 lines (148 loc) · 6.36 KB
/
DnsClient.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace ndot
{
public static class DnsClient
{
static ushort IDIncrement = 1024;
//static UdpClient dnsClient = new UdpClient();
static DnsClient()
{
//dnsClient.Connect(IPAddress.Parse("8.8.8.8"), 53);
}
public static Task<DnsResponse> ResolveNameAsync(string hostname, DnsRequestType reqtype = DnsRequestType.A, bool secure = true, int dnsport = 53)
{
dynbitfield bf = bitfield_ctor(hostname, reqtype);
var length = BitConverter.GetBytes((ushort)bf.bytes.Count).Rev(); // [5], 4.2.2. TCP usage
var frame = bf.bytes.ToArray();
if (secure)
{
TcpClient dnsClient = new TcpClient();
dnsClient.Connect(IPAddress.Parse(secureDNSAddress), 853);
var ns = dnsClient.GetStream();
SslStream ssl = new SslStream(ns);
ssl.AuthenticateAsClient(clientOpts);
byte[] recv = new byte[512];
var read = ssl.ReadAsync(recv, 0, recv.Length).ContinueWith<DnsResponse>((t) =>
{
var size = BitConverter.ToUInt16(new byte[2] { recv[1], recv[0] });
var raw = new byte[size];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = recv[i + 2];
}
return DnsResponse.FromRaw(raw);
});
ssl.Write(length, 0, length.Length);
ssl.Write(frame, 0, frame.Length);
return read;
}
else
{
UdpClient dnsClient = new UdpClient();
dnsClient.Connect(IPAddress.Parse(defaultDNSAddress), dnsport);
//byte[] recv = new byte[512];
var t = dnsClient.ReceiveAsync().ContinueWith<DnsResponse>((r) =>
{
try
{
var udpr = r.Result;
var resp = DnsResponse.FromRaw(udpr.Buffer);
return resp;
}
catch (Exception e)
{
return default;
}
});
dnsClient.Send(frame, frame.Length);
return t;
}
}
static dynbitfield bitfield_ctor(string hostname, DnsRequestType reqtype = DnsRequestType.A)
{
dynbitfield bf = new dynbitfield();
bf.bytes = new List<byte>(14);
for (int i = 0; i < 12; i++)
bf.bytes.Add(0);
// === HEADER START ===
IDIncrement++;
var id = BitConverter.GetBytes(IDIncrement).Rev();
bf.bytes[0] = id[0];
bf.bytes[1] = id[1];
var isresponse = false;
if (isresponse)
bf.bytes[2] |= (byte)0b_1000_0000; // QR: 0 = query, 1 = response
var istruncated = false;
if (istruncated)
bf.bytes[2] |= (byte)0b_0000_0010; // TC: 0 = not truncated, 1 = truncated
var dorecursive = true;
if (dorecursive)
bf.bytes[2] |= (byte)0b_0000_0001; // RD: 0 = recursion not desired, 1 = else
var Z = false;
if (Z)
bf.bytes[3] |= (byte)0b_0100_0000; // Z: reserved
var nonauth = false;
if (nonauth)
bf.bytes[3] |= (byte)0b_0001_0000; // NA: 0 = don't send non-auth data, 1 = else
var QCount = BitConverter.GetBytes((ushort)1).Rev(); // Questions Count [u16]
bf.bytes[4] = QCount[0];
bf.bytes[5] = QCount[1];
var ANCount = BitConverter.GetBytes((ushort)0).Rev(); // Answer Record Count [u16]
bf.bytes[6] = ANCount[0];
bf.bytes[7] = ANCount[1];
var NSCount = BitConverter.GetBytes((ushort)0).Rev(); // Authority Record Count [u16]
bf.bytes[8] = NSCount[0];
bf.bytes[9] = NSCount[1];
var ARCount = BitConverter.GetBytes((ushort)0).Rev(); // Additional Record Count [u16]
bf.bytes[10] = ARCount[0];
bf.bytes[11] = ARCount[1];
// === HEADER END ===
// === QUESTION START ===
var split = hostname.Split('.');
foreach (var s in split)
{
var len = (byte)s.Length;
bf.bytes.Add(len);
bf.bytes.AddRange(s.UTF8Encode());
}
bf.bytes.Add(0);
byte[] QType = BitConverter.GetBytes((ushort)reqtype).Rev();
bf.bytes.AddRange(QType);
byte QClass0 = 0;
byte QClass1 = 1;
byte[] QClass = new byte[2] { QClass0, QClass1 };
bf.bytes.AddRange(QClass);
// === QUESTION END ===
return bf;
}
public static string defaultDNSAddress = NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().DnsAddresses[0].ToString();
public static string secureDNSAddress = "8.8.4.4";
static SslClientAuthenticationOptions clientOpts = new SslClientAuthenticationOptions
{
EncryptionPolicy = EncryptionPolicy.RequireEncryption,
TargetHost = secureDNSAddress,
RemoteCertificateValidationCallback = CheckDnsServerCertCallback,
};
static bool CheckDnsServerCertCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
var c2 = certificate as X509Certificate2;
if (c2 != null)
{
var veresult = c2.Verify();
return veresult;
}
return true;
}
return false;
}
}
}