Fix for --graphql error-based detection ignoring HTTP 5xx error responses

This commit is contained in:
Miroslav Štampar 2026-11-12 17:09:04 +01:00
parent ed8ad1ec77
commit 6fb39709f2
3 changed files with 20 additions and 19 deletions

View file

@ -19,7 +19,7 @@ from lib.core.enums import OS
from thirdparty import six
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
VERSION = "1.10.11.63"
VERSION = "1.10.11.64"
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

View file

@ -357,8 +357,9 @@ def _cell(value):
# --- HTTP transport ---------------------------------------------------------
def _gqlSend(endpoint, query, variables=None):
def _gqlSend(endpoint, query, variables=None, errorBody=False):
# POST a JSON GraphQL request to `endpoint`, returning (body, http_code)
# Note: errorBody keeps 5xx bodies (e.g. SQL error inside GraphQL 'errors') for error-message fingerprinting
body = {"query": query}
if variables:
body["variables"] = variables
@ -382,7 +383,7 @@ def _gqlSend(endpoint, query, variables=None):
return None, 0
finally:
kb.postHint = oldPostHint
if blockedStatus(code): # WAF / rate-limit / 5xx is not a usable oracle sample
if blockedStatus(code) and not (errorBody and code >= 500): # WAF / rate-limit / 5xx is not a usable oracle sample
return None, code
return page or "", code
@ -944,16 +945,16 @@ def _detectError(slot, endpoint):
# influence. Require the signature to be ABSENT from a benign baseline, appear only AFTER the
# injected value, and REPRODUCE before accepting it.
benign = _buildQuery(slot, "1")
basePage = _gqlSend(endpoint, benign)[0] if benign else None
basePage = _gqlSend(endpoint, benign, errorBody=True)[0] if benign else None
baseErr = _errorText(basePage) or ""
def _appearsOnlyAfterInjection(query, matcher):
# matcher(text) -> re.Match or None; True only if it hits the injected response, is absent
# from the benign baseline, and reproduces on a second injected request
err = _errorText(_gqlSend(endpoint, query)[0])
err = _errorText(_gqlSend(endpoint, query, errorBody=True)[0])
if not err or not matcher(err) or matcher(baseErr):
return None
err2 = _errorText(_gqlSend(endpoint, query)[0])
err2 = _errorText(_gqlSend(endpoint, query, errorBody=True)[0])
return matcher(err) if (err2 and matcher(err2)) else None
for payload in _SQL_ERROR_PAYLOADS:
@ -963,7 +964,7 @@ def _detectError(slot, endpoint):
for pattern in ERROR_PARSING_REGEXES:
m = _appearsOnlyAfterInjection(query, lambda t, p=pattern: re.search(p, t))
if m:
detail = m.group("result") if "result" in m.groupdict() else _errorText(_gqlSend(endpoint, query)[0])[:200]
detail = m.group("result") if "result" in m.groupdict() else _errorText(_gqlSend(endpoint, query, errorBody=True)[0])[:200]
return "error-based", detail, payload
for payload in (_NOSQL_NE, _NOSQL_IN):
@ -972,7 +973,7 @@ def _detectError(slot, endpoint):
continue
m = _appearsOnlyAfterInjection(query, lambda t: re.search(NOSQL_ERROR_REGEX, t))
if m:
return "nosql-error-based", (_errorText(_gqlSend(endpoint, query)[0]) or "")[:200], payload
return "nosql-error-based", (_errorText(_gqlSend(endpoint, query, errorBody=True)[0]) or "")[:200], payload
return None, None, None

View file

@ -250,7 +250,7 @@ class TestGraphqlBooleanDetection(unittest.TestCase):
gi.conf = type("C", (), {"url": "http://test/graphql"})()
pages = {"true": MATCH, "false": NOMATCH}
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'1'='1" in query:
return pages["true"], 200
if "'1'='2" in query:
@ -276,7 +276,7 @@ class TestGraphqlBooleanDetection(unittest.TestCase):
def test_graphql_two_true_transport_failures_do_not_confirm(self):
# the TRUE query fails transport (-> None), the FALSE succeeds: two None trues must NOT be
# read as a reproducible page that "differs" from false (the classic fabricated confirmation)
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'1'='1" in query:
return None, 0 # transport failure on the true payload
return NOMATCH, 200
@ -288,7 +288,7 @@ class TestGraphqlBooleanDetection(unittest.TestCase):
def test_graphql_false_page_is_replayed(self):
# a FALSE page that does not reproduce (jitter) must not establish an oracle
state = {"n": 0}
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'1'='1" in query:
return MATCH, 200
state["n"] += 1
@ -304,7 +304,7 @@ class TestGraphqlBooleanDetection(unittest.TestCase):
# P0-3: the FALSE payload trips a stable HTTP-200 resolver error ({data:null, errors:[...]}),
# which yields the same {"user":null} observation as a genuine false. That is NOT a boolean
# oracle (it belongs to error-based detection) - _detectBoolean must reject the errored pair.
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'1'='1" in query: # true -> real rows
return MATCH, 200
return '{"data":{"user":null},"errors":[{"message":"resolver failed","path":["user"]}]}', 200
@ -330,7 +330,7 @@ class TestGraphqlErrorDetection(unittest.TestCase):
self._conf = gi.conf
gi.conf = type("C", (), {"url": "http://test/graphql"})()
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'" in query and "'1'='1" not in query:
return DB_ERROR, 500
return NOMATCH, 200
@ -349,7 +349,7 @@ class TestGraphqlErrorDetection(unittest.TestCase):
# boolean payloads do NOT diverge (both -> NOMATCH) so boolean detection fails; only the error
# payloads trip a DB error. The reported reproducer must be the WINNING error payload, never the
# generic ' OR '1'='1 boolean payload.
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "'1'='" in query: # both boolean payloads ('1'='1 / '1'='2) -> identical, no oracle
return NOMATCH, 200
if "'" in query: # error payloads (', '', '") -> DB error
@ -415,7 +415,7 @@ class TestGraphqlEndpointDetection(unittest.TestCase):
def setUp(self):
self._gql = gi._gqlSend
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if endpoint.endswith("/graphql") and "__typename" in query:
return '{"data":{"__typename":"Query"}}', 200
return 'Not Found', 404
@ -433,7 +433,7 @@ class TestGraphqlEndpointDetection(unittest.TestCase):
self.assertEqual(endpoint, "http://test/graphql")
def test_not_graphql_endpoint(self):
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
return 'Not Found', 404
gi._gqlSend = fakeSend
endpoint, page = gi._detectEndpoint("http://test", probePaths=True)
@ -454,7 +454,7 @@ class TestGraphqlIntrospectionFallback(unittest.TestCase):
def test_fallback_without_specifiedByURL(self):
calls = []
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
calls.append(query)
if "specifiedByURL" in query:
return '{"errors":[{"message":"Unknown field specifiedByURL"}]}', 400
@ -684,7 +684,7 @@ class TestGraphqlInference(unittest.TestCase):
NOMATCHV = '{"data":{"user":null}}'
saved = gi._gqlSend
try:
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "1=1" in query:
return MATCHV, 200
if "1=2" in query:
@ -733,7 +733,7 @@ class TestGraphqlMakeOracle(unittest.TestCase):
def setUp(self):
self._gql = gi._gqlSend
def fakeSend(endpoint, query, variables=None):
def fakeSend(endpoint, query, variables=None, **kwargs):
if "a0:" in query: # batched, aliased request
data = {}
for m in re.finditer(r'(a\d+):\w+\(\w+:"[^"]*\((1=1|1=2)\)', query):