Adding switches --odata and --sparql

This commit is contained in:
Miroslav Štampar 2026-08-09 11:04:44 +02:00
parent fb5339d783
commit dd57fd91e2
18 changed files with 2505 additions and 62 deletions

View file

@ -40,6 +40,8 @@ from lib.core.settings import SSTI_ERROR_REGEX
from lib.core.settings import XPATH_ERROR_REGEX
from lib.core.settings import XSLT_ERROR_REGEX
from lib.core.settings import XXE_ERROR_REGEX
from lib.core.settings import SPARQL_ERROR_REGEX
from lib.core.settings import ODATA_ERROR_REGEX
ENGINES = (
("nosql", NOSQL_ERROR_REGEX),
@ -50,6 +52,8 @@ ENGINES = (
("hql", HQL_ERROR_REGEX),
("xslt", XSLT_ERROR_REGEX),
("xxe", XXE_ERROR_REGEX),
("sparql", SPARQL_ERROR_REGEX),
("odata", ODATA_ERROR_REGEX),
)
# (owning engine, back-end, verbatim error output). NOBODY means no engine may match it
@ -142,6 +146,19 @@ CORPUS = (
("xslt", "libxslt (live)", "XSLT error StartTag: invalid element name, line 4, column 45 ( , line 4)"),
("xxe", "libxml2 (live)", "Parsed document; content: Parser warnings: failed to load \"file:///nonexistent\": No such file or directory"),
("sparql", "Jena / Fuseki", "org.apache.jena.query.QueryParseException: Encountered \" <VAR1> \"?x\"\" at line 1"),
("sparql", "Virtuoso", "Virtuoso 37000 Error SP030: SPARQL compiler, line 1: syntax error at '}'"),
("sparql", "RDF4J / GraphDB", "org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException: MalformedQueryException"),
("sparql", "Stardog", "com.complexible.stardog.plan.eval.operator.OperatorException: parse error"),
# captured live off Apache Jena Fuseki with a broken-out string literal
("sparql", "Jena (live)", "Parse error: Lexical error at line 1, column 136. Encountered: <EOF> after prefix"),
("odata", "Microsoft OData", "The query specified in the URI is not valid. Syntax error at position 12 in 'Name eq'."),
("odata", "Olingo (Java)", "org.apache.olingo.server.api.ODataApplicationException: The URI is malformed"),
# captured live off ASP.NET Core OData with a broken-out $filter string literal
("odata", "Microsoft OData (live)", "The query specified in the URI is not valid. There is an unterminated string literal at position 17 in 'Name eq 'luther'''."),
("odata", "Microsoft OData property (live)", "The query specified in the URI is not valid. Could not find a property named 'Xyz' on type 'Default.Product'."),
# a plain SQL injection error belongs to the SQL engine. No non-SQL switch may claim it
(NOBODY, "MySQL", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version"),
(NOBODY, "Microsoft SQL Server", "Incorrect syntax near 'MERGE'."),

343
tests/test_odata.py Normal file
View file

@ -0,0 +1,343 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
Offline, deterministic tests for the OData '$filter' injection engine. A mock oracle mirrors the
boolean-blind semantics of a real OData service (a broken-out filter reduced to its injected predicate),
so detection, OData-only confirmation, version fingerprinting and per-entity blind extraction are
exercised without a live OData endpoint.
"""
import os
import re
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _testutils import bootstrap
bootstrap()
import lib.techniques.odata.inject as odata
from lib.core.settings import ODATA_ERROR_REGEX
_ENTITIES = (
{"Id": 1, "Name": "luther", "Secret": "S3CR3Tvalue"},
{"Id": 2, "Name": "fluffy", "Secret": "hunter2"},
{"Id": 3, "Name": "wu", "Secret": "letmein"},
)
_FIELDS = ("Id", "Name", "Secret")
def _wrapped(expr):
if not (expr.startswith("(") and expr.endswith(")")):
return False
depth = 0
for i, ch in enumerate(expr):
depth += (ch == "(") - (ch == ")")
if depth == 0 and i < len(expr) - 1:
return False
return True
def _split(expr, sep):
parts, buf = [], []
for token in expr.split(sep):
buf.append(token)
chunk = sep.join(buf)
if chunk.count("(") == chunk.count(")"):
parts.append(chunk)
buf = []
if buf:
parts.append(sep.join(buf))
return parts
class _ODErr(Exception):
pass
def _atom(e, a):
a = a.strip()
while _wrapped(a):
a = a[1:-1].strip()
m = re.match(r"^length\('([^']*)'\) eq (\d+)$", a)
if m:
return len(m.group(1)) == int(m.group(2))
m = re.match(r"^startswith\('([^']*)','([^']*)'\)$", a)
if m:
return m.group(1).startswith(m.group(2))
m = re.match(r"^contains\('([^']*)','([^']*)'\)$", a)
if m:
return m.group(2) in m.group(1)
if a.startswith("substringof("):
raise _ODErr("v4 has no substringof")
m = re.match(r"^'([^']*)' eq '([^']*)'$", a)
if m:
return m.group(1) == m.group(2)
m = re.match(r"^(\d+) eq (\d+)$", a)
if m:
return m.group(1) == m.group(2)
m = re.match(r"^(\w+) eq '([^']*)'$", a)
if m:
if m.group(1) not in _FIELDS:
raise _ODErr("unknown property")
return "%s" % e.get(m.group(1)) == m.group(2)
m = re.match(r"^(\w+) ne null$", a)
if m:
if m.group(1) not in _FIELDS:
raise _ODErr("unknown property")
return e.get(m.group(1)) is not None
m = re.match(r"^(\w+) (eq|ge|gt|le|lt) (-?\d+)$", a)
if m:
p, op, num = m.group(1), m.group(2), int(m.group(3))
if p not in _FIELDS:
raise _ODErr("unknown property")
v = e.get(p)
if not isinstance(v, int):
return False
return {"eq": v == num, "ge": v >= num, "gt": v > num, "le": v <= num, "lt": v < num}[op]
m = re.match(r"^length\((\w+)\) (eq|ge) (\d+)$", a)
if m:
p, op, num = m.group(1), m.group(2), int(m.group(3))
if p not in _FIELDS:
raise _ODErr("unknown property")
n = len("%s" % e.get(p, ""))
return n == num if op == "eq" else n >= num
# an inner single quote arrives DOUBLED, the way the OData spec escapes it
m = re.match(r"^substring\((\w+),(\d+),1\) eq '(''|.)'$", a)
if m:
p, pos, ch = m.group(1), int(m.group(2)), m.group(3)
ch = "'" if ch == "''" else ch
if p not in _FIELDS:
raise _ODErr("unknown property")
t = "%s" % e.get(p, "")
return pos < len(t) and t[pos] == ch
raise _ODErr("syntax error")
def _eval(e, expr):
expr = expr.strip()
while _wrapped(expr):
expr = expr[1:-1].strip()
ors = _split(expr, " or ")
if len(ors) > 1:
return any(_eval(e, o) for o in ors)
ands = _split(expr, " and ")
if len(ands) > 1:
return all(_eval(e, a) for a in ands)
return _atom(e, expr)
_EMPTY = "<html><body><ul></ul></body></html>"
def _render(matched):
return "<html><body><ul>%s</ul></body></html>" % "".join("<li>%s: %s</li>" % (e["Id"], e["Name"]) for e in matched)
def _mockSend(place, parameter, value, raw=False):
expr = "Name eq '%s'" % value
if expr.count("'") % 2:
return "<pre>The query specified in the URI is not valid. There is an unterminated string literal at position 8</pre>" if raw else None
try:
matched = [e for e in _ENTITIES if _eval(e, expr)]
except _ODErr:
return "<pre>Could not find a property named 'x' on type 'Default.Product'. Microsoft.OData</pre>" if raw else None
return _render(matched)
class TestHelpers(unittest.TestCase):
def test_is_error_and_backend(self):
self.assertTrue(odata._isError("The query specified in the URI is not valid. Microsoft.OData"))
self.assertFalse(odata._isError(_EMPTY))
self.assertEqual(odata._backendFromError("Could not find a property named 'X'"), "Microsoft OData (WebAPI/.NET)")
def test_error_regex_matches_real(self):
self.assertIsNotNone(re.search(ODATA_ERROR_REGEX, "There is an unterminated string literal at position 17 in 'Name eq'"))
class TestNoAmpersandInvariant(unittest.TestCase):
"""A raw '&' in a GET value is the parameter delimiter; OData uses the 'or'/'and' keywords, so no
payload may carry one."""
def test_boundaries_and_predicates_have_no_ampersand(self):
for row in odata._BOUNDARY_TABLE:
for field in row:
self.assertNotIn("&", field)
for t, f in odata._ODATA_PREDICATES:
self.assertNotIn("&", t)
self.assertNotIn("&", f)
def test_send_url_encodes_the_payload(self):
"""The boundaries being '&'-free was never enough: the character scan emits '&' (and '+') as
DATA. _send() must URL-encode, or those probes split the request, the truncated $filter 400s,
and the resulting InconclusiveError aborts the WHOLE property (it dumped as '?')."""
sent = []
savedParams, odata.conf.parameters = odata.conf.parameters, {odata.PLACE.GET: "name=luther"}
savedGet = odata.Request.getPage
odata.Request.getPage = staticmethod(
lambda **kwargs: (sent.append(odata.conf.parameters[odata.PLACE.GET]), "", None, 200)[1:])
try:
odata._send(odata.PLACE.GET, "name", odata._literal(ord("&")))
finally:
odata.Request.getPage = savedGet
odata.conf.parameters = savedParams
self.assertEqual(len(sent), 1)
self.assertNotIn("&", sent[0].split("=", 1)[1], sent[0])
self.assertIn("%26", sent[0])
class TestCharsetCoverage(unittest.TestCase):
"""The scan is EXACT equality, not a bisection, so the ORDER is free but the COVERAGE is not: an
excluded codepoint is simply never recoverable. The one character an OData literal cannot carry
raw, the single quote, is doubled per the spec rather than dropped.
(This replaces an earlier assertion pinning 0x27/0x5c as EXCLUDED, which cost coverage of two
characters common in real names and paths for no correctness gain.)"""
def test_charset_covers_every_printable(self):
self.assertEqual(sorted(odata._CS_ORDS),
list(range(odata.ODATA_CHAR_MIN, odata.ODATA_CHAR_MAX + 1)))
def test_quote_is_doubled(self):
self.assertEqual(odata._literal(0x27), "''''")
self.assertEqual(odata._literal(0x5c), "'\\'")
self.assertEqual(odata._literal(ord("a")), "'a'")
def _mockSendV23(place, parameter, value, raw=False):
"""A v2/v3 service: contains() is an unknown function (400), substringof() is the one that parses."""
if "contains(" in value:
return "<pre>Syntax error at position 0. Microsoft.OData</pre>" if raw else None
return _mockSend(place, parameter, value.replace("substringof('sql','sqlmap')", "1 eq 1"), raw)
def _mockSendQuiet(place, parameter, value, raw=False):
"""An endpoint that SWALLOWS the service's 400: same boolean oracle, no error surface at all - a
failed $filter is rendered as the ordinary empty result page, error body and status included."""
expr = "Name eq '%s'" % value
if expr.count("'") % 2:
return _EMPTY
try:
return _render([e for e in _ENTITIES if _eval(e, expr)])
except _ODErr:
return _EMPTY
class TestVersionFingerprintDoesNotCrash(unittest.TestCase):
"""Both version probes are EXPECTED to fail on the dialect that does not own them. An unknown
$filter function is a 400 the oracle can only call inconclusive, and that must read as a negative
answer - not as an exception that aborts the scan before the finding is even reported, on exactly
the v2/v3 services the second branch exists to name."""
def setUp(self):
self.saved, self.savedParams = odata._send, odata.conf.parameters
odata.conf.parameters = {odata.PLACE.GET: "name=luther"}
odata.SENTINEL = "zzsentinelzz"
def tearDown(self):
odata._send, odata.conf.parameters = self.saved, self.savedParams
def test_v23_service_is_named_not_fatal(self):
odata._send = _mockSendV23
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
oracle = odata._makeOracle(odata.PLACE.GET, "name", boundary)
self.assertIsNotNone(oracle)
self.assertEqual(odata._fingerprintVersion(oracle), "v2/v3")
def test_service_speaking_neither_returns_none(self):
odata._send = lambda place, parameter, value, raw=False: _mockSend(
place, parameter, value.replace("contains('sqlmap','sql')", "unknownfn()"), raw)
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
oracle = odata._makeOracle(odata.PLACE.GET, "name", boundary)
self.assertIsNone(odata._fingerprintVersion(oracle))
class TestErrorSurfaceGate(unittest.TestCase):
"""_fieldExists() is an error/no-error split, so on an endpoint that swallows the service's 400 it
answers 'exists' for EVERY name - which reported all 37 candidate properties as reachable and
dumped a wall of empty columns. _hasErrorSurface() tells the two apart in one request."""
def setUp(self):
self.saved, self.savedParams = odata._send, odata.conf.parameters
odata.conf.parameters = {odata.PLACE.GET: "name=luther"}
odata.SENTINEL = "zzsentinelzz"
def tearDown(self):
odata._send, odata.conf.parameters = self.saved, self.savedParams
def _boundary(self):
return odata._detectBoolean(odata.PLACE.GET, "name")[2]
def test_error_surface_present(self):
odata._send = _mockSend
boundary = self._boundary()
self.assertTrue(odata._hasErrorSurface(odata.PLACE.GET, "name", boundary))
def test_error_surface_absent(self):
odata._send = _mockSendQuiet
boundary = self._boundary()
self.assertFalse(odata._hasErrorSurface(odata.PLACE.GET, "name", boundary))
# the error-based check is useless here - every unknown name looks real
self.assertTrue(odata._fieldExists(odata.PLACE.GET, "name", boundary, "TotallyMadeUp"))
# ...so existence falls through to the boolean oracle, which still separates them. The dump
# must keep working on a blind target, not be surrendered because one probe went blind.
oracle = odata._makeOracle(odata.PLACE.GET, "name", boundary, truePredicate="(Id eq 1)")
self.assertIsNotNone(oracle)
self.assertTrue(odata._fieldExistsBlind(oracle, "Id", 1, "Secret"))
self.assertFalse(odata._fieldExistsBlind(oracle, "Id", 1, "TotallyMadeUp"))
class TestDetectionAndExtraction(unittest.TestCase):
def setUp(self):
self.saved = odata._send
odata._send = _mockSend
self.savedParams = odata.conf.parameters
odata.conf.parameters = {odata.PLACE.GET: "name=luther"}
odata.SENTINEL = "zzsentinelzz"
def tearDown(self):
odata._send = self.saved
odata.conf.parameters = self.savedParams
def test_boolean_detection(self):
template, payload, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
self.assertIsNotNone(template)
def test_confirms_odata(self):
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
self.assertTrue(odata._confirmOData(odata.PLACE.GET, "name", boundary))
def test_version_fingerprint_v4(self):
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
oracle = odata._makeOracle(odata.PLACE.GET, "name", boundary)
self.assertEqual(odata._fingerprintVersion(oracle), "v4")
def test_field_existence(self):
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
self.assertTrue(odata._fieldExists(odata.PLACE.GET, "name", boundary, "Secret"))
self.assertFalse(odata._fieldExists(odata.PLACE.GET, "name", boundary, "Nope"))
def test_key_and_entities(self):
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
key, keys = odata._findKeyAndEntities(odata.PLACE.GET, "name", boundary, _EMPTY)
self.assertEqual(key, "Id")
self.assertEqual(keys, [1, 2, 3])
def test_blind_field_extraction_reaches_secret(self):
_t, _p, boundary = odata._detectBoolean(odata.PLACE.GET, "name")
oracle = odata._makeOracle(odata.PLACE.GET, "name", boundary, truePredicate="(Id eq 1)")
self.assertIsNotNone(oracle)
self.assertEqual(odata._inferField(oracle, "Id", 1, "Name"), "luther")
self.assertEqual(odata._inferField(oracle, "Id", 1, "Secret"), "S3CR3Tvalue")
def test_plain_sql_endpoint_not_confirmed(self):
odata._send = lambda place, parameter, value, raw=False: _render(_ENTITIES) # always same
template, _p, _b = odata._detectBoolean(odata.PLACE.GET, "name")
self.assertIsNone(template)
if __name__ == "__main__":
unittest.main(verbosity=2)

329
tests/test_sparql.py Normal file
View file

@ -0,0 +1,329 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
Offline, deterministic tests for the SPARQL injection engine. A mock oracle mirrors the boolean-blind
semantics of a real triple store (a broken-out FILTER reduced to its injected predicate), so detection,
SPARQL-only confirmation, error fingerprinting and schema-agnostic blind extraction are exercised
without a live SPARQL endpoint.
"""
import os
import re
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _testutils import bootstrap
bootstrap()
import lib.techniques.sparql.inject as sparql
from lib.core.settings import SPARQL_ERROR_REGEX
# a tiny fixed graph the mock evaluates against
_TRIPLES = (
("http://example.org/p1", "http://xmlns.com/foaf/0.1/name", "luther"),
("http://example.org/p1", "http://xmlns.com/foaf/0.1/mbox", "luther@example.org"),
("http://example.org/secret", "http://example.org/flag", "S3CR3Tvalue"),
# every character class the transport used to mangle or the charset used to alias:
# '&' split the request, '+' arrived as a space, '"' and '\\' were holes in the bisection
("http://example.org/edge", "http://example.org/raw", 'a&b+c"d\\e()'),
)
_PREDICATES = sorted(set(_[1] for _ in _TRIPLES))
_OBJECTS = sorted(_[2] for _ in _TRIPLES)
def _bind(inner, offset):
if "COUNT(*)" in inner:
return len(_TRIPLES)
if "COUNT(DISTINCT ?p)" in inner:
return len(_PREDICATES)
if "DISTINCT ?p" in inner:
return _PREDICATES[offset] if offset < len(_PREDICATES) else None
if "SELECT ?o" in inner:
return _OBJECTS[offset] if offset < len(_OBJECTS) else None
return None
def _cmp(value, expr):
match = re.match(r"^\?v >= (\d+)$", expr)
if match:
return isinstance(value, int) and value >= int(match.group(1))
match = re.match(r"^STRLEN\(STR\(\?v\)\) >= (\d+)$", expr)
if match:
return len("%s" % value) >= int(match.group(1))
# the literal may be escaped ('\"' / '\\'), the way a real store receives it
match = re.match(r'^SUBSTR\(STR\(\?v\),(\d+),1\) >= "(\\.|.)"$', expr)
if match:
pos, ch = int(match.group(1)), match.group(2)
ch = {'\\"': '"', "\\\\": "\\"}.get(ch, ch)
text = "%s" % value
return (text[pos - 1] if pos <= len(text) else "") >= ch
return False
def _predicate(pred):
pred = pred.strip()
if pred in ("1=1", "(1=1)"):
return True
if pred in ("1=2", "(1=2)"):
return False
if "FILTER(!isIRI(?zo))" in pred:
return False
if pred == "EXISTS { ?zs ?zp ?zo }":
return True
match = re.match(r"^EXISTS \{ SELECT \?v WHERE \{ (.*) FILTER\((.*)\) \} \}$", pred)
if match:
inner, expr = match.group(1).strip(), match.group(2).strip()
off = re.search(r"OFFSET (\d+)", inner)
value = _bind(inner, int(off.group(1)) if off else 0)
return value is not None and _cmp(value, expr)
return False
def _evaluate(value):
for quote, tail in (('"', '""!="'), ("'", "''!='")):
marker, suffix = '%s || (' % quote, ') || %s' % tail
if marker in value and value.endswith(suffix):
return _predicate(value.split(marker, 1)[1][:-len(suffix)])
match = re.match(r"^\d+ \|\| \((.*)\)$", value) # numeric / unquoted slot
if match:
return _predicate(match.group(1))
if value.count('"') % 2 or value.rstrip().endswith(("'", ")", ".")):
return "ERROR"
return any(o == value for _s, p, o in _TRIPLES if p.endswith("name"))
_ROWS = "<html><body><ul><li>luther</li></ul></body></html>"
_EMPTY = "<html><body><ul></ul></body></html>"
_ERROR = "<html><body><pre>Parse error: Lexical error at line 1, column 42.</pre></body></html>"
def _mockSend(place, parameter, value, raw=False):
verdict = _evaluate(value)
if verdict == "ERROR":
return _ERROR if raw else None # a 500 is nulled for the oracle, kept for the error probe
return _ROWS if verdict else _EMPTY
class TestHelpers(unittest.TestCase):
def test_is_error(self):
self.assertTrue(sparql._isError("Parse error: Lexical error at line 1, column 42."))
self.assertFalse(sparql._isError("<html><body><ul><li>luther</li></ul></body></html>"))
def test_backend_from_error(self):
self.assertEqual(sparql._backendFromError("Lexical error at line 1, column 8."), "Apache Jena / Fuseki")
self.assertEqual(sparql._backendFromError("org.eclipse.rdf4j.query.parser.ParseException"), "RDF4J / GraphDB")
self.assertIsNone(sparql._backendFromError("plain results page"))
def test_error_regex_matches_real_jena(self):
self.assertIsNotNone(re.search(SPARQL_ERROR_REGEX, "Parse error: Lexical error at line 1, column 136."))
class TestNoAmpersandInvariant(unittest.TestCase):
"""A raw '&' in a GET value is the parameter delimiter, so any payload carrying '&&' would be split
in transit and arrive as a truncated query (a 500 that mimics a dead oracle). Every boundary and
attribution predicate must therefore use SPARQL's '||' / two-FILTER conjunction, never '&&'."""
def test_boundaries_have_no_ampersand(self):
for row in sparql._BOUNDARY_TABLE:
for field in row[:4]:
self.assertNotIn("&", field, row)
def test_confirm_predicates_have_no_ampersand(self):
for truePred, falsePred in sparql._SPARQL_PREDICATES:
self.assertNotIn("&", truePred)
self.assertNotIn("&", falsePred)
def test_send_url_encodes_the_payload(self):
"""The boundaries being '&'-free was never enough: the character probes emit '&' (and '+') as
DATA. _send() must URL-encode, or those probes split the request and the bisection is answered
by a truncated query."""
sent = []
savedParams, sparql.conf.parameters = sparql.conf.parameters, {sparql.PLACE.GET: "q=luther"}
savedGet = sparql.Request.getPage
sparql.Request.getPage = staticmethod(
lambda **kwargs: (sent.append(sparql.conf.parameters[sparql.PLACE.GET]), "", None, 200)[1:])
try:
sparql._send(sparql.PLACE.GET, "q", sparql._cmpChar(1, ord("&")))
finally:
sparql.Request.getPage = savedGet
sparql.conf.parameters = savedParams
self.assertEqual(len(sent), 1)
self.assertNotIn("&", sent[0].split("=", 1)[1], sent[0])
self.assertIn("%26", sent[0])
class TestCharsetContiguity(unittest.TestCase):
"""The character recovery is a lexicographic '>=' BISECTION, so the charset must have no holes.
Excluding a codepoint does not make it come back as '?' - it makes the bisection converge on the
hole's neighbour and report a DIFFERENT character with no warning. '"' and '\\' consequently stay in
the charset and travel as their SPARQL escapes.
(This replaces an earlier assertion that pinned the opposite - that 0x22/0x5c were EXCLUDED. That
contract was wrong: live against Jena Fuseki it silently decoded 'A&x', 'A)x' and 'A+x' all as
'A%x'. The gap is the bug, not the fix.)"""
def test_charset_is_contiguous(self):
self.assertEqual(sparql._CS_ORDS,
list(range(sparql.SPARQL_CHAR_MIN, sparql.SPARQL_CHAR_MAX + 1)))
def test_meta_characters_travel_escaped(self):
self.assertEqual(sparql._literal(0x22), '"\\""')
self.assertEqual(sparql._literal(0x5c), '"\\\\"')
self.assertEqual(sparql._literal(ord('a')), '"a"')
def test_every_char_probe_is_a_well_formed_literal(self):
# exactly one opening and one closing quote, everything inside either plain or backslash-escaped
for ordinal in sparql._CS_ORDS:
probe = sparql._cmpChar(1, ordinal)
body = probe.split(">= ", 1)[1]
self.assertTrue(body.startswith('"') and body.endswith('"'), probe)
inner = body[1:-1]
self.assertEqual(inner.replace('\\"', "").replace("\\\\", "").count('"'), 0, probe)
def _mockSendNumeric(place, parameter, value, raw=False):
"""An UNQUOTED numeric slot: FILTER(?age = <value>). A string boundary lands a bare word / stray
quote in a term position, which a real store rejects outright - only the numeric shape parses."""
match = re.match(r"^\d+ \|\| \((.*)\)$", value)
if match:
return _ROWS if _predicate(match.group(1)) else _EMPTY
if value.isdigit():
return _ROWS
return _ERROR if raw else None # syntax error: nulled for the oracle, kept for the probe
class TestNumericBoundary(unittest.TestCase):
"""The unquoted numeric/term slot. The base has to be a valid SPARQL TERM there - a random word is
not one, it is a syntax error, so the row as first written could never fire (verified live: the old
shape returned 500 'Lexical error ... after prefix "zzsentinelzz"' from Jena, the new one 200)."""
def setUp(self):
self.saved, self.savedParams = sparql._send, sparql.conf.parameters
sparql._send = _mockSendNumeric
sparql.conf.parameters = {sparql.PLACE.GET: "age=30"}
sparql.SENTINEL, sparql.NUMBER_SENTINEL = "zzsentinelzz", "961962811"
def tearDown(self):
sparql._send, sparql.conf.parameters = self.saved, self.savedParams
def test_numeric_row_uses_a_numeric_base(self):
row = [_ for _ in sparql._BOUNDARY_TABLE if _[4] == sparql._BASE_NUMBER]
self.assertEqual(len(row), 1)
trueBreak, falseBreak, prefix, suffix, _kind = row[0]
boundary = sparql.Boundary(prefix, suffix, sparql._BASE_NUMBER)
self.assertEqual(sparql._base(boundary), "961962811")
# the payload must be a valid term followed by an OR - no quote to close, no paren to re-balance
self.assertEqual(sparql._base(boundary) + trueBreak, "961962811 || (1=1)")
self.assertEqual(sparql._base(boundary) + falseBreak, "961962811 || (1=2)")
self.assertEqual(sparql._wrap(sparql._base(boundary), boundary, "PRED"), "961962811 || (PRED)")
def test_string_rows_keep_the_word_base(self):
for row in [_ for _ in sparql._BOUNDARY_TABLE if _[4] == sparql._BASE_STRING]:
boundary = sparql.Boundary(row[2], row[3], sparql._BASE_STRING)
self.assertEqual(sparql._base(boundary), "zzsentinelzz")
def test_numeric_slot_detects_and_extracts(self):
template, payload, boundary = sparql._detectBoolean(sparql.PLACE.GET, "age")
self.assertIsNotNone(template)
self.assertEqual(boundary.base, sparql._BASE_NUMBER)
self.assertTrue(payload.startswith("961962811"))
truth = sparql._makeOracle(sparql.PLACE.GET, "age", boundary)
self.assertIsNotNone(truth)
self.assertEqual(sparql._inferString(truth, sparql._nthObject(0)), _OBJECTS[0])
class TestErrorStatusIsNeverAnOracleSample(unittest.TestCase):
"""A 4xx body must be a NON-ANSWER, not a cheap false. Nulling only 5xx let a front-end that serves
its parser failure as a generic 400 feed that body to the oracle, and when it resembled the FALSE
model the bit was decided FALSE instead of INCONCLUSIVE. Measured live: a stored 'A(x' came back as
'A\'x'. The error probe still needs the body, hence raw=True."""
def _sendWithCode(self, code, raw):
savedParams, sparql.conf.parameters = sparql.conf.parameters, {sparql.PLACE.GET: "q=luther"}
savedGet = sparql.Request.getPage
sparql.Request.getPage = staticmethod(lambda **kwargs: ("BODY", None, code))
try:
return sparql._send(sparql.PLACE.GET, "q", "x", raw=raw)
finally:
sparql.Request.getPage = savedGet
sparql.conf.parameters = savedParams
def test_4xx_and_5xx_are_nulled_for_the_oracle(self):
for code in (400, 403, 404, 429, 500, 503):
self.assertIsNone(self._sendWithCode(code, raw=False), code)
def test_2xx_and_3xx_are_usable(self):
for code in (200, 204, 302):
self.assertEqual(self._sendWithCode(code, raw=False), "BODY", code)
def test_raw_keeps_the_body_for_the_error_probe(self):
for code in (400, 500):
self.assertEqual(self._sendWithCode(code, raw=True), "BODY", code)
class TestOriginalValue(unittest.TestCase):
def setUp(self):
self.savedParams = sparql.conf.parameters
self.savedDict = sparql.conf.paramDict
sparql.conf.parameters = {sparql.PLACE.GET: "q=luther"}
def tearDown(self):
sparql.conf.parameters = self.savedParams
sparql.conf.paramDict = self.savedDict
def test_reads_and_replaces(self):
self.assertEqual(sparql._originalValue(sparql.PLACE.GET, "q"), "luther")
self.assertEqual(sparql._replaceSegment(sparql.PLACE.GET, "q", "X"), "q=X")
class TestDetectionAndExtraction(unittest.TestCase):
def setUp(self):
self.saved = sparql._send
sparql._send = _mockSend
self.savedParams = sparql.conf.parameters
sparql.conf.parameters = {sparql.PLACE.GET: "q=luther"}
sparql.SENTINEL = "zzsentinelzz"
def tearDown(self):
sparql._send = self.saved
sparql.conf.parameters = self.savedParams
def test_boolean_detection(self):
template, payload, boundary = sparql._detectBoolean(sparql.PLACE.GET, "q")
self.assertIsNotNone(template)
self.assertEqual(boundary.base, sparql._BASE_STRING)
def test_confirms_sparql(self):
_t, _p, boundary = sparql._detectBoolean(sparql.PLACE.GET, "q")
self.assertTrue(sparql._confirmSparql(sparql.PLACE.GET, "q", boundary))
def test_error_probe_fingerprints_jena(self):
backend, _page = sparql._probeError(sparql.PLACE.GET, "q")
self.assertEqual(backend, "Apache Jena / Fuseki")
def test_blind_extraction_recovers_objects(self):
_t, _p, boundary = sparql._detectBoolean(sparql.PLACE.GET, "q")
truth = sparql._makeOracle(sparql.PLACE.GET, "q", boundary)
self.assertIsNotNone(truth)
self.assertEqual(sparql._inferCount(truth, sparql._COUNT_TRIPLES, 10 ** 9), len(_TRIPLES))
self.assertEqual(sparql._inferCount(truth, sparql._COUNT_PREDICATES, 64), len(_PREDICATES))
# every seeded object comes back BYTE-FOR-BYTE, including the edge value whose '&', '+', '"'
# and '\' used to be silently rewritten (transport split / charset hole)
for offset, expected in enumerate(_OBJECTS):
self.assertEqual(sparql._inferString(truth, sparql._nthObject(offset)), expected)
self.assertEqual(sparql._inferString(truth, sparql._nthPredicate(0)), _PREDICATES[0])
def test_plain_sql_endpoint_is_not_confirmed(self):
# an endpoint where the injected predicate has no effect (always same page) must not confirm
sparql._send = lambda place, parameter, value, raw=False: _ROWS
template, _p, boundary = sparql._detectBoolean(sparql.PLACE.GET, "q")
self.assertIsNone(template) # no true/false divergence -> not even boolean-detected
if __name__ == "__main__":
unittest.main(verbosity=2)

View file

@ -24,7 +24,10 @@ from _testutils import bootstrap
bootstrap()
from lib.core.settings import XSLT_ERROR_REGEX
from lib.core.settings import XSLT_RCE_PROBES
from lib.core.settings import XSLT_BRIDGES
from lib.core.settings import XSLT_BRIDGE_PHP
from lib.core.settings import XSLT_BRIDGE_JAVA
from lib.core.settings import XSLT_ADVISORY_PROBES
from lib.core.settings import XSLT_VENDOR_PROPERTIES
from lib.core.settings import XQUERY_CAPABILITY_PROBES
from lib.core.settings import XQUERY_FILE_READ
@ -55,6 +58,48 @@ class XsltPayloadTest(unittest.TestCase):
self.assertEqual([_ for _, __ in _xslt._BUILDERS], [_xslt.CONTEXT_ELEMENT, _xslt.CONTEXT_VALUE])
class XsltAttributeEscapingTest(unittest.TestCase):
"""Every slot lands inside select="...". XPath 1.0 has no escape character, so _quote() falls back
to DOUBLE quotes for any value holding an apostrophe - and unescaped those terminate the attribute,
the stylesheet never compiles, and the probe silently returns nothing. Live against libxslt that
was '--os-cmd="echo \'hi\'"' reporting "no output captured", and '--file-read' of a path with an
apostrophe or an '&' failing while the same file at a plain path read fine."""
# command / path what makes it hostile
HOSTILE = ("echo 'hello world'", # apostrophes -> _quote uses "..."
"grep -r 'x' /etc",
"/tmp/o'brien.xml",
"/tmp/a&b.xml", # bare '&' ends the attribute value
"cat /etc/passwd && id",
"/tmp/<script>.xml")
def _wellFormed(self, payload):
from xml.dom.minidom import parseString
parseString('<r xmlns:xsl="http://www.w3.org/1999/XSL/Transform">%s</r>' % payload)
def test_element_payloads_stay_well_formed_xml(self):
for value in self.HOSTILE:
self._wellFormed(_xslt._elementPayload(_xslt._wrap("php:function('system',%s)" % _xslt._quote(value))))
def test_bridge_payloads_stay_well_formed_xml(self):
for label, _kind, prefix, uri, readT, execT in XSLT_BRIDGES:
for value in self.HOSTILE:
for template in [_ for _ in (readT, execT) if _]:
payload = _xslt._bridgeElementPayload(prefix, uri, template % _xslt._quote(value))
self._wellFormed(payload)
def test_value_slot_payloads_stay_attribute_safe(self):
for value in self.HOSTILE:
payload = _xslt._valuePayload(_xslt._wrap("string(%s)" % _xslt._quote(value)))
self._wellFormed('<xsl:value-of select="%s"/>' % payload)
def test_escaping_is_minimal(self):
# '>' and "'" are legal inside a double-quoted attribute and must survive untouched, so a shell
# redirect and an XPath literal stay readable in the payload log
self.assertEqual(_xslt._attr("a>b'c"), "a>b'c")
self.assertEqual(_xslt._attr('x&y<z"w'), "x&amp;y&lt;z&quot;w")
class XsltReflectionSafetyTest(unittest.TestCase):
"""The tier that reports a finding must not be satisfiable by an application that merely echoes the
parameter. This is the exact defect two reviewers found: a plain search page was reported vulnerable
@ -161,21 +206,37 @@ class XsltFingerprintTest(unittest.TestCase):
self.assertTrue(prop.startswith("xsl:"), prop)
class XsltRcePolicyTest(unittest.TestCase):
"""The extension surfaces are reported, never invoked - the probes must only ASK whether a function
exists, never call it."""
class XsltBridgePolicyTest(unittest.TestCase):
"""Read/exec bridges are DRIVEN (file read is automatic, command exec only under --os-cmd/--os-shell);
the file-WRITE and eval surfaces are advisory - reported, never invoked."""
def test_probes_only_test_availability(self):
for label, expression in XSLT_RCE_PROBES:
def test_every_bridge_is_well_formed(self):
for label, kind, prefix, uri, readT, execT in XSLT_BRIDGES:
self.assertIn(kind, (XSLT_BRIDGE_PHP, XSLT_BRIDGE_JAVA), label)
self.assertTrue(prefix and uri.startswith("http"), label)
self.assertIn("%s", readT, label) # read template takes the quoted path
self.assertNotIn("%s%s", readT, label)
if execT is not None:
self.assertIn("%s", execT, label)
def test_labels_name_the_primitive(self):
joined = " ".join(bridge[0] for bridge in XSLT_BRIDGES).lower()
self.assertIn("php", joined)
self.assertIn("java", joined)
def test_advisory_probes_only_ask_availability(self):
# the file-write / eval surfaces must be pure availability checks, never a call that acts
for label, expression in XSLT_ADVISORY_PROBES:
self.assertTrue(expression.startswith("string(function-available(")
or expression.startswith("string(element-available("), expression)
self.assertNotIn("Runtime.exec", expression)
self.assertNotIn("system(", expression)
self.assertNotIn("document(", expression.replace("element-available", ""))
def test_labels_say_what_the_primitive_is(self):
joined = " ".join(label for label, _ in XSLT_RCE_PROBES).lower()
for expected in ("php", "exsl", "saxon", "java"):
self.assertIn(expected, joined)
def test_write_surface_is_advisory_not_a_bridge(self):
# exsl:document is a WRITE primitive and must never sit among the driven read/exec bridges
self.assertTrue(any("exsl" in label.lower() for label, _ in XSLT_ADVISORY_PROBES))
self.assertFalse(any("exsl" in bridge[0].lower() for bridge in XSLT_BRIDGES))
class XQueryTierTest(unittest.TestCase):