Skip to content

Commit aa42dc0

Browse files
committed
Fixing if2case tamper corrupting arguments that contain a function call (e.g. IF(...,SLEEP(5),...))
1 parent 0a6dbd2 commit aa42dc0

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from thirdparty import six
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.10.7.232"
23+
VERSION = "1.10.7.233"
2424
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2525
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2626
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

tamper/if2case.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,34 @@
1414
def dependencies():
1515
pass
1616

17+
def _unwrap(expr):
18+
"""
19+
Strips only FULLY-wrapping outer parentheses (e.g. '(1=1)' -> '1=1'), leaving a bare function
20+
call such as 'SLEEP(5)' intact - unlike str.strip('()') which would drop its trailing ')'
21+
"""
22+
23+
expr = expr.strip()
24+
25+
while len(expr) > 1 and expr[0] == '(' and expr[-1] == ')':
26+
depth = 0
27+
wrapper = True
28+
29+
for i in xrange(len(expr)):
30+
if expr[i] == '(':
31+
depth += 1
32+
elif expr[i] == ')':
33+
depth -= 1
34+
if depth == 0 and i != len(expr) - 1: # the opening '(' closes before the end
35+
wrapper = False
36+
break
37+
38+
if not wrapper:
39+
break
40+
41+
expr = expr[1:-1].strip()
42+
43+
return expr
44+
1745
def tamper(payload, **kwargs):
1846
"""
1947
Replaces instances like 'IF(A, B, C)' with 'CASE WHEN (A) THEN (B) ELSE (C) END' counterpart
@@ -62,9 +90,9 @@ def tamper(payload, **kwargs):
6290
depth -= 1
6391

6492
if len(commas) == 2 and end:
65-
a = payload[index + len("IF("):commas[0]].strip("()")
66-
b = payload[commas[0] + 1:commas[1]].lstrip().strip("()")
67-
c = payload[commas[1] + 1:end].lstrip().strip("()")
93+
a = _unwrap(payload[index + len("IF("):commas[0]])
94+
b = _unwrap(payload[commas[0] + 1:commas[1]])
95+
c = _unwrap(payload[commas[1] + 1:end])
6896
newVal = "CASE WHEN (%s) THEN (%s) ELSE (%s) END" % (a, b, c)
6997
payload = payload[:index] + newVal + payload[end + 1:]
7098
else:

0 commit comments

Comments
 (0)