SQLMap绕过脚本tamper(from 《Web安全攻防渗透测试实战指南》)

目录

1 tamper作用

2 tamper格式

3 检测是否有IDS/IPS/WAF


1 tamper作用

SQLMap在默认情况下除了使用CHAR()函数防止出现单引号,没有对注入的数据进行修改,读者还可以使用 --tamper参数对数据做修改来绕过WAF等设备,其中大部分脚本主要用正则模块替换攻击载荷字符编码的方式尝试绕过WAF的检测规则,命令如下所示。

sqlmap.py XXXXX --tamper "模块名"

SQLMap1.4.9.3 本版本目前官方提供63个绕过脚本,下面是一个 tamper脚本的格式。

#!/usr/bin/env python

"""
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""

from lib.core.enums import PRIORITY

__priority__ = PRIORITY.LOWEST

def dependencies():
    pass

def tamper(payload, **kwargs):
    """
    Replaces apostrophe character (') with its UTF-8 full width counterpart (e.g. ' -> %EF%BC%87)

    References:
        * http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65280&number=128
        * http://lukasz.pilorz.net/testy/unicode_conversion/
        * http://sla.ckers.org/forum/read.php?13,11562,11850
        * http://lukasz.pilorz.net/testy/full_width_utf/index.phps

    >>> tamper("1 AND '1'='1")
    '1 AND %EF%BC%871%EF%BC%87=%EF%BC%871'
    """

    return payload.replace('\'', "%EF%BC%87") if payload else payload

这个脚本的作用是将引号替换为utf-8,用于过滤单引号。

使用脚本前语句为

1 AND '1'='1

使用脚本后

1 AND %EF%BC%871%EF%BC%87=%EF%BC%871

2 tamper格式

不难看出,一个最小的 tamper脚本结构为 priority变量定义和dependencies、 tamper函数定义。
priority定义脚本的优先级,用于有多个 tamper脚本的情况dependencies函数声明该脚本适用/不适用的范围,可以为空。

下面以一个转大写字符绕过的脚本为例, tamper绕过脚本主要由dependencies和 tamper两个函数构成。 def tamper(payload, **kwargs)函数接收 payload和**kwargs返回一个 Payload。下面这段代码的意思是通过正则匹配所有字符,将所有攻击载荷中的字符转换为大写字母。

#!/usr/bin/env python

"""
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
See the file 'LICENSE' for copying permission
"""

import re

from lib.core.data import kb
from lib.core.enums import PRIORITY

__priority__ = PRIORITY.NORMAL

def dependencies():
    pass

def tamper(payload, **kwargs):
    """
    Replaces each keyword character with upper case value (e.g. select -> SELECT)

    Tested against:
        * Microsoft SQL Server 2005
        * MySQL 4, 5.0 and 5.5
        * Oracle 10g
        * PostgreSQL 8.3, 8.4, 9.0

    Notes:
        * Useful to bypass very weak and bespoke web application firewalls
          that has poorly written permissive regular expressions
        * This tamper script should work against all (?) databases

    >>> tamper('insert')
    'INSERT'
    """

    retVal = payload

    if payload:
        for match in re.finditer(r"[A-Za-z_]+", retVal):
            word = match.group()

            if word.upper() in kb.keywords:
                retVal = retVal.replace(word, word.upper())

    return retVal

3 检测是否有IDS/IPS/WAF

在日常使用中,我们会对一些网站是否有安全防护(WAF/IDS/IPS)进行试探,可以使用参数--identify-waf进行检测。

虽然 SQLMap自带的 tamper可以做很多事情,但在实际环境中,往往比较复杂,可能会遇到很多情况, tamper不可能很全面地应对各种环境,所以建议在学习如何使用自带的 tamper的同时,最好能够掌握 Tamper的编写规则,这样在应对各种实战环境时才能更自如。

你可能感兴趣的:(渗透测试)