/src/PcapPlusPlus/Packet++/src/DnsResource.cpp
Line | Count | Source |
1 | 0 | #define LOG_MODULE PacketLogModuleDnsLayer |
2 | | |
3 | | #include "DnsResource.h" |
4 | | #include "Logger.h" |
5 | | #include <limits> |
6 | | #include <sstream> |
7 | | #include <vector> |
8 | | #include "EndianPortable.h" |
9 | | |
10 | | namespace pcpp |
11 | | { |
12 | | |
13 | | IDnsResource::IDnsResource(DnsLayer* dnsLayer, size_t offsetInLayer) |
14 | 0 | : m_DnsLayer(dnsLayer), m_OffsetInLayer(offsetInLayer), m_NextResource(nullptr), m_ExternalRawData(nullptr) |
15 | 0 | { |
16 | 0 | char decodedName[4096]; |
17 | 0 | m_NameLength = decodeName((const char*)getRawData(), decodedName); |
18 | 0 | if (m_NameLength > 0) |
19 | 0 | m_DecodedName = decodedName; |
20 | 0 | } |
21 | | |
22 | | IDnsResource::IDnsResource(uint8_t* emptyRawData) |
23 | 0 | : m_DnsLayer(nullptr), m_OffsetInLayer(0), m_NextResource(nullptr), m_DecodedName(""), m_NameLength(0), |
24 | 0 | m_ExternalRawData(emptyRawData) |
25 | 0 | {} |
26 | | |
27 | | uint8_t* IDnsResource::getRawData() const |
28 | 0 | { |
29 | 0 | if (m_DnsLayer == nullptr) |
30 | 0 | return m_ExternalRawData; |
31 | | |
32 | 0 | return m_DnsLayer->m_Data + m_OffsetInLayer; |
33 | 0 | } |
34 | | |
35 | | static size_t cleanup(char* resultPtr, char* result, size_t encodedNameLength) |
36 | 0 | { |
37 | | // remove the last "." |
38 | 0 | if (resultPtr > result) |
39 | 0 | { |
40 | 0 | result[resultPtr - result - 1] = 0; |
41 | 0 | } |
42 | |
|
43 | 0 | if (resultPtr - result < 256) |
44 | 0 | { |
45 | | // add the last '\0' to encodedNameLength |
46 | 0 | resultPtr[0] = 0; |
47 | 0 | encodedNameLength++; |
48 | 0 | } |
49 | |
|
50 | 0 | return encodedNameLength; |
51 | 0 | } |
52 | | |
53 | | size_t IDnsResource::decodeName(const char* encodedName, char* result, int iteration) |
54 | 0 | { |
55 | 0 | size_t encodedNameLength = 0; |
56 | 0 | size_t decodedNameLength = 0; |
57 | 0 | char* resultPtr = result; |
58 | 0 | resultPtr[0] = 0; |
59 | |
|
60 | 0 | size_t curOffsetInLayer = (uint8_t*)encodedName - m_DnsLayer->m_Data; |
61 | 0 | if (curOffsetInLayer + 1 > m_DnsLayer->m_DataLen) |
62 | 0 | return encodedNameLength; |
63 | | |
64 | 0 | if (iteration > 20) |
65 | 0 | { |
66 | 0 | return encodedNameLength; |
67 | 0 | } |
68 | | |
69 | 0 | uint8_t wordLength = encodedName[0]; |
70 | | |
71 | | // A string to parse |
72 | 0 | while (wordLength != 0) |
73 | 0 | { |
74 | | // A pointer to another place in the packet |
75 | 0 | if ((wordLength & 0xc0) == 0xc0) |
76 | 0 | { |
77 | 0 | if (curOffsetInLayer + 2 > m_DnsLayer->m_DataLen || encodedNameLength > 255) |
78 | 0 | return cleanup(resultPtr, result, encodedNameLength); |
79 | | |
80 | 0 | uint16_t offsetInLayer = |
81 | 0 | (wordLength & 0x3f) * 256 + (0xFF & encodedName[1]) + m_DnsLayer->m_OffsetAdjustment; |
82 | 0 | if (offsetInLayer < sizeof(dnshdr) || offsetInLayer >= m_DnsLayer->m_DataLen) |
83 | 0 | { |
84 | 0 | PCPP_LOG_ERROR("DNS parsing error: name pointer is illegal"); |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | 0 | char tempResult[4096]; |
89 | 0 | memset(tempResult, 0, sizeof(tempResult)); |
90 | 0 | int i = 0; |
91 | 0 | decodeName((const char*)(m_DnsLayer->m_Data + offsetInLayer), tempResult, iteration + 1); |
92 | 0 | while (tempResult[i] != 0 && decodedNameLength < 255) |
93 | 0 | { |
94 | 0 | resultPtr[0] = tempResult[i++]; |
95 | 0 | resultPtr++; |
96 | 0 | decodedNameLength++; |
97 | 0 | } |
98 | |
|
99 | 0 | resultPtr[0] = 0; |
100 | | |
101 | | // in this case the length of the pointer is: 1 byte for 0xc0 + 1 byte for the offset itself |
102 | 0 | return encodedNameLength + sizeof(uint16_t); |
103 | 0 | } |
104 | 0 | else |
105 | 0 | { |
106 | | // return if next word would be outside of the DNS layer or overflow the buffer behind resultPtr |
107 | 0 | if (curOffsetInLayer + wordLength + 1 > m_DnsLayer->m_DataLen || encodedNameLength + wordLength > 255) |
108 | 0 | { |
109 | | // add the last '\0' to the decoded string |
110 | 0 | if (encodedNameLength == 256) |
111 | 0 | { |
112 | 0 | resultPtr--; |
113 | | // cppcheck-suppress unreadVariable |
114 | 0 | decodedNameLength--; |
115 | 0 | } |
116 | 0 | else |
117 | 0 | { |
118 | 0 | encodedNameLength++; |
119 | 0 | } |
120 | |
|
121 | 0 | resultPtr[0] = 0; |
122 | 0 | return encodedNameLength; |
123 | 0 | } |
124 | | |
125 | 0 | memcpy(resultPtr, encodedName + 1, wordLength); |
126 | 0 | resultPtr += wordLength; |
127 | 0 | resultPtr[0] = '.'; |
128 | 0 | resultPtr++; |
129 | 0 | decodedNameLength += wordLength + 1; |
130 | 0 | encodedName += wordLength + 1; |
131 | 0 | encodedNameLength += wordLength + 1; |
132 | |
|
133 | 0 | curOffsetInLayer = (uint8_t*)encodedName - m_DnsLayer->m_Data; |
134 | 0 | if (curOffsetInLayer + 1 > m_DnsLayer->m_DataLen) |
135 | 0 | { |
136 | | // add the last '\0' to the decoded string |
137 | 0 | if (encodedNameLength == 256) |
138 | 0 | { |
139 | | // cppcheck-suppress unreadVariable |
140 | 0 | decodedNameLength--; |
141 | 0 | resultPtr--; |
142 | 0 | } |
143 | 0 | else |
144 | 0 | { |
145 | 0 | encodedNameLength++; |
146 | 0 | } |
147 | |
|
148 | 0 | resultPtr[0] = 0; |
149 | 0 | return encodedNameLength; |
150 | 0 | } |
151 | | |
152 | 0 | wordLength = encodedName[0]; |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | return cleanup(resultPtr, result, encodedNameLength); |
157 | 0 | } |
158 | | |
159 | | void IDnsResource::encodeName(const std::string& decodedName, char* result, size_t& resultLen) |
160 | 0 | { |
161 | 0 | resultLen = 0; |
162 | 0 | std::stringstream strstream(decodedName); |
163 | 0 | std::string word; |
164 | 0 | while (getline(strstream, word, '.')) |
165 | 0 | { |
166 | | // pointer to a different hostname in the packet |
167 | 0 | if (word[0] == '#') |
168 | 0 | { |
169 | | // convert the number from string to int |
170 | 0 | std::stringstream stream(word.substr(1)); |
171 | 0 | int pointerInPacket = 0; |
172 | 0 | stream >> pointerInPacket; |
173 | | |
174 | | // verify it's indeed a number and that is in the range of [0-255] |
175 | 0 | if (stream.fail() || pointerInPacket < 0 || pointerInPacket > 0xff) |
176 | 0 | { |
177 | 0 | PCPP_LOG_ERROR("Error encoding the string '" << decodedName << "'"); |
178 | 0 | return; |
179 | 0 | } |
180 | | |
181 | | // set the pointer to the encoded string result |
182 | 0 | result[0] = (uint8_t)0xc0; |
183 | 0 | result[1] = (uint8_t)pointerInPacket; |
184 | 0 | resultLen += 2; |
185 | 0 | return; // pointer always comes last |
186 | 0 | } |
187 | | |
188 | 0 | result[0] = word.length(); |
189 | 0 | result++; |
190 | 0 | memcpy(result, word.c_str(), word.length()); |
191 | 0 | result += word.length(); |
192 | 0 | resultLen += word.length() + 1; |
193 | 0 | } |
194 | | |
195 | 0 | result[0] = 0; |
196 | 0 | resultLen++; |
197 | 0 | } |
198 | | |
199 | | DnsType IDnsResource::getDnsType() const |
200 | 0 | { |
201 | 0 | uint16_t dnsType = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength); |
202 | 0 | return static_cast<DnsType>(be16toh(dnsType)); |
203 | 0 | } |
204 | | |
205 | | void IDnsResource::setDnsType(DnsType newType) |
206 | 0 | { |
207 | 0 | uint16_t newTypeAsInt = htobe16((uint16_t)newType); |
208 | 0 | memcpy(getRawData() + m_NameLength, &newTypeAsInt, sizeof(uint16_t)); |
209 | 0 | } |
210 | | |
211 | | DnsClass IDnsResource::getDnsClass() const |
212 | 0 | { |
213 | 0 | uint16_t dnsClass = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength + sizeof(uint16_t)); |
214 | 0 | return static_cast<DnsClass>(be16toh(dnsClass)); |
215 | 0 | } |
216 | | |
217 | | void IDnsResource::setDnsClass(DnsClass newClass) |
218 | 0 | { |
219 | 0 | uint16_t newClassAsInt = htobe16((uint16_t)newClass); |
220 | 0 | memcpy(getRawData() + m_NameLength + sizeof(uint16_t), &newClassAsInt, sizeof(uint16_t)); |
221 | 0 | } |
222 | | |
223 | | bool IDnsResource::setName(const std::string& newName) |
224 | 0 | { |
225 | 0 | char encodedName[4096]; |
226 | 0 | size_t encodedNameLen = 0; |
227 | 0 | encodeName(newName, encodedName, encodedNameLen); |
228 | 0 | if (m_DnsLayer != nullptr) |
229 | 0 | { |
230 | 0 | if (encodedNameLen > m_NameLength) |
231 | 0 | { |
232 | 0 | if (!m_DnsLayer->extendLayer(m_OffsetInLayer, encodedNameLen - m_NameLength, this)) |
233 | 0 | { |
234 | 0 | PCPP_LOG_ERROR("Couldn't set name for DNS query, unable to extend layer"); |
235 | 0 | return false; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | else if (encodedNameLen < m_NameLength) |
239 | 0 | { |
240 | 0 | if (!m_DnsLayer->shortenLayer(m_OffsetInLayer, m_NameLength - encodedNameLen, this)) |
241 | 0 | { |
242 | 0 | PCPP_LOG_ERROR("Couldn't set name for DNS query, unable to shorten layer"); |
243 | 0 | return false; |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } |
247 | 0 | else |
248 | 0 | { |
249 | 0 | size_t size = getSize(); |
250 | 0 | char* tempData = new char[size]; |
251 | 0 | memcpy(tempData, m_ExternalRawData, size); |
252 | 0 | memcpy(m_ExternalRawData + encodedNameLen, tempData, size); |
253 | 0 | delete[] tempData; |
254 | 0 | } |
255 | | |
256 | 0 | memcpy(getRawData(), encodedName, encodedNameLen); |
257 | 0 | m_NameLength = encodedNameLen; |
258 | 0 | m_DecodedName = newName; |
259 | |
|
260 | 0 | return true; |
261 | 0 | } |
262 | | |
263 | | void IDnsResource::setDnsLayer(DnsLayer* dnsLayer, size_t offsetInLayer) |
264 | 0 | { |
265 | 0 | memcpy(dnsLayer->m_Data + offsetInLayer, m_ExternalRawData, getSize()); |
266 | 0 | m_DnsLayer = dnsLayer; |
267 | 0 | m_OffsetInLayer = offsetInLayer; |
268 | 0 | m_ExternalRawData = nullptr; |
269 | 0 | } |
270 | | |
271 | | uint32_t DnsResource::getTTL() const |
272 | 0 | { |
273 | 0 | uint32_t ttl = *reinterpret_cast<uint32_t*>(getRawData() + m_NameLength + 2 * sizeof(uint16_t)); |
274 | 0 | return be32toh(ttl); |
275 | 0 | } |
276 | | |
277 | | void DnsResource::setTTL(uint32_t newTTL) |
278 | 0 | { |
279 | 0 | newTTL = htobe32(newTTL); |
280 | 0 | memcpy(getRawData() + m_NameLength + 2 * sizeof(uint16_t), &newTTL, sizeof(uint32_t)); |
281 | 0 | } |
282 | | |
283 | | size_t DnsResource::getDataLength() const |
284 | 0 | { |
285 | |
|
286 | 0 | size_t sizeToRead = m_NameLength + 2 * sizeof(uint16_t) + sizeof(uint32_t); |
287 | | |
288 | | // Heap buffer overflow may occur here, check boundary of m_DnsLayer->m_Data first |
289 | | // Due to dataLength which is uint16_t, here m_DnsLayer->m_Data must have at least 2 bytes to read |
290 | 0 | if (m_DnsLayer && m_OffsetInLayer + sizeToRead >= m_DnsLayer->m_DataLen - 1) |
291 | 0 | { |
292 | 0 | return 0; |
293 | 0 | } |
294 | | |
295 | 0 | uint16_t dataLength = *reinterpret_cast<uint16_t*>(getRawData() + sizeToRead); |
296 | 0 | return be16toh(dataLength); |
297 | 0 | } |
298 | | |
299 | | DnsResourceDataPtr DnsResource::getData() const |
300 | 0 | { |
301 | 0 | uint8_t* resourceRawData = getRawData() + m_NameLength + 3 * sizeof(uint16_t) + sizeof(uint32_t); |
302 | 0 | size_t dataLength = getDataLength(); |
303 | |
|
304 | 0 | switch (getDnsType()) |
305 | 0 | { |
306 | 0 | case DNS_TYPE_A: |
307 | 0 | { |
308 | 0 | return DnsResourceDataPtr(new IPv4DnsResourceData(resourceRawData, dataLength)); |
309 | 0 | } |
310 | | |
311 | 0 | case DNS_TYPE_AAAA: |
312 | 0 | { |
313 | 0 | return DnsResourceDataPtr(new IPv6DnsResourceData(resourceRawData, dataLength)); |
314 | 0 | } |
315 | | |
316 | 0 | case DNS_TYPE_NS: |
317 | 0 | case DNS_TYPE_CNAME: |
318 | 0 | case DNS_TYPE_DNAM: |
319 | 0 | case DNS_TYPE_PTR: |
320 | 0 | { |
321 | 0 | return DnsResourceDataPtr(new StringDnsResourceData( |
322 | 0 | resourceRawData, dataLength, const_cast<IDnsResource*>(static_cast<const IDnsResource*>(this)))); |
323 | 0 | } |
324 | | |
325 | 0 | case DNS_TYPE_MX: |
326 | 0 | { |
327 | 0 | return DnsResourceDataPtr(new MxDnsResourceData( |
328 | 0 | resourceRawData, dataLength, const_cast<IDnsResource*>(static_cast<const IDnsResource*>(this)))); |
329 | 0 | } |
330 | | |
331 | 0 | default: |
332 | 0 | { |
333 | 0 | return DnsResourceDataPtr(new GenericDnsResourceData(resourceRawData, dataLength)); |
334 | 0 | } |
335 | 0 | } |
336 | 0 | } |
337 | | |
338 | | size_t DnsResource::getDataOffset() const |
339 | 0 | { |
340 | 0 | return (size_t)(m_OffsetInLayer + m_NameLength + 3 * sizeof(uint16_t) + sizeof(uint32_t)); |
341 | 0 | } |
342 | | |
343 | | bool DnsResource::setData(IDnsResourceData* data) |
344 | 0 | { |
345 | 0 | if (data == nullptr) |
346 | 0 | { |
347 | 0 | PCPP_LOG_ERROR("Given data is nullptr"); |
348 | 0 | return false; |
349 | 0 | } |
350 | | |
351 | 0 | switch (getDnsType()) |
352 | 0 | { |
353 | 0 | case DNS_TYPE_A: |
354 | 0 | { |
355 | 0 | if (!data->isTypeOf<IPv4DnsResourceData>()) |
356 | 0 | { |
357 | 0 | PCPP_LOG_ERROR("DNS record is of type A but given data isn't of type IPv4DnsResourceData"); |
358 | 0 | return false; |
359 | 0 | } |
360 | 0 | break; |
361 | 0 | } |
362 | | |
363 | 0 | case DNS_TYPE_AAAA: |
364 | 0 | { |
365 | 0 | if (!data->isTypeOf<IPv6DnsResourceData>()) |
366 | 0 | { |
367 | 0 | PCPP_LOG_ERROR("DNS record is of type AAAA but given data isn't of type IPv6DnsResourceData"); |
368 | 0 | return false; |
369 | 0 | } |
370 | 0 | break; |
371 | 0 | } |
372 | | |
373 | 0 | case DNS_TYPE_NS: |
374 | 0 | case DNS_TYPE_CNAME: |
375 | 0 | case DNS_TYPE_DNAM: |
376 | 0 | case DNS_TYPE_PTR: |
377 | 0 | { |
378 | 0 | if (!data->isTypeOf<StringDnsResourceData>()) |
379 | 0 | { |
380 | 0 | PCPP_LOG_ERROR( |
381 | 0 | "DNS record is of type NS, CNAME, DNAM or PTR but given data isn't of type StringDnsResourceData"); |
382 | 0 | return false; |
383 | 0 | } |
384 | 0 | break; |
385 | 0 | } |
386 | | |
387 | 0 | case DNS_TYPE_MX: |
388 | 0 | { |
389 | 0 | if (!data->isTypeOf<MxDnsResourceData>()) |
390 | 0 | { |
391 | 0 | PCPP_LOG_ERROR("DNS record is of type MX but given data isn't of type MxDnsResourceData"); |
392 | 0 | return false; |
393 | 0 | } |
394 | 0 | break; |
395 | 0 | } |
396 | | |
397 | 0 | default: |
398 | 0 | { |
399 | | // do nothing |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | | // convert the IDnsResourceData to a byte array |
404 | 0 | std::vector<uint8_t> dataAsByteArr = data->toBytes(this); |
405 | 0 | if (dataAsByteArr.empty()) |
406 | 0 | { |
407 | 0 | PCPP_LOG_ERROR("Cannot convert DNS resource data to byte array, data is probably invalid"); |
408 | 0 | return false; |
409 | 0 | } |
410 | | |
411 | 0 | size_t dataLength = dataAsByteArr.size(); |
412 | | |
413 | | // DNS RR data length is encoded in a 16-bit field. Parentheses around max avoid a clash with the |
414 | | // Windows max macro |
415 | 0 | if (dataLength > (std::numeric_limits<uint16_t>::max)()) |
416 | 0 | { |
417 | 0 | PCPP_LOG_ERROR("DNS resource data is too large: " << dataLength); |
418 | 0 | return false; |
419 | 0 | } |
420 | | |
421 | 0 | size_t dataLengthOffset = m_NameLength + (2 * sizeof(uint16_t)) + sizeof(uint32_t); |
422 | 0 | size_t dataOffset = dataLengthOffset + sizeof(uint16_t); |
423 | |
|
424 | 0 | if (m_DnsLayer != nullptr) |
425 | 0 | { |
426 | 0 | size_t curLength = getDataLength(); |
427 | 0 | if (dataLength > curLength) |
428 | 0 | { |
429 | 0 | if (!m_DnsLayer->extendLayer(m_OffsetInLayer + dataOffset, dataLength - curLength, this)) |
430 | 0 | { |
431 | 0 | PCPP_LOG_ERROR("Couldn't set data for DNS query, unable to extend layer"); |
432 | 0 | return false; |
433 | 0 | } |
434 | 0 | } |
435 | 0 | else if (dataLength < curLength) |
436 | 0 | { |
437 | 0 | if (!m_DnsLayer->shortenLayer(m_OffsetInLayer + dataOffset, curLength - dataLength, this)) |
438 | 0 | { |
439 | 0 | PCPP_LOG_ERROR("Couldn't set data for DNS query, unable to shorten layer"); |
440 | 0 | return false; |
441 | 0 | } |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | // write data to resource |
446 | 0 | memcpy(getRawData() + dataOffset, dataAsByteArr.data(), dataLength); |
447 | | // update data length in resource |
448 | 0 | dataLength = htobe16((uint16_t)dataLength); |
449 | 0 | memcpy(getRawData() + dataLengthOffset, &dataLength, sizeof(uint16_t)); |
450 | |
|
451 | 0 | return true; |
452 | 0 | } |
453 | | |
454 | | uint16_t DnsResource::getCustomDnsClass() const |
455 | 0 | { |
456 | 0 | uint16_t value = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength + sizeof(uint16_t)); |
457 | 0 | return be16toh(value); |
458 | 0 | } |
459 | | |
460 | | void DnsResource::setCustomDnsClass(uint16_t customValue) |
461 | 0 | { |
462 | 0 | memcpy(getRawData() + m_NameLength + sizeof(uint16_t), &customValue, sizeof(uint16_t)); |
463 | 0 | } |
464 | | |
465 | | } // namespace pcpp |