|
| 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