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 x

Get File Contents

SELECT BulkColumn FROM OPENROWSET(BULK '<path>', SINGLE_CLOB) AS x

Data Types

Option
Storage Type
Use Case

SINGLE_CLOB

varchar

Text files

SINGLE_BLOB

varbinary

Binary files

SINGLE_NCLOB

nvarchar

Unicode text


Required Permissions

Bulk operations require one of:

  • ADMINISTER BULK OPERATIONS

  • ADMINISTER DATABASE BULK OPERATIONS

Check Permissions Query

SQLi Payload

URL Encode & Test

Response: taken = Permissions granted βœ…


Boolean-based File Read

Attack Strategy

  1. Find file length using LEN()

  2. Extract each character using SUBSTRING() + ASCII()

  3. Use bisection for efficiency

Python Script

Output


Query Templates

Check File Length

Extract Character at Position

Bisection Query


Common Files to Read

Windows

File
Path

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

File
Purpose

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_BLOB and 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