|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.org/) |
| 5 | +See the file 'doc/COPYING' for copying permission |
| 6 | +""" |
| 7 | + |
| 8 | +import re |
| 9 | + |
| 10 | +from lib.core.common import zeroDepthSearch |
| 11 | +from lib.core.enums import PRIORITY |
| 12 | + |
| 13 | +__priority__ = PRIORITY.HIGHEST |
| 14 | + |
| 15 | +def dependencies(): |
| 16 | + pass |
| 17 | + |
| 18 | +def tamper(payload, **kwargs): |
| 19 | + """ |
| 20 | + Replaces plus ('+') character with ODBC function {fn CONCAT()} |
| 21 | +
|
| 22 | + Tested against: |
| 23 | + * Microsoft SQL Server 2008 |
| 24 | +
|
| 25 | + Requirements: |
| 26 | + * Microsoft SQL Server 2008+ |
| 27 | +
|
| 28 | + Notes: |
| 29 | + * Useful in case ('+') character is filtered |
| 30 | + * https://msdn.microsoft.com/en-us/library/bb630290.aspx |
| 31 | +
|
| 32 | + >>> tamper('SELECT CHAR(113)+CHAR(114)+CHAR(115) FROM DUAL') |
| 33 | + 'SELECT {fn CONCAT({fn CONCAT(CHAR(113),CHAR(114))},CHAR(115))} FROM DUAL' |
| 34 | +
|
| 35 | + >>> tamper('SELECT (CHAR(113)+CHAR(114)+CHAR(115)) FROM DUAL') |
| 36 | + 'SELECT {fn CONCAT({fn CONCAT(CHAR(113),CHAR(114))},CHAR(115))} FROM DUAL' |
| 37 | + """ |
| 38 | + |
| 39 | + retVal = payload |
| 40 | + |
| 41 | + if payload: |
| 42 | + while True: |
| 43 | + indexes = zeroDepthSearch(retVal, '+') |
| 44 | + |
| 45 | + if indexes: |
| 46 | + first, last = 0, 0 |
| 47 | + for i in xrange(1, len(indexes)): |
| 48 | + if ' ' in retVal[indexes[0]:indexes[i]]: |
| 49 | + break |
| 50 | + else: |
| 51 | + last = i |
| 52 | + |
| 53 | + start = retVal[:indexes[first]].rfind(' ') + 1 |
| 54 | + end = (retVal[indexes[last] + 1:].find(' ') + indexes[last] + 1) if ' ' in retVal[indexes[last] + 1:] else len(retVal) - 1 |
| 55 | + |
| 56 | + count = 0 |
| 57 | + chars = [char for char in retVal] |
| 58 | + for index in indexes[first:last + 1]: |
| 59 | + if count == 0: |
| 60 | + chars[index] = ',' |
| 61 | + else: |
| 62 | + chars[index] = '\x01' |
| 63 | + count += 1 |
| 64 | + |
| 65 | + retVal = "%s%s%s)}%s" % (retVal[:start], "{fn CONCAT(" * count, ''.join(chars)[start:end].replace('\x01', ")},"), retVal[end:]) |
| 66 | + else: |
| 67 | + match = re.search(r"\((CHAR\(\d+.+CHAR\(\d+\))\)", retVal) |
| 68 | + if match: |
| 69 | + part = match.group(0) |
| 70 | + indexes = set(zeroDepthSearch(match.group(1), '+')) |
| 71 | + if not indexes: |
| 72 | + break |
| 73 | + |
| 74 | + count = 0 |
| 75 | + chars = [char for char in part] |
| 76 | + for i in xrange(1, len(chars)): |
| 77 | + if i - 1 in indexes: |
| 78 | + if count == 0: |
| 79 | + chars[i] = ',' |
| 80 | + else: |
| 81 | + chars[i] = '\x01' |
| 82 | + count += 1 |
| 83 | + |
| 84 | + replacement = "%s%s}" % (("{fn CONCAT(" * count)[:-1], "".join(chars).replace('\x01', ")},")) |
| 85 | + retVal = retVal.replace(part, replacement) |
| 86 | + else: |
| 87 | + break |
| 88 | + |
| 89 | + return retVal |
0 commit comments