Skip to content

Commit 4585243

Browse files
committed
Implements tamper script if2case (#5301)
1 parent fbfed06 commit 4585243

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

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.six import unichr as _unichr
2121

2222
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
23-
VERSION = "1.7.1.12"
23+
VERSION = "1.7.2.0"
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Copyright (c) 2006-2023 sqlmap developers (https://sqlmap.org/)
5+
See the file 'doc/COPYING' for copying permission
6+
"""
7+
8+
from lib.core.compat import xrange
9+
from lib.core.enums import PRIORITY
10+
11+
__priority__ = PRIORITY.HIGHEST
12+
13+
def dependencies():
14+
pass
15+
16+
def tamper(payload, **kwargs):
17+
"""
18+
Replaces instances like 'IF(A, B, C)' with 'CASE WHEN (A) THEN (B) ELSE (C) END' counterpart
19+
20+
Requirement:
21+
* MySQL
22+
* SQLite (possibly)
23+
* SAP MaxDB (possibly)
24+
25+
Tested against:
26+
* MySQL 5.0 and 5.5
27+
28+
Notes:
29+
* Useful to bypass very weak and bespoke web application firewalls
30+
that filter the IF() functions
31+
32+
>>> tamper('IF(1, 2, 3)')
33+
'CASE WHEN (1) THEN (2) ELSE (3) END'
34+
>>> tamper('SELECT IF((1=1), (SELECT "foo"), NULL)')
35+
'SELECT CASE WHEN (1=1) THEN (SELECT "foo") ELSE (NULL) END'
36+
"""
37+
38+
if payload and payload.find("IF") > -1:
39+
while payload.find("IF(") > -1:
40+
index = payload.find("IF(")
41+
depth = 1
42+
commas, end = [], None
43+
44+
for i in xrange(index + len("IF("), len(payload)):
45+
if depth == 1 and payload[i] == ',':
46+
commas.append(i)
47+
48+
elif depth == 1 and payload[i] == ')':
49+
end = i
50+
break
51+
52+
elif payload[i] == '(':
53+
depth += 1
54+
55+
elif payload[i] == ')':
56+
depth -= 1
57+
58+
if len(commas) == 2 and end:
59+
a = payload[index + len("IF("):commas[0]].strip("()")
60+
b = payload[commas[0] + 1:commas[1]].lstrip().strip("()")
61+
c = payload[commas[1] + 1:end].lstrip().strip("()")
62+
newVal = "CASE WHEN (%s) THEN (%s) ELSE (%s) END" % (a, b, c)
63+
payload = payload[:index] + newVal + payload[end + 1:]
64+
else:
65+
break
66+
67+
return payload

0 commit comments

Comments
 (0)