Skip to content
10 lessons
09Cybersecurity hands-on · Lesson 9 of 10 · lab

Defending: patch the bug you found

Attack, then fix, then prove the fix: replace string-built SQL with a prepared statement.

Roman Urdu: Jo bug mila usay theek kro: SQL ko prepared statement se likho aur dobara test kr ke sabit kro.

Video transcript

Finding a bug is half the job. Knowing the fix makes your reports better, and your own code safer.

Why does SQL injection happen? The app builds its database query by gluing the user's input into the query text. The database receives one string, and cannot tell which part is your query and which part is their input. So input can change what the query does.

The fix is a prepared statement. The app sends the shape of the query first, with a placeholder where the value goes. Then it sends the value separately. The database treats it as data, and never as part of the query.

A fix is not done until you prove it. Run the same attack again: it should fail. Run the scanner again: it should find nothing. And check the page still works for a normal user.

So: keep input out of the query, then prove the fix. In the lab, you will fix the injection in the training app's source, and show that the scanner no longer finds it.

Video file (MP4, 1 min 7 s, 2.8 MB) · Captions (WebVTT)

Rules: Only test systems you own or have written permission to test. Everything in this track runs against practice targets inside your own machine. Scanning or attacking anyone else’s system without permission is a crime in most countries.

Goal

Fix the DVWA SQL injection in the source and show that sqlmap no longer finds it.

Why it matters

Finding bugs is half the job. Knowing the fix makes your reports better and your own code safer.

Your machine · learn-cyber · lab cyber-09

A terminal on your own cyber machine, right in this page. It keeps your files between lessons.

First time: make your course machine from any terminal (Mac, Linux and Windows 10+ all have ssh). Your ssh key is your login; a new key picks a handle and accepts the terms first.

$ ssh -t -p 2200 m.logicpacks.io new cyber --name learn-cyber

After that, come back to the same machine any time. Your files are still there:

$ ssh -t -p 2200 learn-cyber@m.logicpacks.io

To use the terminal in this page, sign this browser in once: run this and open the link it prints.

$ ssh -p 2200 m.logicpacks.io web

All your machines in the browser: logicpacks.io/m. A guest key gets one machine: if you already use it for something else, rm that one or link a free account (3 machines). How machines work, credits and terms: logicpacks.io/machines

Step by step

  1. DVWA runs straight from the source in your lab folder, so a change there is live at once. Read the vulnerable code: the id goes straight into the query.

    $ cat ~/lab/dvwa/vulnerabilities/sqli/source/low.php
    Diagram: on the left, user input is glued into the query text, so the database sees one string and the input can change the query. On the right, a prepared statement sends the query with a placeholder and the input separately, so the input stays data.
    Injection comes from gluing input into the query. A prepared statement sends the value separately.
  2. Compare it with the safe version DVWA ships: a prepared statement with a bound parameter.

    $ diff ~/lab/dvwa/vulnerabilities/sqli/source/low.php ~/lab/dvwa/vulnerabilities/sqli/source/impossible.php
  3. Keep a backup, then patch low.php to use a prepared statement (copy the pattern from impossible.php, or ask your agent).

    $ cp ~/lab/dvwa/vulnerabilities/sqli/source/low.php ~/low.php.orig
    $ nano ~/lab/dvwa/vulnerabilities/sqli/source/low.php
  4. Prove it: re-run the lesson 4 sqlmap command. It should now say the parameter does not seem to be injectable.

Check my work

Run this in your machine. It checks your work and prints PASS, or FAIL with a hint about what is missing.

$ lab check cyber-09

Saved in this browser. Use ← and → to move between lessons.