310 Red Teaming MS SQL Server PDF
310 Red Teaming MS SQL Server PDF
eXtreme
By the end of this module, you should have a better understanding of:
Introduction
Red Teaming MS
SQL Server
Go to Slide 135 and 136 to check out the full links of the above references PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.17
1.3 Locating & Accessing SQL Servers
>> Get-SQLInstanceScanUDP
Databases in Azure
Another thing to look for when dealing with databases
residing in Azure environments is configuration files,
containing connection strings, on public repositories.
>> Get-SQLInstanceLocal
Go to Slide 136 to check out the full links of the above references PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.26
1.3 Locating & Accessing SQL Servers
>> Get-SQLInstanceDomain
Escalating Privileges
within SQL Server
*To return to slide 58, click HERE. **To return to slide 60, click HERE. PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.42
1.4 Escalating privileges within SQL Server
Gaining Initial Foothold on SQL Server
Please refer to PowerUpSQL’s wiki page below, to find more
examples on how to use PowerUpSQL in various occasions.
• https://github.com/NetSPI/PowerUpSQL/wiki
Now that we have gained initial foothold let’s try to work our
way up from public role level privileges to sysadmin level
privileges.
SELECT SUSER_NAME(1)
SELECT SUSER_NAME(2)
SELECT SUSER_NAME(3)
…
c) Grab the first 48 bytes of the full RID, to get the SID for
the domain. Then, create a new RID (that will be
associated with a domain object) by appending a hex
number value to the abovementioned SID.
SELECT SUSER_NAME(RID)
2. Impersonation
a. Impersonate Privilege
There is a privilege/permission in SQL Server which allows
a less privileged user to impersonate a user with more
access.
Impersonate Privilege
There is a requirement for OS command execution though,
the database has to be configured as trustworthy.
Impersonate Privilege
For example, to manually check if you can impersonate the
sa login, execute the following queries.
SELECT SYSTEM_USER
SELECT IS_SRVROLEMEMBER('sysadmin')
EXECUTE AS LOGIN = 'sa'
SELECT SYSTEM_USER
SELECT IS_SRVROLEMEMBER('sysadmin')
Impersonate Privilege
USE MyDB
GO
CREATE PROCEDURE elevate
WITH EXECUTE AS OWNER
AS
EXEC sp_addsrvrolemember
'simple_user','sysadmin'
GO
auxiliary/admin/mssql/mssql_escalate_dbowner
auxiliary/admin/mssql/mssql_escalate_dbowner_sqli
PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.81
1.4 Escalating privileges within SQL Server
https://blog.netspi.com/hacking-sql-server-stored-
procedures-part-3-sqli-and-user-impersonation/
• https://blog.netspi.com/sql-server-link-crawling-
powerupsql/
PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.101
4.4 Video
Injection
Process Migration
x x x x x x
(Remote DLL or Shellcode Injection)
Log into SQL Server in Single User Mode as a local administrator X DBATools
IMPORTANT NOTE:
While trying to escalate our privileges, we should always
check for unencrypted SQL Server communications. If this
is the case, via man-in-the-middle attack techniques we can
inject our own queries. Based on the victim’s privileges we
may be able to apply the sysadmin role to our SQL login.
Common Post-
Exploitation Activities
1. Persistence
Persistence
For example, to establish persistence, we could set up a
debugger for utilman.exe, that will run cmd.exe when it's called.
This can be done as follows, with sysadmin privileges only! Then,
we could RDP into the machine, press the windows key + “u” key
combination and be presented with a command prompt.
>> import-module .\PowerUpSQL.psd1
Persistence
In another example, we could leverage CurrentVersion\Run
to establish persistence with xp_regwrite, using PowerUp
SQL as follows. This can be done with sysadmin privileges
only!
>> import-module .\PowerUpSQL.psd1
Persistence
In yet another example, we could also export all custom CLR
assemblies to DLLs, backdoor any of those DLLs and finally
import the backdoored CLR assembly to establish persistence.
This can be done as follows, using PowerUpSQL and having
sysadmin privileges only!
>> import-module .\PowerUpSQL.psd1
https://github.com/lgandx/Responder/pull/58
Nmap
https://nmap.org/
Nessus
https://www.tenable.com/products/nessus-vulnerability-scanner
SQLping3
http://www.sqlsecurity.com/downloads
OSQL
https://docs.microsoft.com/en-us/sql/tools/osql-utility
Click HERE to return to Slide 17 PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.135
References
SQLCMD
https://docs.microsoft.com/en-us/sql/tools/sqlcmd-utility
PowerUpSQL
https://github.com/NetSPI/PowerUpSQL
setspn.exe
https://social.technet.microsoft.com/wiki/contents/articles/717.service-principal-names-spns-setspn-
syntax-setspn-exe.aspx
adfind.exe
http://www.joeware.net/freetools/tools/adfind/index.htm
Get-Spn.psm1
https://github.com/nullbind/Powershellery/blob/master/Stable-ish/Get-SPN/Get-SPN.psm1
Click slide # to return to the appropriate slide – Slide 17, Slide PTXv2: Section 3, Module 1 - Caendra Inc. © 2020 | p.136
26.
References
sqlmitm.py
https://gist.github.com/anonymous/edb02df90942dc4df0e41f3cbb78660b
Anitian
https://www.anitian.com/
RottenPotato
https://github.com/foxglovesec/RottenPotato
juicy-potato
https://github.com/decoder-it/juicy-potato
Inveigh
https://github.com/Kevin-Robertson/Inveigh
impacket
https://github.com/CoreSecurity/impacket
Responder
https://github.com/lgandx/Responder/pull/58