Skip to main content
Penetration Testing / Article

sqlmap Tutorial: Validate SQL Injection in an Authorized Lab

Set up sqlmap with Python 3, prepare one authorized lab request, distinguish errors from evidence, and verify a parameterized-query repair.

Start reading
sqlmap Tutorial: Validate SQL Injection in an Authorized Lab

sqlmap helps investigate SQL injection in an application you are authorized to test. It can send many requests, so the first useful skill is choosing a small, repeatable test. An error message, a numeric query parameter, or a scanner alert by itself does not prove a vulnerability.

This walkthrough separates installation, request preparation, validation, and reporting. Use a local training application with synthetic records. Agree the target and permitted actions before using the tool against any shared environment.

Install and verify the tool

Use Python 3 for a new installation. Python 2 reached end of support in 2020; a tool retaining compatibility does not make Python 2 a good choice for a new workstation. Check the interpreter rather than assuming every Linux or macOS machine already has it.

python3 --version
git clone --depth 1 https://github.com/sqlmapproject/sqlmap.git sqlmap-dev
cd sqlmap-dev
python3 sqlmap.py --version
python3 sqlmap.py -h

On Windows, a Python installation may expose the launcher as py -3. Use the command that actually starts your Python 3 interpreter. The project repository is the reference for installation and supported options. Record your installed version in the investigation notes so a later retest has a known starting point.

Understand the request before automating it

In your local lab, find a read-only operation such as looking up a synthetic catalogue item. Make it work normally first. Note the HTTP method, path, query or form fields, authentication state, and the part of the response that proves the intended item was returned. A login redirect or expired session is not a valid baseline.

Example lab request — substitute your own local training endpoint
GET /catalogue?item=3 HTTP/1.1
Host: 127.0.0.1:8080
Accept: text/html

Save the request as request.txt only after checking its contents. Use disposable lab accounts. If a real engagement requires session material, treat the request file as sensitive evidence: it may contain cookies or authorization headers. Keep it out of Git and remove it according to the engagement's retention rules.

Limit the first test to one input

For that explicitly authorized local request, choose the parameter you intend to investigate. The following example names only item and uses the lowest risk and level settings. Those settings reduce the test surface; they do not guarantee that a fragile application cannot be affected.

python3 sqlmap.py -r request.txt -p item --level=1 --risk=1

Read the prompts instead of accepting decisions you do not understand. If a redirect changes the target, authentication fails, or the application becomes unstable, stop and correct the setup. Do not broaden the target or enable data extraction simply because a menu offers it. The objective of this exercise is to validate a flaw with the least necessary interaction.

Decide what the result actually supports

A changed response can have several explanations: input validation, a cache, a rate limit, an expired session, or a database expression influenced by input. Compare the tool's evidence with the baseline. Repeat the narrow case and inspect application logs or source when the lab provides them. Document which input and which behavior establish the finding.

ObservationWhat you can conclude
A quotation mark causes a 500 responseThe input triggered an error. The cause still needs investigation.
The test redirects to sign-inThe session or request setup is no longer valid for the intended endpoint.
Controlled input changes query behavior reproduciblyThere is evidence to investigate as injection; record the exact affected operation.
The scanner reports no findingThis test did not establish injection. It does not prove the whole application is secure.

SQL injection concerns data being interpreted as part of a SQL query. It is not restricted to PHP, and finding a URL containing ?id= does not establish that SQL is involved. Likewise, fixing SQL injection does not fix unrelated cross-site scripting, authentication, or availability problems.

Repair the query boundary

Use the database driver's parameter interface for values. Here is a small SQLite example showing the intended separation:

import sqlite3

connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE catalogue (id INTEGER, name TEXT)")
connection.execute("INSERT INTO catalogue VALUES (?, ?)", (3, "Practice notebook"))
requested_id = "3"
row = connection.execute(
    "SELECT name FROM catalogue WHERE id = ?", (requested_id,)
).fetchone()
print(row)

The placeholder syntax depends on the driver. Do not copy SQLite's question mark into an unrelated client without checking its documentation. Dynamic identifiers such as column names usually need a fixed allowlist rather than a value placeholder. Restrict the application's database privileges as another layer of protection.

Retest and write a bounded report

Retest the original case after the repair, then confirm normal catalogue lookups still work. Include an invalid identifier and a value that previously caused an error. Save the version, endpoint, parameter, observed result, and remediation evidence. Keep synthetic output in a learning portfolio; do not publish session files or other people's records.

Use the sqlmap usage documentation to understand options before expanding a test. For the underlying fix, read the OWASP prevention guidance. Next, practise documenting one authorized test with the web-security worksheet.

Related Articles

COMMENTS (0)

Sign in to post intel on this briefing.

NEWPractise supported commands in browser simulations, alongside the lesson.Try a browser practice exercise.Try a labCodelivly is 100% free, no paywall. If it helped you, chip in to keep it running.100% free. Help keep it so.Support us