Exfiltrating Database Content via SQLi
Once SQL injection is confirmed and you have query execution, the goal shifts to extracting and preserving high-value data efficiently. This page covers targeted extraction of credentials, schema mapping for scope awareness, and bulk export techniques. Run this after establishing injection type through SQL injection exploitation.
Map the Database Schema First
Before pulling data, understand what exists to prioritize targets. Pull all schemas and tables in one pass:
webapp.users,webapp.sessions,webapp.orders,webapp.api_keys
Prioritize tables with obvious high-value names before dumping everything:
- users, credentials, accounts: usernames, password hashes, roles
- sessions, tokens: active session IDs for hijacking
- api_keys, oauth_tokens: direct access to external services
- config, settings: database credentials, SMTP passwords, encryption keys
- orders, payments: PCI-relevant data for report findings
orders and logs are secondary unless credential material is expected there.
Extract Credentials and Hashes
Pull usernames, passwords, and roles from the users table in one query:
admin:5f4dcc3b5aa765d61d8327deb882cf99:administrator editor:8d3533d75ae2c3966d7e0d4fcc69216b:editor user1:d8578edf8458ce06fbc5bb76a58c5ca4:user
Crack the extracted hashes immediately. MD5 hashes use hashcat mode 0, bcrypt uses mode 3200, SHA1 uses mode 100:
5f4dcc3b5aa765d61d8327deb882cf99:password 8d3533d75ae2c3966d7e0d4fcc69216b:iloveyou
Extract API Keys and Tokens
Application tokens and API keys in the database often provide direct access to other services:
1:sk-prod-xK2aB1cD3eF4gH5iJ6kL7mN8oP9 2:sk-prod-qR1sT2uV3wX4yZ5aB6cD7eF8gH9
SQL Injection Data Exfiltration with sqlmap
For full database dumps when manual extraction is too slow, sqlmap handles the complete extraction including all tables:
Important
sqlmap is prohibited in the OSCP exam. Do not use during exam environments.
File System Read via SQLi
When the database user has file read privileges, read sensitive files directly through the SQL injection vector without needing LFI.
MySQL: LOAD_FILE
Requires FILE privilege. Check with SELECT File_priv FROM mysql.user WHERE user=user() first.
root:x:0:0:root:/root:/bin/bash
DB_PASSWORD=SuperSecret123! APP_KEY=base64:abc123...
PostgreSQL: pg_read_file
pg_read_file() reads files from the server filesystem. Requires superuser by default, or membership in the pg_read_server_files role on PostgreSQL 11+. The path is relative to the PostgreSQL data directory unless an absolute path is given. Absolute paths require superuser.
root:x:0:0:root:/root:/bin/bash
When pg_read_file() is not available (non-superuser, pre-11 without the role), try reading files through COPY FROM into a temp table via stacked queries, then SELECT from the table.
MSSQL: OPENROWSET BULK
OPENROWSET(BULK...) reads local files into a result set. Requires the ADMINISTER BULK OPERATIONS permission or membership in the bulkadmin server role. Ad Hoc Distributed Queries must be enabled on the server.
SINGLE_CLOB reads the file as a single text value. Use SINGLE_BLOB for binary files. Common targets on MSSQL servers: C:\inetpub\wwwroot\web.config (connection strings, app keys), C:\Windows\System32\drivers\etc\hosts (internal hostnames), and application config files in the web root. If Ad Hoc Distributed Queries is disabled and you have sysadmin, enable it through stacked queries:
Stage and Save Extracted Data
Organize extracted data into files on Kali for reporting and follow-on use:
Saved 3 hashes
References
-
Automated SQL injection data extraction with dump and output options
-
Hashcat: Mode ReferenceTOOL: Hashcat: Mode Reference, hashcat.net/wiki/doku.php?id=hashcat (opens in new tab)
Hash mode reference for MD5, SHA1, bcrypt and other common password hash formats
-
PostgreSQL: Admin FunctionsDOCS: PostgreSQL: Admin Functions, www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-GENFILE (opens in new tab)
pg_read_file and related filesystem access functions