blib/lib/Nmap/Parser.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Nmap::Parser;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use XML::Twig;
Subroutine BEGIN: called CC=1 SCAR=2.6
5  
6 our $VERSION = 1.37;
7  
8  
9 sub new {
10  
11 my ( $class, $self ) = shift;
12 $class = ref($class) || $class;
13  
14 $self->{twig} = new XML::Twig(
15 start_tag_handlers => { nmaprun => sub {$self->_nmaprun_start_tag_hdlr(@_)} },
16  
17 twig_roots => {
18 scaninfo => sub {$self->_scaninfo_tag_hdlr(@_)},
19 prescript => sub {$self->_prescript_tag_hdlr(@_)},
20 postscript => sub {$self->_postscript_tag_hdlr(@_)},
21 finished => sub {$self->_finished_tag_hdlr(@_)},
22 host => sub {$self->_host_tag_hdlr(@_)},
23 },
24 ignore_elts => {
25 addport => 1,
26 debugging => 1,
27 verbose => 1,
28 hosts => 1,
29 taskbegin => 1,
30 taskend => 1,
31 taskprogress => 1
32 }
33 );
34  
35 bless( $self, $class );
36 return $self;
37 }
38  
39 #/*****************************************************************************/
40 # NMAP::PARSER OBJECT METHODS
41 #/*****************************************************************************/
42  
43 #Safe parse and parsefile will return $@ which will contain the error
44 #that occured if the parsing failed (it might be empty when no error occurred)
45  
46 sub callback {
47 my $self = shift;
48 my $callback = shift; #first arg is CODE
49 if ( ref($callback) eq 'CODE' ) {
50 $self->{callback}{coderef} = $callback;
51 $self->{callback}{is_registered} = 1;
52 }
53 else {
54 $self->{callback}{is_registered} = 0;
55 }
56  
57 #returns if a callback is registered or not
58 return $self->{callback}{is_registered};
59 }
60  
61 sub _parse {
62 my $type = shift;
63 my $self = shift;
64 $self->{HOSTS} = undef;
65 $self->{SESSION} = undef;
66 {
67 file => sub { $self->{twig}->safe_parsefile(@_); },
68 string => sub { $self->{twig}->safe_parse(@_); },
69 }->{$type}->(@_);
70 if ($@) { die $@; }
71 $self->purge;
72 return $self;
73 }
74  
75 sub parse {
76 return _parse('string', @_);
77 }
78  
79 sub parsefile {
80 return _parse('file', @_);
81 }
82  
83 sub parsescan {
84 my $self = shift;
85 my $nmap = shift;
86 my $args = shift;
87 my @ips = @_;
88 my $FH;
89  
90 if ( $args =~ /-o(?:X|N|G)/ ) {
91 die
92 "[Nmap-Parser] Cannot pass option '-oX', '-oN' or '-oG' to parsecan()";
93 }
94  
95 my $cmd;
96  
97 #if output file is defined, point it to a localfile then call parsefile instead.
98 if ( defined( $self->{cache_file} ) ) {
99 $cmd =
100 "$nmap $args -v -v -v -oX "
101 . $self->{cache_file} . " "
102 . ( join ' ', @ips );
103 `$cmd`; #remove output from STDOUT
104 $self->parsefile( $self->{cache_file} );
105 }
106 else {
107 $cmd = "$nmap $args -v -v -v -oX - " . ( join ' ', @ips );
108 open $FH,
109 "$cmd |" || die "[Nmap-Parser] Could not perform nmap scan - $!";
110 $self->parse($FH);
111 close $FH;
112 }
113  
114 return $self;
115 }
116  
117 sub cache_scan {
118 my $self = shift;
119 $self->{cache_file} = shift || 'nmap-parser-cache.' . time() . '.xml';
120 }
121  
122 sub purge {
123 my $self = shift;
124 $self->{twig}->purge;
125 return $self;
126 }
127  
128 sub addr_sort {
129 my $self = shift if ref $_[0];
130  
131 return (
132 map { unpack("x16A*", $_) }
133 sort { $a cmp $b }
134 map {
135 my @vals;
136 if( /:/ ) { #IPv6
137 @vals = split /:/;
138 @vals = map { $_ eq '' ? (0) x (8-$#vals) : hex } @vals
139 } else { #IPv4
140 my @v4 = split /\./;
141 # Sort as IPv4-mapped IPv6, per RFC 4291 Section 2.5.5.2
142 @vals = ( (0) x 5, 0xffff, map { 256*$v4[$_] + $v4[$_+1] } (0,2) );
143 }
144 pack("n8A*", @vals, $_)
145 } @_
146 );
147 }
148  
149 #MAIN SCAN INFORMATION
150 sub get_session {
151 my $self = shift;
152 my $obj = Nmap::Parser::Session->new( $self->{SESSION} );
153 return $obj;
154 }
155  
156 #HOST STUFF
157 sub get_host {
158 my ( $self, $ip ) = (@_);
159 if ( $ip eq '' ) {
160 warn "[Nmap-Parser] No IP address given to get_host()\n";
161 return undef;
162 }
163 $self->{HOSTS}{$ip};
164 }
165  
166 sub del_host {
167 my ( $self, $ip ) = (@_);
168 if ( $ip eq '' ) {
169 warn "[Nmap-Parser] No IP address given to del_host()\n";
170 return undef;
171 }
172 delete $self->{HOSTS}{$ip};
173 }
174  
175 sub all_hosts {
176 my $self = shift;
177 my $status = shift || '';
178  
179 return ( values %{ $self->{HOSTS} } ) if ( $status eq '' );
180  
181 my @hosts = grep { $_->{status} eq $status } ( values %{ $self->{HOSTS} } );
182 return @hosts;
183 }
184  
185 sub get_ips {
186 my $self = shift;
187 my $status = shift || '';
188  
189 return $self->addr_sort( keys %{ $self->{HOSTS} } ) if ( $status eq '' );
190  
191 my @hosts =
192 grep { $self->{HOSTS}{$_}{status} eq $status }
193 ( keys %{ $self->{HOSTS} } );
194 return $self->addr_sort(@hosts);
195  
196 }
197  
198 #/*****************************************************************************/
199 # PARSING TAG HANDLERS FOR XML::TWIG
200 #/*****************************************************************************/
201  
202 sub _nmaprun_start_tag_hdlr {
203  
204 my ($self, $twig, $tag ) = @_;
205  
206 $self->{SESSION}{start_time} = $tag->{att}->{start};
207 $self->{SESSION}{nmap_version} = $tag->{att}->{version};
208 $self->{SESSION}{start_str} = $tag->{att}->{startstr};
209 $self->{SESSION}{xml_version} = $tag->{att}->{xmloutputversion};
210 $self->{SESSION}{scan_args} = $tag->{att}->{args};
211 $self->{SESSION} = Nmap::Parser::Session->new( $self->{SESSION} );
212  
213 $twig->purge;
214  
215 }
216  
217 sub _scaninfo_tag_hdlr {
218 my ( $self, $twig, $tag ) = @_;
219 my $type = $tag->{att}->{type};
220 my $proto = $tag->{att}->{protocol};
221 my $numservices = $tag->{att}->{numservices};
222  
223 if ( defined($type) ) { #there can be more than one type in one scan
224 $self->{SESSION}{type}{$type} = $proto;
225 $self->{SESSION}{numservices}{$type} = $numservices;
226 }
227 $twig->purge;
228 }
229  
230 sub _prescript_tag_hdlr {
231 my ($self, $twig, $tag ) = @_;
232 my $scripts_hashref;
233 for my $script ( $tag->children('script') ) {
234 $scripts_hashref->{ $script->{att}->{id} } =
235 __script_tag_hdlr( $script );
236 }
237 $self->{SESSION}{prescript} = $scripts_hashref;
238 $twig->purge;
239 }
240  
241 sub _postscript_tag_hdlr {
242 my ($self, $twig, $tag ) = @_;
243 my $scripts_hashref;
244 for my $script ( $tag->children('script') ) {
245 $scripts_hashref->{ $script->{att}->{id} } =
246 __script_tag_hdlr( $script );
247 }
248 $self->{SESSION}{postscript} = $scripts_hashref;
249 $twig->purge;
250 }
251  
252 sub _finished_tag_hdlr {
253 my ($self, $twig, $tag ) = @_;
254 $self->{SESSION}{finish_time} = $tag->{att}->{time};
255 $self->{SESSION}{time_str} = $tag->{att}->{timestr};
256 $twig->purge;
257 }
258  
259 #parses all the host information in one swoop (calling __host_*_tag_hdlrs)
260 sub _host_tag_hdlr {
261 my ($self, $twig, $tag ) = @_;
262 my $id = undef;
263  
264 return undef unless ( defined $tag );
265  
266 #GET ADDRESS INFO
267 my $addr_hashref;
268 $addr_hashref = __host_addr_tag_hdlr($tag);
269  
270 #use this as the identifier
271 $id =
272 $addr_hashref->{ipv4}
273 || $addr_hashref->{ipv6}
274 || $addr_hashref->{mac}; #worstcase use MAC
275  
276 $self->{HOSTS}{$id}{addrs} = $addr_hashref;
277  
278 return undef unless ( defined($id) || $id ne '' );
279  
280 #GET HOSTNAMES
281 $self->{HOSTS}{$id}{hostnames} = __host_hostnames_tag_hdlr($tag);
282  
283 #GET STARTTIME
284 $self->{HOSTS}{$id}{starttime} = $tag->{att}->{starttime};
285  
286 #GET STATUS
287 $self->{HOSTS}{$id}{status} = $tag->first_child('status')->{att}->{state};
288  
289 #CONTINUE PROCESSING IF STATUS IS UP - OTHERWISE NO MORE XML
290 if ( lc( $self->{HOSTS}{$id}{status} ) eq 'up' ) {
291  
292 $self->{HOSTS}{$id}{ports} = __host_port_tag_hdlr($tag);
293 $self->{HOSTS}{$id}{os} = __host_os_tag_hdlr($tag);
294 $self->{HOSTS}{$id}{uptime} = __host_uptime_tag_hdlr($tag);
295 $self->{HOSTS}{$id}{tcpsequence} = __host_tcpsequence_tag_hdlr($tag);
296 $self->{HOSTS}{$id}{ipidsequence} = __host_ipidsequence_tag_hdlr($tag);
297 $self->{HOSTS}{$id}{tcptssequence} = __host_tcptssequence_tag_hdlr($tag);
298 $self->{HOSTS}{$id}{hostscript} = __host_hostscript_tag_hdlr($tag);
299 $self->{HOSTS}{$id}{distance} =
300 __host_distance_tag_hdlr($tag); #returns simple value
301 $self->{HOSTS}{$id}{trace} = __host_trace_tag_hdlr($tag);
302 $self->{HOSTS}{$id}{trace_error} = __host_trace_error_tag_hdlr($tag);
303 }
304  
305 #CREATE HOST OBJECT FOR USER
306 $self->{HOSTS}{$id} = Nmap::Parser::Host->new( $self->{HOSTS}{$id} );
307  
308 if ( $self->{callback}{is_registered} ) {
309 &{ $self->{callback}{coderef} }( $self->{HOSTS}{$id} );
310 delete $self->{HOSTS}{$id};
311 }
312  
313 $twig->purge;
314  
315 }
316  
317 sub __host_addr_tag_hdlr {
318 my $tag = shift;
319 my $addr_hashref;
320  
321 #children() will return all children with tag name address
322 for my $addr ( $tag->children('address') ) {
323 if ( lc( $addr->{att}->{addrtype} ) eq 'mac' ) {
324  
325 #we'll assume for now, only 1 MAC address per system
326 $addr_hashref->{mac}{addr} = $addr->{att}->{addr};
327 $addr_hashref->{mac}{vendor} = $addr->{att}->{vendor};
328 }
329 elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv4' ) {
330 $addr_hashref->{ipv4} = $addr->{att}->{addr};
331 } #support for ipv6? we'll see
332 elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv6' ) {
333 $addr_hashref->{ipv6} = $addr->{att}->{addr};
334 }
335  
336 }
337  
338 return $addr_hashref;
339 }
340  
341 sub __host_hostnames_tag_hdlr {
342 my $tag = shift;
343  
344 my $hostnames_tag = $tag->first_child('hostnames');
345 return undef unless ( defined $hostnames_tag );
346  
347 my @hostnames;
348  
349 for my $name ( $hostnames_tag->children('hostname') ) {
350 push @hostnames, $name->{att}->{name};
351 }
352  
353 return \@hostnames;
354  
355 }
356  
357 sub __host_port_tag_hdlr {
358 my $tag = shift;
359 my ( $port_hashref, $ports_tag );
360  
361 $ports_tag = $tag->first_child('ports');
362  
363 return undef unless ( defined $ports_tag );
364  
365 #Parsing Extraports
366 my $extraports_tag = $ports_tag->first_child('extraports');
367 if ( defined $extraports_tag && $extraports_tag ne '' ) {
368 $port_hashref->{extraports}{state} = $extraports_tag->{att}->{state};
369 $port_hashref->{extraports}{count} = $extraports_tag->{att}->{count};
370 }
371  
372 #Parsing regular port information
373  
374 my ( $tcp_port_count, $udp_port_count ) = ( 0, 0 );
375  
376 for my $port_tag ( $ports_tag->children('port') ) {
377 my $proto = $port_tag->{att}->{protocol};
378 my $portid = $port_tag->{att}->{portid};
379 my $state = $port_tag->first_child('state');
380 my $owner = $port_tag->first_child('owner') || undef;
381  
382 $tcp_port_count++ if ( $proto eq 'tcp' );
383 $udp_port_count++ if ( $proto eq 'udp' );
384  
385 $port_hashref->{$proto}{$portid}{state} = $state->{att}->{state}
386 || 'unknown'
387 if ( $state ne '' );
388  
389 $port_hashref->{$proto}{$portid}{reason_ttl} = $state->{att}->{reason_ttl}
390 || 'unknown'
391 if($state ne '');
392  
393 #GET SERVICE INFORMATION
394 $port_hashref->{$proto}{$portid}{service} =
395 __host_service_tag_hdlr( $port_tag, $portid )
396 if ( defined($proto) && defined($portid) );
397  
398 #GET SCRIPT INFORMATION
399 $port_hashref->{$proto}{$portid}{service}{script} =
400 __host_script_tag_hdlr( $port_tag, $portid)
401 if ( defined($proto) && defined($portid) );
402  
403 #GET OWNER INFORMATION
404 $port_hashref->{$proto}{$portid}{service}{owner} = $owner->{att}->{name}
405 if ( defined($owner) );
406  
407 #These are added at the end, otherwise __host_service_tag_hdlr will overwrite
408 #GET PORT STATE
409  
410 }
411  
412 $port_hashref->{tcp_port_count} = $tcp_port_count;
413 $port_hashref->{udp_port_count} = $udp_port_count;
414  
415 return $port_hashref;
416  
417 }
418  
419 sub __host_service_tag_hdlr {
420 my $tag = shift;
421 my $portid = shift; #need a way to remember what port this service runs on
422 my $service = $tag->first_child('service[@name]');
423 my $service_hashref;
424 $service_hashref->{port} = $portid;
425  
426 if ( defined $service ) {
427 $service_hashref->{name} = $service->{att}->{name} || 'unknown';
428 $service_hashref->{version} = $service->{att}->{version};
429 $service_hashref->{product} = $service->{att}->{product};
430 $service_hashref->{devicetype} = $service->{att}->{devicetype};
431 $service_hashref->{extrainfo} = $service->{att}->{extrainfo};
432 $service_hashref->{proto} =
433 $service->{att}->{proto}
434 || $service->{att}->{protocol}
435 || 'unknown';
436 $service_hashref->{rpcnum} = $service->{att}->{rpcnum};
437 $service_hashref->{tunnel} = $service->{att}->{tunnel};
438 $service_hashref->{method} = $service->{att}->{method};
439 $service_hashref->{confidence} = $service->{att}->{conf};
440 $service_hashref->{fingerprint} = $service->{att}->{servicefp};
441 }
442  
443 return $service_hashref;
444 }
445  
446 sub __host_script_tag_hdlr {
447 my $tag = shift;
448 my $script_hashref;
449  
450 for ( $tag->children('script') ) {
451 $script_hashref->{ $_->{att}->{id} } =
452 __script_tag_hdlr($_);
453 }
454  
455 return $script_hashref;
456 }
457  
458 sub __host_os_tag_hdlr {
459 my $tag = shift;
460 my $os_tag = $tag->first_child('os');
461 my $os_hashref;
462 my $portused_tag;
463 my $os_fingerprint;
464  
465 if ( defined $os_tag ) {
466  
467 #get the open port used to match os
468 $portused_tag = $os_tag->first_child("portused[\@state='open']");
469 $os_hashref->{portused}{open} = $portused_tag->{att}->{portid}
470 if ( defined $portused_tag );
471  
472 #get the closed port used to match os
473 $portused_tag = $os_tag->first_child("portused[\@state='closed']");
474 $os_hashref->{portused}{closed} = $portused_tag->{att}->{portid}
475 if ( defined $portused_tag );
476  
477 #os fingerprint
478 $os_fingerprint = $os_tag->first_child("osfingerprint");
479 $os_hashref->{os_fingerprint} =
480 $os_fingerprint->{'att'}->{'fingerprint'}
481 if ( defined $os_fingerprint );
482  
483 #This will go in Nmap::Parser::Host::OS
484 my $osmatch_index = 0;
485 my $osclass_index = 0;
486 for my $osmatch ( $os_tag->children('osmatch') ) {
487 $os_hashref->{osmatch_name}[$osmatch_index] =
488 $osmatch->{att}->{name};
489 $os_hashref->{osmatch_name_accuracy}[$osmatch_index] =
490 $osmatch->{att}->{accuracy};
491 $osmatch_index++;
492 for my $osclass ( $osmatch->children('osclass') ) {
493 $os_hashref->{osclass_osfamily}[$osclass_index] =
494 $osclass->{att}->{osfamily};
495 $os_hashref->{osclass_osgen}[$osclass_index] =
496 $osclass->{att}->{osgen};
497 $os_hashref->{osclass_vendor}[$osclass_index] =
498 $osclass->{att}->{vendor};
499 $os_hashref->{osclass_type}[$osclass_index] =
500 $osclass->{att}->{type};
501 $os_hashref->{osclass_class_accuracy}[$osclass_index] =
502 $osclass->{att}->{accuracy};
503 $osclass_index++;
504 }
505 }
506 $os_hashref->{'osmatch_count'} = $osmatch_index;
507  
508 #parse osclass tags
509 for my $osclass ( $os_tag->children('osclass') ) {
510 $os_hashref->{osclass_osfamily}[$osclass_index] =
511 $osclass->{att}->{osfamily};
512 $os_hashref->{osclass_osgen}[$osclass_index] =
513 $osclass->{att}->{osgen};
514 $os_hashref->{osclass_vendor}[$osclass_index] =
515 $osclass->{att}->{vendor};
516 $os_hashref->{osclass_type}[$osclass_index] =
517 $osclass->{att}->{type};
518 $os_hashref->{osclass_class_accuracy}[$osclass_index] =
519 $osclass->{att}->{accuracy};
520 $osclass_index++;
521 }
522 $os_hashref->{'osclass_count'} = $osclass_index;
523 }
524  
525 return $os_hashref;
526  
527 }
528  
529 sub __host_uptime_tag_hdlr {
530 my $tag = shift;
531 my $uptime = $tag->first_child('uptime');
532 my $uptime_hashref;
533  
534 if ( defined $uptime ) {
535 $uptime_hashref->{seconds} = $uptime->{att}->{seconds};
536 $uptime_hashref->{lastboot} = $uptime->{att}->{lastboot};
537  
538 }
539  
540 return $uptime_hashref;
541  
542 }
543  
544 sub __host_tcpsequence_tag_hdlr {
545 my $tag = shift;
546 my $sequence = $tag->first_child('tcpsequence');
547 my $sequence_hashref;
548 return undef unless ($sequence);
549 $sequence_hashref->{class} = $sequence->{att}->{class};
550 $sequence_hashref->{difficulty} = $sequence->{att}->{difficulty};
551 $sequence_hashref->{values} = $sequence->{att}->{values};
552 $sequence_hashref->{index} = $sequence->{att}->{index};
553  
554 return $sequence_hashref;
555  
556 }
557  
558 sub __host_ipidsequence_tag_hdlr {
559 my $tag = shift;
560 my $sequence = $tag->first_child('ipidsequence');
561 my $sequence_hashref;
562 return undef unless ($sequence);
563 $sequence_hashref->{class} = $sequence->{att}->{class};
564 $sequence_hashref->{values} = $sequence->{att}->{values};
565 return $sequence_hashref;
566  
567 }
568  
569 sub __host_tcptssequence_tag_hdlr {
570 my $tag = shift;
571 my $sequence = $tag->first_child('tcptssequence');
572 my $sequence_hashref;
573 return undef unless ($sequence);
574 $sequence_hashref->{class} = $sequence->{att}->{class};
575 $sequence_hashref->{values} = $sequence->{att}->{values};
576 return $sequence_hashref;
577 }
578  
579 sub __host_hostscript_tag_hdlr {
580 my $tag = shift;
581 my $scripts = $tag->first_child('hostscript');
582 my $scripts_hashref;
583 return undef unless ($scripts);
584 for my $script ( $scripts->children('script') ) {
585 $scripts_hashref->{ $script->{att}->{id} } =
586 __script_tag_hdlr( $script );
587 }
588 return $scripts_hashref;
589 }
590  
591 sub __host_distance_tag_hdlr {
592 my $tag = shift;
593 my $distance = $tag->first_child('distance');
594 return undef unless ($distance);
595 return $distance->{att}->{value};
596 }
597  
598 sub __host_trace_tag_hdlr {
599 my $tag = shift;
600 my $trace_tag = $tag->first_child('trace');
601 my $trace_hashref = { hops => [], };
602  
603 if ( defined $trace_tag ) {
604  
605 my $proto = $trace_tag->{att}->{proto};
606 $trace_hashref->{proto} = $proto if defined $proto;
607  
608 my $port = $trace_tag->{att}->{port};
609 $trace_hashref->{port} = $port if defined $port;
610  
611 for my $hop_tag ( $trace_tag->children('hop') ) {
612  
613 # Copy the known hop attributes, they will go in
614 # Nmap::Parser::Host::TraceHop
615 my %hop_data;
616 $hop_data{$_} = $hop_tag->{att}->{$_} for qw( ttl rtt ipaddr host );
617 delete $hop_data{rtt} if $hop_data{rtt} !~ /^[\d.]+$/;
618  
619 push @{ $trace_hashref->{hops} }, \%hop_data;
620 }
621  
622 }
623  
624 return $trace_hashref;
625 }
626  
627 sub __host_trace_error_tag_hdlr {
628 my $tag = shift;
629 my $trace_tag = $tag->first_child('trace');
630  
631 if ( defined $trace_tag ) {
632  
633 my $error_tag = $trace_tag->first_child('error');
634 if ( defined $error_tag ) {
635  
636 # If an error happens, always provide a true value even if
637 # it doesn't contains a useful string
638 my $errorstr = $error_tag->{att}->{errorstr} || 1;
639 return $errorstr;
640 }
641 }
642  
643 return;
644 }
645  
646 sub __script_tag_hdlr {
647 my $tag = shift;
648 my $script_hashref = {
649 output => $tag->{att}->{output}
650 };
651 chomp %$script_hashref;
652 if ( not $tag->is_empty()) {
653 $script_hashref->{contents} = __script_table($tag);
654 }
655 return $script_hashref;
656 }
657  
658 sub __script_table {
659 my $tag = shift;
660 my ($ref, $subref);
661 my $fc = $tag->first_child();
662 if ($fc) {
663 if ($fc->is_text) {
664 $ref = $fc->text;
665 }
666 else {
667 if ($fc->{att}->{key}) {
668 $ref = {};
669 $subref = sub {
670 $ref->{$_->{att}->{key}} = shift;
671 };
672 }
673 else {
674 $ref = [];
675 $subref = sub {
676 push @$ref, shift;
677 };
678 }
679 for ($tag->children()) {
680 if ($_->tag() eq "table") {
681 $subref->(__script_table( $_ ));
682 }
683 else {
684 $subref->($_->text);
685 }
686 }
687 }
688 }
689 return $ref
690 }
691  
692 #/*****************************************************************************/
693 # NMAP::PARSER::SESSION
694 #/*****************************************************************************/
695  
696 package Nmap::Parser::Session;
697 use vars qw($AUTOLOAD);
698  
699 sub new {
700 my $class = shift;
701 $class = ref($class) || $class;
702 my $self = shift || {};
703 bless( $self, $class );
704 return $self;
705 }
706  
707 #Support for:
708 #start_time, start_str, finish_time, time_str, nmap_version, xml_version, scan_args
709 sub AUTOLOAD {
710 ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
711 return if ( $param eq 'DESTROY' );
712 no strict 'refs';
713 *$AUTOLOAD = sub { return $_[0]->{ lc $param } };
714 goto &$AUTOLOAD;
715 }
716  
717 sub numservices {
718 my $self = shift;
719 my $type = shift
720 || ''; #(syn|ack|bounce|connect|null|xmas|window|maimon|fin|udp|ipproto)
721  
722 return unless ( ref( $self->{numservices} ) eq 'HASH' );
723  
724 if ( $type ne '' ) { return $self->{numservices}{$type}; }
725 else {
726 my $total = 0;
727 for ( values %{ $self->{numservices} } ) { $total += $_; }
728 return $total;
729 } #(else) total number of services together
730 }
731  
732 sub scan_types {
733 return sort { $a cmp $b } ( keys %{ $_[0]->{type} } )
734 if ( ref( $_[0]->{type} ) eq 'HASH' );
735 }
736 sub scan_type_proto { return $_[1] ? $_[0]->{type}{ $_[1] } : undef; }
737  
738 sub prescripts {
739 my $self = shift;
740 my $id = shift;
741 unless ( defined $id ) {
742 return sort keys %{ $self->{prescript} };
743 }
744 else {
745 return $self->{prescript}{$id};
746 }
747 }
748  
749 sub postscripts {
750 my $self = shift;
751 my $id = shift;
752 unless ( defined $id ) {
753 return sort keys %{ $self->{postscript} };
754 }
755 else {
756 return $self->{postscript}{$id};
757 }
758 }
759  
760 #/*****************************************************************************/
761 # NMAP::PARSER::HOST
762 #/*****************************************************************************/
763  
764 package Nmap::Parser::Host;
765 use vars qw($AUTOLOAD);
766  
767 sub new {
768 my $class = shift;
769 $class = ref($class) || $class;
770 my $self = shift || {};
771 bless( $self, $class );
772 return $self;
773 }
774  
775 sub starttime { return $_[0]->{starttime}; }
776  
777 sub status { return $_[0]->{status}; }
778  
779 sub addr {
780 my $default = $_[0]->{addrs}{ipv4} || $_[0]->{addrs}{ipv6};
781 return $default;
782 }
783  
784 sub addrtype {
785 if ( $_[0]->{addrs}{ipv4} ) { return 'ipv4'; }
786 elsif ( $_[0]->{addrs}{ipv6} ) { return 'ipv6'; }
787 }
788  
789 sub ipv4_addr { return $_[0]->{addrs}{ipv4}; }
790 sub ipv6_addr { return $_[0]->{addrs}{ipv6}; }
791  
792 sub mac_addr { return $_[0]->{addrs}{mac}{addr}; }
793 sub mac_vendor { return $_[0]->{addrs}{mac}{vendor}; }
794  
795 #returns the first hostname
796 sub hostname {
797 my $self = shift;
798 my $index = shift || 0;
799 if ( ref( $self->{hostnames} ) ne 'ARRAY' ) { return ''; }
800 if ( scalar @{ $self->{hostnames} } <= $index ) {
801 $index = scalar @{ $self->{hostnames} } - 1;
802 }
803 return $self->{hostnames}[$index] if ( scalar @{ $self->{hostnames} } );
804 }
805  
806 sub all_hostnames { return @{ $_[0]->{hostnames} || [] }; }
807 sub extraports_state { return $_[0]->{ports}{extraports}{state}; }
808 sub extraports_count { return $_[0]->{ports}{extraports}{count}; }
809 sub distance { return $_[0]->{distance}; }
810  
811 sub hostscripts {
812 my $self = shift;
813 my $id = shift;
814 unless ( defined $id ) {
815 return sort keys %{ $self->{hostscript} };
816 }
817 else {
818 return $self->{hostscript}{$id};
819 }
820 }
821  
822 sub all_trace_hops {
823  
824 my $self = shift;
825  
826 return unless defined $self->{trace}->{hops};
827 return map { Nmap::Parser::Host::TraceHop->new( $_ ) }
828 @{ $self->{trace}->{hops} };
829 }
830  
831 sub trace_port { return $_[0]->{trace}->{port} }
832 sub trace_proto { return $_[0]->{trace}->{proto} }
833 sub trace_error { return $_[0]->{trace_error} }
834  
835 sub _del_port {
836 my $self = shift;
837 my $proto = pop; #portid might be empty, so this goes first
838 my @portids = @_;
839 @portids = grep { $_ + 0 } @portids;
840  
841 unless ( scalar @portids ) {
842 warn "[Nmap-Parser] No port number given to del_port()\n";
843 return undef;
844 }
845  
846 delete $self->{ports}{$proto}{$_} for (@portids);
847 }
848  
849 sub _get_ports {
850 my $self = shift;
851 my $proto = pop; #param might be empty, so this goes first
852 my $state = shift; #open, filtered, closed or any combination
853 my @matched_ports = ();
854  
855 #if $state is undef, then tcp_ports or udp_ports was called for all ports
856 #therefore, only return the keys of all ports found
857 if ( not defined $state ) {
858 return sort { $a <=> $b } ( keys %{ $self->{ports}{$proto} } );
859 }
860 else {
861 $state = lc($state)
862 }
863  
864 #the port parameter can be set to either any of these also 'open|filtered'
865 #can count as 'open' and 'filetered'. Therefore I need to use a regex from now on
866 #if $param is empty, then all ports match.
867  
868 for my $portid ( keys %{ $self->{ports}{$proto} } ) {
869  
870 #escape metacharacters ('|', for example in: open|filtered)
871 #using \Q and \E
872 push( @matched_ports, $portid )
873 if ( $self->{ports}{$proto}{$portid}{state} =~ /\Q$state\E/ );
874  
875 }
876  
877 return sort { $a <=> $b } @matched_ports;
878  
879 }
880  
881 sub _get_port_state {
882 my $self = shift;
883 my $proto = pop; #portid might be empty, so this goes first
884 my $portid = lc(shift);
885  
886 return undef unless ( exists $self->{ports}{$proto}{$portid} );
887 return $self->{ports}{$proto}{$portid}{state};
888  
889 }
890  
891 sub _get_port_state_ttl {
892 my $self = shift;
893 my $proto = pop;
894 my $portid = lc(shift);
895  
896 return undef unless ( exists $self->{ports}{$proto}{$portid} );
897 return $self->{ports}{$proto}{$portid}{reason_ttl};
898 }
899  
900 #changed this to use _get_ports since it was similar code
901 sub tcp_ports { return _get_ports( @_, 'tcp' ); }
902 sub udp_ports { return _get_ports( @_, 'udp' ); }
903  
904 sub tcp_port_count { return $_[0]->{ports}{tcp_port_count}; }
905 sub udp_port_count { return $_[0]->{ports}{udp_port_count}; }
906  
907 sub tcp_port_state_ttl { return _get_port_state_ttl( @_, 'tcp' ); }
908  
909 sub tcp_port_state { return _get_port_state( @_, 'tcp' ); }
910 sub udp_port_state { return _get_port_state( @_, 'udp' ); }
911  
912 sub tcp_del_ports { return _del_port( @_, 'tcp' ); }
913 sub udp_del_ports { return _del_port( @_, 'udp' ); }
914  
915 sub tcp_service {
916 my $self = shift;
917 my $portid = shift;
918 if ( $portid eq '' ) {
919 warn "[Nmap-Parser] No port number passed to tcp_service()\n";
920 return undef;
921 }
922 return Nmap::Parser::Host::Service->new(
923 $self->{ports}{tcp}{$portid}{service} );
924 }
925  
926 sub udp_service {
927 my $self = shift;
928 my $portid = shift;
929 if ( $portid eq '' ) {
930 warn "[Nmap-Parser] No port number passed to udp_service()\n";
931 return undef;
932 }
933 return Nmap::Parser::Host::Service->new(
934 $self->{ports}{udp}{$portid}{service} );
935  
936 }
937  
938 #usually the first one is the highest accuracy
939  
940 sub os_sig { return Nmap::Parser::Host::OS->new( $_[0]->{os} ); }
941  
942 #Support for:
943 #tcpsequence_class, tcpsequence_values, tcpsequence_index,
944 #ipidsequence_class, ipidsequence_values, tcptssequence_values,
945 #tcptssequence_class, uptime_seconds, uptime_lastboot
946 #tcp_open_ports, udp_open_ports, tcp_filtered_ports, udp_filtered_ports,
947 #tcp_closed_ports, udp_closed_ports
948 sub AUTOLOAD {
949 ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
950 return if ( $param eq 'DESTROY' );
951 my ( $type, $val ) = split /_/, lc($param);
952  
953 #splits the given method name by '_'. This will determine the function and param
954 no strict 'refs';
955  
956 if ( ( $type eq 'tcp' || $type eq 'udp' )
957 && ( $val eq 'open' || $val eq 'filtered' || $val eq 'closed' ) )
958 {
959  
960 #they must be looking for port info: tcp or udp. The $val is either open|filtered|closed
961 *$AUTOLOAD = sub { return _get_ports( $_[0], $val, $type ); };
962 goto &$AUTOLOAD;
963  
964 }
965 elsif ( defined $type && defined $val ) {
966  
967 #must be one of the 'sequence' functions asking for class/values/index
968 *$AUTOLOAD = sub { return $_[0]->{$type}{$val} };
969 goto &$AUTOLOAD;
970 }
971 else { die '[Nmap-Parser] method ->' . $param . "() not defined!\n"; }
972 }
973  
974 #/*****************************************************************************/
975 # NMAP::PARSER::HOST::SERVICE
976 #/*****************************************************************************/
977  
978 package Nmap::Parser::Host::Service;
979 use vars qw($AUTOLOAD);
980  
981 sub new {
982 my $class = shift;
983 $class = ref($class) || $class;
984 my $self = shift || {};
985 bless( $self, $class );
986 return $self;
987 }
988  
989 sub scripts {
990 my $self = shift;
991 my $id = shift;
992  
993 unless ( defined $id ) {
994 return sort keys %{ $self->{script} };
995 }
996 else {
997 return $self->{script}{$id};
998 }
999 }
1000  
1001 #Support for:
1002 #name port proto rpcnum owner version product extrainfo tunnel method confidence
1003 #this will now only load functions that will be used. This saves
1004 #on delay (increase speed) and memory
1005  
1006 sub AUTOLOAD {
1007 ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
1008 return if ( $param eq 'DESTROY' );
1009 no strict 'refs';
1010  
1011 *$AUTOLOAD = sub { return $_[0]->{ lc $param } };
1012 goto &$AUTOLOAD;
1013 }
1014  
1015 #/*****************************************************************************/
1016 # NMAP::PARSER::HOST::OS
1017 #/*****************************************************************************/
1018  
1019 package Nmap::Parser::Host::OS;
1020 use vars qw($AUTOLOAD);
1021  
1022 sub new {
1023 my $class = shift;
1024 $class = ref($class) || $class;
1025 my $self = shift || {};
1026 bless( $self, $class );
1027 return $self;
1028 }
1029  
1030 sub portused_open { return $_[0]->{portused}{open}; }
1031 sub portused_closed { return $_[0]->{portused}{closed}; }
1032 sub os_fingerprint { return $_[0]->{os_fingerprint}; }
1033  
1034 sub name_count { return $_[0]->{osmatch_count}; }
1035  
1036 sub all_names {
1037 my $self = shift;
1038 @_ = ();
1039 if ( $self->{osclass_count} < 1 ) { return @_; }
1040 if ( ref( $self->{osmatch_name} ) eq 'ARRAY' ) {
1041 return sort @{ $self->{osmatch_name} };
1042 }
1043  
1044 } #given by decreasing accuracy
1045  
1046 sub class_count { return $_[0]->{osclass_count}; }
1047  
1048 #Support for:
1049 #name,names, name_accuracy, osfamily, vendor, type, osgen, class_accuracy
1050 sub AUTOLOAD {
1051 ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
1052 return if ( $param eq 'DESTROY' );
1053 no strict 'refs';
1054 $param = lc($param);
1055  
1056 $param = 'name' if ( $param eq 'names' );
1057 if ( $param eq 'name' || $param eq 'name_accuracy' ) {
1058  
1059 *$AUTOLOAD = sub { _get_info( $_[0], $_[1], $param, 'osmatch' ); };
1060 goto &$AUTOLOAD;
1061 }
1062 else {
1063  
1064 *$AUTOLOAD = sub { _get_info( $_[0], $_[1], $param, 'osclass' ); };
1065 goto &$AUTOLOAD;
1066 }
1067 }
1068  
1069 sub _get_info {
1070 my ( $self, $index, $param, $type ) = @_;
1071 $index ||= 0;
1072  
1073 #type is either osclass or osmatch
1074 if ( $index >= $self->{ $type . '_count' } ) {
1075 $index = $self->{ $type . '_count' } - 1;
1076 }
1077 return $self->{ $type . '_' . $param }[$index];
1078 }
1079  
1080 #/*****************************************************************************/
1081 # NMAP::PARSER::HOST::TRACEHOP
1082 #/*****************************************************************************/
1083  
1084 package Nmap::Parser::Host::TraceHop;
1085 use vars qw($AUTOLOAD);
1086  
1087 sub new {
1088 my $class = shift;
1089 $class = ref($class) || $class;
1090 my $self = shift || {};
1091 bless( $self, $class );
1092 return $self;
1093 }
1094  
1095 sub AUTOLOAD {
1096 ( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
1097 return if ( $param eq 'DESTROY' );
1098 no strict 'refs';
1099 $param = lc($param);
1100  
1101 # Supported accessors:
1102 my %subs;
1103 @subs{ qw( ttl rtt ipaddr host ) } = 1;
1104  
1105 if ( exists $subs{$param} ) {
1106  
1107 *$AUTOLOAD = sub { $_[0]->{$param} };
1108 goto &$AUTOLOAD;
1109 }
1110 else { die '[Nmap-Parser] method ->' . $param . "() not defined!\n"; }
1111 }
1112  
1113 1;
1114  
1115 __END__
↑ Summary