PHP — 1 vulnerability check

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

← All products · Feeds on GitHub

PHP <= 4.2.2 mail() argument and header injection

CVECVE-2002-0985, CVE-2002-0986
Severityhigh · CVSS 7.5
Categoryrce — Remote code execution
Serviceshttp, https
Detectiontcp_connect, script, match · highest QoD 70
Feedtests/vulnerabilities/php/php-mail-function-injection.yaml

PHP 4.2.2 and earlier have two flaws in the mail() function. Its fifth argument is passed to the mail transfer agent (sendmail) even in safe mode, so a script that forwards user input there lets an attacker add MTA command-line options, which can lead to command execution (CVE-2002-0985). Its other arguments are not stripped of ASCII control characters, so a script that puts user input in the recipient or subject lets an attacker inject mail headers and body content and use the server as a spam relay (CVE-2002-0986). PHP 4.2.3 fixed both.

The test sends one plain `GET / HTTP/1.0` request (over TLS on HTTPS services) and reads the `Server:` and `X-Powered-By:` response headers. It reports the host when one of them carries a `PHP/` product token whose version is 4.2.2 or older (any 0.x to 3.x, 4.0.x, 4.1.x, 4.2.0 to 4.2.2). PHP 4.2.3 and later, and servers that hide their PHP version (`expose_php = Off`), are not reported; PHP tokens elsewhere in the response are ignored.

This is a version check: exploiting either flaw needs an application script that passes request data to mail(), and would send mail, so nothing is sent beyond the request above. Distributions that backported the fix without changing the version are reported too.

How noctis detects it

  1. Raw TCP (TLS on HTTPS), sent verbatim: GET / HTTP/1.0
  2. Parses / computes the next value with a Rhai script
  3. Checks ver.version for ^[0-9] → finding QoD 70: Header '…': PHP …, 4.2.2 or older (mail() fifth-argument and control-character injection; not triggered).

References:

Feed source — tests/vulnerabilities/php/php-mail-function-injection.yaml
# Ported from OpenVAS/Greenbone NASL pre2008/php_mail_func_header_spoof.nasl
# (OID 1.3.6.1.4.1.25623.1.0.11444, GPL-2.0-only), which reports any HTTP response
# header block containing `PHP/([0-3]\..*|4\.[01]\..*|4\.2\.[0-2][^0-9])`.
#
# Better than the NASL:
#  - only the Server: and X-Powered-By: header lines are searched (the NASL regex runs
#    over every header, so e.g. a cookie or custom header quoting "PHP/4.1" was enough);
#  - the PHP token needs a boundary in front of it ("NotPHP/4.0" is not read as PHP);
#  - the version is compared numerically, so a token ending right after the version
#    (`X-Powered-By: PHP/4.2.2` with nothing after it: the NASL's `[^0-9]` needs one
#    more byte) is covered, and "PHP/4.2.20"-style numbers are never mistaken for 4.2.2;
#  - raw tcp_connect, so HTTPS services are covered the same way.
#
# No generic benign exploit exists: both bugs are in PHP's mail() function, reachable
# only through an application script that passes request data to mail() — there is no
# such script in a stock PHP install, and triggering one would send mail. Hence a
# version check (QoD 70). NVD cpe ranges: CVE-2002-0985 every PHP <= 4.2.2,
# CVE-2002-0986 PHP 3.0.18 and 4.0 to 4.2.2.
uid: 41e2693b-d283-4f93-816f-fa08795364c8
name: "PHP <= 4.2.2 mail() argument and header injection (CVE-2002-0985, CVE-2002-0986)"
description: |
  PHP 4.2.2 and earlier have two flaws in the mail() function. Its fifth argument
  is passed to the mail transfer agent (sendmail) even in safe mode, so a script
  that forwards user input there lets an attacker add MTA command-line options,
  which can lead to command execution (CVE-2002-0985). Its other arguments are not
  stripped of ASCII control characters, so a script that puts user input in the
  recipient or subject lets an attacker inject mail headers and body content and
  use the server as a spam relay (CVE-2002-0986). PHP 4.2.3 fixed both.

  The test sends one plain `GET / HTTP/1.0` request (over TLS on HTTPS services)
  and reads the `Server:` and `X-Powered-By:` response headers. It reports the
  host when one of them carries a `PHP/` product token whose version is 4.2.2 or
  older (any 0.x to 3.x, 4.0.x, 4.1.x, 4.2.0 to 4.2.2). PHP 4.2.3 and later, and
  servers that hide their PHP version (`expose_php = Off`), are not reported;
  PHP tokens elsewhere in the response are ignored.

  This is a version check: exploiting either flaw needs an application script
  that passes request data to mail(), and would send mail, so nothing is sent
  beyond the request above. Distributions that backported the fix without
  changing the version are reported too.
cves: [CVE-2002-0985, CVE-2002-0986]
cvss: 7.5  # NVD publishes only CVSS v2 base scores (7.5 for CVE-2002-0985, 5.0 for CVE-2002-0986)
severity: high
confidence_base: 0.30
category: rce
tags: [php, mail, safe-mode-bypass, header-injection, version-check, unauthenticated]
services: [http, https]
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2002-0985"
  - "https://nvd.nist.gov/vuln/detail/CVE-2002-0986"
  - "http://www.php.net/ChangeLog-4.php#4.2.3"

steps:
  # 1 — one plain request; only its response headers are used.
  - id: probe
    action: tcp_connect
    port: "{{port}}"
    send: "GET / HTTP/1.0\r\nHost: {{target_host}}\r\n\r\n"
    timeout_secs: 10
    store_as: resp

  # 2 — scan the Server: and X-Powered-By: lines of the header block for a PHP/
  #     token and compare its version with the last vulnerable release 4.2.2.
  - id: version
    action: script
    condition: "resp.connected == true"
    store_as: ver
    code: |
      let out = #{ vulnerable: false, version: "", header: "" };
      if type_of(resp.banner) == "string" {
        let head = resp.banner;
        let cut = stridx(head, "\r\n\r\n");
        if cut < 0 { cut = stridx(head, "\n\n"); }
        if cut >= 0 { head = substr(head, 0, cut); }
        for raw in split(head, "\n") {
          if out.vulnerable { break; }
          let line = eregmatch("^(server|x-powered-by):[ \\t]*([^\\r]*)", raw, true);
          if line.len() < 3 { continue; }
          let m = eregmatch("(^|[^A-Za-z0-9_])PHP/([0-9]{1,6}(\\.[0-9]{1,6})*)", line[2]);
          if m.len() < 3 { continue; }
          let parts = split(m[2], ".");
          let last = [4, 2, 2];
          let vuln = true;
          for i in 0..3 {
            let n = if i < parts.len() { int(parts[i]) } else { 0 };
            if n < last[i] { break; }
            if n > last[i] { vuln = false; break; }
          }
          if vuln {
            out.vulnerable = true;
            out.version = m[2];
            out.header = line[1] + ": " + line[2];
          }
        }
      }
      out

  - id: vulnerable_version
    action: match
    condition: "ver.vulnerable == true"
    source: ver.version
    pattern: "^[0-9]"
    on_match:
      stop: true
      finding:
        confidence_delta: 0.45
        qod: 70
        evidence: "Header '{{ver.header}}': PHP {{ver.version}}, 4.2.2 or older (mail() fifth-argument and control-character injection; not triggered)."

View these feeds on GitHub