PISA Pakistan β€” Community CERT

← βš™οΈ Red Team Fundamentals (Ethical)

18. Database Penetration Testing: SQL, NoSQL, and Data Store Attacks

⏱ 11 min

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:

  1. Error-Based Injection: Trigger database errors to extract information
  2. Boolean-Based Blind Injection: Infer data by observing true/false responses
  3. Time-Based Blind Injection: Use SLEEP() or WAITFOR DELAY for inference
  4. 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:

  1. Identify sa account misconfigurations
  2. Enable xp_cmdshell stored procedure (if disabled)
  3. 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:

  1. Login Bypass: Submit username=admin&password[$ne]=
  2. Data Exfiltration: Craft queries that return all documents matching conditions
  3. JavaScript Injection: Exploit eval() or $where clauses 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_owner to sysadmin via xp_cmdshell
  • MySQL: Exploit LOAD_FILE() and INTO OUTFILE for web shell placement
  • PostgreSQL: Use COPY to write files; exploit pg_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.

← PrevNext β†’