/
subHijack.pl
192 lines (170 loc) · 4.25 KB
/
subHijack.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
#!/usr/bin/perl
#
# DNS check
# Hijacking expired subdomains
#
use strict;
use warnings;
use Net::DNS;
use Net::Ping;
use Try::Tiny;
use Getopt::Long;
use Time::Out qw(timeout);
# Command line options
my $help;
my $version;
my $version_num = 'version 0.1 Alpha';
my $opt_domain;
my $opt_file;
my $opt_result;
my $backup = "0";
my $options = GetOptions(
"help" => \$help,
"version" => \$version,
"domain" => \$opt_domain,
"file" => \$opt_file,
"result" => \$opt_result # TODO
);
my ( $target, $result ) = @ARGV;
show_header();
help() if $help;
quit($version_num) if $version;
start_scan();
sub start_scan {
if ( not defined( $opt_domain or $opt_file ) ) {
print "Usage: perl $0 -domain github.com\n";
print "Usage: perl $0 -file axfr.txt\n";
die "Usage: perl $0 -help\n\n";
}
if ( defined( $opt_domain and $target ) ) {
scan_domain($target);
}
elsif ( defined( $opt_file and $target ) ) {
open (F, $target) || die "Could not open $ARGV[0]: $!\n";
my @f = <F>;
close F;
foreach my $dom (@f) {
chomp($dom);
scan_domain($dom);
}
}
}
sub scan_domain {
my ($dom) = @_;
my @ns;
my @subdomains;
my @cname_domains;
print "\n[i] Checking ".$dom." for subdomain hijacking\n";
my $res = Net::DNS::Resolver->new;
# dns object
my $query = $res->query($dom,"NS");
# query method call for nameservers
if($query) {
# query of NS was successful
foreach my $rr (grep{$_->type eq 'NS'} $query->answer) {
push (@ns, $rr->nsdname);
}
print "[i] Found ".scalar(@ns)." nameservers\n";
print "[i] Trying zone transfer:\n";
foreach my $ns (@ns) {
try {
$res->nameservers($ns);
} catch {
# warn "caught NS error: ".$dom;
};
# set the name server
my @subs = timeout 2 => sub {
try {
return $res->axfr($dom);
} catch {
# warn "caught axfr error: ".$dom;
};
};
if ($#subs > 0) {
print "\t[NS] ".$ns." ~ OK\n";
push(@subdomains, @subs);
}
else {
print "\t[NS] ".$ns." ~ Failed\n";
}
}
if (scalar @subdomains > 0) {
my @filtered = uniq(@subdomains);
print "[i] Found ".scalar(@filtered)." subdomains\n";
print "[i] Checking CNAME records\n";
foreach my $subdomain (@filtered) {
if ($subdomain->type eq 'CNAME') {
my @cname_domain = split(/\./, $subdomain->cname);
my $cname_tld = pop(@cname_domain); #org
my $cname_baredomain = pop(@cname_domain); #zoelife4u
my $cname_domain = $cname_baredomain.".".$cname_tld;
if ($cname_domain ne $dom) {
push(@cname_domains, $cname_domain);
}
}
}
my @cname_filtered = uniq2(@cname_domains);
print "[i] Found ".scalar(@cname_filtered)." unique CNAME domains\n";
print "[i] Checking whois records\n";
foreach my $check_domain (@cname_filtered) {
if (`whois $check_domain` =~ "No match for") {
foreach my $subdomain (@filtered) {
if (($subdomain->type eq 'CNAME') and ($subdomain->cname eq $check_domain)) {
print "[!] Found expired domain\n";
print "\t[x] ".$subdomain->name." > CNAME > ".$check_domain."\n";
print "\t[x] ".$check_domain." is expired!\n\n";
select(undef, undef, undef, 0.5); # Sleep for 500 milliseconds
}
}
}
}
}
else {
print "[i] No subdomains :(\n";
}
}
else {
# Something went wrong:
warn "[q] query failed: ", $res->errorstring,"\n";
}
}
sub show_header {
system("clear");
print <<EndHead;
_ _ _ _ _ _
___ _ _| |__ | | | (_)(_) __ _ ___| | __
/ __| | | | '_ \\| |_| | || |/ _` |/ __| |/ /
\\__ \\ |_| | |_) | _ | || | (_| | (__| <
|___/\\__,_|_.__/|_| |_|_|/ |\\__,_|\\___|_|\\_\\
|__/
EndHead
}
sub help {
print <<EOHELP;
subHijack.pl at https://github.com/vavkamil/subHijack
Usage: perl $0 -domain github.com
Usage: perl $0 -file axfr.txt
Overview:
Hijacking forgotten & misconfigured subdomains
Options:
-domain Single domain input.
-file File with domains.
-output Output file.
-version Print current version.
-help This help message.
EOHELP
exit;
}
sub uniq {
my %seen;
grep !$seen{$_->name}++, @_;
}
sub uniq2 {
my %seen;
grep !$seen{$_}++, @_;
}
sub quit {
my ($text) = @_;
print "$text\n\n";
exit;
}