blib/lib/Net/Nessus/ScanLite.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Net::Nessus::ScanLite;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Net::Nessus::Client;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Net::Nessus::Message;
11  
12  
13 require Exporter;
14  
15 our @ISA = qw(Exporter IO::Socket::SSL Net::Nessus::Client Net::Nessus::Message );
16  
17 our %EXPORT_TAGS = ( 'all' => [ qw( ) ] );
18 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
19 our @EXPORT = qw( );
20  
21 our $VERSION = '0.01';
22  
23 sub new
24 {
25 my $class = shift;
26 $class = ref($class) || $class;
27 my %args = @_;
28 my $self = bless {
29 _code => 0,
30 _error => "",
31 ntp_version => '1.2',
32 host => undef,
33 port => 1241,
34 user => undef,
35 password => undef,
36 ssl_version => 'TLSv1',
37 timeout => 1,
38 ssl => 1,
39 debug => 1,
40 _cfg => undef,
41 _section => 'nessus',
42 _duration => 0,
43 _prefsect => 'preferences',
44 _defsection => 'defaults',
45 _holes => [],
46 _info => [],
47 preferences => {},
48  
49 },$class;
50 # Handle ini config handle or path
51 if( $args{Cfg} )
52 {
53 $self->cfg($args{Cfg});
54 if( ref($self->cfg) )
55 { $self->init_cfg; } # assume handle
56 else
57 { $self->init_cfg_path; }
58 }
59 @{$self}{keys %args} = values %args;
60 return($self);
61 }
62  
63 #----------------------------------------------------#
64 # IniFiles methods
65 #----------------------------------------------------#
66 sub init_cfg_path
67 {
68 my $this = shift;
69 my $file = $this->cfg;
70 my $cfg = new Config::IniFiles( -file => $file, -default => $this->ini_default );
71 return($this->set_error(100,"Config file error for $file ($!)")) unless($cfg);
72 $this->cfg($cfg);
73 $this->ok("Config file $file is ok.");
74 $this->init_cfg;
75 }
76 sub init_cfg
77 {
78 my $this = shift;
79 my $cfg = $this->cfg;
80 # Get host/login defaults from nessus section override keys in main class.
81 init_section($cfg,$this,$this->section);
82 # Get preferences from preferences section put them under preferences.
83 init_section($cfg,$this->{preferences},$this->pref_section);
84 }
85 sub init_section
86 {
87 my ($cfg,$hash,$section) = @_;
88 if( $cfg->SectionExists( $section ) )
89 {
90 foreach( $cfg->Parameters($section) )
91 {
92 $hash->{$_} = $cfg->val($section,$_);
93 }
94 }
95 }
96 sub pref_section
97 {
98 my $this = shift;
99 my $key = '_prefsect';
100 $this->{$key} = shift if( @_ );
101 return($this->{$key});
102 }
103 sub ini_default
104 {
105 my $this = shift;
106 my $key = '_defsection';
107 $this->{$key} = shift if( @_ );
108 return($this->{$key});
109 }
110  
111  
112 #----------------------------------------------------#
113 # Nessus methods
114 #----------------------------------------------------#
115 sub plugin_set
116 {
117 my $this = shift;
118 my $class = 'preferences';
119 my $key = 'plugin_set';
120 $this->preference($key,shift) if( @_ );
121 return($this->preference($key));
122 }
123 sub preferences
124 {
125 my $this = shift;
126 my $class = 'preferences';
127 $this->{$class} = shift if( @_ );
128 return($this->{$class});
129 }
130  
131 sub preference
132 {
133 my ($this,$key,$value) = @_;
134 my $class = 'preferences';
135 return(undef) unless($key);
136 $this->{$class}->{$key} = $value if($value);
137 return(undef) unless($this->{$class}->{$key});
138 return($this->{$class}->{$key});
139 }
140 sub login
141 {
142 my $this = shift;
143 my $i = 0;
144 $this->user(shift) if( @_ );
145 $this->password(shift) if( @_ );
146 $this->__connect;
147 return(0) if( $this->code );
148 my $ssl = $this->socket;
149 $ssl->autoflush();
150 $ssl->print($this->ntp_fast);
151 my $r = join(' ',$ssl->getline);
152 chomp($r);
153 if( $r ne $this->ntp_proto )
154 {
155 $this->set_error(1,"Protocol error $r");
156 return(0);
157 }
158 $ssl->print( $this->user . "\n");
159 $ssl->print( $this->password . "\n");
160 $ssl->print( "CLIENT <|> NESSUS_VERSION <|> CLIENT\n");
161 $r = join(' ',$ssl->getline);
162 chomp($r);
163 if( $r =~ /Bad login/gis )
164 {
165 $this->set_error(1,"Bad login ". $this->user);
166 return(0);
167 }
168 return(1);
169 }
170 sub setprefs
171 {
172 my $this = shift;
173 my $p = "CLIENT <|> PREFERENCES <|>\n";
174 $this->socket->flush;
175 my $h = $this->preferences;
176 foreach( sort keys %$h )
177 { $p .= sprintf("%s <|> %s\n",$_,$h->{$_}); }
178 $p .= " <|> CLIENT\n";
179 $this->socket->print($p);
180 my $msg = Net::Nessus::Message->new('socket' => $this->socket,
181 'sender' => 'SERVER',
182 'type' => 'PREFERENCES_ERRORS');
183 }
184  
185 sub __connect
186 {
187 my $this = shift;
188 my $sock = undef;
189 if( $this->ssl )
190 {
191 # $IO::Socket::SSL::DEBUG = $this->{debug};
192 $sock = IO::Socket::SSL->new(
193 PeerAddr => $this->host,
194 PeerPort => $this->port,
195 SSL_version => $this->ssl_version,
196 Timeout => $this->timeout
197 )
198 or $this->set_error(1,sprintf("Connect to %s failed. (%s)",$this->hostport,&IO::Socket::SSL::errstr()));
199 }
200 else
201 {
202 $sock = IO::Socket::INET->new(
203 PeerAddr => $this->host,
204 PeerPort => $this->port,
205 Timeout => $this->timeout
206 )
207 or $this->set_error(2,sprintf("Connect to %s failed. ($!)",$this->hostport));
208 }
209 $this->socket($sock) if($sock);
210 return($this->code);
211 }
212  
213 sub ShowHOLE
214 {
215 my ($this,$msg) = @_;
216 my $key = '_holes';
217 push(@{$this->{$key}},$msg);
218 }
219 sub ShowINFO
220 {
221 my ($this,$msg) = @_;
222 my $key = '_info';
223 push(@{$this->{$key}},$msg);
224 }
225  
226 sub total_holes
227 {
228 my $this = shift;
229 my $key = '_holes';
230 my $a = $this->{$key};
231 return( scalar @$a);
232 }
233 sub total_info
234 {
235 my $this = shift;
236 my $key = '_info';
237 my $a = $this->{$key};
238 return(scalar @$a);
239 }
240 sub holes
241 {
242 my $this = shift;
243 my $key = '_holes';
244 my $a = $this->{$key};
245 return($a);
246 }
247 sub info
248 {
249 my $this = shift;
250 my $key = '_info';
251 my $a = $this->{$key};
252 return($a);
253 }
254 sub info_list
255 {
256 my $this = shift;
257 return(@{$this->info});
258 }
259 sub hole_list
260 {
261 my $this = shift;
262 return(@{$this->holes});
263 }
264 sub holes2tmpl
265 {
266 my $this = shift;
267 return(nessus2tmpl($this->holes));
268 }
269 sub infos2tmpl
270 {
271 my $this = shift;
272 return(nessus2tmpl($this->info));
273 }
274  
275 sub nessus2tmpl
276 {
277 my $list = shift;
278 my $array = [];
279 foreach( @$list )
280 {
281 my $msg = $_;
282 push(@$array,
283 {
284 port => $msg->Port,
285 host => $msg->Host,
286 description => $msg->Description,
287 service => $msg->Service(),
288 proto => $msg->Proto,
289 scanid => $msg->ScanID,
290 });
291 }
292 return($array);
293 }
294  
295 sub ntp_fast
296 {
297 my $this = shift;
298 return(sprintf("%s< fast_login >\n",$this->ntp_proto));
299 }
300 sub ntp_version
301 {
302 my $this = shift;
303 my $key = 'ntp_version';
304 $this->{$key} = shift if( @_ );
305 return($this->{$key});
306 }
307  
308 sub ntp_proto
309 {
310 my $this = shift;
311 return(sprintf("< NTP/%s >",$this->ntp_version));
312 }
313  
314  
315  
316 sub attack
317 {
318 my ($this,$host) = @_;
319 my $start = time;
320 $this->setprefs;
321 my @hosts = ( $host );
322 my $status = $this->Attack(@hosts);
323 $this->duration(time - $start);
324 }
325 sub user
326 {
327 my $this = shift;
328 my $key = 'user';
329 $this->{$key} = shift if( @_ );
330 return($this->{$key});
331 }
332 sub password
333 {
334 my $this = shift;
335 my $key = 'password';
336 $this->{$key} = shift if( @_ );
337 return($this->{$key});
338 }
339 sub hostport
340 {
341 my $this = shift;
342 return($this->host . ':' . $this->port);
343 }
344 sub host
345 {
346 my $this = shift;
347 my $key = 'host';
348 $this->{$key} = shift if( @_ );
349 return($this->{$key});
350 }
351 sub port
352 {
353 my $this = shift;
354 my $key = 'port';
355 $this->{$key} = shift if( @_ );
356 return($this->{$key});
357 }
358  
359 sub ssl
360 {
361 my $this = shift;
362 my $key = 'ssl';
363 $this->{$key} = shift if( @_ );
364 return($this->{$key});
365 }
366  
367 # Note: Net::Nessus::Message is expecting $this->{socket} to be there
368 sub socket
369 {
370 my $this = shift;
371 my $key = 'socket';
372 $this->{$key} = shift if( @_ );
373 return($this->{$key});
374 }
375  
376 sub ssl_version
377 {
378 my $this = shift;
379 my $key = 'ssl_version';
380 $this->{$key} = shift if( @_ );
381 return($this->{$key});
382 }
383 sub timeout
384 {
385 my $this = shift;
386 my $key = 'timeout';
387 $this->{$key} = shift if( @_ );
388 return($this->{$key});
389 }
390 sub cfg
391 {
392 my $this = shift;
393 my $key = '_cfg';
394 $this->{$key} = shift if( @_ );
395 return($this->{$key});
396 }
397 sub section
398 {
399 my $this = shift;
400 my $key = '_section';
401 $this->{$key} = shift if( @_ );
402 return($this->{$key});
403 }
404  
405 sub duration
406 {
407 my $this = shift;
408 my $key = '_duration';
409 $this->{$key} = shift if( @_ );
410 return($this->{$key});
411 }
412  
413  
414  
415 sub ok
416 {
417 my $this = shift;
418 $this->error(shift) if( @_ );
419 return($this->code(0));
420 }
421  
422 sub set_error
423 {
424 my ($this,$code,$msg) = @_;
425 $this->error($msg);
426 return($this->code($code));
427 }
428  
429  
430 sub code
431 {
432 my $this = shift;
433 my $key = '_code';
434 $this->{$key} = shift if( @_ );
435 return($this->{$key});
436 }
437 sub error
438 {
439 my $this = shift;
440 my $key = '_error';
441 $this->{$key} = shift if( @_ );
442 return($this->{$key});
443 }
444  
445 # Fixes the GLOB reference when SSL.pm shuts down, recent?
446 sub DESTROY { $_[0]->socket->close if( $_[0]->socket ); }
447  
448  
449  
450  
451  
452  
453  
454  
455  
456  
457  
458
459  
460
461  
462  
463  
464  
465 # Preloaded methods go here.
466  
467 1;
468 __END__
↑ Summary