sqlmap/tamper/ifnull2ifisnull.py

50 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file 'doc/COPYING' for copying permission
"""
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.HIGHEST
2011-04-04 12:18:26 +04:00
def tamper(payload):
"""
Replaces 'IFNULL(A, B)' with 'IF(ISNULL(A), B, A)'
Example: 'IFNULL(1, 2)' becomes 'IF(ISNULL(1), 2, 1)'
"""
2011-04-04 12:18:26 +04:00
if payload and payload.find("IFNULL") > -1:
2011-04-04 12:18:26 +04:00
while payload.find("IFNULL(") > -1:
index = payload.find("IFNULL(")
deepness = 1
comma, end = None, None
2011-04-04 12:18:26 +04:00
for i in xrange(index + len("IFNULL("), len(payload)):
if deepness == 1 and payload[i] == ',':
comma = i
2011-04-04 12:18:26 +04:00
elif deepness == 1 and payload[i] == ')':
end = i
break
2011-04-04 12:18:26 +04:00
elif payload[i] == '(':
deepness += 1
2011-04-04 12:18:26 +04:00
elif payload[i] == ')':
deepness -= 1
if comma and end:
2011-04-04 12:18:26 +04:00
A = payload[index + len("IFNULL("):comma]
B = payload[comma + 1:end]
newVal = "IF(ISNULL(%s),%s,%s)" % (A, B, A)
2011-04-04 12:18:26 +04:00
payload = payload[:index] + newVal + payload[end+1:]
else:
break
2011-04-04 12:18:26 +04:00
return payload