跳到正文
← Offensive security: how the red team thinks

第 3 课,共 4 课 · 9 分钟

Exploitation basics: buffer overflows, SQL injection and XSS, and how to prevent them

Three of the most famous classes of software flaw, explained conceptually: why they happen, what an attacker gains, and the coding practices that stop them.

此页面暂时只有英文版。

Most successful attacks exploit a mistake in software. Three classes of mistakes have been around for decades and still appear in vulnerability reports every year: buffer overflows, SQL injection and cross-site scripting (XSS). This lesson explains how each works in principle and, more importantly, how developers and defenders prevent them. It deliberately contains no attack code: understanding the idea is enough to recognise and fix the flaw.

All three share one root cause: a program treats data from outside as if it were trustworthy. Input that should only ever be data ends up being interpreted as instructions, whether by the processor, the database or the browser.

Buffer overflows

A buffer is a fixed-size area of memory reserved to hold data, such as a name typed into a form. If the program copies more data into it than it can hold without checking the length, the extra bytes spill over into neighbouring memory. MITRE’s Common Weakness Enumeration calls this a classic buffer overflow (CWE-120). Depending on what gets overwritten, the result ranges from a crash to an attacker redirecting the program to run code of their choosing.

Overflows mostly affect languages such as C and C++, which let programmers manage memory directly. Defenses work in layers: memory-safe languages (Swift, Rust, Go, Java) that check bounds automatically; careful length checks in older code; and operating-system protections such as address space layout randomisation and non-executable memory, which make exploitation much harder even when a bug exists. Apple’s platform security guidance describes several such protections, and Apple silicon adds hardware features like pointer authentication.

SQL injection

Many applications build database queries by combining a fixed query with something the user typed, such as a search term or a username. If the input is pasted straight into the query text, a carefully crafted input can change the query’s meaning, for example to return every record instead of one, or to modify data. This is SQL injection, CWE-89, and it has caused some of the largest data breaches on record.

The fix is well known and reliable: parameterised queries, also called prepared statements. The query structure is sent to the database separately from the data, so input can never be interpreted as part of the command. The OWASP SQL Injection Prevention Cheat Sheet recommends this as the primary defense, supported by least-privilege database accounts and input validation.

Cross-site scripting (XSS)

XSS happens when a website includes content from one user in a page shown to another user, without neutralising it. If that content contains script, the victim’s browser runs it as if it came from the trusted site. From there it can read what the page shows, act as the logged-in user or steal session information. CWE-79 describes this weakness, and it comes in stored, reflected and DOM-based variants depending on where the untrusted content enters the page.

The OWASP Cross-Site Scripting Prevention Cheat Sheet centres on output encoding: whenever untrusted data is placed into a page, encode it for that exact context (HTML text, an attribute, JavaScript, a URL) so the browser treats it as text, not code. Modern web frameworks do much of this automatically, and a Content Security Policy adds a second layer by limiting which scripts a page may run at all.

The pattern behind all three

FlawUntrusted input becomes…Primary prevention
Buffer overflowMemory the program relies onMemory-safe languages, bounds checks, OS protections
SQL injectionPart of a database commandParameterised queries (prepared statements)
Cross-site scriptingScript running in someone else’s browserContext-aware output encoding, Content Security Policy

The OWASP Top 10 groups injection flaws among the most critical web application risks, and CISA’s Secure by Design campaign pushes software makers to eliminate whole classes of these bugs rather than patch them one at a time. For testers, finding one of these flaws is only half the job; the report should explain the root cause so developers fix the pattern, not just the single instance.

What this means for users

You cannot fix a vendor’s code, but you can shrink the window between a fix and your safety: install updates promptly, especially for browsers and anything facing the internet. FireAI flags apps on your Mac that need a security update, and asks before a new or modified program goes online, which limits what a successfully exploited app can do on the network.

要点

  • All three flaws come from treating untrusted input as instructions.
  • Buffer overflows: prevented by memory-safe languages, bounds checks and OS protections.
  • SQL injection: prevented by parameterised queries (prepared statements).
  • XSS: prevented by context-aware output encoding, helped by a Content Security Policy.
  • Fix the pattern, not just the instance, and install updates quickly.

自我检测

  1. 1. What is the primary defense against SQL injection recommended by OWASP?

    • Hiding the database
    • Parameterised queries (prepared statements) — 正确。
    • Longer passwords
    • Blocking all apostrophes

    Sending the query structure separately from the data means input can never change the command.

  2. 2. What causes a classic buffer overflow?

    • A slow network
    • Copying more data into a fixed-size memory area than it can hold, without checking the length — 正确。
    • A weak password
    • Too many browser tabs

    The extra data overwrites neighbouring memory the program relies on.

  3. 3. In cross-site scripting, whose browser runs the injected script?

    • The attacker’s only
    • The website’s server
    • Another user who views the page containing the untrusted content — 正确。
    • Nobody, it is blocked by default

    XSS abuses the trust a victim’s browser places in the site that served the page.

用 FireAI 动手做

在你自己的 Mac 上练习这节课的内容。

来源

在你的 Mac 上实践

免费试用全部功能 17 天,无需绑定银行卡。

下载 Mac 版 文档