-
Notifications
You must be signed in to change notification settings - Fork 17
/
Parser.pm
1959 lines (1412 loc) · 57 KB
/
Parser.pm
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
package Nmap::Parser;
use strict;
use XML::Twig;
our $VERSION = 1.37;
sub new {
my ( $class, $self ) = shift;
$class = ref($class) || $class;
$self->{twig} = new XML::Twig(
start_tag_handlers => { nmaprun => sub {$self->_nmaprun_start_tag_hdlr(@_)} },
twig_roots => {
scaninfo => sub {$self->_scaninfo_tag_hdlr(@_)},
prescript => sub {$self->_prescript_tag_hdlr(@_)},
postscript => sub {$self->_postscript_tag_hdlr(@_)},
finished => sub {$self->_finished_tag_hdlr(@_)},
host => sub {$self->_host_tag_hdlr(@_)},
},
ignore_elts => {
addport => 1,
debugging => 1,
verbose => 1,
hosts => 1,
taskbegin => 1,
taskend => 1,
taskprogress => 1
}
);
bless( $self, $class );
return $self;
}
#/*****************************************************************************/
# NMAP::PARSER OBJECT METHODS
#/*****************************************************************************/
#Safe parse and parsefile will return $@ which will contain the error
#that occured if the parsing failed (it might be empty when no error occurred)
sub callback {
my $self = shift;
my $callback = shift; #first arg is CODE
if ( ref($callback) eq 'CODE' ) {
$self->{callback}{coderef} = $callback;
$self->{callback}{is_registered} = 1;
}
else {
$self->{callback}{is_registered} = 0;
}
#returns if a callback is registered or not
return $self->{callback}{is_registered};
}
sub _parse {
my $type = shift;
my $self = shift;
$self->{HOSTS} = undef;
$self->{SESSION} = undef;
{
file => sub { $self->{twig}->safe_parsefile(@_); },
string => sub { $self->{twig}->safe_parse(@_); },
}->{$type}->(@_);
if ($@) { die $@; }
$self->purge;
return $self;
}
sub parse {
return _parse('string', @_);
}
sub parsefile {
return _parse('file', @_);
}
sub parsescan {
my $self = shift;
my $nmap = shift;
my $args = shift;
my @ips = @_;
my $FH;
if ( $args =~ /-o(?:X|N|G)/ ) {
die
"[Nmap-Parser] Cannot pass option '-oX', '-oN' or '-oG' to parsecan()";
}
my $cmd;
#if output file is defined, point it to a localfile then call parsefile instead.
if ( defined( $self->{cache_file} ) ) {
$cmd =
"$nmap $args -v -v -v -oX "
. $self->{cache_file} . " "
. ( join ' ', @ips );
`$cmd`; #remove output from STDOUT
$self->parsefile( $self->{cache_file} );
}
else {
$cmd = "$nmap $args -v -v -v -oX - " . ( join ' ', @ips );
open $FH,
"$cmd |" || die "[Nmap-Parser] Could not perform nmap scan - $!";
$self->parse($FH);
close $FH;
}
return $self;
}
sub cache_scan {
my $self = shift;
$self->{cache_file} = shift || 'nmap-parser-cache.' . time() . '.xml';
}
sub purge {
my $self = shift;
$self->{twig}->purge;
return $self;
}
sub addr_sort {
my $self = shift if ref $_[0];
return (
map { unpack("x16A*", $_) }
sort { $a cmp $b }
map {
my @vals;
if( /:/ ) { #IPv6
@vals = split /:/;
@vals = map { $_ eq '' ? (0) x (8-$#vals) : hex } @vals
} else { #IPv4
my @v4 = split /\./;
# Sort as IPv4-mapped IPv6, per RFC 4291 Section 2.5.5.2
@vals = ( (0) x 5, 0xffff, map { 256*$v4[$_] + $v4[$_+1] } (0,2) );
}
pack("n8A*", @vals, $_)
} @_
);
}
#MAIN SCAN INFORMATION
sub get_session {
my $self = shift;
my $obj = Nmap::Parser::Session->new( $self->{SESSION} );
return $obj;
}
#HOST STUFF
sub get_host {
my ( $self, $ip ) = (@_);
if ( $ip eq '' ) {
warn "[Nmap-Parser] No IP address given to get_host()\n";
return undef;
}
$self->{HOSTS}{$ip};
}
sub del_host {
my ( $self, $ip ) = (@_);
if ( $ip eq '' ) {
warn "[Nmap-Parser] No IP address given to del_host()\n";
return undef;
}
delete $self->{HOSTS}{$ip};
}
sub all_hosts {
my $self = shift;
my $status = shift || '';
return ( values %{ $self->{HOSTS} } ) if ( $status eq '' );
my @hosts = grep { $_->{status} eq $status } ( values %{ $self->{HOSTS} } );
return @hosts;
}
sub get_ips {
my $self = shift;
my $status = shift || '';
return $self->addr_sort( keys %{ $self->{HOSTS} } ) if ( $status eq '' );
my @hosts =
grep { $self->{HOSTS}{$_}{status} eq $status }
( keys %{ $self->{HOSTS} } );
return $self->addr_sort(@hosts);
}
#/*****************************************************************************/
# PARSING TAG HANDLERS FOR XML::TWIG
#/*****************************************************************************/
sub _nmaprun_start_tag_hdlr {
my ($self, $twig, $tag ) = @_;
$self->{SESSION}{start_time} = $tag->{att}->{start};
$self->{SESSION}{nmap_version} = $tag->{att}->{version};
$self->{SESSION}{start_str} = $tag->{att}->{startstr};
$self->{SESSION}{xml_version} = $tag->{att}->{xmloutputversion};
$self->{SESSION}{scan_args} = $tag->{att}->{args};
$self->{SESSION} = Nmap::Parser::Session->new( $self->{SESSION} );
$twig->purge;
}
sub _scaninfo_tag_hdlr {
my ( $self, $twig, $tag ) = @_;
my $type = $tag->{att}->{type};
my $proto = $tag->{att}->{protocol};
my $numservices = $tag->{att}->{numservices};
if ( defined($type) ) { #there can be more than one type in one scan
$self->{SESSION}{type}{$type} = $proto;
$self->{SESSION}{numservices}{$type} = $numservices;
}
$twig->purge;
}
sub _prescript_tag_hdlr {
my ($self, $twig, $tag ) = @_;
my $scripts_hashref;
for my $script ( $tag->children('script') ) {
$scripts_hashref->{ $script->{att}->{id} } =
__script_tag_hdlr( $script );
}
$self->{SESSION}{prescript} = $scripts_hashref;
$twig->purge;
}
sub _postscript_tag_hdlr {
my ($self, $twig, $tag ) = @_;
my $scripts_hashref;
for my $script ( $tag->children('script') ) {
$scripts_hashref->{ $script->{att}->{id} } =
__script_tag_hdlr( $script );
}
$self->{SESSION}{postscript} = $scripts_hashref;
$twig->purge;
}
sub _finished_tag_hdlr {
my ($self, $twig, $tag ) = @_;
$self->{SESSION}{finish_time} = $tag->{att}->{time};
$self->{SESSION}{time_str} = $tag->{att}->{timestr};
$twig->purge;
}
#parses all the host information in one swoop (calling __host_*_tag_hdlrs)
sub _host_tag_hdlr {
my ($self, $twig, $tag ) = @_;
my $id = undef;
return undef unless ( defined $tag );
#GET ADDRESS INFO
my $addr_hashref;
$addr_hashref = __host_addr_tag_hdlr($tag);
#use this as the identifier
$id =
$addr_hashref->{ipv4}
|| $addr_hashref->{ipv6}
|| $addr_hashref->{mac}; #worstcase use MAC
$self->{HOSTS}{$id}{addrs} = $addr_hashref;
return undef unless ( defined($id) || $id ne '' );
#GET HOSTNAMES
$self->{HOSTS}{$id}{hostnames} = __host_hostnames_tag_hdlr($tag);
#GET STARTTIME
$self->{HOSTS}{$id}{starttime} = $tag->{att}->{starttime};
#GET STATUS
$self->{HOSTS}{$id}{status} = $tag->first_child('status')->{att}->{state};
#CONTINUE PROCESSING IF STATUS IS UP - OTHERWISE NO MORE XML
if ( lc( $self->{HOSTS}{$id}{status} ) eq 'up' ) {
$self->{HOSTS}{$id}{ports} = __host_port_tag_hdlr($tag);
$self->{HOSTS}{$id}{os} = __host_os_tag_hdlr($tag);
$self->{HOSTS}{$id}{uptime} = __host_uptime_tag_hdlr($tag);
$self->{HOSTS}{$id}{tcpsequence} = __host_tcpsequence_tag_hdlr($tag);
$self->{HOSTS}{$id}{ipidsequence} = __host_ipidsequence_tag_hdlr($tag);
$self->{HOSTS}{$id}{tcptssequence} = __host_tcptssequence_tag_hdlr($tag);
$self->{HOSTS}{$id}{hostscript} = __host_hostscript_tag_hdlr($tag);
$self->{HOSTS}{$id}{distance} =
__host_distance_tag_hdlr($tag); #returns simple value
$self->{HOSTS}{$id}{trace} = __host_trace_tag_hdlr($tag);
$self->{HOSTS}{$id}{trace_error} = __host_trace_error_tag_hdlr($tag);
}
#CREATE HOST OBJECT FOR USER
$self->{HOSTS}{$id} = Nmap::Parser::Host->new( $self->{HOSTS}{$id} );
if ( $self->{callback}{is_registered} ) {
&{ $self->{callback}{coderef} }( $self->{HOSTS}{$id} );
delete $self->{HOSTS}{$id};
}
$twig->purge;
}
sub __host_addr_tag_hdlr {
my $tag = shift;
my $addr_hashref;
#children() will return all children with tag name address
for my $addr ( $tag->children('address') ) {
if ( lc( $addr->{att}->{addrtype} ) eq 'mac' ) {
#we'll assume for now, only 1 MAC address per system
$addr_hashref->{mac}{addr} = $addr->{att}->{addr};
$addr_hashref->{mac}{vendor} = $addr->{att}->{vendor};
}
elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv4' ) {
$addr_hashref->{ipv4} = $addr->{att}->{addr};
} #support for ipv6? we'll see
elsif ( lc( $addr->{att}->{addrtype} ) eq 'ipv6' ) {
$addr_hashref->{ipv6} = $addr->{att}->{addr};
}
}
return $addr_hashref;
}
sub __host_hostnames_tag_hdlr {
my $tag = shift;
my $hostnames_tag = $tag->first_child('hostnames');
return undef unless ( defined $hostnames_tag );
my @hostnames;
for my $name ( $hostnames_tag->children('hostname') ) {
push @hostnames, $name->{att}->{name};
}
return \@hostnames;
}
sub __host_port_tag_hdlr {
my $tag = shift;
my ( $port_hashref, $ports_tag );
$ports_tag = $tag->first_child('ports');
return undef unless ( defined $ports_tag );
#Parsing Extraports
my $extraports_tag = $ports_tag->first_child('extraports');
if ( defined $extraports_tag && $extraports_tag ne '' ) {
$port_hashref->{extraports}{state} = $extraports_tag->{att}->{state};
$port_hashref->{extraports}{count} = $extraports_tag->{att}->{count};
}
#Parsing regular port information
my ( $tcp_port_count, $udp_port_count ) = ( 0, 0 );
for my $port_tag ( $ports_tag->children('port') ) {
my $proto = $port_tag->{att}->{protocol};
my $portid = $port_tag->{att}->{portid};
my $state = $port_tag->first_child('state');
my $owner = $port_tag->first_child('owner') || undef;
$tcp_port_count++ if ( $proto eq 'tcp' );
$udp_port_count++ if ( $proto eq 'udp' );
$port_hashref->{$proto}{$portid}{state} = $state->{att}->{state}
|| 'unknown'
if ( $state ne '' );
$port_hashref->{$proto}{$portid}{reason_ttl} = $state->{att}->{reason_ttl}
|| 'unknown'
if($state ne '');
#GET SERVICE INFORMATION
$port_hashref->{$proto}{$portid}{service} =
__host_service_tag_hdlr( $port_tag, $portid )
if ( defined($proto) && defined($portid) );
#GET SCRIPT INFORMATION
$port_hashref->{$proto}{$portid}{service}{script} =
__host_script_tag_hdlr( $port_tag, $portid)
if ( defined($proto) && defined($portid) );
#GET OWNER INFORMATION
$port_hashref->{$proto}{$portid}{service}{owner} = $owner->{att}->{name}
if ( defined($owner) );
#These are added at the end, otherwise __host_service_tag_hdlr will overwrite
#GET PORT STATE
}
$port_hashref->{tcp_port_count} = $tcp_port_count;
$port_hashref->{udp_port_count} = $udp_port_count;
return $port_hashref;
}
sub __host_service_tag_hdlr {
my $tag = shift;
my $portid = shift; #need a way to remember what port this service runs on
my $service = $tag->first_child('service[@name]');
my $service_hashref;
$service_hashref->{port} = $portid;
if ( defined $service ) {
$service_hashref->{name} = $service->{att}->{name} || 'unknown';
$service_hashref->{version} = $service->{att}->{version};
$service_hashref->{product} = $service->{att}->{product};
$service_hashref->{devicetype} = $service->{att}->{devicetype};
$service_hashref->{extrainfo} = $service->{att}->{extrainfo};
$service_hashref->{proto} =
$service->{att}->{proto}
|| $service->{att}->{protocol}
|| 'unknown';
$service_hashref->{rpcnum} = $service->{att}->{rpcnum};
$service_hashref->{tunnel} = $service->{att}->{tunnel};
$service_hashref->{method} = $service->{att}->{method};
$service_hashref->{confidence} = $service->{att}->{conf};
$service_hashref->{fingerprint} = $service->{att}->{servicefp};
}
return $service_hashref;
}
sub __host_script_tag_hdlr {
my $tag = shift;
my $script_hashref;
for ( $tag->children('script') ) {
$script_hashref->{ $_->{att}->{id} } =
__script_tag_hdlr($_);
}
return $script_hashref;
}
sub __host_os_tag_hdlr {
my $tag = shift;
my $os_tag = $tag->first_child('os');
my $os_hashref;
my $portused_tag;
my $os_fingerprint;
if ( defined $os_tag ) {
#get the open port used to match os
$portused_tag = $os_tag->first_child("portused[\@state='open']");
$os_hashref->{portused}{open} = $portused_tag->{att}->{portid}
if ( defined $portused_tag );
#get the closed port used to match os
$portused_tag = $os_tag->first_child("portused[\@state='closed']");
$os_hashref->{portused}{closed} = $portused_tag->{att}->{portid}
if ( defined $portused_tag );
#os fingerprint
$os_fingerprint = $os_tag->first_child("osfingerprint");
$os_hashref->{os_fingerprint} =
$os_fingerprint->{'att'}->{'fingerprint'}
if ( defined $os_fingerprint );
#This will go in Nmap::Parser::Host::OS
my $osmatch_index = 0;
my $osclass_index = 0;
for my $osmatch ( $os_tag->children('osmatch') ) {
$os_hashref->{osmatch_name}[$osmatch_index] =
$osmatch->{att}->{name};
$os_hashref->{osmatch_name_accuracy}[$osmatch_index] =
$osmatch->{att}->{accuracy};
$osmatch_index++;
for my $osclass ( $osmatch->children('osclass') ) {
$os_hashref->{osclass_osfamily}[$osclass_index] =
$osclass->{att}->{osfamily};
$os_hashref->{osclass_osgen}[$osclass_index] =
$osclass->{att}->{osgen};
$os_hashref->{osclass_vendor}[$osclass_index] =
$osclass->{att}->{vendor};
$os_hashref->{osclass_type}[$osclass_index] =
$osclass->{att}->{type};
$os_hashref->{osclass_class_accuracy}[$osclass_index] =
$osclass->{att}->{accuracy};
$osclass_index++;
}
}
$os_hashref->{'osmatch_count'} = $osmatch_index;
#parse osclass tags
for my $osclass ( $os_tag->children('osclass') ) {
$os_hashref->{osclass_osfamily}[$osclass_index] =
$osclass->{att}->{osfamily};
$os_hashref->{osclass_osgen}[$osclass_index] =
$osclass->{att}->{osgen};
$os_hashref->{osclass_vendor}[$osclass_index] =
$osclass->{att}->{vendor};
$os_hashref->{osclass_type}[$osclass_index] =
$osclass->{att}->{type};
$os_hashref->{osclass_class_accuracy}[$osclass_index] =
$osclass->{att}->{accuracy};
$osclass_index++;
}
$os_hashref->{'osclass_count'} = $osclass_index;
}
return $os_hashref;
}
sub __host_uptime_tag_hdlr {
my $tag = shift;
my $uptime = $tag->first_child('uptime');
my $uptime_hashref;
if ( defined $uptime ) {
$uptime_hashref->{seconds} = $uptime->{att}->{seconds};
$uptime_hashref->{lastboot} = $uptime->{att}->{lastboot};
}
return $uptime_hashref;
}
sub __host_tcpsequence_tag_hdlr {
my $tag = shift;
my $sequence = $tag->first_child('tcpsequence');
my $sequence_hashref;
return undef unless ($sequence);
$sequence_hashref->{class} = $sequence->{att}->{class};
$sequence_hashref->{difficulty} = $sequence->{att}->{difficulty};
$sequence_hashref->{values} = $sequence->{att}->{values};
$sequence_hashref->{index} = $sequence->{att}->{index};
return $sequence_hashref;
}
sub __host_ipidsequence_tag_hdlr {
my $tag = shift;
my $sequence = $tag->first_child('ipidsequence');
my $sequence_hashref;
return undef unless ($sequence);
$sequence_hashref->{class} = $sequence->{att}->{class};
$sequence_hashref->{values} = $sequence->{att}->{values};
return $sequence_hashref;
}
sub __host_tcptssequence_tag_hdlr {
my $tag = shift;
my $sequence = $tag->first_child('tcptssequence');
my $sequence_hashref;
return undef unless ($sequence);
$sequence_hashref->{class} = $sequence->{att}->{class};
$sequence_hashref->{values} = $sequence->{att}->{values};
return $sequence_hashref;
}
sub __host_hostscript_tag_hdlr {
my $tag = shift;
my $scripts = $tag->first_child('hostscript');
my $scripts_hashref;
return undef unless ($scripts);
for my $script ( $scripts->children('script') ) {
$scripts_hashref->{ $script->{att}->{id} } =
__script_tag_hdlr( $script );
}
return $scripts_hashref;
}
sub __host_distance_tag_hdlr {
my $tag = shift;
my $distance = $tag->first_child('distance');
return undef unless ($distance);
return $distance->{att}->{value};
}
sub __host_trace_tag_hdlr {
my $tag = shift;
my $trace_tag = $tag->first_child('trace');
my $trace_hashref = { hops => [], };
if ( defined $trace_tag ) {
my $proto = $trace_tag->{att}->{proto};
$trace_hashref->{proto} = $proto if defined $proto;
my $port = $trace_tag->{att}->{port};
$trace_hashref->{port} = $port if defined $port;
for my $hop_tag ( $trace_tag->children('hop') ) {
# Copy the known hop attributes, they will go in
# Nmap::Parser::Host::TraceHop
my %hop_data;
$hop_data{$_} = $hop_tag->{att}->{$_} for qw( ttl rtt ipaddr host );
delete $hop_data{rtt} if $hop_data{rtt} !~ /^[\d.]+$/;
push @{ $trace_hashref->{hops} }, \%hop_data;
}
}
return $trace_hashref;
}
sub __host_trace_error_tag_hdlr {
my $tag = shift;
my $trace_tag = $tag->first_child('trace');
if ( defined $trace_tag ) {
my $error_tag = $trace_tag->first_child('error');
if ( defined $error_tag ) {
# If an error happens, always provide a true value even if
# it doesn't contains a useful string
my $errorstr = $error_tag->{att}->{errorstr} || 1;
return $errorstr;
}
}
return;
}
sub __script_tag_hdlr {
my $tag = shift;
my $script_hashref = {
output => $tag->{att}->{output}
};
chomp %$script_hashref;
if ( not $tag->is_empty()) {
$script_hashref->{contents} = __script_table($tag);
}
return $script_hashref;
}
sub __script_table {
my $tag = shift;
my ($ref, $subref);
my $fc = $tag->first_child();
if ($fc) {
if ($fc->is_text) {
$ref = $fc->text;
}
else {
if ($fc->{att}->{key}) {
$ref = {};
$subref = sub {
$ref->{$_->{att}->{key}} = shift;
};
}
else {
$ref = [];
$subref = sub {
push @$ref, shift;
};
}
for ($tag->children()) {
if ($_->tag() eq "table") {
$subref->(__script_table( $_ ));
}
else {
$subref->($_->text);
}
}
}
}
return $ref
}
#/*****************************************************************************/
# NMAP::PARSER::SESSION
#/*****************************************************************************/
package Nmap::Parser::Session;
use vars qw($AUTOLOAD);
sub new {
my $class = shift;
$class = ref($class) || $class;
my $self = shift || {};
bless( $self, $class );
return $self;
}
#Support for:
#start_time, start_str, finish_time, time_str, nmap_version, xml_version, scan_args
sub AUTOLOAD {
( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
return if ( $param eq 'DESTROY' );
no strict 'refs';
*$AUTOLOAD = sub { return $_[0]->{ lc $param } };
goto &$AUTOLOAD;
}
sub numservices {
my $self = shift;
my $type = shift
|| ''; #(syn|ack|bounce|connect|null|xmas|window|maimon|fin|udp|ipproto)
return unless ( ref( $self->{numservices} ) eq 'HASH' );
if ( $type ne '' ) { return $self->{numservices}{$type}; }
else {
my $total = 0;
for ( values %{ $self->{numservices} } ) { $total += $_; }
return $total;
} #(else) total number of services together
}
sub scan_types {
return sort { $a cmp $b } ( keys %{ $_[0]->{type} } )
if ( ref( $_[0]->{type} ) eq 'HASH' );
}
sub scan_type_proto { return $_[1] ? $_[0]->{type}{ $_[1] } : undef; }
sub prescripts {
my $self = shift;
my $id = shift;
unless ( defined $id ) {
return sort keys %{ $self->{prescript} };
}
else {
return $self->{prescript}{$id};
}
}
sub postscripts {
my $self = shift;
my $id = shift;
unless ( defined $id ) {
return sort keys %{ $self->{postscript} };
}
else {
return $self->{postscript}{$id};
}
}
#/*****************************************************************************/
# NMAP::PARSER::HOST
#/*****************************************************************************/
package Nmap::Parser::Host;
use vars qw($AUTOLOAD);
sub new {
my $class = shift;
$class = ref($class) || $class;
my $self = shift || {};
bless( $self, $class );
return $self;
}
sub starttime { return $_[0]->{starttime}; }
sub status { return $_[0]->{status}; }
sub addr {
my $default = $_[0]->{addrs}{ipv4} || $_[0]->{addrs}{ipv6};
return $default;
}
sub addrtype {
if ( $_[0]->{addrs}{ipv4} ) { return 'ipv4'; }
elsif ( $_[0]->{addrs}{ipv6} ) { return 'ipv6'; }
}
sub ipv4_addr { return $_[0]->{addrs}{ipv4}; }
sub ipv6_addr { return $_[0]->{addrs}{ipv6}; }
sub mac_addr { return $_[0]->{addrs}{mac}{addr}; }
sub mac_vendor { return $_[0]->{addrs}{mac}{vendor}; }
#returns the first hostname
sub hostname {
my $self = shift;
my $index = shift || 0;
if ( ref( $self->{hostnames} ) ne 'ARRAY' ) { return ''; }
if ( scalar @{ $self->{hostnames} } <= $index ) {
$index = scalar @{ $self->{hostnames} } - 1;
}
return $self->{hostnames}[$index] if ( scalar @{ $self->{hostnames} } );
}
sub all_hostnames { return @{ $_[0]->{hostnames} || [] }; }
sub extraports_state { return $_[0]->{ports}{extraports}{state}; }
sub extraports_count { return $_[0]->{ports}{extraports}{count}; }
sub distance { return $_[0]->{distance}; }
sub hostscripts {
my $self = shift;
my $id = shift;
unless ( defined $id ) {
return sort keys %{ $self->{hostscript} };
}
else {
return $self->{hostscript}{$id};
}
}
sub all_trace_hops {
my $self = shift;
return unless defined $self->{trace}->{hops};
return map { Nmap::Parser::Host::TraceHop->new( $_ ) }
@{ $self->{trace}->{hops} };
}
sub trace_port { return $_[0]->{trace}->{port} }
sub trace_proto { return $_[0]->{trace}->{proto} }
sub trace_error { return $_[0]->{trace_error} }
sub _del_port {
my $self = shift;
my $proto = pop; #portid might be empty, so this goes first
my @portids = @_;
@portids = grep { $_ + 0 } @portids;
unless ( scalar @portids ) {
warn "[Nmap-Parser] No port number given to del_port()\n";
return undef;
}
delete $self->{ports}{$proto}{$_} for (@portids);
}
sub _get_ports {
my $self = shift;
my $proto = pop; #param might be empty, so this goes first
my $state = shift; #open, filtered, closed or any combination
my @matched_ports = ();
#if $state is undef, then tcp_ports or udp_ports was called for all ports
#therefore, only return the keys of all ports found
if ( not defined $state ) {
return sort { $a <=> $b } ( keys %{ $self->{ports}{$proto} } );
}
else {
$state = lc($state)
}
#the port parameter can be set to either any of these also 'open|filtered'
#can count as 'open' and 'filetered'. Therefore I need to use a regex from now on
#if $param is empty, then all ports match.
for my $portid ( keys %{ $self->{ports}{$proto} } ) {
#escape metacharacters ('|', for example in: open|filtered)
#using \Q and \E
push( @matched_ports, $portid )
if ( $self->{ports}{$proto}{$portid}{state} =~ /\Q$state\E/ );
}
return sort { $a <=> $b } @matched_ports;
}
sub _get_port_state {
my $self = shift;
my $proto = pop; #portid might be empty, so this goes first
my $portid = lc(shift);
return undef unless ( exists $self->{ports}{$proto}{$portid} );
return $self->{ports}{$proto}{$portid}{state};
}
sub _get_port_state_ttl {
my $self = shift;
my $proto = pop;
my $portid = lc(shift);
return undef unless ( exists $self->{ports}{$proto}{$portid} );
return $self->{ports}{$proto}{$portid}{reason_ttl};
}
#changed this to use _get_ports since it was similar code
sub tcp_ports { return _get_ports( @_, 'tcp' ); }
sub udp_ports { return _get_ports( @_, 'udp' ); }
sub tcp_port_count { return $_[0]->{ports}{tcp_port_count}; }
sub udp_port_count { return $_[0]->{ports}{udp_port_count}; }
sub tcp_port_state_ttl { return _get_port_state_ttl( @_, 'tcp' ); }
sub tcp_port_state { return _get_port_state( @_, 'tcp' ); }
sub udp_port_state { return _get_port_state( @_, 'udp' ); }
sub tcp_del_ports { return _del_port( @_, 'tcp' ); }
sub udp_del_ports { return _del_port( @_, 'udp' ); }
sub tcp_service {
my $self = shift;
my $portid = shift;
if ( $portid eq '' ) {
warn "[Nmap-Parser] No port number passed to tcp_service()\n";
return undef;
}
return Nmap::Parser::Host::Service->new(
$self->{ports}{tcp}{$portid}{service} );
}
sub udp_service {
my $self = shift;
my $portid = shift;
if ( $portid eq '' ) {
warn "[Nmap-Parser] No port number passed to udp_service()\n";
return undef;
}
return Nmap::Parser::Host::Service->new(
$self->{ports}{udp}{$portid}{service} );
}
#usually the first one is the highest accuracy
sub os_sig { return Nmap::Parser::Host::OS->new( $_[0]->{os} ); }
#Support for:
#tcpsequence_class, tcpsequence_values, tcpsequence_index,
#ipidsequence_class, ipidsequence_values, tcptssequence_values,
#tcptssequence_class, uptime_seconds, uptime_lastboot
#tcp_open_ports, udp_open_ports, tcp_filtered_ports, udp_filtered_ports,
#tcp_closed_ports, udp_closed_ports
sub AUTOLOAD {
( my $param = $AUTOLOAD ) =~ s{.*::}{}xms;
return if ( $param eq 'DESTROY' );
my ( $type, $val ) = split /_/, lc($param);
#splits the given method name by '_'. This will determine the function and param
no strict 'refs';
if ( ( $type eq 'tcp' || $type eq 'udp' )
&& ( $val eq 'open' || $val eq 'filtered' || $val eq 'closed' ) )
{
#they must be looking for port info: tcp or udp. The $val is either open|filtered|closed
*$AUTOLOAD = sub { return _get_ports( $_[0], $val, $type ); };
goto &$AUTOLOAD;
}
elsif ( defined $type && defined $val ) {
#must be one of the 'sequence' functions asking for class/values/index
*$AUTOLOAD = sub { return $_[0]->{$type}{$val} };
goto &$AUTOLOAD;
}
else { die '[Nmap-Parser] method ->' . $param . "() not defined!\n"; }
}
#/*****************************************************************************/
# NMAP::PARSER::HOST::SERVICE
#/*****************************************************************************/
package Nmap::Parser::Host::Service;
use vars qw($AUTOLOAD);
sub new {
my $class = shift;
$class = ref($class) || $class;
my $self = shift || {};
bless( $self, $class );
return $self;
}
sub scripts {
my $self = shift;
my $id = shift;
unless ( defined $id ) {
return sort keys %{ $self->{script} };
}
else {
return $self->{script}{$id};
}
}