17. Why Learn All This Before Kali Linux?
17.5 From MySQL to SQL Injection
SQL injection (SQL इंजेक्शन) ajunahi web applications madhla sarvaat dhokadayak bug aahe. Part 5 madhe SELECT ... WHERE shiklat – aata tyach query cha gairvapar kasa hoto te samjel.
// VULNERABLE – user input glued into SQL text (never do this)
$sql = "SELECT * FROM users WHERE username = '" . $_POST['username'] . "'";
// input: ' OR '1'='1 → WHERE username = '' OR '1'='1' → every row matches!
// SAFE – prepared statement (what our reels project uses everywhere)
$stmt = $pdo->prepare('SELECT id, password_hash FROM users WHERE username = ?');
$stmt->execute([$_POST['username']]);
| You learnt (Part 5) | Attack side | Defence side |
|---|---|---|
WHERE, OR, comments (--) |
Classic ' OR '1'='1 login bypass |
Prepared statements |
UNION, SELECT columns |
UNION-based data extraction | Prepared statements, no verbose errors |
information_schema |
Enumerating tables and columns | Least-privilege DB user |
Users and GRANT |
Reading other databases, DROP TABLE |
App user with only needed privileges on one database |
| Safe update mode, backups | Destructive injected queries | Backups, monitoring, WAF as an extra layer |
Why this matters for security
sqlmap automates injection, but it cannot tell you why the bug exists or how to fix it. Because you know SQL and PDO, you can read the vulnerable line, prove it in DVWA (your lab only), and write the one-line fix – and explain why least privilege on RDS would have limited the damage anyway.
Ravindra Bagale's Tip
Many students suggest the fix "remove the quote ' from the input" – that's wrong, it can be bypassed. In an interview, give one answer: prepared statements (parameterised queries), along with a least-privilege DB user and input validation. This question comes up in almost every security interview – remember it.
Ravindra Bagale's Tip – मराठी
बरेच students "input मधून quote ' काढून टाका" असा fix सांगतात – तो चुकीचा आहे, bypass होतो. Interview मध्ये एकच उत्तर द्या: prepared statements (parameterised queries), त्यासोबत least-privilege DB user आणि input validation. हा प्रश्न जवळजवळ प्रत्येक security interview मध्ये येतो, लक्षात ठेवा.
Ravindra Bagale's Tip – हिंदी
बहुत से students "input से quote ' हटा दो" जैसा fix बताते हैं – यह गलत है, bypass हो जाता है. Interview में एक ही जवाब दो: prepared statements (parameterised queries), साथ में least-privilege DB user और input validation. यह सवाल लगभग हर security interview में आता है, याद रखो.
Practice task
Take the vulnerable line above and write, on paper, what the final SQL becomes for the inputs admin' -- and ' OR '1'='1. Then point to the exact lines in the reels project (login.php, feed.php) that prevent this.
Extended teaching notes
Why this lesson matters
Web flaws stay in the OWASP Top 10 because real apps still ship them. Your job is not to collect tool screenshots. It is to prevent and fix the class of bug on systems you own.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Real examples
Global: injection and XSS still appear in breach reports and bug bounties every year. Indian company (sample): a Bengaluru SaaS team maps each OWASP item to a CI check or a code-review question so new code cannot silently reintroduce it.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.