SQL Server 2008 Local Administrator Privilege Escalation

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介: IntroductionUnlike previous versions, SQL Server 2008 and 2012 don’t provide local system ad...

Introduction

Unlike previous versions, SQL Server 2008 and 2012 don’t provide local system administrators with database administrator rights by default. This was a great idea by Microsoft to reinforce the practices of least privilege and separation of duties. However, in spite of the fact that their heart was in the right place, it was implemented in such a way that any local administrator (or attacker) can bypass the restriction.

In most environments SQL Server 2008 and 2012 are installed on domain member servers and access is managed via domain groups. As a penetration tester, that means once I obtain Domain Admin privileges I can simply add myself to the database admin groups in active directory to get access. Once in a while I run across an SQL Server instance that is not managed via domain group or is not on the domain at all. That’s when the escalation method covered in this blog can be useful.

Vulnerability Overview

When SQL Server 2008 is installed the “NT AUTHORITY\SYSTEM” account and the SQL Server service account are added to the “sysadmin” fixed server role by default. The “sysadmin” fixed server role is essentially the database administrators group. Any account that has been assigned the role will have complete control over the SQL Server and the associated data. Local administrators can obtain “sysadmin” privileges in two easy steps:

  1. Use psexec to obtain a cmd.exe console running as “NTAUTHORITY\SYSTEM”.
  2. Use osql and a trusted connection to connect the local database with “sysadmin” privileges.

In SQL Server 2012,  “NT AUTHORITY\SYSTEM” no longer has sysadmin privileges by default, but this restriction can be overcome by migrating to the SQL Server service process.

Attack Walkthrough

For those of you who want to test out the attack at home you can follow the steps below.

  1. Install SQL Server 2008 Express. Click. Click. Click. It can be downloaded from Microsoft at http://www.microsoft.com/en-us/download/details.aspx?id=1695.
  2. Log into the Windows server as a local administrator that has not been assigned the “sysadmin” fixed server role.
  3. Run the following SQL query against the local server to check if the current user has been assigned the “sysadmin” fixed server role.osql –E –S “localhost\sqlexpress” –Q “select is_srvrolemember(‘sysadmin’)”The -E switch authenticates to the SQL Server as the current user and does not require a password. The –S switch specifies the SQL Server instance. The query “select is_srvrolemember(‘sysadmin’)” will return a 1 if you have been assigned the “sysadmin” fixed server role, and a 0 if you haven’t.


    Note:
     In some cases, the local administrator or local administrators group is added to the sysadmin group manually during the installation process. I don’t believe that’s what Microsoft intended, but it happens a lot none the less. If that’s the case, this escalation process will not be necessary.

  4. Download psexec. It’s part of the sysinternals tool set and can be downloaded from Microsoft at http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx.
  5. Type the following command to obtain a “NT AUTHORITY\SYSTEM” console with psexec:psexec –s cmd.exeNote: The -s switch tells psexec to run cmd.exe as “NT AUTHORITY\SYSTEM” .  It does this by creating a new service and configuring it to run as “NT AUTHORITY\SYSTEM”.
  6. Type the one of the following command to verify that you are running as “NT AUTHORITY\SYSTEM” whoami”or echo %username%
  7. Now run the same osql query as before to verify that you have “sysadmin” privileges. This time you should get a 1 back instead of a 0.osql –E –S “localhost\sqlexpress” –Q “select is_srvrolemember(‘sysadmin’)”

  8. If you prefer a GUI tool you can also run management studio express as shown in the screenshots below.


Wrap Up

To stream line the process a little bit, I recently created a metasploit post module that will  escalate privileges and add a sysadmin to the target SQL server via an existing meterpreter session.  That module can be downloaded from my git hub account for those who are interested: 

https://github.com/nullbind/Metasploit-Modules/blob/master/mssql_local_auth_bypass.rb

In spite of how easy it is to use this method to gain unauthorized access to databases it appears to be a requirement in SQL Server 2008. At least one Microsoft article stated “Do not delete this account or remove it from the SYSADMIN fixed server role. The NTAUTHORITY\SYSTEM account is used by Microsoft Update and by Microsoft SMS to apply service packs and hotfixes…”.  So in some cases this boils down to missing patching vs. excessive privileges from risk perspective. My guess is that most companies are going to want to keep their servers patched.  :)   Regardless, hopefully this blog was informative and useful. As always, have fun, but hack responsibility.

References

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
SQL 数据库 Windows
The Windows account sa does not exist and cannot be provisioned as a SQL Server system administrator
今天遇到一个案例,在使用命令修改一个测试服务器(SQL Server 2014标准版)的服务器排序规则时,遇到了下面错误信息 (具体账号信息脱敏处理,随机生成一个账号密码) The Windows account sa does not exist and cannot be provisione...
1738 0
|
SQL 安全 关系型数据库
解决 | 此数据库文件跟当前sql server实例不兼容 & sql server2008无法连接到(local)
最近在搞ASP.NET,因实验室VS版本跟PC不一样可能,拷回来一打开就这样子: 眉头一皱的我打开我的古董SQL,自从用了MySQL就没碰它了我的锅。
1364 0
|
SQL 数据库 数据安全/隐私保护
SQL点滴8—the account is currently locked out. The system administrator can unlock it.
原文:SQL点滴8—the account is currently locked out. The system administrator can unlock it. 今天遇到的问题比较有意思。首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.
1045 0
|
SQL 安全 Shell
Windows2003 SQL2005解决系统Administrator密码不知道的问题
原文:Windows2003 SQL2005解决系统Administrator密码不知道的问题 Windows2003 SQL2005解决系统Administrator密码不知道的问题 今天上班的时候,有个同事说不知道谁设置了开机密码,那台电脑一直没有开机密码的他现在进不了桌面 那台电脑没有光驱...
1048 0
|
SQL
SQL Server Exploitation, Escalation, Pilfering - AppSec USA 2012
http://www.slideshare.net/nullbind/sql-server-exploitation-escalation-pilfering-appsec-usa-2012...
668 0
|
SQL 数据库 数据安全/隐私保护
SQL点滴8—the account is currently locked out. The system administrator can unlock it.
今天遇到的问题比较有意思。首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.Data.SqlClient.SqlException: Login failed for user 'dcp_prod'.
759 0
|
5月前
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
7月前
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
182 13
|
7月前
|
SQL
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
109 9