Apache CouchDB — 1 vulnerability check

noctis ships 1 vulnerability check for Apache CouchDB, covering 1 CVE; each one has to observe the bug on the live service before it reports a finding.

← All products · Feeds on GitHub

Apache CouchDB Erlang default-cookie RCE

CVECVE-2022-24706
Severitycritical · CVSS 9.8
Categorydefault-credentials — Default credentials
Servicesepmd
Ports4369
Detectiontcp_dialogue, script, tcp_open, tcp_send, match, tcp_close · highest QoD 97
Feedtests/vulnerabilities/couchdb/CVE-2022-24706.yaml

Asks the Erlang port mapper (epmd) which nodes the host runs and on which port, then speaks the Erlang distribution handshake to the CouchDB node (or the first node listed): sends a name frame with the capabilities current nodes require, and answers the node's challenge with a digest computed from the default magic cookie ("monster") and a random challenge of its own. The finding is raised only when the node replies with a challenge ack whose digest is MD5("monster" + that random challenge), which proves the node accepted and holds the shipped cookie (CVE-2022-24706): a remote peer can then run code.

How noctis detects it

  1. TCP dialogue: one binary protocol frame
  2. Parses / computes the next value with a Rhai script
  3. Opens a persistent TCP/TLS socket
  4. Sends a binary frame on the open socket
  5. Sends … on the open socket
  6. Parses / computes the next value with a Rhai script
  7. Sends a binary frame on the open socket
  8. Parses / computes the next value with a Rhai script
  9. Checks proof for ^true$ → finding QoD 97: Apache CouchDB Erlang default-cookie RCE (CVE-2022-24706)

References:

Feed source — tests/vulnerabilities/couchdb/CVE-2022-24706.yaml
# Apache CouchDB privilege escalation via the Erlang distribution protocol
# (CVE-2022-24706). A CouchDB node before 3.2.2 ships with a default, well-known
# Erlang magic cookie ("monster") and an exposed distribution port. Anyone who
# can reach that port can complete the Erlang distribution handshake with the
# default cookie and gain remote code execution as the CouchDB user.
#
# This is NOT an HTTP check — it speaks the binary Erlang protocols directly:
#
#   1. tcp_dialogue — ask epmd (TCP/4369, the matched service) for its node
#                     list (NAMES_REQ, 00 01 'n'); the reply lists
#                     "name couchdb at port 9100". The node named couchdb is
#                     preferred, else the first one listed.
#   2. tcp_open     — open that distribution port and keep the socket.
#   3. tcp_send     — send the name frame ('n', version 5) with the capability
#                     flags every node since OTP 23 requires (a frame with flags
#                     0 is refused with "snot_allowed", issue #2458) plus
#                     DFLAG_HANDSHAKE_23, so OTP 23+ nodes switch to the
#                     version 6 handshake while older ones keep version 5.
#   4. the node answers a status frame ("sok") and its challenge, either an 'n'
#      frame (version 5, challenge at body byte 7) or an 'N' frame (version 6,
#      challenge at body byte 9). An empty tcp_send re-reads when only the
#      status frame arrived first.
#   5. tcp_send     — send the complement frame ('c': high flags + creation,
#                     version 6 only) and the challenge reply ('r': our random
#                     challenge + MD5("monster" + decimal(node challenge))).
#   6. the node answers a challenge ack ('a') whose digest is
#      MD5("monster" + our random challenge) → it holds the default cookie.
#      Any other reply (e.g. an echo service mirroring our 'r' frame) is ignored.
#
# Flags 0x01075f9c = EXTENDED_REFERENCES | DIST_MONITOR_NAME | FUN_TAGS |
# NEW_FUN_TAGS | EXTENDED_PIDS_PORTS | EXPORT_PTR_TAG | BIT_BINARIES |
# NEW_FLOATS | UNICODE_IO | UTF8_ATOMS | MAP_TAG | BIG_CREATION | HANDSHAKE_23;
# high flags 0x24 = V4_NC | UNLINK_ID (mandatory from OTP 26). Verified on a real
# OTP 25 node (epmd + `-setcookie monster` → ack; other cookie → no ack).
uid: 7b3c9d2e-4f81-4a6c-9e07-2d5b8c1a6f34
name: "Apache CouchDB Erlang default-cookie RCE (CVE-2022-24706)"
cves: [CVE-2022-24706]
cvss: 9.8
severity: critical
category: default-credentials
confidence_base: 0.30
tags: [couchdb, apache, erlang, epmd, rce, default-credentials, kev]
services: [epmd]
ports: [4369]
author: noctis
version: "1.2.0"
description: |
  Asks the Erlang port mapper (epmd) which nodes the host runs and on which
  port, then speaks the Erlang distribution handshake to the CouchDB node (or
  the first node listed): sends a name frame with the capabilities current
  nodes require, and answers the node's challenge with a digest computed from
  the default magic cookie ("monster") and a random challenge of its own. The
  finding is raised only when the node replies with a challenge ack whose
  digest is MD5("monster" + that random challenge), which proves the node
  accepted and holds the shipped cookie (CVE-2022-24706): a remote peer can
  then run code.
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2022-24706"
  - "https://docs.couchdb.org/en/stable/cve/2022-24706.html"
  - "https://www.erlang.org/doc/apps/erts/erl_dist_protocol.html"

steps:
  # 1 — epmd NAMES_REQ: [len=0x0001]['n']. Reply: [epmd port:4] then one
  #     "name <node> at port <port>\n" line per registered node.
  - id: epmd-names
    action: tcp_dialogue
    port: "{{port}}"
    read_banner: false
    send_hex: "00016e"
    timeout_secs: 5
    store_as: epmd

  # 2 — pick the distribution port: the couchdb node first, else the first node.
  - id: pick-node
    action: script
    condition: "epmd.connected == true"
    store_as: node
    code: |
      let txt = "";
      if epmd.responses.len() > 0 { txt = epmd.responses[0]; }
      let m = eregmatch("name (couchdb) at port ([0-9]+)", txt);
      if m.len() == 0 { m = eregmatch("name ([^ \n]+) at port ([0-9]+)", txt); }
      if m.len() == 0 {
        #{ found: false, name: "", port: 0 }
      } else {
        let p = int(m[2]);
        #{ found: p > 0 && p < 65536, name: m[1], port: p }
      }

  # 3 — open the distribution port and keep the socket for the whole handshake.
  - id: open-dist
    action: tcp_open
    handle: erl
    condition: "node.found == true"
    port: "{{node.port}}"
    timeout_secs: 5
    store_as: conn

  # 4 — name frame: [len=0x0015]['n'][version=5][flags=0x01075f9c]["noctis@scanner"].
  - id: send-name
    action: tcp_send
    handle: erl
    condition: "node.found == true && conn.connected == true"
    send_hex: "00156e000501075f9c6e6f63746973407363616e6e6572"
    timeout_secs: 5
    store_as: name_reply

  # 4b — only the status frame ("sok") arrived: read the challenge that follows.
  - id: read-challenge
    action: tcp_send
    handle: erl
    condition: "node.found == true && conn.connected == true && name_reply.response_hex == \"0003736f6b\""
    timeout_secs: 5
    store_as: more

  # 5 — walk the [len:2][body] frames: status 's' must be "ok"; the challenge is
  #     a big-endian u32 at body byte 7 ('n', version 5) or 9 ('N', version 6).
  #     x.frame = (version 6: complement [0x0009]['c'][high flags][creation])
  #               + challenge reply [0x0015]['r'][our challenge:4][digest:16];
  #     x.ack   = the ack a node holding "monster" must send back.
  - id: build-reply
    action: script
    condition: "node.found == true && conn.connected == true && name_reply.responded == true"
    store_as: x
    code: |
      let h = tolower(name_reply.response_hex);
      if is_def_var("more") && type_of(more.response_hex) == "string" {
        h += tolower(more.response_hex);
      }
      let status_ok = false;
      let tag = "";
      let challenge = -1;
      let i = 0;
      while i + 4 <= h.len() {
        let flen = u16be(substr(h, i, i + 3));
        let body = substr(h, i + 4, i + 3 + flen * 2);
        if flen == 0 || body.len() < flen * 2 { break; }
        let t = substr(body, 0, 1);
        if t == "73" {
          status_ok = body.starts_with("736f6b");
        } else if t == "6e" && flen >= 11 {
          challenge = u32be(substr(body, 14, 21));
          tag = "n";
        } else if t == "4e" && flen >= 13 {
          challenge = u32be(substr(body, 18, 25));
          tag = "N";
        }
        i += 4 + flen * 2;
      }
      let ours = rand_int(1, 2147483647);
      let frame = "";
      if tag == "N" {
        frame = "00096300000024" + p32be(rand_int(1, 2147483647));
      }
      frame += "001572" + p32be(ours) + md5("monster" + challenge.to_string());
      #{
        ready: status_ok && challenge >= 0,
        frame: frame,
        ack: "001161" + tolower(md5("monster" + ours.to_string()))
      }

  # 6 — send the (complement +) challenge reply on the same socket. A node that
  #     trusts the default cookie accepts it and acks; a patched node
  #     (non-default cookie) rejects the digest and closes.
  - id: send-challenge-reply
    action: tcp_send
    handle: erl
    condition: "node.found == true && conn.connected == true && name_reply.responded == true && x.ready == true"
    send_hex: "{{x.frame}}"
    timeout_secs: 5
    store_as: ack

  # 6b — only a genuine challenge ack proves the cookie: tag 'a' and the 16-byte
  #      digest of OUR random challenge under "monster". Any other reply (our own
  #      'r' frame mirrored by an echo service, an error, a banner) is not proof.
  - id: verify-ack
    action: script
    condition: "node.found == true && conn.connected == true && name_reply.responded == true && x.ready == true"
    store_as: proof
    code: |
      let ok = false;
      if type_of(ack.response_hex) == "string" {
        ok = tolower(ack.response_hex).starts_with(x.ack);
      }
      ok

  - id: check-ack
    action: match
    source: proof
    pattern: "^true$"
    condition: "node.found == true && x.ready == true && proof == true"
    on_match:
      finding:
        title: "Apache CouchDB Erlang default-cookie RCE (CVE-2022-24706)"
        description: "The Erlang distribution node accepted the default magic cookie 'monster' and proved it holds that cookie by acknowledging a random challenge with the matching digest, confirming CVE-2022-24706 (remote code execution)."
        remediation: "Upgrade CouchDB to >= 3.2.2, set a strong unique Erlang cookie (vm.args -setcookie), and firewall the epmd (4369) and distribution ports."
        confidence_delta: 0.65
        qod: 97
        evidence: "Erlang node {{node.name}} (distribution port {{node.port}}) acked our challenge with MD5(\"monster\" + challenge): {{ack.response_hex}}"
      stop: true

  # 7 — always close the socket.
  - id: close-dist
    action: tcp_close
    handle: erl

View these feeds on GitHub