-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathdns.c
1886 lines (1710 loc) · 61.4 KB
/
dns.c
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** This file is a part of PassiveDNS.
**
** Copyright (C) 2010-2013, Edward Fjellskål <edwardfjellskaal@gmail.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <syslog.h>
#include <pcap.h>
#include "passivedns.h"
#include "dns.h"
#ifdef HAVE_JSON
#include <jansson.h>
#endif /* HAVE_JSON */
extern globalconfig config;
/* The 12th Carol number and 7th Carol prime, 16769023, is also a Carol emirp */
//#define DBUCKET_SIZE 16769023
#define DBUCKET_SIZE 3967 /* Carol that is primes */
pdns_record *dbucket[DBUCKET_SIZE];
uint64_t hash(unsigned char *str)
{
uint64_t hash = 5381;
uint64_t c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % DBUCKET_SIZE;
}
void dns_parser(packetinfo *pi)
{
ldns_status status;
ldns_pkt *dns_pkt;
status = LDNS_STATUS_ERR;
/* In DNS tcp messages, the first 2 bytes signal the
* amount of data to expect. So we need to skip them in the read. */
if (pi->plen <= 2)
return; /* The minimum bytes in a packet, else return */
if (pi->af == AF_INET) {
switch (pi->ip4->ip_p) {
case IP_PROTO_TCP:
status = ldns_wire2pkt(&dns_pkt,pi->payload + 2, pi->plen - 2);
break;
case IP_PROTO_UDP:
status = ldns_wire2pkt(&dns_pkt,pi->payload, pi->plen);
break;
default:
break;
}
}
else if (pi->af == AF_INET6) {
switch (pi->ip6->next) {
case IP_PROTO_TCP:
status = ldns_wire2pkt(&dns_pkt,pi->payload + 2, pi->plen - 2);
break;
case IP_PROTO_UDP:
status = ldns_wire2pkt(&dns_pkt,pi->payload, pi->plen);
break;
default:
break;
}
}
if (status != LDNS_STATUS_OK) {
dlog("[D] ldns_wire2pkt status: %d\n", status);
update_dns_stats(pi,ERROR);
return;
}
/* We don't want to process truncated packets */
if (ldns_pkt_tc(dns_pkt)) {
dlog("[D] DNS packet with Truncated (TC) bit set! Skipping!\n");
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
/* We only care about answers when we record data */
if (ldns_pkt_qr(dns_pkt)) {
/* In situations where the first packet seen is a server response, the
client/server determination is incorrectly marked as an SC_CLIENT session
prior to the parsing of the DNS payload. In high bandwidth data centers
when running against millions of packet capture files, port reuse in the
span of a few minutes is not uncommon. This results in all subsequent
responses on the reused port being thrown out as if they came from a client. */
if (pi->sc == SC_UNKNOWN || pi->cxt->s_total_pkts == 0) {
dlog("[D] DNS Answer without a Question?: Query TID = %d and Answer TID = %d\n",
pi->cxt->plid, ldns_pkt_id(dns_pkt));
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
dlog("[D] DNS Answer\n");
/* Check the DNS TID */
if (pi->cxt->plid == ldns_pkt_id(dns_pkt)) {
dlog("[D] DNS Query TID match Answer TID: %d\n", pi->cxt->plid);
}
else {
dlog("[D] DNS Query TID did not match Answer TID: %d != %d - Skipping!\n",
pi->cxt->plid, ldns_pkt_id(dns_pkt));
ldns_pkt_free(dns_pkt);
update_dns_stats(pi,ERROR);
return;
}
/* From isc.org wording:
* We do not collect any of the query-response traffic that
* occurs when the client sets the RD or "Recursion Desired"
* bit to 1, that is, the traffic that occurs between DNS
* "stub" clients and the caching server itself, since only the
* traffic generated in response to a cache miss (RD bit set to 0)
* is strictly needed in order to build a passive DNS database.
*/
if (ldns_pkt_rd(dns_pkt)) {
dlog("[D] DNS packet with Recursion Desired (RD) bit set!\n");
/* Between DNS-server to DNS-server, we should drop this kind
* of traffic if we are thinking hardening and correctness!
* But for people trying this out in a corporate network etc,
* between a client and a DNS proxy, will need this most likely
* to see any traffic at all. In the future, this might be
* controlled by a cmdline switch.
*/
//ldns_pkt_free(decoded_dns);
//return;
}
if (!ldns_pkt_qdcount(dns_pkt)) {
/* No questions or answers */
dlog("[D] DNS packet did not contain a question. Skipping!\n");
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
/* Send it off for processing */
if (process_dns_answer(pi, dns_pkt) < 0)
dlog("[D] process_dns_answer() returned -1\n");
}
else {
/* We need to get the DNS TID from the Query to later match with the
* DNS TID in the answer - to harden the implementation.
*/
/* With the new SC_UNKNOWN state, only responses from an SC_SERVER should be ignored. */
if (pi->sc == SC_SERVER) {
dlog("[D] DNS Query not from a client? Skipping!\n");
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
/* Check for reuse of a session and a hack for
* no timeout of sessions when reading pcaps atm. :/
* 60 Secs are default UDP timeout in cxt, and should
* be enough for a TCP session of DNS too.
*/
if ((pi->cxt->plid != 0 && pi->cxt->plid != ldns_pkt_id(dns_pkt)) &&
((pi->cxt->last_pkt_time - pi->cxt->start_time) <= 60)) {
dlog("[D] DNS Query on an established DNS session - TID: Old:%d New:%d\n",
pi->cxt->plid, ldns_pkt_id(dns_pkt));
/* Some clients have bad or strange random src
* port generator and will gladly reuse the same
* src port several times in a short time period.
* To implement this fully, each cxt should be include
* the TID in its tuple, but still this will make a mess :/
*/
}
else
dlog("[D] New DNS Query\n");
if (!ldns_pkt_qdcount(dns_pkt)) {
/* No questions or answers */
dlog("[D] DNS Query packet did not contain a question? Skipping!\n");
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
if ((pi->cxt->plid = ldns_pkt_id(dns_pkt)))
dlog("[D] DNS Query with TID = %d\n", pi->cxt->plid);
else {
dlog("[E] Error getting DNS TID from Query!\n");
ldns_pkt_free(dns_pkt);
update_dns_stats(pi, ERROR);
return;
}
}
ldns_pkt_free(dns_pkt);
}
int process_dns_answer(packetinfo *pi, ldns_pkt *dns_pkt)
{
int rrcount_query;
int j;
ldns_rr_list *dns_query_domains;
ldns_buffer *dns_buff;
dns_query_domains = ldns_pkt_question(dns_pkt);
rrcount_query = ldns_rr_list_rr_count(dns_query_domains);
dns_buff = ldns_buffer_new(LDNS_MIN_BUFLEN);
dlog("[*] rrcount_query: %d\n", rrcount_query);
/* Do we ever have more than one question?
If we do, are we handling it correctly? */
for (j = 0; j < rrcount_query; j++)
{
ldns_rdf *rdf_data;
rdf_data = ldns_rr_owner(ldns_rr_list_rr(dns_query_domains, j));
dlog("[D] rdf_data: %p\n", rdf_data);
if (cache_dns_objects(pi, rdf_data, dns_buff, dns_pkt) != 0)
dlog("[D] cache_dns_objects() returned error\n");
}
ldns_buffer_free(dns_buff);
update_dns_stats(pi, SUCCESS);
return 0;
}
int cache_dns_objects(packetinfo *pi, ldns_rdf *rdf_data,
ldns_buffer *buff, ldns_pkt *dns_pkt)
{
int j;
int dns_answer_domain_cnt;
uint64_t dnshash;
ldns_status status;
pdns_record *pr = NULL;
ldns_rr_list *dns_answer_domains;
unsigned char *domain_name = 0;
ldns_buffer_clear(buff);
status = ldns_rdf2buffer_str(buff, rdf_data);
if (status != LDNS_STATUS_OK) {
dlog("[D] Error in ldns_rdf2buffer_str(): %d\n", status);
return -1;
}
dns_answer_domains = ldns_pkt_answer(dns_pkt);
dns_answer_domain_cnt = ldns_rr_list_rr_count(dns_answer_domains);
domain_name = (unsigned char *) ldns_buffer2str(buff);
if (domain_name == NULL) {
dlog("[D] Error in ldns_buffer2str(%p)\n", buff);
return -1;
}
else {
dlog("[D] domain_name: %s\n", domain_name);
dlog("[D] dns_answer_domain_cnt: %d\n",dns_answer_domain_cnt);
}
if (dns_answer_domain_cnt == 0 && ldns_pkt_get_rcode(dns_pkt) != 0) {
uint16_t rcode = ldns_pkt_get_rcode(dns_pkt);
dlog("[D] Error return code: %d\n", rcode);
/* PROBLEM:
* As there is no valid ldns_rr here and we can't fake one that will
* be very unique, we cant push this to the normal
* bucket[hash->linked_list]. We should probably allocate a static
* bucket[MAX_NXDOMAIN] to hold NXDOMAINS, and when that is full, pop
* out the oldest (LRU). A simple script querying for random non-existing
* domains could easily put stress on passivedns (think conficker etc.)
* if the bucket is to big or non-efficient. We would still store data
* such as: firstseen,lastseen,client_ip,server_ip,class,query,NXDOMAIN
*/
if (config.dnsfe & (pdns_chk_dnsfe(rcode))) {
ldns_rr_list *dns_query_domains;
ldns_rr *rr;
dnshash = hash(domain_name);
dlog("[D] Hash: %lu\n", dnshash);
/* Check if the node exists, if not, make it */
pr = get_pdns_record(dnshash, pi, domain_name);
/* Set the SRC flag: */
//lname_node->srcflag |= pdns_chk_dnsfe(rcode);
dns_query_domains = ldns_pkt_question(dns_pkt);
rr = ldns_rr_list_rr(dns_query_domains, 0);
if ((pr->last_seen.tv_sec - pr->last_print.tv_sec) >= config.dnsprinttime) {
/* Print the SRC Error record */
print_passet(pr, NULL, rr, rdf_data, rcode);
}
}
else
dlog("[D] Error return code %d was not processed:%d\n",
pdns_chk_dnsfe(rcode), config.dnsfe);
free(domain_name);
return 0;
}
for (j = 0; j < dns_answer_domain_cnt; j++)
{
int offset = -1;
int to_offset = -1;
int len;
ldns_rr *rr;
ldns_rdf *rname;
char *rdomain_name = NULL;
char *tmp1 = NULL;
char *tmp2 = NULL;
rr = ldns_rr_list_rr(dns_answer_domains, j);
switch (ldns_rr_get_type(rr)) {
case LDNS_RR_TYPE_LOC:
if (config.dnsf & DNS_CHK_LOC)
offset = 0;
break;
case LDNS_RR_TYPE_GPOS:
if (config.dnsf & DNS_CHK_LOC) {
offset = 0;
to_offset = 3;
}
break;
case LDNS_RR_TYPE_RRSIG:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 9;
}
break;
case LDNS_RR_TYPE_DNSKEY:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 4;
}
break;
#ifdef LDNS_RR_TYPE_NSEC3PARAM
case LDNS_RR_TYPE_NSEC3PARAM:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 4;
}
break;
#endif /* LDNS_RR_TYPE_NSEC3PARAM */
case LDNS_RR_TYPE_NSEC3:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 5;
}
break;
case LDNS_RR_TYPE_NSEC:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 2;
}
break;
case LDNS_RR_TYPE_HINFO:
if (config.dnsf & DNS_CHK_HINFO) {
offset = 0;
to_offset = 2;
}
break;
case LDNS_RR_TYPE_DS:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
to_offset = 4;
}
break;
case LDNS_RR_TYPE_SSHFP:
if (config.dnsf & DNS_CHK_SSHFP) {
offset = 0;
to_offset = 3;
}
break;
case LDNS_RR_TYPE_AAAA:
if (config.dnsf & DNS_CHK_AAAA)
offset = 0;
break;
case LDNS_RR_TYPE_A:
if (config.dnsf & DNS_CHK_A)
offset = 0;
break;
case LDNS_RR_TYPE_PTR:
if (config.dnsf & DNS_CHK_PTR)
offset = 0;
break;
case LDNS_RR_TYPE_CNAME:
if (config.dnsf & DNS_CHK_CNAME)
offset = 0;
break;
case LDNS_RR_TYPE_DNAME:
if (config.dnsf & DNS_CHK_DNAME)
offset = 0;
break;
case LDNS_RR_TYPE_NAPTR:
if (config.dnsf & DNS_CHK_NAPTR) {
offset = 0;
to_offset = 6;
}
break;
case LDNS_RR_TYPE_RP:
if (config.dnsf & DNS_CHK_RP)
offset = 0;
break;
case LDNS_RR_TYPE_SRV:
if (config.dnsf & DNS_CHK_SRV)
offset = 3;
break;
case LDNS_RR_TYPE_TXT:
if (config.dnsf & DNS_CHK_TXT)
offset = 0;
break;
case LDNS_RR_TYPE_SPF:
if (config.dnsf & DNS_CHK_SPF)
offset = 0;
break;
case LDNS_RR_TYPE_SOA:
if (config.dnsf & DNS_CHK_SOA)
offset = 0;
break;
case LDNS_RR_TYPE_MX:
if (config.dnsf & DNS_CHK_MX)
offset = 1;
break;
case LDNS_RR_TYPE_NS:
if (config.dnsf & DNS_CHK_NS)
offset = 0;
break;
default:
offset = -1;
dlog("[D] ldns_rr_get_type: %d\n", ldns_rr_get_type(rr));
break;
}
if (offset == -1) {
dlog("[D] LDNS_RR_TYPE not enabled/supported: %d\n",
ldns_rr_get_type(rr));
continue;
}
do {
/* Get the rdf data from the rr */
ldns_buffer_clear(buff);
rname = ldns_rr_rdf(rr, offset);
if (rname == NULL) {
dlog("[D] ldns_rr_rdf returned: NULL\n");
break;
}
ldns_rdf2buffer_str(buff, rname);
rdomain_name = (char *) ldns_buffer2str(buff);
if (rdomain_name == NULL)
continue;
len = strlen(rdomain_name) + 5;
if (tmp1 != NULL)
len += strlen(tmp1);
tmp2 = malloc(len);
if (tmp1 != NULL) {
tmp2 = strcpy(tmp2, tmp1);
tmp2 = strcat(tmp2, " ");
}
else
tmp2 = strcpy(tmp2, "");
free(tmp1);
tmp2 = strcat(tmp2, rdomain_name);
tmp1 = tmp2;
free(rdomain_name);
offset ++;
} while (offset < to_offset);
rdomain_name = tmp1;
if (rname == NULL)
continue;
if (rdomain_name == NULL && offset <= 1) {
dlog("[D] ldns_buffer2str returned: NULL\n");
continue;
}
dlog("[D] rdomain_name: %s\n", rdomain_name);
if (pr == NULL) {
dnshash = hash(domain_name);
dlog("[D] Hash: %lu\n", dnshash);
/* Check if the node exists, if not, make it */
pr = get_pdns_record(dnshash, pi, domain_name);
}
/* Update the pdns record with the pdns asset */
update_pdns_record_asset(pi, pr, rr, (unsigned char*)rdomain_name);
/* If CNAME, free domain_name, and cp rdomain_name to domain_name */
if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_CNAME) {
if (config.dnsf & DNS_CHK_CNAME) {
int len;
free(domain_name);
len = strlen((char *)rdomain_name);
domain_name = calloc(1, (len + 1));
strncpy((char *)domain_name, (char *)rdomain_name, len);
dnshash = hash(domain_name);
dlog("[D] Hash: %lu\n", dnshash);
pr = get_pdns_record(dnshash, pi, domain_name);
}
}
/* Free the rdomain_name */
free(rdomain_name);
}
free(domain_name);
return 0;
}
void update_pdns_record_asset(packetinfo *pi, pdns_record *pr,
ldns_rr *rr, unsigned char *rdomain_name)
{
pdns_asset *passet = pr->passet;
pdns_asset *head = passet;
ldns_rr *prr = NULL;
uint32_t len = 0;
dlog("Searching: %u, %s, %s\n", rr->_rr_type, pr->qname, rdomain_name);
while (passet != NULL)
{
/* If found, update */
dlog("Matching: %u, %s, %s\n",passet->rr->_rr_type, pr->qname,
passet->answer);
dlog("[*] RR:%u, %u\n", passet->rr->_rr_type, rr->_rr_type);
if (passet->rr->_rr_type == rr->_rr_type) {
dlog("[*] rr match\n");
dlog("r:%s == a:%s\n", rdomain_name, passet->answer);
if (strcmp((const char *)rdomain_name,
(const char *)passet->answer) == 0 ) {
dlog("[*] rname/answer match\n");
/* We have this, update and if its over 24h since last print -
print it, then return */
passet->seen++;
memcpy( &passet->last_seen, &pi->pheader->ts, sizeof( struct timeval ) );
passet->af = pi->cxt->af;
passet->cip = pi->cxt->s_ip; /* This should always be the client IP */
passet->sip = pi->cxt->d_ip; /* This should always be the server IP */
if (rr->_ttl > passet->rr->_ttl)
passet->rr->_ttl = rr->_ttl; /* Catch the highest TTL seen */
dlog("[*] DNS asset updated...\n");
if ((passet->last_seen.tv_sec -
passet->last_print.tv_sec) >= config.dnsprinttime)
print_passet(pr, passet, passet->rr, NULL, 0);
return;
}
}
passet = passet->next;
}
/* Else, we got a new passet :) */
if (passet == NULL) {
passet = (pdns_asset*) calloc(1, sizeof(pdns_asset));
dlog("[*] Allocated a new dns asset...\n");
config.p_s.dns_assets++;
config.dns_assets++;
prr = (ldns_rr*) calloc(1, sizeof(ldns_rr));
prr->_owner = rr->_owner;
prr->_ttl = rr->_ttl;
prr->_rd_count = rr->_rd_count;
prr->_rr_type = rr->_rr_type;
prr->_rr_class = rr->_rr_class;
prr->_rdata_fields = rr->_rdata_fields;
passet->seen = 1;
passet->rr = prr;
}
else
dlog("[D] BAD\n");
if (head != NULL ) {
head->prev = passet;
passet->next = head;
}
else
passet->next = NULL;
/* Populate new values */
memcpy( &passet->first_seen, &pi->pheader->ts, sizeof( struct timeval ) );
memcpy( &passet->last_seen, &pi->pheader->ts, sizeof( struct timeval ) );
passet->af = pi->cxt->af;
passet->cip = pi->cxt->s_ip; /* This should always be the client IP */
passet->sip = pi->cxt->d_ip; /* This should always be the server IP */
if (pi-> eth_hdr) {
memcpy(passet->cmac, pi->eth_hdr->ether_dst, 6 * sizeof(u_char));
memcpy(passet->smac, pi->eth_hdr->ether_src, 6 * sizeof(u_char));
}
passet->prev = NULL;
len = strlen((char *)rdomain_name);
passet->answer = calloc(1, (len + 1));
strncpy((char *)passet->answer, (char *)rdomain_name, len);
dlog("[D] Adding: %u, %s, %s\n",passet->rr->_rr_type, pr->qname,
rdomain_name);
pr->passet = passet;
print_passet(pr, passet, passet->rr, NULL, 0);
}
const char *u_ntop(const struct in6_addr ip_addr, int af, char *dest)
{
if (af == AF_INET) {
if (!inet_ntop(AF_INET, &IP4ADDR(&ip_addr), dest,
INET_ADDRSTRLEN + 1)) {
dlog("[E] Something died in inet_ntop\n");
return NULL;
}
}
else if (af == AF_INET6) {
if (!inet_ntop(AF_INET6, &ip_addr, dest, INET6_ADDRSTRLEN + 1)) {
dlog("[E] Something died in inet_ntop\n");
return NULL;
}
}
return dest;
}
void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
ldns_rdf *lname, uint16_t rcode)
{
FILE *fd = NULL;
static char ip_addr_s[INET6_ADDRSTRLEN];
static char ip_addr_c[INET6_ADDRSTRLEN];
char *d = config.log_delimiter;
char *proto;
char *rr_class;
char *rr_type;
char *rr_rcode;
char buffer[1000] = "";
char *output = buffer;
int offset = 0;
uint8_t is_err_record = 0;
#ifdef HAVE_JSON
json_t *jdata;
json_t *json_timestamp_ymdhms;
json_t *json_timestamp_s;
json_t *json_timestamp_ms;
json_t *json_hostname;
json_t *json_client;
json_t *json_server;
json_t *json_proto;
json_t *json_class;
json_t *json_query;
json_t *json_query_len;
json_t *json_type;
json_t *json_answer;
json_t *json_answer_len;
json_t *json_ttl;
json_t *json_count;
size_t data_flags = 0;
/* Print in the same order as inserted */
data_flags |= JSON_PRESERVE_ORDER;
/* No whitespace between fields */
data_flags |= JSON_COMPACT;
#endif /* HAVE_JSON */
/* If pdns_asset is not defined, then this is a NXD record */
if (p == NULL)
is_err_record = 1;
/* Use the correct file descriptor */
if (is_err_record && config.output_log_nxd) {
if (config.logfile_all)
fd = config.logfile_fd;
else
fd = config.logfile_nxd_fd;
if (fd == NULL)
return;
}
else if (!is_err_record && config.output_log) {
fd = config.logfile_fd;
if (fd == NULL) return;
}
if (is_err_record) {
u_ntop(l->sip, l->af, ip_addr_s);
u_ntop(l->cip, l->af, ip_addr_c);
}
else {
u_ntop(p->sip, p->af, ip_addr_s);
u_ntop(p->cip, p->af, ip_addr_c);
}
proto = malloc(4);
rr_class = malloc(10);
rr_type = malloc(12);
rr_rcode = malloc(20);
switch (l->proto) {
case IP_PROTO_TCP:
snprintf(proto, 4, "tcp");
break;
case IP_PROTO_UDP:
snprintf(proto, 4, "udp");
break;
default:
snprintf(proto, 4, "%d", l->proto);
break;
}
switch (ldns_rr_get_class(rr)) {
case LDNS_RR_CLASS_IN:
snprintf(rr_class, 10, "IN");
break;
case LDNS_RR_CLASS_CH:
snprintf(rr_class, 10, "CH");
break;
case LDNS_RR_CLASS_HS:
snprintf(rr_class, 10, "HS");
break;
case LDNS_RR_CLASS_NONE:
snprintf(rr_class, 10, "NONE");
break;
case LDNS_RR_CLASS_ANY:
snprintf(rr_class, 10, "ANY");
break;
default:
snprintf(rr_class, 10, "%d", ldns_rr_get_class(rr));
break;
}
switch (ldns_rr_get_type(rr)) {
case LDNS_RR_TYPE_HINFO:
snprintf(rr_type, 10, "HINFO");
break;
case LDNS_RR_TYPE_SSHFP:
snprintf(rr_type, 10, "SSHFP");
break;
case LDNS_RR_TYPE_GPOS:
snprintf(rr_type, 10, "GPOS");
break;
case LDNS_RR_TYPE_LOC:
snprintf(rr_type, 10, "LOC");
break;
case LDNS_RR_TYPE_DNSKEY:
snprintf(rr_type, 10, "DNSKEY");
break;
#ifdef LDNS_RR_TYPE_NSEC3PARAM
case LDNS_RR_TYPE_NSEC3PARAM:
snprintf(rr_type, 11, "NSEC3PARAM");
break;
#endif /* LDNS_RR_TYPE_NSEC3PARAM */
case LDNS_RR_TYPE_NSEC3:
snprintf(rr_type, 10, "NSEC3");
break;
case LDNS_RR_TYPE_NSEC:
snprintf(rr_type, 10, "NSEC");
break;
case LDNS_RR_TYPE_RRSIG:
snprintf(rr_type, 10, "RRSIG");
break;
case LDNS_RR_TYPE_DS:
snprintf(rr_type, 10, "DS");
break;
case LDNS_RR_TYPE_PTR:
snprintf(rr_type, 10, "PTR");
break;
case LDNS_RR_TYPE_A:
snprintf(rr_type, 10, "A");
break;
case LDNS_RR_TYPE_AAAA:
snprintf(rr_type, 10, "AAAA");
break;
case LDNS_RR_TYPE_CNAME:
snprintf(rr_type, 10, "CNAME");
break;
case LDNS_RR_TYPE_DNAME:
snprintf(rr_type, 10, "DNAME");
break;
case LDNS_RR_TYPE_NAPTR:
snprintf(rr_type, 10, "NAPTR");
break;
case LDNS_RR_TYPE_RP:
snprintf(rr_type, 10, "RP");
break;
case LDNS_RR_TYPE_SRV:
snprintf(rr_type, 10, "SRV");
break;
case LDNS_RR_TYPE_TXT:
snprintf(rr_type, 10, "TXT");
break;
case LDNS_RR_TYPE_SPF:
snprintf(rr_type, 10, "SPF");
break;
case LDNS_RR_TYPE_SOA:
snprintf(rr_type, 10, "SOA");
break;
case LDNS_RR_TYPE_NS:
snprintf(rr_type, 10, "NS");
break;
case LDNS_RR_TYPE_MX:
snprintf(rr_type, 10, "MX");
break;
default:
if (is_err_record)
snprintf(rr_type, 10, "%d", ldns_rdf_get_type(lname));
else
snprintf(rr_type, 10, "%d", p->rr->_rr_type);
break;
}
if (is_err_record) {
switch (rcode) {
case 1:
snprintf(rr_rcode, 20, "FORMERR");
break;
case 2:
snprintf(rr_rcode, 20, "SERVFAIL");
break;
case 3:
snprintf(rr_rcode, 20, "NXDOMAIN");
break;
case 4:
snprintf(rr_rcode, 20, "NOTIMPL");
break;
case 5:
snprintf(rr_rcode, 20, "REFUSED");
break;
case 6:
snprintf(rr_rcode, 20, "YXDOMAIN");
break;
case 7:
snprintf(rr_rcode, 20, "YXRRSET");
break;
case 8:
snprintf(rr_rcode, 20, "NXRRSET");
break;
case 9:
snprintf(rr_rcode, 20, "NOTAUTH");
break;
case 10:
snprintf(rr_rcode, 20, "NOTZONE");
break;
default:
snprintf(rr_rcode, 20, "UNKNOWN-ERROR-%d", rcode);
break;
}
}
#ifdef HAVE_JSON
if ((is_err_record && config.use_json_nxd) ||
(!is_err_record && config.use_json)) {
jdata = json_object();
/* Print timestamp(ymdhms) */
if (config.fieldsf & FIELD_TIMESTAMP_YMDHMS) {
struct tm *tmpTime;
char timestr[200];
tmpTime = localtime(&l->last_seen.tv_sec);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", tmpTime);
json_timestamp_ymdhms = json_string(timestr);
json_object_set(jdata, JSON_TIMESTAMP, json_timestamp_ymdhms);
json_decref(json_timestamp_ymdhms);
}
/* Print timestamp(s) */
if (config.fieldsf & FIELD_TIMESTAMP_S) {
json_timestamp_s = json_integer(l->last_seen.tv_sec);
json_object_set(jdata, JSON_TIMESTAMP_S, json_timestamp_s);
json_decref(json_timestamp_s);
}
/* Print timestamp(ms) */
if (config.fieldsf & FIELD_TIMESTAMP_MS) {
json_timestamp_ms = json_integer(l->last_seen.tv_usec);
json_object_set(jdata, JSON_TIMESTAMP_MS, json_timestamp_ms);
json_decref(json_timestamp_ms);
}
/* Print hostname */
if (config.fieldsf & FIELD_HOSTNAME) {
json_hostname = json_string(config.hostname);
json_object_set(jdata, JSON_HOSTNAME, json_hostname);
json_decref(json_hostname);
}
/* Print client IP */
if (config.fieldsf & FIELD_CLIENT) {
json_client = json_string(ip_addr_c);
json_object_set(jdata, JSON_CLIENT, json_client);
json_decref(json_client);
}
/* Print server IP */
if (config.fieldsf & FIELD_SERVER) {
json_server = json_string(ip_addr_s);
json_object_set(jdata, JSON_SERVER, json_server);
json_decref(json_server);
}
/* Print protocol */
if (config.fieldsf & FIELD_PROTO) {
json_proto = json_string(proto);
json_object_set(jdata, JSON_PROTO, json_proto);
json_decref(json_proto);
}
/* Print class */
if (config.fieldsf & FIELD_CLASS) {
json_class = json_string(rr_class);
json_object_set(jdata, JSON_CLASS, json_class);
json_decref(json_class);
}
/* Print query */
if (config.fieldsf & FIELD_QUERY) {
json_query = json_string((const char *)l->qname);
json_object_set(jdata, JSON_QUERY, json_query);
json_decref(json_query);
}
/* Print query length */
if (config.fieldsf & FIELD_QUERY_LEN) {
json_query_len = json_integer(strlen(l->qname));
json_object_set(jdata, JSON_QUERY_LEN, json_query_len);
json_decref(json_query_len);
}
/* Print type */
if (config.fieldsf & FIELD_TYPE) {
json_type = json_string(rr_type);
json_object_set(jdata, JSON_TYPE, json_type);
json_decref(json_type);
}
if (is_err_record) {
/* Print answer */
if (config.fieldsf & FIELD_ANSWER) {
json_answer = json_string(rr_rcode);
json_object_set(jdata, JSON_ANSWER, json_answer);
json_decref(json_answer);
}
/* Print answer length */
if (config.fieldsf & FIELD_ANSWER_LEN) {
json_answer_len = json_integer(strlen(rr_rcode));
json_object_set(jdata, JSON_ANSWER_LEN, json_answer_len);
json_decref(json_answer_len);
}
/* Print TTL */
if (config.fieldsf & FIELD_TTL) {
json_ttl = json_integer(PASSET_ERR_TTL);
json_object_set(jdata, JSON_TTL, json_ttl);
json_decref(json_ttl);
}
/* Print count */
if (config.fieldsf & FIELD_COUNT) {
json_count = json_integer(PASSET_ERR_COUNT);
json_object_set(jdata, JSON_COUNT, json_count);
json_decref(json_count);
}
}
else {
/* Print answer */
if (config.fieldsf & FIELD_ANSWER) {
json_answer = json_string((const char *)p->answer);
json_object_set(jdata, JSON_ANSWER, json_answer);
json_decref(json_answer);
}
/* Print answer length */
if (config.fieldsf & FIELD_ANSWER_LEN) {
json_answer_len = json_integer(strlen(p->answer));