-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsubtld.pl
More file actions
executable file
·2188 lines (1992 loc) · 48 KB
/
Copy pathsubtld.pl
File metadata and controls
executable file
·2188 lines (1992 loc) · 48 KB
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
#!/usr/local/bin/perl
use strict;
###############################################################################
#
# subtld.pl
#
# Kevin White
# kwhite@jasadvisors.com
# JAS Global Advisors
#
# Incorporating original shell and Perl extraction/processing/filtering
# code from:
# Roy Hooper <roy@demandmedia.com>
# Demand Media
#
# All in one processing script that reads the raw data files and
# produces the by-tld files, as well as JAS categorized data files
#
# Copyright (c) 2013, JAS Global Advisors, LLC.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
###############################################################################
use Getopt::Long;
use Data::Dumper;
use POSIX ":sys_wait_h";
use File::Path qw(make_path);
use File::Find; # will use to replace system call to find
use Net::IDN::Encode ':all';
use Time::Local;
#use NetPacket::IP;
#use NetPacket::TCP;
#use NetPacket::UDP;
#use DBM::Deep;
$SIG{CHLD} = \&REAPER;
our $VERSION = '$Id: subtld.pl 100 2013-11-26 20:36:11Z kwhite $';
#use PerlIO::gzip;
#use threads;
# bytld entries aren't used. They used to be, but they are calculated
# now, and assumed to be somewhere in the DSTBASE tree. I have left
# the definitions in here as a note, in case their use needs to be
# resurrected. Use case: one user wishes to use processed data in
# another user's directory.
# For 2010, the first row is the official DITL data. There are other
# datasets available in that year, because lots of DITL captures were
# run, due to the roots being signed. If you wish to include any of
# the other sets, uncomment the lines.
my %LOC = (
2013 => { 'date' => 'DITL-20130528',
'raw' => '/mnt/oarc-pool2/DITL-20130528/RAW',
'bytld' => '/home/roy/gtld/by-tld'},
2012 => { 'date' => 'DITL-20120417',
'raw' => '/mnt/oarc-pool4/DITL-20120417/RAW',
'bytld' => '/home/roy/gtld/by-tld'},
2011 => { 'date' => 'DITL-20110412',
'raw' => '/mnt/oarc-pool4/DITL-20110412/RAW',
'bytld' => '/home/kwhite/gtld/by-tld'},
2010 => { 'date' => 'DITL-20100413',
# 'date' => 'DITL-2010',
'raw' => ['/mnt/oarc-pool4/DITL-20100413/RAW',
# '/mnt/oarc-pool3/DITL-20100112/CLEAN',
# '/mnt/oarc-pool3/DITL-20100119/CLEAN',
# '/mnt/oarc-pool3/DITL-20100126/CLEAN',
# '/mnt/oarc-pool3/DITL-20100323/CLEAN',
# '/mnt/oarc-pool3/DITL-20100504/RAW',
# '/mnt/oarc-pool3/DITL-20100525/RAW',
# '/mnt/oarc-pool3/DITL-20100714/CLEAN',
# '/mnt/oarc-pool4/DITL-20100209/RAW',
# '/mnt/oarc-pool4/DITL-20100302/RAW',
],
'bytld' => '/home/kwhite/gtld/by-tld'},
2009 => { 'date' => 'DITL-200903',
# 'raw' => '/mnt/oarc-pool4/DITL-200903/RAW',
'raw' => '/mnt/oarc-pool4/DITL-200903/CLEAN-ROOTS',
'bytld' => '/home/kwhite/gtld/by-tld'},
2008 => { 'date' => 'DITL-200803',
'raw' => '/mnt/oarc-pool4/DITL-200803/RAW',
'bytld' => '/home/kwhite/gtld/by-tld'},
2007 => { 'date' => 'DITL-200701',
'raw' => '/mnt/oarc-pool3/DITL-200701/RAW',
'bytld' => '/home/kwhite/gtld/by-tld'},
2006 => { 'date' => 'DITL-200601',
'raw' => '/mnt/oarc-pool3/DITL-200601/RAW',
'bytld' => '/home/kwhite/gtld/by-tld'},
);
my (%ROOTMAP, %ROOTIP, %ROOTBYIP);
my (%OVERRIDE10, %OVERRIDE13);
my $DNS_OARCBASE = '/mnt/oarc-pool3/collisions';
my $DSTBASE = $ENV{HOME}.'/jas/gtld';
my $MAXPROC = 12;
my $MAXPROC_TCPDUMP = 40;
#my $MAXPROC_TCPDUMP = 1;
my $MAXPROC_SORT = 15;
# Data structures for global TLD/Interisle categorization
my %OLDGTLD;
my %NEWGTLD;
my %INSIP;
my %INXMP;
# Data structures to handle children
my %CHILDREN;
my @STIFFS;
# Globals to configure checks
my $DO_HYPHEN_CHECKS = 0;
my $DO_LDH_CHECKS = 0;
my $DO_LEN_CHECKS = 0;
my $DO_RANDOM_CHECKS = 0;
my $DO_PUNYCODE_CHECKS = 1;
my $DO_RAW_DECODE = 1;
###############################################################################
main();
###############################################################################
sub REAPER {
my $stiff;
while (($stiff = waitpid(-1, &WNOHANG)) > 0) {
# do something with $stiff if you want
# delete($CHILDREN{$stiff});
push(@STIFFS, $stiff);
}
$SIG{CHLD} = \&REAPER; # install *after* calling waitpid
}
###############################################################################
sub CalcTS {
my ($date, $time) = @_;
my $ts;
#2011-04-12 20:54:06.876091
$date =~ /^(\d{4})-(\d{2})-(\d{2})$/;
my $year = $1 - 1900;
my $mon = $2 - 1;
my $mday = $3;
$time =~ /^(\d{2}):(\d{2}):(\d{2}).(\d*)$/;
my $hour = $1;
my $min = $2;
my $sec = $3;
my $msec = $4;
$ts = timegm( $sec, $min, $hour, $mday, $mon, $year );
# print "$date|$time -> $ts\n";
return($ts);
}
###############################################################################
sub GroupRandoms {
my ($randoms) = @_;
my ($countall, $countyes);
$countall = $countyes = 0;
# print "Before:\n";
# print Dumper($randoms);
foreach my $sip (keys(%$randoms)) {
my ($i1, $i2, $i3);
foreach my $dt (sort(keys(%{$randoms->{$sip}}))) {
foreach my $elem (@{$randoms->{$sip}{$dt}}) {
$countall++;
my $elemdt = { };
$elemdt->{dt} = $dt;
$elemdt->{elem} = $elem;
$elem->[11] = "OK";
$i1 = $i2;
$i2 = $i3;
$i3 = $elemdt;
if ((defined $i1) && (defined $i1->{dt}) &&
($i1->{dt} >= $dt - 30))
{
# We're looking at element 3. There is an
# element 1 who's time is within 30 seconds behind
# ours. Thus, all 3 are in a group. Mark all 3
# as random, and clear them out.
$i1->{elem}->[11] = "RANDOM10";
$i2->{elem}->[11] = "RANDOM10";
$i3->{elem}->[11] = "RANDOM10";
$i1 = undef;
$i2 = undef;
$i3 = undef;
$countyes += 3;
}
}
}
}
# print "After: $countyes random out of $countall\n";
# print Dumper($randoms);
return($countyes, $countall);
}
###############################################################################
sub CopyAndSub {
my ($src, $dst_loc, $gtld, $year) = @_;
my $dst = $dst_loc."/".$gtld.".gz";
my $dst_log = $dst_loc."/".$gtld."_log.gz";
my $dst_sld = $dst_loc."/".$gtld."_sld";
my $dst_dbfilename = $dst_loc."/".$gtld.".db";
my $dst_log_nocollapse = $dst_loc."/".$gtld."_lognocollapse.gz";
my $dst_interisle_log = $dst_loc."/".$gtld."_interisle.gz";
my $droplist;
my %intsum;
# print "$src\t$dst\t$gtld\t$year\n";
my ($use_db, $db);
if (($year == 2013) && ($gtld eq "home")) { $use_db = 1};
if (($gtld eq "home") || ($gtld eq "corp")) { return();};
my $randoms = { };
# Attempt to use an on-disk database instead of RAM. About 3
# orders of magnitude too slow. Would need to write something
# much more optimized, and still probably not enough.
# if ($use_db) {
# $droplist = DBM::Deep->new($dst_dbfilename);
# }
# else {
$droplist = { };
# }
open(IN, "gunzip -c $src |") || die "ERROR; failed to open input file $src: $!";
open(my $out, "| gzip -c > $dst") || die "ERROR: failed to open output file $dst: $!";
while(<IN>) {
my ($scrub, $interisle, $retrow, $qstr, $sld, $elem) =
ScrubAndInterisle($_);
if (!defined $scrub) { next; }
if ($scrub eq "RANDOM10") {
my $date = $elem->[0];
my $time = $elem->[1];
my $sip = $elem->[6];
# my $scrub = $elem->[11];
# my $qstr = $elem->[13];
# my $sld = $elem->[3];
# Remove final .sourceport
# print "sip: $sip -> ";
substr($sip, rindex($sip, ".")) = "";
# print "$sip\n";
#2011-04-12 20:54:06.876091
my $dt = CalcTS($date, $time);
push(@{$randoms->{$sip}{$dt}}, $elem);
}
else {
addToDrop($droplist, $scrub, $qstr, $sld);
$intsum{$interisle}++;
print $out $retrow."\n";
}
}
close(IN);
my ($randyes, $randtot) = GroupRandoms($randoms);
foreach my $sip (keys(%$randoms)) {
foreach my $dt(sort(keys(%{$randoms->{$sip}}))) {
foreach my $elem (@{$randoms->{$sip}{$dt}}) {
my $scrub = $elem->[11];
my $interisle = $elem->[12];
my $qstr = $elem->[13];
my $sld = $elem->[3];
my $retrow = join(" ", @$elem);
addToDrop($droplist, $scrub, $qstr, $sld);
$intsum{$interisle}++;
print $out $retrow."\n";
}
}
}
close($out);
print "$gtld: random: $randyes out of $randtot\n";
# Create two summary files. $dst_log_nocollapse just has one row
# per qstr, with a count, sorted by sld. $dst_log is more
# difficult. Start with an SLD. A row is present in this file if
# there were any _valid_ queries to a name in that SLD. If all of
# the queries present were invalid, or random, the SLD is removed
# from the list.
open(my $outlog, "| gzip -c > $dst_log") || die "ERROR: failed to open output file $dst_log: $!";
open(my $outlogsld, ">$dst_sld") || die "ERROR: failed to open output file $dst_sld: $!";
open(my $outlog2, "| gzip -c > $dst_log_nocollapse") || die "ERROR: failed to open output file $dst_log_nocollapse: $!";
foreach my $sld (sort(keys(%$droplist))) {
if ($droplist->{$sld}{valid})
{
# If {valid} exists, or is > 0, that means there was at least
# one OK query for this sld
# That means, we want to print it out, because it was a problem.
# Print out the total count of queries for that sld.
print $outlog "OK"." ".$sld." ".
$droplist->{$sld}{valid}."\n";
print $outlogsld $sld."\n";
}
foreach my $section (sort(keys(%{$droplist->{$sld}{scrubs}}))) {
# print $outlog $section."\n";
my $query;
foreach my $query (sort keys(%{$droplist->{$sld}{scrubs}{$section}})) {
print $outlog2 $section." ".$query." ".
$sld." ".
$droplist->{$sld}{scrubs}{$section}{$query}{count}."\n";
}
}
}
close($outlog);
close($outlog2);
close($outlogsld);
open(my $outlog, "| gzip -c > $dst_interisle_log") || die "ERROR: failed to open output file $dst_interisle_log: $!";
foreach my $i (sort(keys(%intsum))) {
print $outlog "$i $intsum{$i}\n";
}
close($outlog);
}
###############################################################################
sub ScrubAndInterisle
{
my ($src) = @_;
# binmode STDOUT, ":encoding(utf8)";
chomp($src);
my $interisle = "INNONE";
my $scrub = "OK";
my @elem = split(/ /, $src);
#($date, $time, $gtld, $sld, $filenum, $protocol, "$sip.$sport", "$dip.$dport", $root, $type, $namelen, $name);
#OLD###2011-04-12 20:54:06.876091 gree blog a-root 119.63.192.232 198.41.0.4 blog.gree
my $gtld = $elem[2];
my $root = $elem[8];
if ((!defined $root) || ($root eq "")) {
# This only happened due to an earlier bug in the raw stage.
# It shouldn't happen in future runs.
# print "no root: $_\n";
return(undef);
}
my $qstr = $elem[11];
my $qstrlen = $elem[10];
my @labels = split(/\./, lc($qstr));
my %labels;
foreach my $l (@labels) { $labels{$l}++; }
my $labelcount = scalar(@labels);
my $sld = ($labelcount >= 2) ? ($labels[$labelcount - 2]) : undef;
my $flabel = $labels[0]; # "first label"
# Interisle categories
if ($OLDGTLD{$sld}) {
$interisle = "INCATB";
}
elsif ($NEWGTLD{$sld}) {
$interisle = "INCATC";
}
elsif ((length($sld) == 10) && ($sld =~ /^[a-z]*$/)) {
$interisle = "INCATD";
}
elsif (lc($qstr) eq "www.$gtld") {
$interisle = "INCATE";
}
elsif (($flabel eq "_ldap") || ($flabel eq "_kerberos")) {
$interisle = "INCATF";
}
elsif (defined $labels{"_dnssd"}) {
$interisle = "INCATG";
}
elsif (substr($flabel, 0, 10) eq "File moved") {
$interisle = "INCATH";
}
elsif ($INSIP{$flabel}) {
$interisle = "INCATI";
}
elsif ($INXMP{$flabel}) {
$interisle = "INCATJ";
}
elsif (($flabel eq "mail") || ($sld eq "mail")) {
$interisle = "INCATK";
}
elsif (!defined $sld) {
$interisle = "INCATL";
}
# Various validity checks
my $scrubbed;
if ($DO_LEN_CHECKS) {
if ($qstrlen > 253) {
$scrub = "INVALID";
$scrubbed++;
}
if (!$scrubbed && defined $sld) {
if (length($sld) < 3) {
# print "SHORTSLD: $qstr\n";
$scrub = "SHORTSLD";
$scrubbed++;
}
}
}
if (!$scrubbed && (substr($sld, 0, 4) eq 'xn--') && ($DO_PUNYCODE_CHECKS)) {
my $unic;
eval {
$unic = domain_to_unicode($sld);
};
if ($@) {
# There was an error
$scrub = "INVALIDPUNYCODE";
print "INVALIDPUNYCODE: $sld -> $@\n";
$scrubbed++;
}
else {
# print "VALIDPUNYCODE: $sld -> $unic\n";
}
}
if ($DO_LDH_CHECKS) {
if (!$scrubbed && defined $sld) {
# If sld has any invalid characters
# elsif ($sld =~ /[^a-zA-Z0-9\-]/) {
if ($sld =~ /[^a-zA-Z0-9\-]/) {
$scrub = "INVALID";
$scrubbed++;
}
elsif ((substr($sld, 2, 2) eq "--") &&
(substr($sld, 0, 2) ne "xn")) {
# print "INVALIDDASH $qstr\n";
$scrub = "INVALID";
$scrubbed++;
}
# If sld starts or ends with a hyphen
elsif ((substr($sld, 0, 1) eq "-") ||
(substr($sld, -1, 1) eq "-")) {
$scrub = "INVALID";
$scrubbed++;
}
}
}
if ($DO_HYPHEN_CHECKS) {
# Walk through the labels, look for any that start or end with a
# hyphen.
if (!$scrubbed) {
LABELWALK: foreach my $label (@labels) {
if ((substr($label, 0, 1) eq "-") ||
(substr($label, -1, 1) eq "-")) {
$scrub = "INVALID";
$scrubbed++;
last LABELWALK;
}
# else {
# Commented out, because this shouldn't be
# possible. The labels have to come from a DNS
# packet. The DNS packets can't hold labels > 63
# characters. So, only corrupt TCPDUMP output
# could cause this to happen. Not worth the
# calculation.
# Same ^ sub code as above.
# my $labellen = $label;
# $labellen =~ s/\^\^/\$/g;
# $labellen =~ s/\^//g;
#
# if (length($labellen) > 63)) {
#
# $scrub = "INVALID";
# $scrubbed++;
# last LABELWALK;
# }
}
}
}
if ($DO_RANDOM_CHECKS) {
# Look for 10/13 character random
if (!$scrubbed) {
if ((length($flabel) == 10) && (!$OVERRIDE10{$flabel})) {
# If it only consists of a-z, we consider it "random"
if ($flabel =~ /^[a-zA-Z]*$/) {
$scrub = "RANDOM10";
$scrubbed++;
}
}
# elsif ((length($flabel) == 13) && (substr($flabel, 0, 4) ne "xn--") && (!$OVERRIDE13{$flabel})) {
# # If it starts with a-z and contains a digit, we consider it "random"
# # can contain dashes. Mostly only see up to 2, but this regexp
# # allows any number. Also, if it starts with "xn--", is isn't random.
# # That check was needed when dashes were added.
# if ($flabel =~ /^[a-z][a-z0-9\-]*[0-9][a-z0-9\-]*$/) {
# $scrub = "RANDOM13";
# $scrubbed++;
# }
# }
}
}
$elem[11] = $scrub;
$elem[12] = $interisle;
$elem[13] = $qstr;
my $retrow = join(" ", @elem);
return($scrub, $interisle, $retrow, $qstr, $sld, \@elem);
}
###############################################################################
sub addToDrop {
my ($droplist, $scrub, $string, $sld) = @_;
# Maintain the droplist. By sld, store :
# {valid} count of valid queries
# {invalid} count of invalid queries
# {scrubs} a hash, by scrub type, of each string that was removed,
# and the count.
$droplist->{$sld}{scrubs}{$scrub}{$string}{count}++;
if ($scrub eq "OK") {
$droplist->{$sld}{valid}++;
}
else {
$droplist->{$sld}{invalid}++;
}
}
###############################################################################
sub VerifySrc {
my ($src) = @_;
my @src;
if (ref($src) eq "ARRAY") {
@src = @$src;
}
else {
@src[0] = $src;
}
# A source should exist and be a directory
my $badcount;
foreach my $src (@src) {
if (!-d $src) {
print "Source $src is not a directory.\n";
$badcount++;
}
}
return ($badcount);
}
###############################################################################
sub VerifyDst {
my (@dst) = @_;
# A destination shouldn't exist. Make it, verify that it then
# does exist.
my $badcount;
foreach my $dst (@dst) {
if (-d $dst) {
print "Destination $dst already exists.\n";
$badcount++;
next;
}
make_path($dst, {error => \my $err});
if (@$err) {
for my $diag (@$err) {
my ($file, $message) = %$diag;
if ($file eq '') {
print "general error: $message\n";
}
else {
print "problem mkdir'ing $file: $message\n";
}
}
}
if (!-d $dst) {
print "Desgination $dst was not created.\n";
$badcount++;
}
}
if ($badcount) { exit(3); }
}
###############################################################################
sub CalcSrcDst {
my ($year, $suffix, $jassuffix, $systemwide_archive) = @_;
my ($raw, $intermediate, $bytld, $jas);
$raw = $LOC{$year}{raw};
my $mydstbase = ($systemwide_archive) ? $DNS_OARCBASE : $DSTBASE;
$intermediate = $mydstbase."/intermediate/".$LOC{$year}{date}."-".$suffix;
$bytld = $mydstbase."/by-tld/".$LOC{$year}{date}."-".$suffix;
$jas = $DSTBASE."/jas/".$LOC{$year}{date}."-".$suffix;
$jas .= "-".$jassuffix if defined $jassuffix;
return($raw, $intermediate, $bytld, $jas)
}
###############################################################################
sub FindRawFiles {
my ($year, $map, $src_in) = @_;
my @src;
if (ref($src_in) eq "ARRAY") {
@src = @$src_in;
}
else {
@src[0] = $src_in;
}
my $rawFiles = [ ];
# print Dumper($map);
foreach my $root (sort(keys(%$map)))
{
foreach my $src (@src)
{
my $locref = $map->{$root}{loc};
my @loc;
if (ref($locref) eq "ARRAY") {
@loc = @$locref;
}
else {
@loc[0] = $locref;
}
foreach my $loc (@loc) {
my $cmd = "find \"$src/$loc\" -type f";
print "$cmd\n";
my @output = qx/$cmd/;
foreach my $line (@output) {
chomp $line;
my %file;
$file{root} = $root;
$file{src} = $line;
# Calculate a destination name for the intermediate file.
# Start past the root, and replace all / by _ to flatten
# out the filename/directory structure
my $dst = substr($line, index($line, $root) + length($root) + 1);
$dst =~ s/\//_/g;
$file{dst} = $dst;
push(@$rawFiles, \%file);
# print Dumper(\%file);
}
}
}
}
return($rawFiles);
}
###############################################################################
sub DumpRawMap {
my ($rawFiles, @dsts) = @_;
foreach my $dst (@dsts) {
my $ofile = $dst."/pcapmap";
open(OUT, ">$ofile") || die "ERROR failed to open output file $ofile: $!";
my $i = 0;
foreach my $file (@$rawFiles) {
print OUT $i++." ".$file->{src}."\n";
}
close(OUT);
}
}
###############################################################################
sub ParseRawFiles {
my ($rawFiles, $dst, $bytld_dst) = @_;
my $sleeptime = 0;
my $qlen = scalar(@$rawFiles);
my $i = 0;
while ($qlen > 0) {
while (scalar(keys(%CHILDREN)) >= $MAXPROC_TCPDUMP) {
while (@STIFFS) {
my $stiff = shift(@STIFFS);
delete($CHILDREN{$stiff});
}
sleep(5);
}
my $file = shift(@$rawFiles);
my $pid = fork();
if ($pid) {
# parent
$CHILDREN{$pid}++;
}
elsif ($pid == 0) {
# child
# print "ChildStart: $qlen $file\n";
my $fulldest = $dst."/".$file->{root};
make_path($fulldest);
make_path($bytld_dst);
DumpPcap($file->{src}, $fulldest."/".$file->{dst}, $bytld_dst, $i);
# print "ChildEnd: $file\n";
exit(0);
}
$qlen = scalar(@$rawFiles);
$i++;
}
while (scalar(keys(%CHILDREN)) > 0) {
while (@STIFFS) {
my $stiff = shift(@STIFFS);
delete($CHILDREN{$stiff});
}
sleep(5);
}
}
###############################################################################
sub findOffset {
my ($data, $linktype) = @_;
my $debugout = 0;
# Start with a packet from tcpdump, starting with the Ethernet
# Frame past the Premable. (Not sure why I didn't get the
# preamble when I did -XX, but I didn't.) Look for the EtherType
# of 8100 to detemine if we need to jump over the VLAN tag. Then,
# decipher IPv4 or IPv6, then TCP or UDP, to get to the DNS
# packet, then find the start of the name.
my $protocol;
my $offset = 12;
my $skipip;
my ($ip_verlen, $ip_ver);
if ($linktype == 1) {
my $ethertype = ($data->[$offset] * 256) + $data->[$offset + 1];
if (($ethertype == 0x8100) || ($ethertype == 0x0800) ||
($ethertype == 0x86dd)) {
$offset += 2;
$offset += 4 if ($ethertype == 0x8100);
}
else {
printf("ethertype %04x unknown\n", $ethertype) if $debugout;
return(-1);
}
}
elsif ($linktype == 101) {
print "linktype $linktype nolink " if $debugout;
$offset = 0;
$ip_verlen = $data->[$offset];
$ip_ver = ($ip_verlen & (128 + 64 + 32 + 16)) >> 4;
if (($ip_ver == 4) || ($ip_ver == 6)) {
$skipip++;
print "$offset: $ip_verlen ip_ver: $ip_ver " if $debugout;
}
else {
return(-1);
}
}
elsif ($linktype == 108) {
print "linktype $linktype OpenBSD skip 4 " if $debugout;
$offset = 4;
$ip_verlen = $data->[$offset];
$ip_ver = ($ip_verlen & (128 + 64 + 32 + 16)) >> 4;
if (($ip_ver == 4) || ($ip_ver == 6)) {
$skipip++;
print "$offset: $ip_verlen ip_ver: $ip_ver " if $debugout;
}
else {
return(-1);
}
}
else {
print "linktype $linktype UNKNOWN nolink " if $debugout;
$offset = 0;
$ip_verlen = $data->[$offset];
$ip_ver = ($ip_verlen & (128 + 64 + 32 + 16)) >> 4;
if (($ip_ver == 4) || ($ip_ver == 6)) {
$skipip++;
print "$offset: $ip_verlen ip_ver: $ip_ver " if $debugout;
}
else {
return(-1);
}
}
if (!$skipip) {
$ip_verlen = $data->[$offset];
$ip_ver = ($ip_verlen & (128 + 64 + 32 + 16)) >> 4;
print "$offset: $ip_verlen ip_ver: $ip_ver " if $debugout;
}
if ($ip_ver == 6) {
my $next_header = $data->[$offset + 6];
print "next_header: $next_header " if $debugout;
$offset += 40;
while (!(($next_header == 6) || ($next_header == 17))) {
$next_header = $data->[$offset + 0];
$offset += 8 + (8 * $data->[$offset + 1]);
last if $offset > 9000;
}
if ($next_header == 6) { $protocol = "tcp";}
elsif ($next_header == 17) { $protocol = "udp"; }
print "$offset " if $debugout;
}
elsif ($ip_ver == 4) {
my $ip_len = ($ip_verlen & (8 + 4 + 2 + 1));
print "$ip_verlen -> $ip_len " if $debugout;
my $prnum = $data->[$offset + 9];
$offset += (4 * $ip_len);
if ($prnum == 6) { $protocol = "tcp";}
elsif ($prnum == 17) { $protocol = "udp"; }
print "$offset " if $debugout;
}
if ($protocol eq "tcp") {
my $len = $data->[$offset + 12];
$len = ($len % (128 + 64 + 32 + 16)) >> 4;
$offset += ($len * 4);
}
elsif ($protocol eq "udp") {
$offset += 8;
}
# DNS
$offset += 12;
$offset += 2 if ($protocol eq "tcp"); # two extra bytes, length
print "$offset " if $debugout;
if ($offset > 9000) {
$offset = -1;
print " -> $offset" if $debugout;
}
print "\n" if $debugout;
return($offset);
}
###############################################################################
sub getNameFromData {
my ($data, $linktype) = @_;
# In the case that the tcpdump output has control characters (^
# and M-) that can't be reversed, go to the actual tcpdump output
# to resurrect the name
my ($name, $namelen);
my $offset = findOffset($data, $linktype);
if ($offset < 0) {
print "ERROR: no offset\n";
return(undef, -1);
}
my ($sld, $tld);
my $i = $offset;
my $labelcount = 0;
my $labellen = $data->[$i];
while ((defined $labellen) && ($labellen > 0)) {
$labelcount++;
my $label;
if ($labelcount > 127) {
print "ERROR: too many labels\n";
return(undef, -2);
}
my $flags = $labellen & (128 + 64);
$labellen = $labellen & (32 + 16 + 8 + 4 + 2 + 1);
if ($flags > 0) {
print "ERROR: label length ".$data->[$i]." has flags set.\n";
return(undef, -3);
}
foreach (my $count = 0; $count < $labellen; $count++) {
# before now, $i is pointing at the labellen. move past.
$i++;
my $char = $data->[$i];
$namelen++;
# char 32 is the space, so space and below
# 46 is .
# 92 is \
# 127 is DEL, above is undefined in ASCII
if (($char < 33) || ($char == 46) || ($char == 92) ||
($char > 126)) {
$name .= '\\'.sprintf("%02x", $char);
$label .= '\\'.sprintf("%02x", $char);
}
else {
$name .= pack("C", $char);
$label .= pack("C", $char);
}
}
$sld = $tld;
$tld = $label;
$i++;
$labellen = $data->[$i];
# $labellen is now holding the length of the _next_ label.
# Cheat here and check to see if that's 0. If it isn't, add
# the "." label separator to our name.
if ($labellen != 0) {
$name .= ".";
$namelen++;
}
}
if (!defined $labellen) {
# We broke out of the loop becaose we walked past the end of
# the byte array, which means something wasn't right.
print "ERROR: got to end of data.\n";
return(undef, -4);
}
return($name, $namelen, $sld);
}
###############################################################################
sub DumpPcap {
my ($src, $dst, $bytld_dst, $filenum) = @_;
open(my $outlog, "| gzip -c > $dst") || die "ERROR failed to open output file $dst: $!";
# print "gunzip -dc \"$src\" | tcpdump -tttt -X -n -s 0 -r -\n";
my $gzip = ($src =~ /\.gz$/);
my $IN;
if ($gzip) {
open($IN, "gunzip -c $src|") || die "ERROR failed to open input file and tcpdump $src: $!";
}
else {
open($IN, $src) || die "ERROR failed to open input file and tcpdump $src: $!";
}
my $hdr;
sysread($IN, $hdr, 24);
my ($magic, $maj, $min, $off, $acc, $snaplen, $linktype) = unpack("LSSLLLL", $hdr);
# print "linktype: $linktype\n";
close($IN);
my $raw_option = ($DO_RAW_DECODE) ? "-XX" : "";
if ($gzip) {
open($IN, "gunzip -dc \"$src\" | tcpdump -tttt $raw_option -n -s 0 -r - 2> /dev/null |") || die "ERROR failed to open input file and tcpdump $src: $!";
}
else {
open($IN, "/usr/sbin/tcpdump -tttt $raw_option -n -s 0 -r \"$src\" 2> /dev/null |") || die "ERROR failed to open input file and tcpdump $src: $!";
}