-
Notifications
You must be signed in to change notification settings - Fork 1
/
nmapxml2openports.pl
executable file
·165 lines (143 loc) · 4.48 KB
/
nmapxml2openports.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
#!/usr/bin/perl
# (C) Kost. Distributed under GPL.
use strict;
use XML::Simple;
use HTML::Template;
use Data::Dumper;
if ($#ARGV<0) {
print STDERR "NMAP open port sum (C) Kost. Distributed under GPL.\n\n";
print STDERR "Usage: $0 <nmap-file.xml> ...\n";
print STDERR "Example: $0 nmap-host1.xml nmap-host2.xml\n";
exit 0;
}
my $totalhosts=0;
my $totalup=0;
my $totalopen=0;
my $totalclosed=0;
my $totalfiltered=0;
my $totalother=0;
my $totalports=0;
my $begport=0;
my $endport=65535;
my $notscanned="Not scanned";
my %openports;
my @ohost;
while (my $nmapfile = shift) {
print STDERR "Processing $nmapfile...\n";
my $nmapxml;
eval {
$nmapxml = XMLin($nmapfile, ForceArray => 1, KeyAttr => '');
} or die ("Check your XML file $nmapfile! Error parsing XML file: $!");
my @doneports;
if ({$nmapxml->{'scaninfo'}->[0]}) {
my $scannedports=$nmapxml->{'scaninfo'}->[0]->{'services'};
foreach my $entry (split(",",$scannedports)) {
if ($entry =~ /-/) {
my ($beg,$end) = split("-",$entry);
for (my $i=$beg; $i<=$end; $i++) {
push @doneports,$i;
}
} else {
push @doneports, $entry;
}
}
} # if my $scaninfo ....
# foreach my $doneport (@doneports) { print STDERR "=$doneport\n"; }
foreach my $host (@{$nmapxml->{'host'}}) {
my %hostinfo;
my %hostports;
$hostinfo{'addr'} = $host->{'address'}->[0]->{'addr'};
# $totalhosts++;
# $totalup++ if ($host->{'status'}->[0]->{'state'} eq "up");
$hostinfo{'hostname'} = $host->{'hostnames'}->[0]->{'hostname'}->[0]->{'name'};
if (($host->{'os'}->[0]->{'osclass'}) || ($host->{'os'}->[0]->{'osmatch'})) {
$hostinfo{'type'}=$host->{'os'}->[0]->{'osclass'}->[0]->{'type'};
$hostinfo{'name'}=$host->{'os'}->[0]->{'osmatch'}->[0]->{'name'};
$hostinfo{'acc'}=$host->{'os'}->[0]->{'osmatch'}->[0]->{'accuracy'};
}
my @oports;
# print Dumper (@{$host->{'ports'}->[0]->{'port'}});
if (@{$host->{'ports'}->[0]->{'port'}}) {
foreach my $port (@{$host->{'ports'}->[0]->{'port'}}) {
my ($fstate,$fstateo,$fstatec,$fstatef,$fstatea);
$fstate=$port->{'state'}->[0]->{'state'};
my $portnr=$port->{'portid'};
$hostports{$portnr}=$fstate;
if ($fstate eq "open") {
$fstateo="1";
$totalopen++;
$openports{$portnr}++;
} elsif ($fstate eq "closed") {
$fstatec="1";
$totalclosed++;
} elsif ($fstate eq "filtered") {
$fstatef="1";
$totalfiltered++;
} else {
$totalother++;
$fstatea=$fstate;
}
my %portinfo=(
'protocol' => $port->{'protocol'},
'portid' => $port->{'portid'},
'state' => $fstate,
'open' => $fstateo,
'closed' => $fstatec,
'filtered' => $fstatef,
'other' => $fstatea,
'name' => $port->{'service'}->[0]->{'name'},
'product' => $port->{'service'}->[0]->{'product'},
'conf' => $port->{'service'}->[0]->{'conf'}
);
push @oports, \%portinfo;
$hostinfo{'fportsopen'}="Y";
} # foreach
} # not empty
$hostinfo{'ports'} = \@oports;
my @extraports;
if ((@{$host->{'ports'}->[0]->{'extraports'}})) {
foreach my $port (@{$host->{'ports'}->[0]->{'extraports'}}) {
my ($fstateo,$fstatec,$fstatef,$fstatea);
foreach my $doneport (@doneports) {
# print STDERR $doneport."-\n";
if ($hostports{$doneport} eq $notscanned) {
$hostports{$doneport} = $port->{'state'};
}
}
if ($port->{'state'} eq "open") {
$fstateo="1";
} elsif ($port->{'state'} eq "closed") {
$fstatec="1";
} elsif ($port->{'state'} eq "filtered") {
$fstatef="1";
} else {
$fstatea=$port->{'state'};
}
my %extraport = (
'count' => $port->{'count'},
'state' => $port->{'state'},
'open' => $fstateo,
'closed' => $fstatec,
'filtered' => $fstatef,
'other' => $fstatea
);
$totalopen=$totalopen+$port->{'count'}*$fstateo;
$totalclosed=$totalclosed+$port->{'count'}*$fstatec;
$totalfiltered=$totalfiltered+$port->{'count'}*$fstatef;
$totalother=$totalother+$port->{'count'}*$fstatea;
push @extraports, \%extraport;
}
$hostinfo{'fextraports'}="Y";
} # if (@{$...
$hostinfo{'extraports'}=\@extraports;
push @ohost, \%hostinfo;
}
$totalhosts=$totalhosts+$nmapxml->{'runstats'}->[0]->{'hosts'}->[0]->{'total'};
$totalup=$totalup+$nmapxml->{'runstats'}->[0]->{'hosts'}->[0]->{'up'};
}
while (my ($portnum, $count) = each(%openports)) {
print $portnum.";".$count."\n";
}
$totalports=$totalopen+$totalclosed+$totalfiltered+$totalother;
print STDERR "Total host: $totalup up from $totalhosts hosts scanned\n";
print STDERR "Total ports:\n Open: $totalopen\n Closed: $totalclosed\n Filtered: $totalfiltered\n Other:$totalother\n(Total: $totalports ports scanned)\n";