Skip to content

Commit beb0535

Browse files
committed
DNS Initial Support
1 parent 54cb7d2 commit beb0535

File tree

10 files changed

+90
-63
lines changed

10 files changed

+90
-63
lines changed

Containerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Using www.github.com/wtfbbqhax/krakatoa
2-
FROM arm64v8/krakatoa AS libpacket_dev_env
2+
FROM amd64/krakatoa AS libpacket_dev_env
33

44
USER root
55
RUN apk update
File renamed without changes.

piglet-bpf-filter/Makefile renamed to bin/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
piglet-bpf-filter: piglet-bpf-filter.cc daq_print.cc daq_print.h
3+
dns-hog: dns_hog.cc daq_print.cc daq_print.h
44
c++ -ggdb -std=c++14 -lstdc++ $^ -lpcap -ldaq -lpacket -o $@
55

66
clean:

piglet-bpf-filter/daq_print.cc renamed to bin/daq_print.cc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,22 @@ int print_dns(dns const& dns)
3636
{
3737
bool is_response = DNS_QR(dns.h.flags);
3838

39-
if (is_response)
39+
//if (is_response)
4040
{
41-
printf("[dns response] [rcode:%d, id:%d, qdcount: %d, ancount: %d, nscount: %d, arcount:%d]\n",
41+
printf("[dns] [rcode:%d, id:%d, qdcount: %d, ancount: %d, nscount: %d, arcount:%d]\n",
4242
DNS_RCODE(dns.h.flags),
4343
dns.h.id,
4444
dns.h.qdcount,
4545
dns.h.ancount,
4646
dns.h.nscount,
4747
dns.h.arcount);
4848
}
49-
else
50-
{
51-
printf("[dns query] [id:%d, qdcount: %d]\n",
52-
dns.h.id,
53-
dns.h.qdcount);
54-
}
49+
//else
50+
//{
51+
// printf("[dns query] [id:%d, qdcount: %d]\n",
52+
// dns.h.id,
53+
// dns.h.qdcount);
54+
//}
5555

5656
// Parsing Question Section
5757
for (int i = 0; i < dns.h.qdcount; i++)
@@ -78,7 +78,11 @@ int print_dns(dns const& dns)
7878
{
7979
inet_ntop(AF_INET, a.data.data(), addr, sizeof(addr));
8080
human.append(addr, strnlen(addr, INET6_ADDRSTRLEN));
81-
81+
}
82+
else if (a.dns_atype == 28)
83+
{
84+
inet_ntop(AF_INET6, a.data.data(), addr, sizeof(addr));
85+
human.append(addr, strnlen(addr, INET6_ADDRSTRLEN));
8286
}
8387
else
8488
{
@@ -95,6 +99,7 @@ int print_dns(dns const& dns)
9599
// (void)dns[0].questions[i].dns_qclass;
96100
}
97101

102+
printf("\n");
98103
return 0;
99104
}
100105

@@ -179,21 +184,17 @@ print_packet(int const instance_id, DAQ_PktHdr_t const* hdr, uint8_t const * dat
179184
packet_frag_mf(&packet) ? "mf" : "");
180185
}
181186

187+
uint32_t max = packet_paysize(&packet);
188+
const uint8_t *payload = packet_payload(&packet);
182189
if (sport == 53 || dport == 53)
183190
{
184191
dns _dns;
185-
decode_dns(packet_payload(&packet),
186-
packet_paysize(&packet),
187-
&_dns);
192+
decode_dns(payload, max, &_dns);
188193
print_dns(_dns);
189194
}
190195

191-
uint32_t max = packet_paysize(&packet);
192-
const uint8_t *payload = packet_payload(&packet);
193196
max = max > 128 ? 128 : max;
194-
print_data(payload, max);
195-
196-
//print_data(data, len);
197+
//print_data(payload, max);
197198

198199
#ifdef PRINT_PACKET_STATS
199200
// Packet stats are useful for determining decoding errors
File renamed without changes.

piglet-bpf-filter/piglet-bpf-filter.cc renamed to bin/dns_hog.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
#include <packet/dns.h>
2222
#include <pcap.h>
2323

24-
//#include <sfbpf.h>
25-
//#include <sfbpf_dlt.h>
26-
24+
#define TXT_FG_PURPLE(str) "\e[35m" str "\e[0m"
2725

2826
#include "daq_print.h"
2927

@@ -441,7 +439,8 @@ class DataPlaneWorker
441439
}
442440

443441
verdicts.verdicts[i] = verdict;
444-
printf(matched ? "[match|%s] " : "[%s]", str_from_verdict(verdict));
442+
printf(matched ? "[" TXT_FG_PURPLE("match") "] " : "");
443+
printf("[%s] ", str_from_verdict(verdict));
445444
print_packet(id, hdr, data, hdr->pktlen);
446445
}
447446
}
@@ -508,9 +507,9 @@ int main(int argc, char const* argv[])
508507
//{ "debug", "true" },
509508
};
510509

511-
if (argc < 3)
510+
if (argc < 2)
512511
{
513-
fprintf(stderr, "Usage: piglet-bpf-filter [pass|block|allowlist|blocklist] <BPF expression>\n");
512+
fprintf(stderr, "Usage: piglet-bpf-filter <BPF expression>\n");
514513
exit(1);
515514
}
516515

@@ -520,13 +519,12 @@ int main(int argc, char const* argv[])
520519

521520
DaqConfig pcap_config("pcap", "pcaps/dns.pcap", DAQ_MODE_READ_FILE, vars);
522521
DataPlaneWorker wk0(pcap_config, 0, filter, match_verdict, default_verdict);
522+
523523
sleep(2);
524+
524525
wk0.stop();
525526
wk0.join();
526527

527-
//DataPlaneWorker wk1(vpp_inline_config, 1, filter, match_verdict, default_verdict);
528-
//wk1.stop(); wk1.join();
529-
530528
DAQ::unload_modules();
531529
return 0;
532530
}

bin/pcaps/dns-label-loop.pcap

216 Bytes
Binary file not shown.
File renamed without changes.

include/packet/dns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ enum dns_types {
6464
TYPE_TXT // 16
6565
};
6666

67-
int decode_dns(uint8_t const *pkt, uint32_t const len, dns* dns);
67+
extern "C" int decode_dns(uint8_t const *pkt, uint32_t const len, dns* dns);
6868

6969
#endif /* LIBPACKET_DECODE_DNS_H */

src/dns.cc

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,43 @@
1616
#include "packet_private.h"
1717
#include "packet/dns.h"
1818

19+
#define IS_SET(test, bits) (((test) & (bits)) == (bits))
20+
1921
struct dns_stats s_dns_stats;
2022

2123
uint32_t constexpr MINIMUM_DNS_HEADER_SIZE = (sizeof(dns_header));
2224

25+
//static inline void
26+
extern "C" void
27+
decode_label(uint8_t const* raw, uint8_t const* ptr, std::string& _label)
28+
{
29+
uint8_t off;
30+
uint8_t label_len;
31+
32+
if (ptr[0] == 0) {
33+
return;
34+
}
35+
36+
scan_again:
37+
if (IS_SET(ptr[0], 0xC0)) {
38+
off = ptr[1];
39+
ptr = &raw[off];
40+
}
41+
42+
label_len = ptr[0];
43+
ptr++;
44+
45+
_label.append(reinterpret_cast<char const*>(ptr), label_len);
46+
_label.append(".");
47+
ptr += label_len;
48+
49+
if (ptr[0] != 0) {
50+
goto scan_again;
51+
}
52+
}
53+
2354
// Function to decode the DNS protocol
24-
int
55+
extern "C" int
2556
decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
2657
{
2758
if (len < MINIMUM_DNS_HEADER_SIZE) {
@@ -113,8 +144,23 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
113144
s_dns_stats.dns_tooshort++;
114145
return -1;
115146
}
116-
// FIXME: Store this value?
117-
uint16_t name = ntohs(*(uint16_t *)ptr);
147+
148+
// The "name" appears to be a partial label, but I can't find that
149+
// documented in rfc1035, more testing is needed.
150+
std::string name;
151+
if (IS_SET(ptr[0], 0xC0)) {
152+
uint8_t off = ptr[1];
153+
uint8_t const* label = &pkt[off];
154+
uint8_t label_len = *label;
155+
while(label[0] != 0)
156+
{
157+
label++;
158+
name.append(reinterpret_cast<char const*>(label), label_len);
159+
name.append(".");
160+
label += label_len;
161+
}
162+
}
163+
118164
ptr += 2;
119165
remaining_len -= 2;
120166

@@ -150,43 +196,25 @@ decode_dns(uint8_t const * pkt, uint32_t const len, dns* dns)
150196
return -1;
151197
}
152198

153-
const uint8_t *rdata = ptr;
154-
ptr += rdlength;
155-
remaining_len -= rdlength;
156-
157-
// Store answer information (assuming a structure in Packet to store this information)
199+
// Store answer information (assuming a structure in Packet to store
200+
// this information)
158201
a->dns_atype = atype;
159202
a->dns_aclass = aclass;
160203
a->dns_ttl = ttl;
161204

162-
// Parse rdata
163-
while (rdata < ptr)
205+
if (atype == 1 || atype == 28)
164206
{
165-
uint16_t us = *reinterpret_cast<uint16_t const*>(rdata);
166-
if (us == name)
167-
{
168-
rdata += 2;
169-
rdlength -= 2;
170-
continue;
171-
}
172-
173-
uint8_t len = 0;
174-
if (rdlength >= 1)
175-
{
176-
len = *rdata;
177-
rdata += 1;
178-
}
179-
// abort if len == 0 && rdatalength > 0 // ANOMALY?
180-
181-
len = rdlength > len ? len : rdlength;
182-
183-
if (len)
184-
{
185-
a->data.append(reinterpret_cast<char const*>(rdata), len);
186-
a->data.append(".");
187-
}
188-
rdata += len;
207+
a->data.append(reinterpret_cast<char const*>(ptr), rdlength);
208+
}
209+
else
210+
{
211+
// Parse rdata
212+
decode_label(pkt, ptr, a->data);
189213
}
214+
215+
//const uint8_t *rdata = ptr;
216+
ptr += rdlength;
217+
remaining_len -= rdlength;
190218
}
191219

192220
return 0;

0 commit comments

Comments
 (0)