1
2use strict;
3
4use Class::Generate qw(class);
5
6class 'Nmap::Scanner::Host' => {qw(
7        addresses   @Nmap::Scanner::Address
8        ports       %
9        hostnames   @Nmap::Scanner::Host
10        smurf       $
11        status      $
12        extra_ports Nmap::Scanner::ExtraPorts
13        distance    Nmap::Scanner::Distance
14        os          Nmap::Scanner::OS
15    ),
16    '&add_address'  => q!push(@{$self->{'addresses'}}, $_[0]) if $_[0];!,
17    '&hostname'     => q!
18        # this returns the first hostname
19        return $self->hostnames(0)->name() if $self->hostnames(0);
20        return "";
21    !,
22    '&add_hostname' => q!push(@{ $self->{'hostnames'} }, $_[0]) if $_[0];!,
23    '&add_port'     => q!
24
25        my $port = shift;
26
27        return unless defined $port;
28
29        Nmap::Scanner::debug("Adding port with proto: " . $port->protocol());
30        $self->{'ports'}->{lc $port->protocol()}->{$port->portid()} = $port;
31    !,
32    '&get_port'     => q!
33        return $self->ports(lc $_[0])->{$_[1]}
34            if $self->ports(lc $_[0])->{$_[1]};
35    !,
36
37    '&get_udp_port' => q!
38        return $self->ports('udp')->{$_[0]}
39            if $self->ports('udp')->{$_[0]};
40    !,
41    '&get_tcp_port' => q!
42        return $self->ports('tcp')->{$_[0]}
43            if $self->ports('tcp')->{$_[0]};
44    !,
45    '&get_port_list' => q!
46        return Nmap::Scanner::PortList->new(
47            $self->ports('tcp'), $self->ports('udp')
48        );
49    !,
50    '&get_ip_port_list' => q!
51        return Nmap::Scanner::PortList->new(undef, $self->ports('ip'));
52    !,
53    '&get_tcp_port_list' => q!
54        return Nmap::Scanner::PortList->new($self->ports('tcp'));
55    !,
56    '&get_udp_port_list' => q!
57        return Nmap::Scanner::PortList->new(undef, $self->ports('udp'));
58    !,
59    '&as_xml' => q!
60
61    my $xml = qq(<host><status state="$self->{status}"/>\n);
62
63    for my $a ($self->addresses()) {
64        $xml .= $a->as_xml() . "\n";
65    }
66
67    $xml .= qq(<smurf responses="$self->{smurf}"/>\n)
68                if $self->{smurf} > 0;
69
70    my $hxml = '';
71
72    foreach my $h ($self->hostnames()) {
73        $hxml .= $h->as_xml() if (keys %$h);
74    }
75
76    $xml .= "<hostnames>$hxml</hostnames>\n" if $hxml;
77
78    $xml .= $self->os()->as_xml() . "\n" if $self->os();
79
80    my $pxml .= $self->extra_ports()->as_xml() ."\n"
81                if $self->extra_ports();
82
83    $pxml .= $self->get_tcp_port_list()->as_xml();
84    $pxml .= $self->get_udp_port_list()->as_xml();
85    $pxml .= $self->get_ip_port_list()->as_xml();
86
87    $xml .= "<ports>$pxml</ports>\n" if $pxml;
88    $xml .= $self->distance()->as_xml() . "\n" if $self->distance();
89    $xml .= "</host>\n";
90
91    return $xml;
92
93    !
94};
95
96=pod
97
98=head1 DESCRIPTION
99
100This class represents a host as repsented by the output
101of an nmap scan.
102
103=head1 PROPERTIES
104
105=head2 status()
106
107Whether the host is reachable or not: `up' or `down'
108
109=head2 addresses()
110
111Addresses of the host as determined by nmap (Nmap::Scanner::Address references).
112
113=head2 add_address()
114
115Add an address to the list of addresses for this host
116
117=head2 hostname()
118
119First hostname of the host as determined by nmap (single hostname string).
120
121=head2 hostnames()
122
123Hostnames of the host as determined by nmap (Array of Address references).
124
125=head2 add_hostname()
126
127Add a hostname to the list of hostnames for this host
128
129=head2 smurf()
130
131    True (1) if the host responded to a ping of a broadcast address and
132    is therefore vulnerable to a Smurf-style attack.
133
134=head2 extra_ports()
135
136Nmap::Scanner::ExtraPorts instance associated with this host.
137
138=head2 os()
139
140holds a reference to an Nmap::Scanner::OS object that
141describes the operating system and TCP fingerprint for this
142host, as determined by nmap.  Only present if guess_os()
143is called on the Nmap::Scanner::Scanner object AND nmap is
144able to determine the OS type via TCP fingerprinting.  See the
145nmap manual for more details.
146
147=head2 add_port($port_object_reference)
148
149=head2 get_port($proto, $number)
150
151Returns reference to requested port object.
152
153=head2 get_udp_port($number)
154
155Returns reference to requested UDP port object.
156
157=head2 get_tcp_port($number)
158
159Returns reference to requested TCP port object.
160
161=head2 ENUMERATION METHODS
162
163All these methods return lists of objects that
164can be enumration through using a while loop.
165
166my $ports = $host->get_port_list();
167
168while (my $p = $ports->get_next()) {
169    #  Do something with port reference here.
170}
171
172=head2 get_port_list()
173
174=head2 get_ip_port_list()
175
176=head2 get_tcp_port_list()
177
178=head2 get_udp_port_list()
179
180=head2 distance()
181
182The distance in hops this host is from the scanning host as
183estimated by nmap.
184
185=cut
186
1871;
188