blib/lib/NetworkInfo/Discovery/Nmap.pm

filter: Statementstmt 62.5% 65 / 104 Branchbran 46.6% 14 / 30 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 54.6% 94 / 172 CC 27 SCAR 45.0
CC
27
Cov
54%
do_it
191.4
__ANON__
4.5
new
4.1
CRAP 89.7
↑ Summary
1 package NetworkInfo::Discovery::Nmap;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Nmap::Scanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use NetworkInfo::Discovery::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶{ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 $VERSION = '0.02';
9 6 @ISA = qw(NetworkInfo::Discovery::Detect);
10 }
11  
12 =head1 NAME
13  
14 NetworkInfo::Discovery::Nmap - NetworkInfo::Discovery extension using Nmap
15  
16 =head1 VERSION
17  
18 Version 0.02
19  
20 =head1 SYNOPSIS
21  
22 use NetworkInfo::Discovery::Nmap;
23  
24 my $scanner = NetworkInfo::Discovery::Nmap->new(
25 hosts => [ qw( 192.168.0.0/24 192.168.1.0/24 ) ],
26 );
27 $scanner->do_it;
28  
29 See F<eg/nmapdisc.pl> for a more complete example.
30  
31 =head1 DESCRIPTION
32  
33 This module is an extension to C<NetworkInfo::Discovery> which uses the
34 Nmap utility to scan networks, find active hosts, their services and
35 operating system.
36  
37 =head1 METHODS
38  
39 =over 4
40  
41 =item new()
42  
43 Creates and returns a new C<NetworkInfo::Discovery::Nmap> object, which
44 derives from C<NetworkInfo::Discovery::Detect>.
45  
46 B<Options>
47  
48 =over 4
49  
50 =item *
51  
52 C<hosts> - expects a scalar or an arrayref of IP addresses in CIDR notation
53  
54 =item *
55  
56 C<ports> - expects a scalar or an arrayref with a port or a ports range
57  
58 =item *
59  
60 C<guess_system> - when enabled, tells nmap to use TCP/IP fingerprinting
61 to guess remote operating system (note: root privileges required).
62 Default is 0 (disabled).
63  
64 =back
65  
66 B<Example>
67  
68 # specify one host
69 my $scanner = new NetworkInfo::Discovery::Nmap hosts => '192.168.0.0/24';
70
71 # specify several hosts
72 my $scanner = new NetworkInfo::Discovery::Nmap hosts => [ qw(192.168.0.0/24) ];
73  
74 =cut
75  
76 8 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
77 8 my $class = shift;
78 8 my $self = $class->SUPER::new();
79 8 my %args = @_;
80
81 8 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
82 8 bless $self, $class;
83
84 # add private fields
85 8 $self->{_hosts_to_scan} = [];
86 8 $self->{_ports_to_scan} = [];
87
88 # treat given arguments
89 8 for my $attr (keys %args) {
90 5 ▶ $self->$attr($args{$attr}) if $self->can($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attr) 5 0 5
91 }
92
93 8 return $self
94 }
95  
96 =item do_it()
97  
98 Run the scan.
99  
100 =cut
101  
102 1 ▶sub do_it {
Subroutine do_it: called CC=17 SCAR=52.5
Pod documented
103 1 my $self = shift;
104 1 my @hosts = ();
105
106 # initialize the scanner
107 1 my $scanner = new Nmap::Scanner;
108 1 map { $scanner->add_target($_) } $self->hosts;
109 0 map { $scanner->add_scan_port($_) } $self->ports;
110 1 $scanner->no_ping;
111 1 $scanner->version_scan;
112 1 ▶ $scanner->guess_os if $self->{_nmap_options}{guess_system};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_nmap_options"}{"guess_system"} 0 1 1
113
114 # run the scan
115 1 my $results = $scanner->scan;
116
117 # add found information
118 0 my $list = $results->get_host_list;
119 0 ▶ while(my $host = $list->get_next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = $list->get_next) 0 0 0
120 0 ▶ next unless $host->status eq 'up';
Branch: 1 branch
BranchTrueFalseTotal
unless $host->status eq "up" 0 0 0
121 0 my %host_info = ( services => [] );
122
123 # get its address
124 0 my %addrs = map { $_->addr => $_->addrtype } $host->addresses;
125 0 for my $addr (keys %addrs) {
126 0 ▶ $host_info{ip} = $addr if $addrs{$addr} eq 'ipv4';
Branch: 1 branch
BranchTrueFalseTotal
if $addrs{$addr} eq "ipv4" 0 0 0
127 0 ▶ $host_info{mac} = $addr if $addrs{$addr} eq 'mac';
Branch: 1 branch
BranchTrueFalseTotal
if $addrs{$addr} eq "mac" 0 0 0
128 }
129 0 $host_info{smurf} = $host->smurf;
130
131 # get ports information
132 0 my $portlist = $host->get_port_list;
133 0 ▶ while(my $port = $portlist->get_next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = $portlist->get_next) 0 0 0
134 0 my %port_info = ();
135 0 $port_info{port} = $port->portid;
136 0 ▶ map { $port_info{$_} = $port->$_ || '' } qw(owner protocol state);
Condition0%0/2
$port->$_ || ''
l!l
00
MC/DC0%0/1
$port->$_ || ''
$port->$_
Truth table0%0/2
A
A= $port->$_
Aresult
11
00
137 0 ▶ $port_info{name} = $port->service->name || '';
Condition0%0/2
$port->service->name || ''
l!l
00
MC/DC0%0/1
$port->service->name || ''
$port->service->name
Truth table0%0/2
A
A= $port->service->name
Aresult
11
00
138 0 ▶ $port_info{rpcnum} = $port->service->rpcnum || '';
Condition0%0/2
$port->service->rpcnum || ''
l!l
00
MC/DC0%0/1
$port->service->rpcnum || ''
$port->service->rpcnum
Truth table0%0/2
A
A= $port->service->rpcnum
Aresult
11
00
139 0 $port_info{application} = {};
140 0 ▶ map { $port_info{application}{$_} = $port->service->$_ || '' } qw(product version extrainfo);
Condition0%0/2
$port->service->$_ || ''
l!l
00
MC/DC0%0/1
$port->service->$_ || ''
$port->service->$_
Truth table0%0/2
A
A= $port->service->$_
Aresult
11
00
141 0 push @{$host_info{services}}, { %port_info };
142 }
143
144 # get operating system information
145 0 ▶ if(my $guess_os = $host->os) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $guess_os = $host->os) 0 0 0
146 $host_info{system_info} = {
147 0 ▶ os_classes => [],
Condition0%0/4
$guess_os->uptime->seconds || 0
l!l
00
$guess_os->uptime->lastboot || ''
l!l
00
MC/DC0%0/1
$guess_os->uptime->seconds || 0
$guess_os->uptime->seconds
MC/DC0%0/1
$guess_os->uptime->lastboot || ''
$guess_os->uptime->lastboot
Truth table0%0/2
A
A= $guess_os->uptime->seconds
Aresult
11
00
Truth table0%0/2
A
A= $guess_os->uptime->lastboot
Aresult
11
00
148 os_matches => [],
149 uptime => $guess_os->uptime->seconds || 0,
150 last_boot => $guess_os->uptime->lastboot || '',
151 };
152
153 0 for my $os_class ($guess_os->osclasses) {
154 0 push @{$host_info{system_info}{os_classes}}, {
155 vendor => $os_class->vendor, type => $os_class->type,
156 family => $os_class->osfamily, version => $os_class->osgen,
157 accuracy => $os_class->accuracy
158 }
159 }
160
161 0 for my $os_match ($guess_os->osmatches) {
162 0 push @{$host_info{system_info}{os_matches}}, {
163 name => $os_match->name, accuracy => $os_match->accuracy
164 }
165 }
166 }
167
168 0 push @hosts, { %host_info };
169 }
170
171 # add found hosts
172 0 $self->add_interface(@hosts);
173
174 # return list of found hosts
175 0 return $self->get_interfaces
176 }
177  
178 =item hosts()
179  
180 Add hosts or networks to the scan list. Expects addresses in CIDR notation.
181 Return the current list of hosts when called with no argument.
182  
183 B<Examples>
184  
185 $scanner->hosts('192.168.4.53'); # add one host
186 $scanner->hosts('192.168.5.48/29'); # add a subnet
187 $scanner->hosts(qw(192.168.6.0/30 10.0.0.3/28)); # add two subnets
188  
189 =cut
190  
191 10 ▶sub hosts {
Subroutine hosts: called CC=4 SCAR=13.9
Pod documented
192 10 my $self = shift;
193 10 ▶ if(@_ ==0) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 0) { } 1 9 10
elsif (ref $_[0] eq 'ARRAY') { } 3 6 9
elsif (ref $_[0]) { } 4 2 6
194 1 return @{$self->{_hosts_to_scan}}
195 } elsif(ref $_[0] eq 'ARRAY') {
196 3 push @{$self->{_hosts_to_scan}}, @{$_[0]}
197 } elsif(ref $_[0]) {
198 4 croak "Don't know how to deal with a ", lc(ref($_[0])), "ref."
199 } else {
200 2 push @{$self->{_hosts_to_scan}}, @_
201 }
202 }
203  
204 =item ports()
205  
206 Add ports to the scan list.
207 Expects single ports (e.g. '8080') or ports ranges (e.g. '1-123').
208 Return the current list of ports when called with no argument.
209  
210 B<Examples>
211  
212 $scanner->ports('8080'); # add one port
213 $scanner->ports('1-123'); # add a ports range
214 $scanner->ports(qw(137-139 201-208)); # add two ports ranges
215  
216 =cut
217  
218 9 ▶sub ports {
Subroutine ports: called CC=4 SCAR=13.9
Pod documented
219 9 my $self = shift;
220 9 ▶ if(@_ ==0) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 0) { } 1 8 9
elsif (ref $_[0] eq 'ARRAY') { } 2 6 8
elsif (ref $_[0]) { } 4 2 6
221 1 return @{$self->{_ports_to_scan}}
222 } elsif(ref $_[0] eq 'ARRAY') {
223 2 push @{$self->{_ports_to_scan}}, @{$_[0]}
224 } elsif(ref $_[0]) {
225 4 croak "Don't know how to deal with a ", lc(ref($_[0])), "ref."
226 } else {
227 2 push @{$self->{_ports_to_scan}}, @_
228 }
229 }
230  
231 =item guess_system()
232  
233 When enabled, tells nmap to use TCP/IP fingerprinting to guess remote
234 operating system (note: root privileges required).
235 Default is 0 (disabled).
236  
237 =cut
238  
239 # accessors
240 6 for my $attr (qw(guess_system)) {
241 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 6 ▶ *{__PACKAGE__.'::'.$attr} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.1
243 0 my $self = shift;
244 0 my $old = $self->{_nmap_options}{$attr};
245 0 ▶ $self->{_nmap_options}{$attr} = $_[0] if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
246 0 return $old;
247 }
248 6 }
249  
250 =back
251  
252 =head1 SEE ALSO
253  
254 L<NetworkInfo::Discovery>, L<Nmap::Scanner>
255  
256 =head1 AUTHOR
257  
258 SE<eacute>bastien Aperghis-Tramoni, E<lt>sebastien@aperghis.netE<gt>
259  
260 =head1 BUGS
261  
262 Please report any bugs or feature requests to
263 C<bug-networkinfo-discovery-nmap@rt.cpan.org>, or through the web interface at
264 L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=NetworkInfo-Discovery-Nmap>.
265 I will be notified, and then you'll automatically be notified of progress
266 on your bug as I make changes.
267  
268 =head1 COPYRIGHT & LICENSE
269  
270 Copyright 2004 SE<eacute>bastien Aperghis-Tramoni, All Rights Reserved.
271  
272 This program is free software; you can redistribute it and/or modify it
273 under the same terms as Perl itself.
274  
275 =cut
276  
277 6 1; # End of NetworkInfo::Discovery::Nmap
↑ Summary