MSSQL Injection
MSSQL Injection
Active Directory opsporing
Dit mag moontlik wees om domein gebruikers te opspoor via SQL-inspuiting binne 'n MSSQL bediener met behulp van die volgende MSSQL funksies:
SELECT DEFAULT_DOMAIN()
: Kry huidige domeinnaam.master.dbo.fn_varbintohexstr(SUSER_SID('DOMAIN\Administrator'))
: As jy die naam van die domein (DOMAIN in hierdie voorbeeld) ken, sal hierdie funksie die SID van die gebruiker Administrator in hex formaat teruggee. Dit sal lyk soos0x01050000000[...]0000f401
, let op hoe die laaste 4 bytes die nommer 500 in big endian formaat is, wat die gewone ID van die gebruiker administrator is. Hierdie funksie sal jou toelaat om die ID van die domein te ken (al die bytes behalwe die laaste 4).SUSER_SNAME(0x01050000000[...]0000e803)
: Hierdie funksie sal die gebruikersnaam van die aangeduide ID teruggee (indien enige), in hierdie geval 0000e803 in big endian == 1000 (gewoonlik is dit die ID van die eerste gewone gebruiker ID wat geskep is). Dan kan jy jou voorstel dat jy gebruikers-ID's van 1000 tot 2000 kan brute-force en waarskynlik al die gebruikersname van die gebruikers van die domein kan kry. Byvoorbeeld deur 'n funksie soos die volgende te gebruik:
Alternatiewe Fout-gebaseerde vektore
Fout-gebaseerde SQL-inspuitings lyk tipies soos konstruksies soos +AND+1=@@version--
en variasies gebaseer op die «OR» operator. Vrae wat sulke uitdrukkings bevat, word gewoonlik deur WAFs geblokkeer. As 'n omseiling, konkateer 'n string met die %2b karakter met die resultaat van spesifieke funksie-aanroepe wat 'n datatipe-omskakelingsfout op gesogte data veroorsaak.
Sommige voorbeelde van sulke funksies:
SUSER_NAME()
USER_NAME()
PERMISSIONS()
DB_NAME()
FILE_NAME()
TYPE_NAME()
COL_NAME()
Voorbeeld gebruik van funksie USER_NAME()
:
SSRF
Hierdie SSRF truuks is hier geneem
fn_xe_file_target_read_file
fn_xe_file_target_read_file
Dit vereis VIEW SERVER STATE
toestemming op die bediener.
fn_get_audit_file
fn_get_audit_file
Dit vereis die CONTROL SERVER
toestemming.
fn_trace_gettabe
fn_trace_gettabe
Dit vereis die CONTROL SERVER
toestemming.
xp_dirtree
, xp_fileexists
, xp_subdirs
xp_dirtree
, xp_fileexists
, xp_subdirs
Gestoor prosedures soos xp_dirtree
, hoewel nie amptelik gedokumenteer deur Microsoft nie, is deur ander aanlyn beskryf weens hul nut in netwerkbedrywighede binne MSSQL. Hierdie prosedures word dikwels gebruik in Out of Band Data eksfiltrasie, soos getoon in verskeie voorbeelde en plase.
Die xp_dirtree
gestoor prosedure, byvoorbeeld, word gebruik om netwerk versoeke te maak, maar dit is beperk tot slegs TCP poort 445. Die poortnommer is nie aanpasbaar nie, maar dit laat lees van netwerk gedeeltes toe. Die gebruik word gedemonstreer in die SQL skrip hieronder:
It's noteworthy that this method might not work on all system configurations, such as on Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64)
running on a Windows Server 2016 Datacenter
with default settings.
Additionally, there are alternative stored procedures like master..xp_fileexist
and xp_subdirs
that can achieve similar outcomes. Further details on xp_fileexist
can be found in this TechNet article.
xp_cmdshell
xp_cmdshell
Dit is duidelik dat jy ook xp_cmdshell
kan gebruik om iets uit te voer wat 'n SSRF aktiveer. Vir meer inligting lees die relevante afdeling op die bladsy:
MSSQL User Defined Function - SQLHttp
Om 'n CLR UDF (Common Language Runtime User Defined Function) te skep, wat kode is wat in enige .NET-taal geskryf is en in 'n DLL gecompileer is, om binne MSSQL gelaai te word vir die uitvoering van pasgemaakte funksies, is 'n proses wat dbo
toegang vereis. Dit beteken dit is gewoonlik slegs haalbaar wanneer die databasisverbinding as sa
of met 'n Administrateur rol gemaak word.
'n Visual Studio-projek en installasie-instruksies word in hierdie Github-repositori verskaf om die laai van die binêre in MSSQL as 'n CLR-assemblage te vergemaklik, wat die uitvoering van HTTP GET-versoeke vanuit MSSQL moontlik maak.
Die kern van hierdie funksionaliteit is ingekapsuleer in die http.cs
-lêer, wat die WebClient
-klas gebruik om 'n GET-versoek uit te voer en inhoud te verkry soos hieronder geïllustreer:
Voordat die CREATE ASSEMBLY
SQL-opdrag uitgevoer word, word dit aanbeveel om die volgende SQL-snippet te loop om die SHA512-has van die assembly by die bediener se lys van vertroude assemblies te voeg (beskikbaar via select * from sys.trusted_assemblies;
):
Na suksesvolle toevoeging van die samestelling en die skep van die funksie, kan die volgende SQL-kode gebruik word om HTTP-versoeke uit te voer:
Vinige Exploit: Herwinning van Volledige Tabelinhoud in 'n Enkele Vraag
'n Bondige metode om die volle inhoud van 'n tabel in 'n enkele vraag te onttrek, behels die gebruik van die FOR JSON
klousule. Hierdie benadering is meer beknop as om die FOR XML
klousule te gebruik, wat 'n spesifieke modus soos "raw" vereis. Die FOR JSON
klousule word verkies weens sy beknoptheid.
Hier is hoe om die skema, tabelle en kolomme van die huidige databasis te herwin:
Little tricks for WAF bypasses
Non-standard whitespace characters: %C2%85 или %C2%A0:
https://vuln.app/getItem?id=1%C2%85union%C2%85select%C2%A0null,@@version,null--
So for example, multiple queries such as:
Voeg 'n nuttelose exec() aan die einde by en laat die WAF dink dit is nie 'n geldige navraag nie
admina'union select 1,'admin','testtest123'exec('select 1')--
Dit sal wees:
SELECT id, username, password FROM users WHERE username = 'admina'union select 1,'admin','testtest123' exec('select 1')--'
Gebruik vreemd gebou navrae
admin'exec('update[users]set[password]=''a''')--
Dit sal wees:
SELECT id, username, password FROM users WHERE username = 'admin' exec('update[users]set[password]=''a''')--'
Of om xp_cmdshell in te skakel
admin'exec('sp_configure''show advanced option'',''1''reconfigure')exec('sp_configure''xp_cmdshell'',''1''reconfigure')--
Dit sal wees
select * from users where username = ' admin' exec('sp_configure''show advanced option'',''1''reconfigure') exec('sp_configure''xp_cmdshell'',''1''reconfigure')--
Last updated