Protego field desk

Web Vulnerabilities: OWASP Top 10 · PTES: Vulnerability analysis

L15. Injection: SQL Injection and XSS

Explain and safely demonstrate SQL injection and cross-site scripting in a lab, and describe the standard defenses.

Lesson 15 of 21 · About 40 minutes including the lab

By the end of this lesson you can

  • Explain how injection happens when untrusted input is interpreted as code.
  • Demonstrate a basic SQL injection in DVWA or a PortSwigger lab.
  • Demonstrate a reflected cross-site scripting payload in a lab and explain the impact.
  • Describe parameterized queries and output encoding as primary defenses.
Video transcript

A search box takes your input. What if the app runs it? Injection happens when untrusted input is treated as code, by a database or by a browser. SQL injection and cross site scripting are the classic examples. In the lab, one quote can break the query and show that your input reaches the database. With cross site scripting, the input comes back in the page and the browser runs it. Parameterized queries and output encoding keep data from being treated as code. The database cannot tell the data from the code. The query structure stays the same, whatever the user types. Try payloads only in DVWA, Juice Shop or PortSwigger labs. Show the flaw with minimal data, and never dump real records. Recommend parameterized queries and output encoding. Keep data and code apart. Check the idea in the knowledge check.

Course outlineLesson 15 of 21

Lesson notes

Injection happens when an application takes data you control and incorporates it into a command or query so that your data changes the meaning of the command. SQL injection and cross-site scripting (XSS) are the two every beginner must understand. Practice them only in labs.

SQL injection: if a login query concatenates a user-supplied string into SQL, a carefully chosen string can change the query’s logic. The fix is parameterized queries (prepared statements), where the SQL structure is fixed and the value is sent separately. Input validation helps but is not enough on its own.

Cross-site scripting: the application includes untrusted input in HTML without encoding it, so a browser runs an attacker’s script in the victim’s session. Reflected XSS comes back in the immediate response. Stored XSS is saved and shown later. DOM XSS happens in client-side script. Impact includes session theft, defacement and actions as the victim. The fix is context-aware output encoding and a Content Security Policy.

When you demonstrate injection in a report, use the mildest proof that shows impact: reading a harmless table row, or popping a dialog in a lab. Do not dump entire customer tables or steal real sessions.

Automated scanners find many injection candidates; humans confirm them. Always confirm in a proxy so you can see the exact request and response you will attach as evidence.

Commands and expected output

A classic SQL injection probe (authorized lab only)

# In DVWA SQL Injection at low security, try a user id of:
1' OR '1'='1
# Then view the request in your proxy.

On a vulnerable query this returns more rows than id=1 alone. On a fixed query (impossible security, or parameterized statements) it returns an error or nothing useful. Never send this to a system that did not authorize it.

A reflected XSS probe (authorized lab only)

# In a lab search box, try a script tag that calls alert(1),
# or an img tag with an onerror handler that calls alert(1)
# if angle brackets are filtered.

If a dialog appears, the page reflected your input as active HTML. Document the request URL and the response. Use alert(1) or a unique string; do not load third-party scripts into a client’s app.

Hands-on labs

Practice only on these legal targets: your own local lab or machines a platform assigns to you. Never use these techniques on systems you do not have written permission to test.

  • PortSwigger Web Security Academy

    SQL injection labs

    Complete the first two SQL injection labs, including one that retrieves hidden data.

  • PortSwigger Web Security Academy

    XSS labs

    Complete a reflected XSS lab and a stored XSS lab.

  • DVWA

    SQL Injection and XSS (reflected)

    At low security, solve SQL Injection and XSS Reflected. Then switch to impossible and observe that the same inputs fail.

  • OWASP Juice Shop

    Injection challenges

    Solve a one-star SQL injection or XSS challenge from the Score Board.

Practice task

In DVWA or a PortSwigger lab, capture one SQL injection request and one XSS request in your proxy. For each, write the vulnerable parameter, the payload, the evidence in the response and the proper fix in one sentence.

Key takeaways

  • Injection: untrusted input changes the meaning of a query or command.
  • Primary SQL defense: parameterized queries. Primary XSS defense: output encoding + CSP.
  • Prove impact with the mildest evidence that still demonstrates the issue.
  • Never test injection on unauthorized systems.
Knowledge Check

1. What is the primary defense against SQL injection?

2. A search box reflects a script tag back as active HTML. What is this?

3. Why prefer a simple dialog proof over loading a remote script when demonstrating XSS in a client lab?

4. Input validation alone is not enough against SQL injection because: