|
| 1 | +# SQL Injection Analysis |
| 2 | + |
| 3 | +**Detect SQL injection vulnerabilities in code with detailed analysis** |
| 4 | + |
| 5 | +## Prompt |
| 6 | + |
| 7 | +```text |
| 8 | +@workspace Analyze this contributions.js file for SQL injection vulnerabilities. |
| 9 | +Focus on database query construction and user input handling. |
| 10 | +Provide specific line numbers and explain the attack vector. |
| 11 | +``` |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## When to Use This Prompt |
| 16 | + |
| 17 | +Use this when you need to: |
| 18 | +- Audit legacy code for SQL injection vulnerabilities |
| 19 | +- Review pull requests for database security issues |
| 20 | +- Identify vulnerable query construction patterns |
| 21 | +- Get specific line-by-line vulnerability analysis |
| 22 | + |
| 23 | +**Applicable to**: Any codebase using SQL databases (MySQL, PostgreSQL, Oracle, SQL Server, SQLite) |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Expected Output |
| 28 | + |
| 29 | +Copilot should identify: |
| 30 | + |
| 31 | +1. **Vulnerable code locations** with exact line numbers |
| 32 | +2. **Attack vector explanation** (how the vulnerability can be exploited) |
| 33 | +3. **Vulnerability classification** (OWASP A03:2021 - Injection) |
| 34 | +4. **Specific code patterns** that are vulnerable (string concatenation, template literals) |
| 35 | + |
| 36 | +### Example Output |
| 37 | + |
| 38 | +```text |
| 39 | +SQL INJECTION VULNERABILITIES FOUND: |
| 40 | +
|
| 41 | +Line 65-75: User input concatenated directly into SQL query |
| 42 | +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ |
| 43 | +
|
| 44 | +Vulnerable Code: |
| 45 | +const query = `SELECT * FROM allocations WHERE userId = ${userId}`; |
| 46 | +
|
| 47 | +Attack Vector: |
| 48 | +An attacker can inject malicious SQL by providing userId = "1 OR 1=1 --" |
| 49 | +This would bypass authentication and return all records. |
| 50 | +
|
| 51 | +Classification: OWASP A03:2021 - Injection, CWE-89 |
| 52 | +Severity: CRITICAL |
| 53 | +``` |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## Customization Guidance |
| 58 | + |
| 59 | +### For Different Languages |
| 60 | + |
| 61 | +**Python/Django**: |
| 62 | +```text |
| 63 | +@workspace Analyze this views.py file for SQL injection vulnerabilities. |
| 64 | +Focus on raw SQL queries, Django ORM misuse, and user input handling. |
| 65 | +Provide specific line numbers and explain the attack vector. |
| 66 | +``` |
| 67 | + |
| 68 | +**Java/Spring**: |
| 69 | +```text |
| 70 | +@workspace Analyze this UserRepository.java for SQL injection vulnerabilities. |
| 71 | +Focus on @Query annotations, JDBC statements, and JPA criteria with user input. |
| 72 | +Provide specific line numbers and explain the attack vector. |
| 73 | +``` |
| 74 | + |
| 75 | +**PHP**: |
| 76 | +```text |
| 77 | +@workspace Analyze this database.php file for SQL injection vulnerabilities. |
| 78 | +Focus on mysqli_query, PDO misuse, and user input handling. |
| 79 | +Provide specific line numbers and explain the attack vector. |
| 80 | +``` |
| 81 | + |
| 82 | +### For Different Databases |
| 83 | + |
| 84 | +**MongoDB (NoSQL Injection)**: |
| 85 | +```text |
| 86 | +@workspace Analyze this model file for NoSQL injection vulnerabilities. |
| 87 | +Focus on MongoDB query construction and unvalidated user input in queries. |
| 88 | +Provide specific line numbers and explain the attack vector. |
| 89 | +``` |
| 90 | + |
| 91 | +**GraphQL**: |
| 92 | +```text |
| 93 | +@workspace Analyze these resolvers for GraphQL injection vulnerabilities. |
| 94 | +Focus on dynamic query construction and parameter injection risks. |
| 95 | +Provide specific line numbers and explain the attack vector. |
| 96 | +``` |
| 97 | + |
| 98 | +### Adding Compliance Context |
| 99 | + |
| 100 | +```text |
| 101 | +@workspace Analyze this contributions.js file for SQL injection vulnerabilities per: |
| 102 | +- OWASP Top 10 2021 A03 (Injection) |
| 103 | +- CWE-89 (SQL Injection) |
| 104 | +- PCI-DSS Requirement 6.5.1 |
| 105 | +
|
| 106 | +Focus on database query construction and user input handling. |
| 107 | +Provide specific line numbers, compliance mappings, and attack vectors. |
| 108 | +``` |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## Follow-up Prompts |
| 113 | + |
| 114 | +After identifying vulnerabilities, use these related prompts: |
| 115 | + |
| 116 | +1. **Understand Exploits**: [Generate SQL Injection Attack Payloads](./02-SQL-Injection-Exploits.md) |
| 117 | +2. **Fix the Code**: [Refactor with Parameterized Queries](./03-SQL-Injection-Remediation.md) |
| 118 | +3. **Prevent Regression**: [Generate SQL Injection Security Tests](./04-SQL-Injection-Tests.md) |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Best Practices |
| 123 | + |
| 124 | +### ✅ Do This |
| 125 | + |
| 126 | +- **Provide file context**: Use `@workspace` or `#file:path` |
| 127 | +- **Request specific output**: Ask for line numbers, not just "find bugs" |
| 128 | +- **Specify the vulnerability type**: "SQL injection" not "security issues" |
| 129 | +- **Ask for attack vectors**: Understanding exploits helps prioritize fixes |
| 130 | + |
| 131 | +### ❌ Avoid This |
| 132 | + |
| 133 | +- **Generic prompts**: "Check for vulnerabilities" is too broad |
| 134 | +- **Missing context**: Without file/codebase context, results are superficial |
| 135 | +- **Skipping verification**: Always manually review Copilot's findings |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Validation Checklist |
| 140 | + |
| 141 | +After using this prompt, verify: |
| 142 | + |
| 143 | +- [ ] Each vulnerability includes a specific line number |
| 144 | +- [ ] Attack vector is explained with example payload |
| 145 | +- [ ] OWASP/CWE classification is provided |
| 146 | +- [ ] All user input points are analyzed |
| 147 | +- [ ] Both direct and indirect SQL execution is checked |
| 148 | +- [ ] ORM misuse is identified (if applicable) |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## Common False Positives |
| 153 | + |
| 154 | +**Issue**: Copilot flags parameterized queries as vulnerable |
| 155 | +**Solution**: Verify the code actually uses parameterization correctly |
| 156 | + |
| 157 | +**Issue**: Safe ORM usage flagged as vulnerable |
| 158 | +**Solution**: Provide framework context in prompt (e.g., "We use Django ORM correctly") |
| 159 | + |
| 160 | +**Issue**: Hardcoded queries flagged as vulnerable |
| 161 | +**Solution**: Add context: "Ignore queries with no user input" |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## Real-World Example |
| 166 | + |
| 167 | +### Prompt Used |
| 168 | +```text |
| 169 | +@workspace Analyze app/routes/contributions.js for SQL injection vulnerabilities. |
| 170 | +Focus on database query construction and user input handling. |
| 171 | +Provide specific line numbers and explain the attack vector. |
| 172 | +``` |
| 173 | + |
| 174 | +### Result |
| 175 | +Copilot identified: |
| 176 | +- **3 critical SQL injection vulnerabilities** in allocation queries |
| 177 | +- **Line-specific locations**: Lines 42, 67, 89 |
| 178 | +- **Attack payloads**: `' OR '1'='1' --`, `'; DROP TABLE users; --` |
| 179 | +- **Impact**: Full database compromise, data exfiltration |
| 180 | + |
| 181 | +### Time Saved |
| 182 | +- Manual code review: ~45 minutes |
| 183 | +- Copilot analysis: ~2 minutes |
| 184 | +- **Time saved: 43 minutes** (95% reduction) |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Related Vulnerabilities |
| 189 | + |
| 190 | +This prompt also helps detect: |
| 191 | +- **Command Injection** (if SQL queries include OS commands) |
| 192 | +- **LDAP Injection** (similar pattern in LDAP queries) |
| 193 | +- **XPath Injection** (similar pattern in XML queries) |
| 194 | +- **NoSQL Injection** (MongoDB, Cassandra, etc.) |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## Technical Background |
| 199 | + |
| 200 | +### What is SQL Injection? |
| 201 | + |
| 202 | +SQL injection occurs when: |
| 203 | +1. User input is incorporated into SQL queries |
| 204 | +2. Input is not properly sanitized/escaped |
| 205 | +3. Attacker injects SQL syntax that alters query logic |
| 206 | + |
| 207 | +### Common Vulnerable Patterns |
| 208 | + |
| 209 | +```javascript |
| 210 | +// ❌ String concatenation |
| 211 | +const query = "SELECT * FROM users WHERE id = " + userId; |
| 212 | + |
| 213 | +// ❌ Template literals |
| 214 | +const query = `SELECT * FROM users WHERE id = ${userId}`; |
| 215 | + |
| 216 | +// ❌ String building |
| 217 | +const query = "SELECT * FROM users WHERE name = '" + userName + "'"; |
| 218 | + |
| 219 | +// ✅ Parameterized query (safe) |
| 220 | +const query = "SELECT * FROM users WHERE id = ?"; |
| 221 | +db.query(query, [userId]); |
| 222 | +``` |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +[← Back to Lesson 01 Index](./README.md) | [Next: SQL Injection Exploits →](./02-SQL-Injection-Exploits.md) |
0 commit comments