2020-07-31
1.4k
SQL注入专项之MySQL基础注入语法
基本上摘自k0rz3n师傅的一篇文章,日常膜。
字符串操作
合并字符串
concat
1 2 3 4 5 6 7
| mysql> select concat('hel','lo','world'); +----------------------------+ | concat('hel','lo','world') | +----------------------------+ | helloworld | +----------------------------+ 1 row in set (0.00 sec)
|
concat_ws
1 2 3 4 5 6 7
| mysql> select concat_ws('+', 'hello', 'world'); +----------------------------------+ | concat_ws('+', 'hello', 'world') | +----------------------------------+ | hello+world | +----------------------------------+ 1 row in set (0.00 sec)
|
截取字符串
substr或substring或mid
1 2 3 4 5 6 7
| mysql> select substring('www.baidu.com','5',5); +----------------------------------+ | substring('www.baidu.com','5',5) | +----------------------------------+ | baidu | +----------------------------------+ 1 row in set (0.00 sec)
|
left
1 2 3 4 5 6 7
| mysql> select left('flag{this_is_test}', 5); +-------------------------------+ | left('flag{this_is_test}', 5) | +-------------------------------+ | flag{ | +-------------------------------+ 1 row in set (0.00 sec)
|
right
1 2 3 4 5 6 7
| mysql> select right('flag{this_is_test}', 5); +--------------------------------+ | right('flag{this_is_test}', 5) | +--------------------------------+ | test} | +--------------------------------+ 1 row in set (0.00 sec)
|
substring_index
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| mysql> select substring_index('flag{111111-222222-333333}','-',1); +-----------------------------------------------------+ | substring_index('flag{111111-222222-333333}','-',1) | +-----------------------------------------------------+ | flag{111111 | +-----------------------------------------------------+ 1 row in set (0.00 sec)
mysql> select substring_index('flag{111111-222222-333333}','-',-1); +------------------------------------------------------+ | substring_index('flag{111111-222222-333333}','-',-1) | +------------------------------------------------------+ | 333333} | +------------------------------------------------------+ 1 row in set (0.00 sec)
mysql> select substring_index('flag{111111-222222-333333}','-',-2); +------------------------------------------------------+ | substring_index('flag{111111-222222-333333}','-',-2) | +------------------------------------------------------+ | 222222-333333} | +------------------------------------------------------+ 1 row in set (0.00 sec)
|
计算字符串长度
char_length()/character_length() 和 length()/octet_length()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select char_length('你好'); +-----------------------+ | char_length('你好') | +-----------------------+ | 2 | +-----------------------+ 1 row in set (0.00 sec)
mysql> select length('你好'); +------------------+ | length('你好') | +------------------+ | 6 | +------------------+ 1 row in set (0.01 sec)
|
char_length()/character_length() 计算的是字符的个数,而length()/octet_length() 计算的是字节的长度
改变存储的字符串
将 colname 列的双引号换成单引号
1
| replace(colname,'"','\'');
|
查找子字符串在字符串中的位置
locate
instr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select instr('123', '12'); +--------------------+ | instr('123', '12') | +--------------------+ | 1 | +--------------------+ 1 row in set (0.00 sec)
mysql> select instr('123', '4'); +-------------------+ | instr('123', '4') | +-------------------+ | 0 | +-------------------+ 1 row in set (0.00 sec)
|
find_in_set
1 2 3 4 5 6 7
| mysql> select find_in_set('a','a,b,c,d,e'); +------------------------------+ | find_in_set('a','a,b,c,d,e') | +------------------------------+ | 1 | +------------------------------+ 1 row in set (0.00 sec)
|
正则匹配
like
常用通配符:
1 2 3 4 5
| % : 匹配0个或任意多个字符
_ : 匹配任意一个字符
escape : 转义字符,可匹配%和_
|
regexp/rlike
常用通配符
1 2 3 4 5 6 7 8 9 10 11
| . : 匹配任意单个字符
* : 匹配0个或多个前一个得到的字符
[] : 匹配任意一个[]内的字符,[ab]*可匹配空串、a、b、或者由任意个a和b组成的字符串。
^ : 匹配开头,如^s匹配以s或者S开头的字符串。
$ : 匹配结尾,如s$匹配以s结尾的字符串。
{n} : 匹配前一个字符反复n次。
|
字符串区分大小写比较
1 2 3 4 5 6 7
| mysql> select 'A' = binary 'a'; +------------------+ | 'A' = binary 'a' | +------------------+ | 0 | +------------------+ 1 row in set (0.01 sec)
|
自定义排序
elt
1
| elt(N ,str1 ,str2 ,str3 ,…)
|
若 N = 1 ,则返回值为 str1 ,若 N = 2 ,则返回值为 str2 ,以此类推。 若 N 小于 1 或大于参数的数目,则返回值为 NULL 。
1 2 3 4 5 6 7
| mysql> SELECT ELT(3,'hello','halo','test','world'); +--------------------------------------+ | ELT(3,'hello','halo','test','world') | +--------------------------------------+ | test | +--------------------------------------+ 1 row in set (0.00 sec)
|
在盲注中的应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select elt(1,0); +----------+ | elt(1,0) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
mysql> select elt(1,1); +----------+ | elt(1,1) | +----------+ | 1 | +----------+ 1 row in set (0.00 sec)
|
field
1
| FIELD(str, str1, str2, str3, ……)
|
字段str按照字符串str1,str2,str3,str4的顺序返回查询到的结果集。如果表中str字段值不存在于str1,str2,str3,str4中的记录,放在结果集最前面返回。
当然这是正常的用法,还可以理解为这个函数返回的是str 在后面这些字符串中的索引。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select field('halo','hello','test','world'); +--------------------------------------+ | field('halo','hello','test','world') | +--------------------------------------+ | 0 | +--------------------------------------+ 1 row in set (0.00 sec)
mysql> select field('halo','hello','test','halo','world'); +---------------------------------------------+ | field('halo','hello','test','halo','world') | +---------------------------------------------+ | 3 | +---------------------------------------------+ 1 row in set (0.00 sec)
|
在盲注中的应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select field(1,0); +------------+ | field(1,0) | +------------+ | 0 | +------------+ 1 row in set (0.00 sec)
mysql> select field(1,1); +------------+ | field(1,1) | +------------+ | 1 | +------------+ 1 row in set (0.00 sec)
|
这里的第二个参数决定了这条语句的执行结果。
条件表达式
if语句
if(condition,result1,result2)
当 condition 结果为真时返回 result1 否则返回 result2,这常常用作我们盲注的开关函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select if(1,1,0); +-----------+ | if(1,1,0) | +-----------+ | 1 | +-----------+ 1 row in set (0.00 sec)
mysql> select if(0,1,0); +-----------+ | if(0,1,0) | +-----------+ | 0 | +-----------+ 1 row in set (0.00 sec)
|
case语句
基本形式:
1
| case(…)when…then…else…end
|
两种变体:
1
| case expr when val1 then result1 when val2 then result2 else result3 end;
|
先会判断 expr 的结果 ,再根据该结果是 val1 还是 val2 或者其他,来执行不同的语句。
1
| case when condition1 then result1 when condition2 result2 else result3 end;
|
直接根据不同情况进行选择。
比如可以构造这样一条注入语句:
1
| select * from test where id =-1 union select 1,case when username like 'a%' then 0 else 2222222222222222222 end,3,4 from tdb_admin
|
子查询
这一块我还没太看明白,先把模板贴下来
模板一
1
| select ... from ... where col = [any|all](select...);
|
模板二
1
| select ....from ... where col [not]in(select...);
|
模板三
1
| select row(value1,valu2...) = [any/some](select col1,col2...);
|
模板四
1
| select ... from ...where col [not]exists(select...);
|
模板五
1
| select ... from (select ...) as name where ...
|