Foundations of Database Security Assessment
Databases represent the crown jewels of any organizationβNADRA stores citizen identity data, financial institutions maintain transaction records, and healthcare providers hold sensitive patient information. Red teamers must master database penetration testing to identify misconfigurations, injection vulnerabilities, and privilege escalation paths that attackers exploit.
In the Pakistani context, data breaches at government portals and financial institutions have exposed millions of records, making database security a critical assessment domain.
Core Database Technologies to Assess:
- Relational: MySQL, PostgreSQL, Microsoft SQL Server (MSSQL), Oracle
- NoSQL: MongoDB, Cassandra, Redis
- Cloud-native: AWS DynamoDB, Azure Cosmos DB
SQL Injection: Classic to Advanced Techniques
Identifying SQL Injection
SQL injection remains one of the OWASP Top 10 most critical web application risks. In Pakistan, multiple e-government portals have historically suffered from SQLi vulnerabilities.
Methodology:
- Error-Based Injection: Trigger database errors to extract information
- Boolean-Based Blind Injection: Infer data by observing true/false responses
- Time-Based Blind Injection: Use
SLEEP()orWAITFOR DELAYfor inference - Union-Based Injection: Combine results from original query with injected SELECT
Tool: sqlmap
sqlmap automates SQL injection detection and exploitation. Configure it with BurpSuite proxy capture:
sqlmap -r request.txt --batch --level=5 --risk=3 --dbs
sqlmap -r request.txt -D nadra_db --tables
sqlmap -r request.txt -D nadra_db -T citizens --dump
Lab Exercise: Exploiting MSSQL with xp_cmdshell
In assessments against Windows environments with MSSQL:
- Identify
saaccount misconfigurations - Enable
xp_cmdshellstored procedure (if disabled) - Execute OS-level commands for full system compromise
sqlmap -r mssql_request.txt --os-shell
NoSQL Injection: Attacking Modern Data Stores
Modern applications increasingly use NoSQL databases that introduce new injection vectors:
MongoDB Injection
- Manipulate query operators:
{"$ne": null}to bypass authentication - Use NoSQLMap for automated injection testing
- Exploit application logic that concatenates user input into MongoDB queries
Attack Scenarios:
- Login Bypass: Submit
username=admin&password[$ne]= - Data Exfiltration: Craft queries that return all documents matching conditions
- JavaScript Injection: Exploit
eval()or$whereclauses for code execution
Database Enumeration and Post-Exploitation
Once SQL injection is confirmed, systematic enumeration follows:
Information Schema Enumeration
-- MySQL/PostgreSQL
SELECT table_name, column_name FROM information_schema.columns;
-- MSSQL
SELECT name FROM sys.tables; SELECT name FROM sys.columns;
Extracting Sensitive Data
- User credential tables: admin_users, system_users
- Customer PII: citizen_records, account_holder, patient_data
- Financial transactions: payment_history, bank_transactions
Privilege Escalation via Database
- MSSQL: Escalate from
db_ownertosysadminviaxp_cmdshell - MySQL: Exploit
LOAD_FILE()andINTO OUTFILEfor web shell placement - PostgreSQL: Use
COPYto write files; exploitpg_execute_server_program
Defensive Detection: Database Attack Signatures
SIEM Correlation Rules for ELK/Splunk:
- Detect SQL injection attempts via error-based signatures
- Alert on bulk data exfiltration queries
- Monitor for unusual database admin commands from application accounts
TheHive/MISP Integration: Document database vulnerabilities for threat intelligence sharing with NCCIA 1799 cybercrime helpline.
Pakistani Compliance and Legal Considerations
Under Pakistan's Prevention of Electronic Crimes Act (PECA 2016), database access without authorization is illegal. Ensure your Rules of Engagement (RoE) explicitly authorize database testing. Coordinate with PTA if testing telecom customer databases.
Takeaway Action
Today: Set up a local lab using Metasploitable 3 with vulnerable MSSQL and MySQL instances. Practice sqlmap automated exploitation and manual UNION-based injection to understand query structure extraction.
