forked from sullo/nikto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nikto.pl
executable file
·377 lines (322 loc) · 11.8 KB
/
nikto.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
#!/usr/bin/env perl
use strict;
#VERSION,2.1.6
###############################################################################
# Modules are now loaded in a function so errors can be trapped and evaluated
load_modules();
###############################################################################
# Nikto #
###############################################################################
# Copyright (C) 2001 Chris Sullo
#
# 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; version 2
# of the License only.
#
# 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
# Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Contact Information:
# Sullo ([email protected])
# http://cirt.net/
#######################################################################
# See the LICENSE.txt file for more information on the License Nikto is distributed under.
#
# This program is intended for use in an authorized manner only, and the author
# can not be held liable for anything done with this program, code, or items discovered
# with this program's use.
#######################################################################
# global var/definitions
use vars qw/$TEMPLATES %CLI %VARIABLES %TESTS/;
use vars qw/%NIKTO %CONFIGFILE %COUNTERS %db_extensions/;
use vars qw/@RESULTS @PLUGINS @DBFILE @REPORTS %CONTENTSEARCH/;
# setup
Getopt::Long::Configure('no_ignore_case');
$COUNTERS{'scan_start'} = time();
$VARIABLES{'DIV'} = "-" x 75;
$VARIABLES{'name'} = "Nikto";
$VARIABLES{'version'} = "2.1.6";
$VARIABLES{'configfile'} = "/etc/nikto.conf"; ### Change if it's having trouble finding it
# signal trap so we can close down reports properly
$SIG{'INT'} = \&safe_quit;
config_init();
setup_dirs();
require "$CONFIGFILE{'PLUGINDIR'}/nikto_core.plugin";
nprint("T:" . localtime($COUNTERS{'scan_start'}) . ": Starting", "d");
require "$CONFIGFILE{'PLUGINDIR'}/LW2.pm";
$VARIABLES{'GMTOFFSET'} = gmt_offset();
# use LW2; ### Change this line to use a different installed version
#set SSL Engine
LW2::init_ssl_engine($CONFIGFILE{'LW_SSL_ENGINE'});
my ($a, $b) = split(/\./, $LW2::VERSION);
die("- You must use LW2 2.4 or later\n") if ($a != 2 || $b < 4);
general_config();
load_databases();
load_databases('u');
nprint("- $VARIABLES{'name'} v$VARIABLES{'version'}");
nprint($VARIABLES{'DIV'});
# No targets - quit while we're ahead
if ($CLI{'host'} eq '') {
nprint("+ ERROR: No host or URL specified");
usage();
}
$COUNTERS{'total_targets'} = $COUNTERS{'hosts_completed'} = 0;
load_plugins();
# Parse the supplied list of targets
my @MARKS = set_targets($CLI{'host'}, $CLI{'ports'}, $CLI{'ssl'}, $CLI{'root'});
if (defined($CLI{'key'}) || defined($CLI{'cert'})) {
$CLI{'key'} = $CLI{'cert'} unless (defined($CLI{'key'}));
$CLI{'cert'} = $CLI{'key'} unless (defined($CLI{'cert'}));
}
# Open reporting
report_head($CLI{'format'}, $CLI{'file'});
# Now check each target is real and remove duplicates/fill in extra information
foreach my $mark (@MARKS) {
$mark->{'test'} = 1;
$mark->{'failures'} = 0;
# Try to resolve the host
($mark->{'hostname'}, $mark->{'ip'}, $mark->{'display_name'}) = resolve($mark->{'ident'});
# Skip if we can't resolve the host - we'll error later
if (!defined $mark->{'ip'}) {
$mark->{'test'} = 0;
next;
}
# Read cookies from conf & set into the cookie jar
if (defined $CONFIGFILE{'STATIC-COOKIE'}) {
$mark->{'cookiejar'} = LW2::cookie_new_jar();
foreach my $p (split(/;/, $CONFIGFILE{'STATIC-COOKIE'})) {
if ($p =~ /"([^=]+)=(.+)"/) {
LW2::cookie_set(\%{ $mark->{'cookiejar'} }, $1, $2);
}
}
}
# Check that the port is open
my $open =
port_check(time(), $mark->{'hostname'}, $mark->{'ip'}, $mark->{'port'}, $CLI{'key'}, $CLI{'cert'});
if (defined $CLI{'vhost'}) { $mark->{'vhost'} = $CLI{'vhost'} }
if ($open == 0) {
$mark->{'test'} = 0;
next;
}
else {
$COUNTERS{'total_targets'}++;
}
$mark->{'ssl'} = $open - 1;
if ($mark->{'ssl'}) {
$mark->{'key'} = $CLI{'key'};
$mark->{'cert'} = $CLI{'cert'};
}
}
# Load db_tests
set_scan_items();
# Start hook to allow plugins to load databases etc
run_hooks("", "start");
# Now we've done the precursor, do the scan
foreach my $mark (@MARKS) {
next unless ($mark->{'test'});
$mark->{'start_time'} = time();
$VARIABLES{'TEMPL_HCTR'}++;
if (defined $CLI{'vhost'}) {
$mark->{'vhost'} = $CLI{'vhost'};
}
# Saving responses
if ($CLI{'saveresults'} ne '') {
$mark->{'save_dir'} = save_createdir($CLI{'saveresults'}, $mark);
$mark->{'save_prefix'} = save_getprefix($mark);
}
$mark->{'total_vulns'} = 0;
$mark->{'total_errors'} = 0;
my %FoF = ();
nfetch($mark, "/", "GET", "", "", { noprefetch => 1, nopostfetch => 1 }, "getinfo");
report_host_start($mark);
if ($CLI{'findonly'}) {
my $protocol = "http";
if ($mark->{'ssl'}) { $protocol .= "s"; }
if ($mark->{'banner'} eq "") {
$mark->{'banner'} = "(no identification possible)";
}
add_vulnerability($mark,
"Server: $protocol://$mark->{'display_name'}:$mark->{'port'}\t$mark->{'banner'}",
0);
}
else {
dump_target_info($mark);
unless ((defined $CLI{'nofof'}) || ($CLI{'plugins'} eq '@@NONE')) { map_codes($mark) }
run_hooks($mark, "recon");
run_hooks($mark, "scan");
}
$mark->{'end_time'} = time();
$mark->{'elapsed'} = $mark->{'end_time'} - $mark->{'start_time'};
if (!$CLI{'findonly'}) {
if (!$mark->{'terminate'}) {
nprint(
"+ $COUNTERS{'totalrequests'} requests: $mark->{'total_errors'} error(s) and $mark->{'total_vulns'} item(s) reported on remote host"
);
}
else {
nprint(
"+ Scan terminated: $mark->{'total_errors'} error(s) and $mark->{'total_vulns'} item(s) reported on remote host"
);
}
nprint( "+ End Time: "
. date_disp($mark->{'end_time'})
. " (GMT$VARIABLES{'GMTOFFSET'}) ($mark->{'elapsed'} seconds)");
}
nprint($VARIABLES{'DIV'});
$COUNTERS{'hosts_completed'}++;
report_host_end($mark);
}
$COUNTERS{'scan_end'} = time();
$COUNTERS{'scan_elapsed'} = ($COUNTERS{'scan_end'} - $COUNTERS{'scan_start'});
report_summary();
report_close();
if (!$CLI{'findonly'}) {
nprint("+ $COUNTERS{'hosts_completed'} host(s) tested");
nprint("+ $COUNTERS{'totalrequests'} requests made in $COUNTERS{'scan_elapsed'} seconds", "v");
send_updates(@MARKS);
}
nprint("T:" . localtime() . ": Ending", "d");
exit;
#################################################################################
# Load config files in order
sub config_init {
# read just the --config option
{
my %optcfg;
Getopt::Long::Configure('pass_through', 'noauto_abbrev');
GetOptions(\%optcfg, "config=s");
Getopt::Long::Configure('nopass_through', 'auto_abbrev');
if (defined $optcfg{'config'}) { $VARIABLES{'configfile'} = $optcfg{'config'}; }
}
# Read the config files in order
my ($error, $home);
my $config_exists = 0;
$error = load_config("$VARIABLES{'configfile'}");
$config_exists = 1 if ($error eq "");
# Guess home directory -- to support Windows
foreach my $var (split(/ /, "HOME USERPROFILE")) {
$home = $ENV{$var} if ($ENV{$var});
}
$error = load_config("$home/nikto.conf");
$config_exists = 1 if ($error eq "");
# Guess Nikto current directory
my $NIKTODIR = $0;
chomp($NIKTODIR);
$NIKTODIR =~ s#[\\/]nikto.pl$##;
$error = load_config("$NIKTODIR/nikto.conf");
$config_exists = 1 if ($error eq "");
$error = load_config("nikto.conf");
$config_exists = 1 if ($error eq "");
if ($config_exists == 0) {
die "- Could not find a valid nikto config file\n";
}
return;
}
###############################################################################
sub load_modules {
my $errors=0;
my @modules = qw/Getopt::Long Time::Local IO::Socket/;
push(@modules,"List::Util qw(sum)");
foreach my $mod (@modules) {
eval "use $mod";
if ($@) {
print "ERROR: Required module not found: $mod\n";
$errors=1;
}
}
@modules = ();
push(@modules,"Time::HiRes qw(sleep ualarm gettimeofday tv_interval)");
push(@modules,"POSIX qw(:termios_h)");
foreach my $mod (@modules) {
eval "use $mod";
if ($@ && $^O !~ /MSWin32/) {
# Allow this to work on Windows
if ($@) { print "ERROR: Required module not found: $mod\n"; $errors=1; }
}
}
if ($errors) { exit; }
}
#################################################################################
# load config file
# error=load_config(FILENAME)
sub load_config {
my $configfile = $_[0];
open(CONF, "<$configfile") || return "+ ERROR: Unable to open config file '$configfile'";
my @CONFILE = <CONF>;
close(CONF);
foreach my $line (@CONFILE) {
$line =~ s/\#.*$//;
chomp($line);
$line =~ s/\s+$//;
$line =~ s/^\s+//;
next if ($line eq "");
my @temp = split(/=/, $line, 2);
if ($temp[0] ne "") { $CONFIGFILE{ $temp[0] } = $temp[1]; }
}
# add CONFIG{'CLIOPTS'} to ARGV if defined...
if (defined $CONFIGFILE{'CLIOPTS'}) {
my @t = split(/ /, $CONFIGFILE{'CLIOPTS'});
foreach my $c (@t) { push(@ARGV, $c); }
}
# Check for necessary config items
check_config_defined("CHECKMETHODS", "HEAD");
check_config_defined('@@DEFAULT', '@@ALL');
return "";
}
#################################################################################
# find plugins directory
sub setup_dirs {
my $CURRENTDIR = $0;
chomp($CURRENTDIR);
$CURRENTDIR =~ s#[\\/]nikto.pl$##;
$CURRENTDIR = "." if $CURRENTDIR =~ /^nikto.pl$/;
# First assume we get it from CONFIGFILE
unless (defined $CONFIGFILE{'EXECDIR'}) {
if (-d "$ENV{'PWD'}/plugins") {
$CONFIGFILE{'EXECDIR'} = $ENV{'PWD'};
}
elsif (-d "$CURRENTDIR/plugins") {
$CONFIGFILE{'EXECDIR'} = $CURRENTDIR;
}
elsif (-d "./plugins") {
$CONFIGFILE{'EXECDIR'} = $CURRENTDIR;
}
else {
print STDERR "Could not work out the nikto EXECDIR, try setting it in nikto.conf\n";
exit;
}
}
unless (defined $CONFIGFILE{'PLUGINDIR'}) {
$CONFIGFILE{'PLUGINDIR'} = "$CONFIGFILE{'EXECDIR'}/plugins";
}
unless (defined $CONFIGFILE{'TEMPLATEDIR'}) {
$CONFIGFILE{'TEMPLATEDIR'} = "$CONFIGFILE{'EXECDIR'}/templates";
}
unless (defined $CONFIGFILE{'DOCDIR'}) {
$CONFIGFILE{'DOCDIR'} = "$CONFIGFILE{'EXECDIR'}/docs";
}
unless (defined $CONFIGFILE{'DBDIR'}) {
$CONFIGFILE{'DBDIR'} = "$CONFIGFILE{'EXECDIR'}/databases";
}
return;
}
######################################################################
## check_config_defined(item, default)
## Checks whether config has been set, warns and sets to a default
sub check_config_defined {
my $item = $_[0];
my $default = $_[1];
if (!defined $CONFIGFILE{$item}) {
print STDERR
"- Warning: $item is not defined in Nikto configuration, setting to \"$default\"\n";
$CONFIGFILE{$item} = $default;
}
return;
}