Ruby WEBrick — 1 vulnerability check

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

← All products · Feeds on GitHub

Ruby WEBrick Terminal Escape Sequence Injection in Logs

CVECVE-2009-4492
Severityhigh · CVSS 7.5
Categoryrce — Remote code execution
Serviceshttp, https
Detectionscript, set_var, tcp_connect, match, http_request · highest QoD 75
Feedtests/vulnerabilities/ruby-webrick/CVE-2009-4492.yaml

Tests Ruby's WEBrick HTTP server for CVE-2009-4492: WEBrick before Ruby 1.8.6-p388, 1.8.7-p249 and 1.9.1-p378 writes client data to its logs without escaping control characters, so a request can plant terminal escape sequences that run when an administrator views the log in a terminal. The feed sends one malformed request line made of a random marker followed by the harmless "reset attributes" escape sequence (ESC [0m). An affected WEBrick answers with a 400 page quoting the same "bad Request-Line" message it writes to its error log. When that message carries the raw ESC byte and the Server header names a Ruby released before 2010-01-10, the log is written unescaped and the server is reported. A fixed WEBrick, including a distribution build with the fix backported, shows the sequence escaped as "\e[0m" and is not reported. Ruby 2.4.2 and later escape control characters when writing the log line instead, so their 400 page shows the raw byte again; the release date in the Server header keeps them from being reported. When a front proxy rejects the malformed line, the feed falls back to the Ruby release date in the WEBrick Server header (before 2010-01-10) and reports it at a lower confidence. The probe only adds one error-log line holding a reset sequence and changes nothing else.

How noctis detects it

  1. Parses / computes the next value with a Rhai script
  2. Parses / computes the next value with a Rhai script
  3. Raw TCP (TLS on HTTPS), sent verbatim: {{bad_line}}
  4. Parses / computes the next value with a Rhai script
  5. Checks wb.proof for ^bad Request-Line `noctis[0-9a-f]{12}<ESC 0x1b>\[0m'$ → finding QoD 75: Ruby WEBrick Terminal Escape Sequence Injection in Logs (CVE-2009-4492)
  6. HTTP(S) request GET /
  7. Parses / computes the next value with a Rhai script
  8. Checks old_ruby for ^Ruby [0-9.]+ released [0-9]{4}-[0-9]{2}-[0-9]{2} \(Server: → finding QoD 70: Ruby WEBrick Terminal Escape Sequence Injection in Logs (CVE-2009-4492) — version check

References:

Feed source — tests/vulnerabilities/ruby-webrick/CVE-2009-4492.yaml
# Ruby WEBrick terminal escape sequence injection in logs — CVE-2009-4492
#
# WEBrick (Ruby's bundled HTTP server) before Ruby 1.8.6-p388, 1.8.7-p249 and
# 1.9.1-p378 writes request data to its access and error logs without
# escaping control characters. A request carrying ESC sequences plants them in
# the log, and they run in the terminal of whoever views it.
#
# The error message for a malformed Request-Line is built in
# WEBrick::HTTPRequest#read_request_line:
#   raise HTTPStatus::BadRequest, "bad Request-Line `#{rl}'."
# HTTPServer#run writes that exact ex.message to the error log
# (@logger.error(ex.message)) and HTTPResponse#set_error prints it in the 400
# page through HTMLUtils.escape, which only touches & " < >. The fix (Ruby
# ChangeLog 2010-01-10, lib/webrick/httpstatus.rb) passes every status message
# through AccessLog.escape, so the ESC byte becomes the two characters "\e".
#
# Detection:
#   1. Send one malformed Request-Line "<random marker>ESC[0m" (no space, so it
#      fails the Request-Line regex). ESC[0m is the terminal "reset attributes"
#      sequence: harmless on any terminal. On a WEBrick reply (Server header),
#      the 400 page quoting "bad Request-Line `<marker>" followed by the raw ESC
#      byte, with a Server header naming a Ruby dated before 2010-01-10, proves
#      the logged message is unescaped → QoD 75. The same page with "\e[0m"
#      proves the fix (also a distro backport) → nothing reported.
#      Ruby 2.4.2 (CVE-2017-10784) moved the escaping from HTTPStatus::Status
#      into the logger (BasicLog#format): from then on the 400 page shows the
#      raw byte again while the log line is escaped, so a raw page on a newer
#      Ruby is not reported (webrick gem >= 1.9.1 also quotes with ' not `).
#   2. Only when no WEBrick 400 page came back (a front proxy rejected the line),
#      fall back to the Server header of GET /: Ruby release date before
#      2010-01-10 → QoD 70.
#
# Improvements over the source NASL (banner only):
#   - In-band behavioural proof first; a distribution build that keeps an old
#     RUBY_RELEASE_DATE in its banner but carries the backported fix (Debian
#     lenny 1.8.7.72, RHEL 1.8.5…) is no longer reported.
#   - The version fallback flags every Ruby dated before 2010-01-10 (1.8.5,
#     1.9.0, 1.9.2 previews…), not only 1.8.6 / 1.8.7 / 1.9.1.
#
# Ported from OpenVAS/Greenbone NVT 2010/ruby_WEBrick_37710.nasl
# (GPL-2.0-only). Vulnerable/fixed code: ruby/ruby tags v1_8_7_248 vs
# v1_8_7_249 (lib/webrick/httprequest.rb, httpstatus.rb, accesslog.rb).
uid: 71521808-7d2b-46f3-b685-e0fe8a109686
name: "Ruby WEBrick Terminal Escape Sequence Injection in Logs (CVE-2009-4492)"
description: |
  Tests Ruby's WEBrick HTTP server for CVE-2009-4492: WEBrick before Ruby
  1.8.6-p388, 1.8.7-p249 and 1.9.1-p378 writes client data to its logs without
  escaping control characters, so a request can plant terminal escape
  sequences that run when an administrator views the log in a terminal.
  The feed sends one malformed request line made of a random marker followed
  by the harmless "reset attributes" escape sequence (ESC [0m). An affected
  WEBrick answers with a 400 page quoting the same "bad Request-Line" message
  it writes to its error log. When that message carries the raw ESC byte and
  the Server header names a Ruby released before 2010-01-10, the log is
  written unescaped and the server is reported. A fixed WEBrick, including a
  distribution build with the fix backported, shows the sequence escaped as
  "\e[0m" and is not reported. Ruby 2.4.2 and later escape control characters
  when writing the log line instead, so their 400 page shows the raw byte
  again; the release date in the Server header keeps them from being
  reported. When a front proxy rejects the malformed line,
  the feed falls back to the Ruby release date in the WEBrick Server header
  (before 2010-01-10) and reports it at a lower confidence. The probe only
  adds one error-log line holding a reset sequence and changes nothing else.
cves: [CVE-2009-4492]
cvss: 7.5
severity: high
category: rce
confidence_base: 0.30
tags: [ruby, webrick, log-injection, escape-sequence, terminal]
services: [http, https]
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2009-4492"
  - "https://www.ruby-lang.org/en/news/2010/01/10/webrick-escape-sequence-injection/"
  - "https://www.ush.it/team/ush/hack_httpd_escape/adv.txt"
  - "https://www.securityfocus.com/bid/37710"
  # OpenVAS/Greenbone NASL 2010/ruby_WEBrick_37710.nasl (GPL-2.0-only)

steps:
  # Random marker for this scan, then the malformed request line: marker + ESC [0m.
  - id: marker
    action: script
    code: |
      "noctis" + oob_token.sub_string(0, 8) + oob_token.sub_string(9, 4)
    store_as: tok

  - id: build-line
    action: script
    code: |
      tok + "\x1b[0m"
    store_as: bad_line

  # Pre-set so the verdict script still runs when the connection fails.
  - id: init-probe
    action: set_var
    var_name: probe
    var_value: {connected: false, banner: ""}

  - id: probe
    action: tcp_connect
    port: "{{port}}"
    send: "{{bad_line}}\r\n\r\n"
    timeout_secs: 10
    store_as: probe

  # WEBrick 400 page verdict: "raw" (vulnerable), "escaped" (fixed, 2010–2017),
  # "logger-escapes" (raw page on Ruby >= 2.4.2, whose logger escapes) or "".
  # The proof text names the ESC byte instead of carrying it.
  - id: verdict
    action: script
    code: |
      let b = if type_of(probe.banner) == "string" { probe.banner } else { "" };
      let cut = b.index_of("\r\n\r\n");
      let hdr = if cut >= 0 { b.sub_string(0, cut) } else { b };
      let webrick = ereg("^HTTP/1\\.[01] 400 ", b) && ereg("(?im)^Server:[ \t]*WEBrick/", hdr);
      // Ruby release date from the WEBrick Server header (yyyymmdd, 0 when absent).
      let d = eregmatch("(?im)^Server:[ \t]*WEBrick/[0-9.]+ \\(Ruby/[0-9][0-9.]*/([0-9]{4})-([0-9]{2})-([0-9]{2})\\)", hdr);
      let rdate = if d.len() == 4 { parse_int(d[1]) * 10000 + parse_int(d[2]) * 100 + parse_int(d[3]) } else { 0 };
      let state = "";
      let proof = "";
      if webrick && (b.contains("bad Request-Line `" + tok + "\x1b[0m'") || b.contains("bad Request-Line '" + tok + "\x1b[0m'")) {
        // Before the fix the page and the log share one unescaped message. Since
        // Ruby 2.4.2 the logger escapes on write while the page stays raw, so a
        // raw page only proves the flaw on a Ruby dated before 2010-01-10.
        if rdate > 0 && rdate < 20100110 {
          state = "raw";
          proof = "bad Request-Line `" + tok + "<ESC 0x1b>[0m'";
        } else {
          state = "logger-escapes";
        }
      } else if webrick && (b.contains("bad Request-Line `" + tok + "\\e[0m'") || b.contains("bad Request-Line '" + tok + "\\e[0m'")) {
        state = "escaped";
      }
      let srv = egrep("(?i)^Server:", hdr);
      #{ state: state, proof: proof, server: srv }
    store_as: wb

  - id: report-raw
    action: match
    source: wb.proof
    pattern: "^bad Request-Line `noctis[0-9a-f]{12}<ESC 0x1b>\\[0m'$"
    condition: "wb.state == \"raw\""
    on_match:
      finding:
        title: "Ruby WEBrick Terminal Escape Sequence Injection in Logs (CVE-2009-4492)"
        description: "WEBrick (Ruby released before 2010-01-10) answered a malformed request line with the error message it writes to its log, and the message kept the client's raw ESC control byte. An attacker can plant terminal escape sequences in the WEBrick logs; they run in the terminal of whoever views the log."
        remediation: "Upgrade Ruby to 1.8.6-p388, 1.8.7-p249, 1.9.1-p378 or a later release (any supported Ruby with the current webrick gem escapes control characters in log messages)."
        confidence_delta: 0.55
        qod: 75
        evidence: "Malformed request line \"{{tok}}<ESC>[0m\" → HTTP 400 from {{wb.server}} quoting: {{wb.proof}}"
      stop: true

  # Fallback only when no WEBrick 400 page was seen (neither raw nor escaped).
  - id: init-home
    action: set_var
    var_name: home
    var_value: {status: 0, headers: {}}
    condition: "wb.state == \"\""

  - id: home-get
    action: http_request
    method: GET
    path: "/"
    port: "{{port}}"
    tls_insecure: true
    follow_redirects: false
    timeout_secs: 10
    condition: "wb.state == \"\""
    store_as: home

  - id: version
    action: script
    condition: "wb.state == \"\""
    code: |
      let s = "";
      if home.status > 0 && type_of(home.headers) == "map" && "server" in home.headers {
        s = home.headers["server"].to_string();
      }
      let m = eregmatch("WEBrick/[0-9.]+ \\(Ruby/([0-9][0-9.]*)/([0-9]{4})-([0-9]{2})-([0-9]{2})\\)", s);
      if m.len() == 5 && (parse_int(m[2]) * 10000 + parse_int(m[3]) * 100 + parse_int(m[4])) < 20100110 {
        "Ruby " + m[1] + " released " + m[2] + "-" + m[3] + "-" + m[4] + " (Server: " + s + ")"
      } else { "" }
    store_as: old_ruby

  - id: report-version
    action: match
    source: old_ruby
    pattern: "^Ruby [0-9.]+ released [0-9]{4}-[0-9]{2}-[0-9]{2} \\(Server: "
    condition: "wb.state == \"\""
    on_match:
      finding:
        title: "Ruby WEBrick Terminal Escape Sequence Injection in Logs (CVE-2009-4492) — version check"
        description: "The WEBrick Server header names a Ruby release dated before 2010-01-10, which lacks the escaping of control characters in log messages. The behavioural check could not reach WEBrick's own error page (the malformed request was answered by another server), so this finding rests on the banner; a distribution build with a backported fix keeps its old release date."
        remediation: "Upgrade Ruby to 1.8.6-p388, 1.8.7-p249, 1.9.1-p378 or a later release (any supported Ruby with the current webrick gem escapes control characters in log messages)."
        confidence_delta: 0.40
        qod: 70
        evidence: "GET / Server header: {{old_ruby}}"

View these feeds on GitHub