File Read
Overview
With correct permissions, we can read files via SQL injection using the OPENROWSET function with bulk operations.
OPENROWSET Syntax
Get File Length
SELECT LEN(BulkColumn) FROM OPENROWSET(BULK '<path>', SINGLE_CLOB) AS xGet File Contents
SELECT BulkColumn FROM OPENROWSET(BULK '<path>', SINGLE_CLOB) AS xData Types
SINGLE_CLOB
varchar
Text files
SINGLE_BLOB
varbinary
Binary files
SINGLE_NCLOB
nvarchar
Unicode text
Required Permissions
Bulk operations require one of:
ADMINISTER BULK OPERATIONSADMINISTER DATABASE BULK OPERATIONS
Check Permissions Query
SQLi Payload
URL Encode & Test
Response: taken = Permissions granted β
Boolean-based File Read
Attack Strategy
Find file length using
LEN()Extract each character using
SUBSTRING()+ASCII()Use bisection for efficiency
Python Script
Output
Query Templates
Check File Length
Extract Character at Position
Bisection Query
Common Files to Read
Windows
Hosts
C:\Windows\System32\drivers\etc\hosts
SAM (requires SYSTEM)
C:\Windows\System32\config\SAM
Web config
C:\inetpub\wwwroot\web.config
IIS logs
C:\inetpub\logs\LogFiles\
Application-specific
web.config
Connection strings, secrets
appsettings.json
.NET Core config
connectionStrings.config
Database credentials
Limitations
Requires bulk operation permissions
File must be accessible to SQL Server service account
Large files take long time (character-by-character)
Binary files need
SINGLE_BLOBand hex encoding
Optimization Tips
Use SQL-Anding for Speed
Parallel Extraction
Extract multiple characters simultaneously using threading.
Error Handling
File Not Found
If file doesn't exist, query will error. Test with known file first.
Permission Denied
Service account may not have read access to file.
Timeout
Large files may cause query timeout. Adjust script accordingly.
Quick Reference
Permission Check
Read File
File Length
Last updated