forked from fwaeytens/dnsenum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsenum.pl
1760 lines (1370 loc) · 41.6 KB
/
dnsenum.pl
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/bin/perl
#
#
# dnsenum.pl VERSION 1.2.4
# This version: - changed version number to the correct one
#
# dnsenum.pl: multithread script to enumerate information on
# a domain and to discover non-contiguous ip blocks.
#
# 1) Get the host's addresse.
# 2) Get the nameservers (threaded).
# 3) get the MX record (threaded).
# 4) Perform axfr queries on nameservers (threaded).
# 5) Get extra names via google scraping.
# 6) Brute force subdomains from file (threaded).
# 7) Calculate C class domain network ranges and perform whois
# queries on them (threaded).
# 8) Perform reverse lookups on C class or/and whois
# network ranges (threaded).
# 9) Write to domain_ips.txt file non-contiguous ip-blocks results.
#
# run perldoc on this script for help.
#
# To install needed modules:
# sudo perl -MCPAN -e shell
# and then e.g.: cpan[1]> install XML::Writer
#
# Copyright (C) 2014 - Filip Waeytens, tixxDZ
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
# Special thanks to all perl developers.
#
# please see perldoc dnsenum.pl for options and arguments
use strict;
use warnings;#it complains about uninitialized values when it doesn't find address in RR; need to fix later
use Config;
use Term::ANSIColor;
use Getopt::Long;
use IO::File;
use Net::IP;
use Net::DNS;
use Net::Netmask;
use XML::Writer;
use Socket;
use String::Random;
my ($ithreads_support, $whois_support, $mech_support, $html_support,$xml_support);
my (%nameservers, %allsubs, %googlesubs);
my (%filesubs, %netranges, %recursubs);
my (@mxservers, @results, @ipblocks, @privateips);
my ($enum, $exp, $help, $noreverse, $nocolor, $update, $whois, $dnsserver);
my ($private, $recursion, $scrap, $threads, $verbose);
my ($dnsfile, $subfile, $dns_tmp, $sub_tmp, $fileips);
my ($domain, $recur, $table, $extend_b, $extend_r);
my ($timeout, $delay, $pages, $ipcount, $ipvalid) = (10, 3, 5, 0, 0);
my ($output);
my $writer;
my $program = 'dnsenum.pl';
my $string_gen = String::Random->new;
my $wildcards = $string_gen->randpattern("cccccccccccc");
my @wildcardaddress;
my @wildcardcname;
my $VERSION = '1.2.4';
#load threads modules (perl must be compiled with ithreads support)
BEGIN {
if ($Config{useithreads}){
eval(" use threads;
use threads::shared;
use Thread::Queue;
");
$ithreads_support = 1 unless $@;
}
}
eval("use Net::Whois::IP qw(whoisip_query);");
$whois_support = 1 unless $@;
eval("use WWW::Mechanize;");
$mech_support = 1 unless $@;
eval("use HTML::Parser;");
$html_support = 1 unless $@;
eval("use XML::Writer;");
$xml_support = 1 unless $@;
print STDOUT $program, " VERSION:", $VERSION, "\n";
GetOptions ( 'dnsserver=s' => \$dnsserver,
'enum' => \$enum,
'd|delay=i' => \$delay,
'e|exclude=s' => \$exp,
'f|file=s' => \$dnsfile,
'h|help' => \$help,
'noreverse' => \$noreverse,
'nocolor' => \$nocolor,
'p|pages=i' => \$pages,
'private' => \$private,
'r|recursion' => \$recursion,
's|scrap=i' => \$scrap,
'subfile=s' => \$subfile,
'threads=i' => \$threads,
't|timeout=i' => \$timeout,
'u|update=s' => \$update,
'v|verbose' => \$verbose,
'w|whois' => \$whois,
'o|out=s' => \$output);
usage() if $help || @ARGV == 0;
$domain = lc $ARGV[0];
$fileips = $domain.'_ips.txt';
#DEFAULT options --threads 5 -s 15 -w
if ($enum) {
$threads = 5;
$scrap = 15;# Google scraping default to 15 to avoid Google Blocking us with captcha's
$whois = 1;
}
#module support
if ($threads) {
if ((!defined $ithreads_support and
warn "Warning: can't use threads, check ithreads support, and ".
"(threads, threads::shared, Thread::Queue) modules.\n") ||
$threads <= 0) {
$threads = undef;
}
else {
#to handle different ips that belongs to the domain
share(@results);
#number of ips that will be queried in reverse lookup
share($ipcount);
#number of valid ips (taken from reverse lookup responses)
share($ipvalid);
#will contain all valid subdomains
share(%allsubs);
if ($recursion) {
share(%recursubs);
share(%nameservers);
}
#to save whois netblocks
share($table);
#whois and reverse lookup results
share(%netranges);
}
}
if ($whois && !defined $whois_support) {
warn "Warning: can't load Net::Whois::IP module, ".
"whois queries disabled.\n";
$whois = undef;
}
if ($whois && !defined $whois_support) {
warn "Warning: can't load Net::Whois::IP module, ".
"whois queries disabled.\n";
$whois = undef;
}
if ($output && !defined $xml_support) {
warn "Warning: can't load XML::Writer module, ".
"xml output disabled.\n";
$output = undef;
}
if(defined($output)) {
my $out = new IO::File(">$output");
$writer = new XML::Writer(OUTPUT=>$out);
$writer->xmlDecl("UTF-8");
$writer->startTag("magictree", "class"=>"MtBranchObject");
$writer->startTag("testdata", "class"=>"MtBranchObject");
}
$scrap = undef
if $scrap && ((not defined $mech_support and
warn "Warning: can't load WWW::Mechanize module".
", Google scraping desabled.\n") ||
(not defined $html_support and
warn "Warning: can't load HTML::Parser module".
", Google scraping desabled.\n") ||
$scrap <= 0 || $pages <= 0);
$timeout = 10 if $timeout < 0 || $timeout > 128;
$delay = 3 if $delay < 0;
$update = undef if $update && !$dnsfile;
unless ($nocolor) {
print color 'bold blue';
}
print STDOUT "\n----- ", $domain ," -----\n";
unless ($nocolor) {
print color 'reset';
}
################START#####################
# (1) get the host's addresses
printheader ("Host's addresses:\n");
my $res = Net::DNS::Resolver->new( tcp_timeout => $timeout,
udp_timeout => $timeout,
defnames => 0);
$res->nameservers($dnsserver) if $dnsserver;
my $packet = $res->query($domain);
if ($packet) {
foreach my $rr (grep { $_->type eq 'A' } $packet->answer) {
printrr($rr->string);
xml_host($rr);
push @results, $rr->address
if $rr->name =~ /$domain$/;
}
}
elsif ($verbose) {
warn " ", $domain ," A query failed: ", $res->errorstring, "\n";
}
# wildcards test - I guess it can be cleaner, but it seems to be working
# tested with opendns servers and ubuntu.org domain
print STDOUT "\n"."-" x 16 ."\nWildcards test:\n"."-" x 16 ."\n"
if $verbose;
my $wildcardpacket=$res->query($wildcards.".".$domain);
# if we get a response resolving our random hostname, it can be a A or a CNAME
if ($wildcardpacket) {
printheader ("Wildcard detection using: ".$wildcards."\n");
foreach my $rr ($wildcardpacket->answer) {
if ($rr->type eq 'A')
{
printrr($rr->string);
#wildcardaddress will hold the IP that's used as a string
my @wcheck= split('\s+',$rr->string);
push @wildcardaddress, $wcheck[4];
}
if ($rr->type eq 'CNAME')
{
printrr($rr->string);
#wildcardcname will hold CNAME that's used as a string
my @wcheck= split('\s+',$rr->string);
push @wildcardcname, $wcheck[4];
}
}
unless ($nocolor) {
print color 'bold red';
}
print "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
print STDOUT " Wildcards detected, all subdomains will point to the same IP address\n";
print STDOUT " Omitting results containing ".join(', ', @wildcardaddress).".\n Maybe you are using OpenDNS servers.\n";
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
unless ($nocolor) {
print color 'reset';
}
#exit(1);#we don't exit when wildcards are detected, because we will miss existing hosts
}
elsif ($verbose) {
print STDOUT " good\n";
}
# (2) get the namservers for the domain
printheader ("Name Servers:\n");
$packet = $res->query($domain, 'NS');
if ($packet) {
foreach my $rr (grep { $_->type eq 'NS' } $packet->answer) {
$nameservers{$rr->nsdname} = 1;
}
die " Error: can't continue no NS record for " , $domain ,"\n"
unless %nameservers;
#read the A record from the additional section
if ($packet->header->arcount) {
my @remain = additionalrecord($packet, keys %nameservers);
#do nslookup on nameservers that are not present in the
#additional section
launchqueries(\&nslookup, @remain)
if scalar @remain;
}
#perform the nslookup on the nameservers
else {
launchqueries(\&nslookup, keys %nameservers);
}
}
#exit if there is no NS record.
else {
die " ", $domain ," NS record query failed: ",
$res->errorstring , "\n";
}
# (3) get the MX record
printheader ("Mail (MX) Servers:\n");
$packet = $res->query($domain, 'MX');
if ($packet) {
foreach my $rr (grep { $_->type eq 'MX' } $packet->answer) {
push @mxservers, $rr->exchange;
}
if (scalar @mxservers) {
if ($packet->header->arcount) {
my @remain = additionalrecord($packet, @mxservers);
launchqueries(\&nslookup, @remain)
if scalar @remain;
}
else {
launchqueries(\&nslookup, @mxservers);
}
}
}
elsif ($verbose) {
warn " ", $domain ," MX record query failed: ",
$res->errorstring, "\n";
}
# (4) perform zonetransfers on nameservers
printheader ("Trying Zone Transfers and getting Bind Versions:\n");
launchqueries(\&zonetransfer, keys %nameservers);
# (5) scrap additional names from google search and do nslookup on them
if ($scrap) {
printheader ("Scraping ".$domain." subdomains from Google:\n");
my @tmp = googlescraping();
if (scalar @tmp) {
#print STDOUT "\n Performing nslookups:\n";
launchqueries(\&nslookup, map { $_ .= '.'.$domain } @tmp);
}
}
#exit if the brute force file is not specified
unless ($dnsfile) {
print STDOUT "\nbrute force file not specified, bay.\n";
if(defined($output)) {
$writer->endTag('testdata');
$writer->endTag('magictree');
}
exit(0);
}
$extend_b = 1 if ($update && $update eq 'a') || $subfile || $recursion;
# (6) brute force subdomains from a file
printheader ("Brute forcing with ".$dnsfile.":\n");
bruteforce('f');
#updating dnsfile with zonetransfer or googlescraping subdomains results
if ($update) {
if ($dns_tmp = IO::File->new_tmpfile()) {
if ($update eq 'z') {
print $dns_tmp $_, "\n"
for uniq_hosts(grep {
$allsubs{$_} eq $update
} keys %allsubs);
}
elsif ($update eq 'g') {
print $dns_tmp $_, "\n"
for uniq_hosts(keys %googlesubs);
}
}
else {
die "Error can't create a temporary file: $!, ".
"to update ", $dnsfile ," file\n";
}
}
undef %googlesubs if %googlesubs;
#launch recursion
if ($recursion) {
if (%allsubs) {
printheader("Performing recursion:\n");
print STDOUT "\n ---- Checking subdomains NS records ----\n";
#select subdomains that are able to recursion
launchqueries(\&selectsubdomains,
map { $_ .= '.'.$domain } sort keys %allsubs);
if (%recursubs) {
my @tmp = keys %recursubs;
undef %recursubs;
#brute force from a list using recursion
bruteforce('l', \@tmp);
}
else {
print STDERR "\n Can't perform recursion ".
"no NS records.\n";
}
}
else {
print STDERR "\n Can't perform recursion no subdomains.\n";
}
undef %filesubs;
}
#updating the brute force file (-u a switch)
if ($update && ($update eq 'a' || $update eq 'all')) {
#save ns and mx servers
my @tmp = keys %nameservers;
push @tmp, @mxservers if scalar @mxservers;
print $dns_tmp $_, "\n"
for uniq_hosts(grep { s/\.$domain// } @tmp);
print $dns_tmp $_, "\n" for uniq_hosts(keys %allsubs);
}
#write subdomains to the subfile
if ($subfile) {
if ($sub_tmp = IO::File->new_tmpfile()) {
my %uniq;
@uniq{keys %nameservers} = ();
@uniq{@mxservers} = () if scalar @mxservers;
print $sub_tmp $_, "\n"
for grep { s/\.$domain// } keys %uniq;
print $sub_tmp $_, "\n" for keys %allsubs;
}
else {
die "Error can't create a temporary file: $!, ".
" to save results to ", $subfile ," file\n";
}
}
undef @mxservers;
undef %allsubs;
# (7) get domain network ranges from brute force results and whois queries
@results = networkranges(@results);
undef %netranges;
# (8) perform reverse lookups on netranges (class C or whois netranges)
unless ($noreverse) {
#to save all valid subdomains discovred in
#the reverse lookup process
$extend_r = 1
if ($update && ($update eq 'r' || $update eq 'a' ||
$update eq 'all')) || $subfile;
printheader("Performing reverse lookup on ".$ipcount." ip addresses:\n");
launchqueries(\&reverselookup, @results);
print STDOUT "\n", $ipvalid ," results out of ",
$ipcount ," IP addresses.\n";
}
else {
#calculate ip blocks
@ipblocks = finalvalidips(sort_by_ip_address(@results));
}
#save final IP blocks to the domain_ips.txt file
writetofile($fileips, "w", @ipblocks);
#show private ips
if ($private && scalar @privateips) {
print STDOUT "\n"."-" x 26 ."\n",
$domain ," private ips:\n".
"-" x 26 ."\n";
print STDOUT " ", $_ , "\n" for @privateips;
#save private ips to domain_ips.txt file
writetofile($fileips, "a+", @privateips);
}
# (9) show non-contiguous IP blocks
printheader($domain." ip blocks:\n");
print STDOUT " ", $_ , "\n" for @ipblocks;
#clean the brute force file
cleanfile($dnsfile, $dns_tmp) if $update;
#clean the subfile
cleanfile($subfile, $sub_tmp) if $subfile;
if(defined($output)) {
$writer->endTag('testdata');
$writer->endTag('magictree');
}
print STDOUT "\ndone.\n";
exit(0);
#--------------------------------------------------
#subroutine that will launch different queries
#(nslookup, zonetransfer, whoisip, reverselookup)
sub launchqueries {
my $querytype = shift;
if ($querytype != \&reverselookup) {
if ($threads) {
my $stream = new Thread::Queue;
$stream->enqueue(@_);
my $thrs = $threads; #don't create unused threads
$thrs = scalar @_ if $threads > scalar @_;
for (1 .. $thrs) {
threads->new($querytype, \$stream);
}
#wait all threads
foreach (threads->list) {
$_->join
if ($_->tid &&
!threads::equal($_, threads->self));
}
}
else {
foreach (@_) {
&$querytype($_);
}
}
}
else {
foreach (@_) {
my $block = new2 Net::Netmask($_);
unless ($block) {
print STDERR " Can't perform reverse lookup: "
, $Net::Netmask::error ,"\n";
next;
}
if ($threads) {
my $stream = new Thread::Queue;
$stream->enqueue($block->enumerate);
for (1 .. $threads) {
threads->new($querytype, \$stream);
}
#wait all threads
foreach (threads->list) {
$_->join
if ($_->tid &&
!threads::equal($_, threads->self));
}
}
else {
&$querytype($block->enumerate);
}
#calculate IP blocks results
if (%netranges) {
my @tmp = finalvalidips(
sort_by_ip_address(keys %netranges));
undef %netranges;
#get final valid ip blocks
push @ipblocks, @tmp;
}
#write reverse lookup hostnames results to files
if ($extend_r && %allsubs) {
if ($update && ($update eq 'r' ||
$update eq 'a' || $update eq 'all')) {
print $dns_tmp $_, "\n"
for uniq_hosts(keys %allsubs);
}
if ($subfile) {
print $sub_tmp $_, "\n"
for keys %allsubs;
}
undef %allsubs;
}
}
}
}
#subroutine to perform reverse lookups
sub reverselookup {
my $stream = shift if $threads;
my $res = Net::DNS::Resolver->new(
tcp_timeout => $timeout,
udp_timeout => $timeout,
persistent_udp => 1);
$res->nameservers(keys %nameservers);
while (defined(my $ip = $threads ? $$stream->dequeue_nb : shift)) {
my $query = $res->query($ip);
if ($query) {
foreach my $rr ( grep { $_->type eq 'PTR' }
$query->answer) {
#exclude non PTR types answers or unwanted hostnames
next if $exp && $rr->ptrdname =~ /$exp/;
if ($rr->ptrdname =~ /(.*)(\.$domain$)/i) {
$allsubs{$1} = 1
if $extend_r && !$allsubs{$1};
#to calculate last valid ip blocks
unless ($netranges{$ip}) {
$netranges{$ip} = 1;
$ipvalid++;
}
printrr($rr->string);
xml_host($rr);
}
#show all answers even if the hostname don't match the domain
elsif ($verbose) {
printrr($rr->string);
xml_host($rr);
}
}
}
#this part is just to check progress
elsif ($verbose) {
print STDOUT " ", $ip ," ...\n";
}
}
}
sub xml_host {
if(defined $output) {
my $rr = shift;
my $ip;
if($rr->type eq 'A') {
$ip = $rr->address;
} else {
my $packed_ip = gethostbyname($rr->name);
if (defined $packed_ip) {
$ip = inet_ntoa($packed_ip);
}
}
if(defined($ip)) {
$writer->startTag("host");
$writer->characters($ip);
$writer->startTag("hostname");
$writer->characters($rr->name);
$writer->endTag("hostname");
$writer->endTag("host");
}
$writer->startTag("fqdn");
$writer->characters($rr->name . '.');
$writer->endTag("fqdn");
}
}
#subroutine for nslookups (A record)
sub nslookup {
my $stream = shift if $threads;
my $res = Net::DNS::Resolver->new( tcp_timeout => $timeout,
udp_timeout => $timeout,
persistent_udp => 1,
dnsrch => 0);
$res->nameservers($dnsserver) if $dnsserver;
while (defined(my $host = $threads ? $$stream->dequeue_nb : shift)) {
my $query = $res->search($host);
if ($query) {
foreach my $rr ($query->answer) {
##we only print / add the result if it doesn't match the wildcardaddress
if (!($rr->can('address') && $rr->address ~~ @wildcardaddress) && !($rr->name ~~ @wildcardcname))
{
printrr($rr->string);
xml_host($rr);
#check if it match the domain
if ($rr->name =~ /(.*)(\.$domain$)/i) {
#save valid subdomains
if ($extend_b) {
$allsubs{$1} = 1
unless $allsubs{$1};
#recursion results
$recursubs{$1} = 1
if $recur &&
!$recursubs{$1};
}
#save ip address
push @results, $rr->address
if $rr->type eq 'A';
}
}
}
}
elsif ($verbose) {
warn " ", $host ," A record query failed: ",
$res->errorstring, "\n";
}
}
}
#subroutine to select subdomains that have NS records
sub selectsubdomains {
my $stream = shift if $threads;
my $res = Net::DNS::Resolver->new( tcp_timeout => $timeout,
udp_timeout => $timeout,
persistent_udp => 1);
$res->nameservers($dnsserver) if $dnsserver;
while (defined(my $host = $threads ? $$stream->dequeue_nb : shift)) {
my $packet = $res->query($host, 'NS');
if ($packet) {
foreach my $rr (grep { $_->type eq 'NS' }
$packet->answer) {
#show all results
#print STDOUT " ", $rr->string ,"\n";
printrr($rr->string);
xml_host($rr);
if ($rr->name =~ /(.*)(\.$domain$)/i) {
if (!$allsubs{$1} ||
$allsubs{$1} ne 'r') {
#bookmark this hostname to
#perform recursion on it and
#to avoid repetition, because
#some domains will use CNAME
#types that point to subs
#that we have already
#processed in a previous
#recursion levels
$allsubs{$1} = 'r';
#select this subdomain
#for recursion
$recursubs{$rr->name} = 1;
}
#perhaps for future additions we save
#ns servers for each domain or
#subdomain, this will be very
#useful in reverse lookup
# --- begin ---
#check if we already have this
#NS server
#next if $nameservers{$rr->nsdname};
#$nameservers{$rr->nsdname} = 1;
#push @tmp, $rr->nsdname;
# --- end ---
}
}
#perhaps for future additions to perform an extrem
#recursion to get the IP addresse of the NS servers
# --- begin ---
#next unless scalar @tmp;
#get the NS servers A record
#if ($packet->header->arcount) {
# @tmp = additionalrecord($packet,@tmp);
# next unless scalar @tmp;
#}
#foreach my $nshost (@tmp) {
# $packet = $res->query($nshost);
# if ($packet) {
# foreach my $rr \
# (grep { $_->type eq 'A' }
# $packet->answer) {
# print STDOUT " ",
# $rr->string , "\n";
# push @results, $rr->address
# if ($rr->name =~ /$domain$/);
# }
# }
# elsif ($verbose) {
# warn " ", $nshost ,
# " A record query failed: ",
# $res->errorstring , "\n";
# }
#}
# --- end ---
}
elsif ($verbose) {
warn " ", $host ," NS record query failed: ",
$res->errorstring , "\n";
}
}
}
#subroutine for zonetransfers
#I got rid of the Bind Versions search...doesn't really add anything and clutters output
sub zonetransfer {
my $stream = shift if $threads;
my $res = Net::DNS::Resolver->new( tcp_timeout => $timeout,
udp_timeout => $timeout);
while (defined(my $ns = $threads ? $$stream->dequeue_nb : shift)) {
$res->nameservers($ns);
my @zone = $res->axfr($domain);
#my $version_query = $res->search("version.bind","TXT","CH");
print STDOUT "\nTrying Zone Transfer for ", $domain ,
" on ", $ns ," ... \n";
if (@zone) {
foreach my $rr (@zone) {
#print all results
printrr($rr->string);
xml_host($rr);
#save data if the record's domain name
#match the domain
if ($rr->name =~ /(.*)(\.$domain$)/i) {
#save hostname
$allsubs{$1} = 'z'
unless $allsubs{$1};
#save the IP address
push @results, $rr->address
if $rr->type eq 'A';
#save new mx servers hostnames
push @mxservers, $rr->exchange
if $rr->type eq 'MX';
#perhaps for future additions
#save NS servers for reverse lookups
# --- begin ---
#$nameservers{$rr->nsdname} = 1
# if ($rr->type eq 'NS' &&
# !$nameservers{$rr->nsdname});
# --- end ---
}
}
}
else {
warn "AXFR record query failed: ",
$res->errorstring, "\n";
}
}
}
#subroutine for scraping domains from google
sub googlescraping {
my ($response, $browser, $form, $parser, $nextpage);
my ($count, $mypage) = (0,1);
my $query = qq[allinurl: -www site:$domain];
my $nexturl = qq[/search?.*q=.*$domain.*start];
#on errors the mech object will call die
$browser = WWW::Mechanize->new( autocheck => 1,
stack_depth => 1,
cookie_jar => undef);
#uncomment for debugging with BURP
#$browser->proxy(['http'], 'http://127.0.0.1:8080');
#setup the browser config
my @agents = $browser->known_agent_aliases();
my $agent = $agents[rand(scalar @agents)];
$browser->agent_alias($agent);
$browser->timeout($timeout);
#get the first page
$response = $browser->get("http://www.google.com/ncr");
$form = $browser->form_number(1)
or return;
$form->find_input('q')->value($query);
$response = $browser->submit_form( form_number => 1,
form_name => 'f');
do {
$nextpage = undef;
print STDOUT "\n ---- Google search page: ",
$mypage ," ---- \n\n";
$parser = HTML::Parser->new(
api_version => 3,
start_h => [sub {
my $attr = shift;
#end of parsing
#(we have enough subdomains)
$parser->eof
unless $count < $scrap;
return unless $attr->{href};
#subdomains checks - if shit goes wrong with googlescraping it's prolly the regex
if ($attr->{href} =~
/(\/url\?q\=http:\/\/)([\w\.-]+)(\.$domain\/)/) {
$allsubs{$2} = 'g'
unless $allsubs{$2};
$googlesubs{$2} = 1
unless $googlesubs{$2};
print STDOUT " ", $2 ,"\n";
$count++;
}
#the next page
elsif ($attr->{href} =~
/^$nexturl=$mypage\d.*/ &&
!defined $nextpage) {
$nextpage = $attr->{href};
}
},'attr']
);
$parser->parse($response->decoded_content);
if ($nextpage) {
$response = $browser->get($nextpage);
$mypage++;
}
} while ($count < $scrap && $mypage <= $pages && $nextpage);
#print STDOUT "\n Google results: ", $count ,"\n";
printheader("Google Results:\n");
if ($count) {
return grep { $allsubs{$_} eq 'g' } keys %allsubs;
}
else {
print STDERR " perhaps Google is blocking our queries.\n Check manually.\n";
return;
}
}
#subroutine to query a whois server for an IP address to get the correct netrange
sub whoisip {
my $stream = shift if $threads;
while (defined(my $ip = $threads ? $$stream->dequeue_nb : shift)) {
my ($inetnum, $block);
#search in the network blocks table to find
#if any of them contains the IP address
next if (findAllNetblock($ip, $table));
#this is very useful on whois servers
#that limit the number of connections
sleep rand $delay;
#catch different exceptions
#(on exceptions netrange will be a class C /24)
eval {
my $response = whoisip_query($ip);
foreach (keys %{$response}) {
next if ($_ !~ /^(inetnum|netrange)$/i);
$inetnum = $response->{$_};