Sub topic 14
SQL Injection and Cross-Site Scripting
This chapter assumes you know what a web application, a database and a browser do. You should also know the basic threat vocabulary from earlier chapters. The A+ Core 2 exam expects you to recognize these two web attacks from a short scenario and name the right countermeasure.
Why web applications are a target
A web application takes input from users: login forms, search boxes, comment fields and URL parameters. If the application trusts that input without checking it, an attacker can supply text that the system treats as a command instead of as data. SQL injection and cross-site scripting (XSS) both come from this one mistake.
SQL injection
SQL injection is an attack where the attacker types database commands into an input field. The application passes them to its database, which runs them. SQL (Structured Query Language) is the language most databases use.
Suppose a login page builds this query from what the user types:
SELECT * FROM users WHERE name = 'alice' AND password = 'secret';An attacker enters ' OR '1'='1 as the password. The query now becomes:
SELECT * FROM users WHERE name = 'alice' AND password = '' OR '1'='1';The condition '1'='1' is always true, so the database returns rows without a valid password. Other injected commands can read customer records, change data or delete tables.
Typical exam clues are a login that accepts odd characters such as quotes or --, or a database that leaks records after someone typed text into a form.
Cross-site scripting (XSS)
Cross-site scripting is an attack where the attacker places malicious script in a web page that other users then view. The victim's browser runs the script because it trusts the site.
For example, an attacker posts a comment on a forum that contains a script tag. The site saves it without cleaning it. Every visitor who opens that page runs the script. It can steal session cookies, redirect the user to a fake site or act as the user.
The key difference is the target:
| Attack | What the attacker abuses | Who is harmed | Exam clue |
|---|---|---|---|
| SQL injection | The site's database | The organization and its stored data | Odd text in a form returns or changes database records |
| XSS | Other users' browsers | Visitors to the site | A script runs when users view a page or click a link |
Countermeasures
Most fixes happen in the application code, which the web developer owns. As a technician you should know the concepts.
- Input validation and sanitization. Check that input has the expected form and remove or reject dangerous characters.
- Parameterized queries. These keep user input separate from the SQL command, so input is never run as a command. This is the main defense against SQL injection.
- Output encoding. This turns characters like
<into harmless text before showing them, which is the main defense against XSS. - Patching and updates. Keep the web server, framework and plug-ins current.
- Least privilege. The application's database account should have only the rights it needs.
Exercise
A company's product search page shows all customer records when someone enters ' OR '1'='1 in the search box. Which attack is this, and what is the best defense?
Show answer
This is SQL injection. The best defense is input validation with parameterized queries, so the input is treated as data and not as part of the SQL command.
Exercise
Visitors to a forum report pop-ups and redirects after viewing one user's comment. Which attack is this, and who is affected?
Show answer
This is cross-site scripting (XSS). The stored script runs in the browsers of visitors who view the page.