From f7bc9324a690bb56d4105ac679e7440b8ae18693 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Thu, 17 Dec 2020 11:44:28 +0100 Subject: [PATCH] Fix #2436 --- apps/protocols/udp/mode/connection.pm | 43 +++++++++++++++++---------- apps/protocols/udp/plugin.pm | 6 ++-- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/apps/protocols/udp/mode/connection.pm b/apps/protocols/udp/mode/connection.pm index 6e9197c98..3fabd6830 100644 --- a/apps/protocols/udp/mode/connection.pm +++ b/apps/protocols/udp/mode/connection.pm @@ -32,12 +32,12 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', }, - "timeout:s" => { name => 'timeout', default => '3' }, - }); + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'timeout:s' => { name => 'timeout', default => '3' } + }); + return $self; } @@ -57,10 +57,16 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Need to specify '--hostname' option"); $self->{output}->option_exit(); } - if (!defined($self->{option_results}->{port})) { + + if (!defined($self->{option_results}->{port}) || $self->{option_results}->{port} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify '--port' option"); $self->{output}->option_exit(); } + $self->{option_results}->{port} = $self->{option_results}->{port} =~ /(\d+)/ ? $1 : -1; + if ($self->{option_results}->{port} < 0 || $self->{option_results}->{port} > 65535) { + $self->{output}->add_option_msg(short_msg => 'Illegal port number (allowed range: 1-65535)'); + $self->{output}->option_exit(); + } } sub run { @@ -74,14 +80,15 @@ sub run { my $read_set = new IO::Select(); $read_set->add($icmp_sock); - my $sock = IO::Socket::INET->new(PeerAddr => $self->{option_results}->{hostname}, - PeerPort => $self->{option_results}->{port}, - Proto => 'udp', - ); + my $sock = IO::Socket::INET->new( + PeerAddr => $self->{option_results}->{hostname}, + PeerPort => $self->{option_results}->{port}, + Proto => 'udp', + ); $sock->send("Hello"); close($sock); - + my ($new_readable) = IO::Select->select($read_set, undef, undef, $self->{option_results}->{timeout}); my $icmp_arrived = 0; foreach $sock (@$new_readable) { @@ -93,11 +100,15 @@ sub run { close($icmp_sock); if ($icmp_arrived == 1) { - $self->{output}->output_add(severity => 'CRITICAL', - short_msg => sprintf("Connection failed on port %s", $self->{option_results}->{port})); + $self->{output}->output_add( + severity => 'CRITICAL', + short_msg => sprintf("Connection failed on port %s", $self->{option_results}->{port}) + ); } else { - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("Connection success on port %s", $self->{option_results}->{port})); + $self->{output}->output_add( + severity => 'OK', + short_msg => sprintf("Connection success on port %s", $self->{option_results}->{port}) + ); } $self->{output}->display(); diff --git a/apps/protocols/udp/plugin.pm b/apps/protocols/udp/plugin.pm index 6c6e28b7c..0bbdc0611 100644 --- a/apps/protocols/udp/plugin.pm +++ b/apps/protocols/udp/plugin.pm @@ -30,9 +30,9 @@ sub new { bless $self, $class; $self->{version} = '0.1'; - %{$self->{modes}} = ( - 'connection' => 'apps::protocols::udp::mode::connection', - ); + $self->{modes} = { + 'connection' => 'apps::protocols::udp::mode::connection' + }; return $self; }