forked from mfontanini/libtins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.cpp
546 lines (489 loc) · 18.1 KB
/
dns.cpp
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* libtins is a net packet wrapper library for crafting and
* interpreting sniffed packets.
*
* Copyright (C) 2011 Nasel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <utility>
#include <stdexcept>
#include <cassert>
#include <memory>
#include "dns.h"
#include "ip_address.h"
using std::string;
using std::list;
namespace Tins {
DNS::DNS() : PDU(255), extra_size(0) {
std::memset(&dns, 0, sizeof(dns));
}
DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
if(total_sz < sizeof(dnshdr))
throw std::runtime_error("Not enough size for a DNS header in the buffer.");
std::memcpy(&dns, buffer, sizeof(dnshdr));
const uint8_t *end(buffer + total_sz);
uint16_t nquestions(questions());
buffer += sizeof(dnshdr);
total_sz -= sizeof(dnshdr);
for(uint16_t i(0); i < nquestions; ++i) {
const uint8_t *ptr(buffer);
while(ptr < end && *ptr)
ptr++;
Query query;
if((ptr + (sizeof(uint16_t) * 2)) >= end)
throw std::runtime_error("Not enough size for a given query.");
query.name = string(buffer, ptr);
ptr++;
const uint16_t *opt_ptr = reinterpret_cast<const uint16_t*>(ptr);
query.type = *(opt_ptr++);
query.qclass = *(opt_ptr++);
queries.push_back(query);
total_sz -= reinterpret_cast<const uint8_t*>(opt_ptr) - buffer;
extra_size += reinterpret_cast<const uint8_t*>(opt_ptr) - buffer;
buffer = reinterpret_cast<const uint8_t*>(opt_ptr);
}
buffer = build_resource_list(ans, buffer, total_sz, answers());
buffer = build_resource_list(arity, buffer, total_sz, authority());
build_resource_list(addit, buffer, total_sz, additional());
}
DNS::DNS(const DNS& rhs) : PDU(rhs) {
copy_fields(&rhs);
}
DNS& DNS::operator=(const DNS& rhs) {
free_list(ans);
free_list(arity);
free_list(addit);
copy_fields(&rhs);
copy_inner_pdu(rhs);
return *this;
}
DNS::~DNS() {
free_list(ans);
free_list(arity);
free_list(addit);
}
void DNS::free_list(ResourcesType &lst) {
while(lst.size()) {
delete lst.front();
lst.pop_front();
}
}
const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_t *ptr, uint32_t &sz, uint16_t nrecs) {
const uint8_t *ptr_end(ptr + sz);
const uint8_t *parse_start(ptr);
for(uint16_t i(0); i < nrecs; ++i) {
const uint8_t *this_opt_start(ptr);
if(ptr + sizeof(uint16_t) > ptr_end)
throw std::runtime_error("Not enough size for a given resource.");
std::auto_ptr<ResourceRecord> res;
// Probably convert to be this constant
if((*ptr & 0xc0)) {
uint16_t offset(*reinterpret_cast<const uint16_t*>(ptr));
offset = Endian::be_to_host(offset) & 0x3fff;
res.reset(new OffsetedResourceRecord(Endian::host_to_be(offset)));
ptr += sizeof(uint16_t);
}
else {
const uint8_t *str_end(ptr);
while(str_end < ptr_end && *str_end)
str_end++;
if(str_end == ptr_end)
throw std::runtime_error("Not enough size for a resource domain name.");
//str_end++;
res.reset(new NamedResourceRecord(string(ptr, str_end)));
ptr = ++str_end;
}
if(ptr + sizeof(res->info) > ptr_end)
throw std::runtime_error("Not enough size for a resource info.");
std::memcpy(&res->info, ptr, sizeof(res->info));
ptr += sizeof(res->info);
if(ptr + sizeof(uint16_t) > ptr_end)
throw std::runtime_error("Not enough size for resource data size.");
// Store the option size.
res->data.resize(
Endian::be_to_host(*reinterpret_cast<const uint16_t*>(ptr))
);
ptr += sizeof(uint16_t);
if(ptr + res->data.size() > ptr_end)
throw std::runtime_error("Not enough size for resource data");
if(contains_dname(res->info.type))
std::copy(ptr, ptr + res->data.size(), res->data.begin());
else if(res->data.size() == sizeof(uint32_t))
*(uint32_t*)&res->data[0] = *(uint32_t*)ptr;
else
throw std::runtime_error("Not enough size for resource data");
ptr += res->data.size();
extra_size += ptr - this_opt_start;
lst.push_back(res.release());
}
sz -= ptr - parse_start;
return ptr;
}
uint32_t DNS::header_size() const {
return sizeof(dns) + extra_size;
}
void DNS::id(uint16_t new_id) {
dns.id = Endian::host_to_be(new_id);
}
void DNS::type(QRType new_qr) {
dns.qr = new_qr;
}
void DNS::opcode(uint8_t new_opcode) {
dns.opcode = new_opcode;
}
void DNS::authoritative_answer(uint8_t new_aa) {
dns.aa = new_aa;
}
void DNS::truncated(uint8_t new_tc) {
dns.tc = new_tc;
}
void DNS::recursion_desired(uint8_t new_rd) {
dns.rd = new_rd;
}
void DNS::recursion_available(uint8_t new_ra) {
dns.ra = new_ra;
}
void DNS::z(uint8_t new_z) {
dns.z = new_z;
}
void DNS::authenticated_data(uint8_t new_ad) {
dns.ad = new_ad;
}
void DNS::checking_disabled(uint8_t new_cd) {
dns.cd = new_cd;
}
void DNS::rcode(uint8_t new_rcode) {
dns.rcode = new_rcode;
}
bool DNS::contains_dname(uint16_t type) {
type = Endian::be_to_host(type);
return type == MX || type == CNAME ||
type == PTR || type == NS;
}
void DNS::add_query(const string &name, QueryType type, QueryClass qclass) {
string new_str;
parse_domain_name(name, new_str);
queries.push_back(
Query(new_str,
Endian::host_to_be<uint16_t>(type),
Endian::host_to_be<uint16_t>(qclass))
);
extra_size += new_str.size() + 1 + (sizeof(uint16_t) << 1);
dns.questions = Endian::host_to_be<uint16_t>(queries.size());
}
void DNS::add_query(const Query &query) {
add_query(
query.name,
static_cast<QueryType>(query.type),
static_cast<QueryClass>(query.qclass)
);
}
void DNS::add_answer(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, IPv4Address ip) {
ResourceRecord *res = make_record(name, type, qclass, ttl, Endian::host_to_be((uint32_t)ip));
ans.push_back(res);
dns.answers = Endian::host_to_be<uint16_t>(ans.size());
}
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
uint32_t ttl, const std::string &dname) {
string new_str;
parse_domain_name(dname, new_str);
ResourceRecord *res = make_record(name, type, qclass, ttl, new_str);
ans.push_back(res);
dns.answers = Endian::host_to_be<uint16_t>(ans.size());
}
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
uint32_t ttl, const uint8_t *data, uint32_t sz) {
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
ans.push_back(res);
dns.answers = Endian::host_to_be<uint16_t>(ans.size());
}
void DNS::add_authority(const string &name, QueryType type,
QueryClass qclass, uint32_t ttl, const uint8_t *data, uint32_t sz) {
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
arity.push_back(res);
dns.authority = Endian::host_to_be<uint16_t>(arity.size());
}
void DNS::add_additional(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
ResourceRecord *res = make_record(name, type, qclass, ttl, ip);
addit.push_back(res);
dns.additional = Endian::host_to_be<uint16_t>(addit.size());
}
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
ip = Endian::host_to_be(ip);
return make_record(name, type, qclass, ttl, reinterpret_cast<uint8_t*>(&ip), sizeof(ip));
}
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const std::string &dname) {
return make_record(name, type, qclass, ttl, reinterpret_cast<const uint8_t*>(dname.c_str()), dname.size() + 1);
}
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, const uint8_t *ptr, uint32_t len) {
string nm;
parse_domain_name(name, nm);
uint16_t index = find_domain_name(nm);
ResourceRecord *res;
if(index)
res = new OffsetedResourceRecord(Endian::host_to_be(index), ptr, len);
else
res = new NamedResourceRecord(nm, ptr, len);
res->info.type = Endian::host_to_be<uint16_t>(type);
res->info.qclass = Endian::host_to_be<uint16_t>(qclass);
res->info.ttl = Endian::host_to_be(ttl);
extra_size += res->size();
return res;
}
uint32_t DNS::find_domain_name(const std::string &dname) {
uint16_t index(sizeof(dnshdr));
list<Query>::const_iterator it(queries.begin());
for(; it != queries.end() && it->name != dname; ++it)
index += it->name.size() + 1 + (sizeof(uint16_t) << 1);
if(it != queries.end() ||
find_domain_name(dname, ans, index) ||
find_domain_name(dname, arity, index) ||
find_domain_name(dname, addit, index))
return index;
else
return 0;
}
bool DNS::find_domain_name(const std::string &dname, const ResourcesType &lst, uint16_t &out) {
ResourcesType::const_iterator it(lst.begin());
while(it != lst.end()) {
if((*it)->matches(dname))
break;
out += (*it)->size();
++it;
}
return it != lst.end();
}
void DNS::parse_domain_name(const std::string &dn, std::string &out) const {
size_t last_index(0), index;
while((index = dn.find('.', last_index+1)) != string::npos) {
out.push_back(index - last_index);
out.append(dn.begin() + last_index, dn.begin() + index);
last_index = index + 1; //skip dot
}
out.push_back(dn.size() - last_index);
out.append(dn.begin() + last_index, dn.end());
}
void DNS::unparse_domain_name(const std::string &dn, std::string &out) const {
if(dn.size()) {
uint32_t index(1), len(dn[0]);
while(index + len < dn.size() && len) {
if(index != 1)
out.push_back('.');
out.append(dn.begin() + index, dn.begin() + index + len);
index += len;
if(index < dn.size() - 1)
len = dn[index];
index++;
}
if(index < dn.size()) {
out.push_back('.');
out.append(dn.begin() + index, dn.end());
}
}
}
void DNS::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
assert(total_sz >= sizeof(dns) + extra_size);
std::memcpy(buffer, &dns, sizeof(dns));
buffer += sizeof(dns);
for(list<Query>::const_iterator it(queries.begin()); it != queries.end(); ++it) {
std::memcpy(buffer, it->name.c_str(), it->name.size() + 1);
buffer += it->name.size() + 1;
*((uint16_t*)buffer) = it->type;
buffer += sizeof(uint16_t);
*((uint16_t*)buffer) = it->qclass;
buffer += sizeof(uint16_t);
}
buffer = serialize_list(ans, buffer);
buffer = serialize_list(arity, buffer);
buffer = serialize_list(addit, buffer);
}
uint8_t *DNS::serialize_list(const ResourcesType &lst, uint8_t *buffer) const {
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it)
buffer += (*it)->write(buffer);
return buffer;
}
void DNS::add_suffix(uint32_t index, const uint8_t *data, uint32_t sz) {
uint32_t i(0), suff_sz(data[0]);
SuffixMap::iterator it;
while((i + suff_sz + 1 <= sz || (suff_sz == 0xc0 && i + 1 < sz)) && suff_sz) {
if((suff_sz & 0xc0)) {
if((it = suffixes.find(data[i+1])) != suffixes.end())
suffix_indices[index + i] = data[i+1];
i += sizeof(uint16_t);
}
else {
++i;
suffixes.insert(std::make_pair(index + i - 1, string(data + i, data + i + suff_sz)));
i += suff_sz;
}
if(i < sz)
suff_sz = data[i];
}
}
uint32_t DNS::build_suffix_map(uint32_t index, const list<ResourceRecord*> &lst) {
const string *str;
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it) {
str = (*it)->dname_pointer();
if(str) {
add_suffix(index, (uint8_t*)str->c_str(), str->size());
index += str->size() + 1;
}
else
index += sizeof(uint16_t);
index += sizeof(ResourceRecord::Info) + sizeof(uint16_t);
uint32_t sz((*it)->data_size());
const uint8_t *ptr = (*it)->data_pointer();
if(Endian::be_to_host((*it)->info.type) == MX) {
ptr += 2;
sz -= 2;
index += 2;
}
if(contains_dname((*it)->info.type))
add_suffix(index, ptr, sz);
index += sz;
}
return index;
}
uint32_t DNS::build_suffix_map(uint32_t index, const list<Query> &lst) {
for(list<Query>::const_iterator it(lst.begin()); it != lst.end(); ++it) {
add_suffix(index, (uint8_t*)it->name.c_str(), it->name.size());
index += it->name.size() + 1 + (sizeof(uint16_t) << 1);
}
return index;
}
void DNS::build_suffix_map() {
uint32_t index(sizeof(dnshdr));
index = build_suffix_map(index, queries);
index = build_suffix_map(index, ans);
index = build_suffix_map(index, arity);
build_suffix_map(index, addit);
}
void DNS::compose_name(const uint8_t *ptr, uint32_t sz, std::string &out) {
uint32_t i(0);
while(i < sz) {
if(i && ptr[i])
out.push_back('.');
if((ptr[i] & 0xc0)) {
uint16_t index = Endian::be_to_host(*((uint16_t*)(ptr + i)));
index &= 0x3fff;
SuffixMap::iterator it(suffixes.find(index));
SuffixIndices::iterator suff_it(suffix_indices.find(index));
//assert(it != suffixes.end() && suff_it == suffix_indices.end());
if(it == suffixes.end() || suff_it == suffix_indices.end())
throw std::runtime_error("Malformed DNS packet");
bool first(true);
do {
if(it != suffixes.end()) {
if(!first)
out.push_back('.');
first = false;
out += it->second;
index += it->second.size() + 1;
}
else
index = suff_it->second;
it = suffixes.find(index);
if(it == suffixes.end())
suff_it = suffix_indices.find(index);
} while(it != suffixes.end() || suff_it != suffix_indices.end());
break;
}
else {
uint8_t suff_sz(ptr[i]);
i++;
if(i + suff_sz <= sz)
out.append(ptr + i, ptr + i + suff_sz);
i += suff_sz;
}
}
}
void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res) {
if(!suffixes.size())
build_suffix_map();
const string *str_ptr;
const uint8_t *ptr;
uint32_t sz;
for(ResourcesType::const_iterator it(lst.begin()); it != lst.end(); ++it) {
string dname, addr;
if((str_ptr = (*it)->dname_pointer()))
compose_name(reinterpret_cast<const uint8_t*>(str_ptr->c_str()), str_ptr->size(), dname);
else {
uint16_t offset = static_cast<OffsetedResourceRecord*>(*it)->offset;
compose_name((uint8_t*)&offset, 2, dname);
}
ptr = (*it)->data_pointer();
sz = (*it)->data_size();
if(sz == 4)
addr = IPv4Address(*(uint32_t*)ptr).to_string();
else {
if(Endian::be_to_host((*it)->info.type) == MX) {
ptr += 2;
sz -= 2;
}
compose_name(ptr, sz, addr);
}
res.push_back(
Resource(dname, addr, Endian::be_to_host((*it)->info.type),
Endian::host_to_be((*it)->info.qclass), Endian::be_to_host((*it)->info.ttl)
)
);
}
}
DNS::queries_type DNS::dns_queries() const {
queries_type output;
for(std::list<Query>::const_iterator it(queries.begin()); it != queries.end(); ++it) {
string dn;
unparse_domain_name(it->name, dn);
output.push_back(Query(dn, Endian::be_to_host(it->type), Endian::be_to_host(it->qclass)));
}
return output;
}
DNS::resources_type DNS::dns_answers() {
resources_type res;
convert_resources(ans, res);
return res;
}
void DNS::copy_fields(const DNS *other) {
std::memcpy(&dns, &other->dns, sizeof(dns));
extra_size = other->extra_size;
queries = other->queries;
copy_list(other->ans, ans);
copy_list(other->arity, arity);
copy_list(other->addit, addit);
}
void DNS::copy_list(const ResourcesType &from, ResourcesType &to) const {
for(ResourcesType::const_iterator it(from.begin()); it != from.end(); ++it) {
to.push_back((*it)->clone());
}
}
// ResourceRecord
uint32_t DNS::ResourceRecord::write(uint8_t *buffer) const {
const uint32_t sz(do_write(buffer));
buffer += sz;
std::memcpy(buffer, &info, sizeof(info));
buffer += sizeof(info);
*((uint16_t*)buffer) = Endian::host_to_be(data.size());
buffer += sizeof(uint16_t);
std::copy(data.begin(), data.end(), buffer);
return sz + sizeof(info) + sizeof(uint16_t) + data.size();
}
DNS::ResourceRecord *DNS::OffsetedResourceRecord::clone() const {
return new OffsetedResourceRecord(*this);
}
DNS::ResourceRecord *DNS::NamedResourceRecord::clone() const {
return new NamedResourceRecord(*this);
}
}