Lektion 3 av 4 · 9 min
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.
Den här sidan finns på engelska tills vidare.
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
| Flaw | Untrusted input becomes… | Primary prevention |
|---|---|---|
| Buffer overflow | Memory the program relies on | Memory-safe languages, bounds checks, OS protections |
| SQL injection | Part of a database command | Parameterised queries (prepared statements) |
| Cross-site scripting | Script running in someone else’s browser | Context-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.
Viktigt att minnas
- 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.
Testa dig själv
1. What is the primary defense against SQL injection recommended by OWASP?
- Hiding the database
- Parameterised queries (prepared statements) — Rätt.
- Longer passwords
- Blocking all apostrophes
Sending the query structure separately from the data means input can never change the command.
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 — Rätt.
- A weak password
- Too many browser tabs
The extra data overwrites neighbouring memory the program relies on.
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 — Rätt.
- Nobody, it is blocked by default
XSS abuses the trust a victim’s browser places in the site that served the page.
Testa det med FireAI
Omsätt den här lektionen i praktiken på din egen Mac.
- Regler: app, webbplats, domän, IP eller ett intervall, för alltid eller tills du startar om — Skriv en regel lika precis som en adress eller lika bred som en hel domän.
- Världskartan — Se var din data faktiskt går, inte bara ett värdnamn du skulle behöva slå upp själv.
- Hitta appar med kända säkerhetsbrister — Täpp till kända hål innan någon utnyttjar dem: uppdatera den enda app som behöver det.
- Säkerhetslägen: Hem, Café, Paranoia, Under attack — Matcha FireAIs stränghet till var din Mac faktiskt är, med ett tryck.
- Undersök en anslutning — Bestäm dig med fakta framför dig, inte en vag varning.
Källor
- OWASP Top 10
- OWASP: SQL Injection Prevention Cheat Sheet
- OWASP: Cross Site Scripting Prevention Cheat Sheet
- OWASP: Buffer Overflow
- MITRE CWE-120: Classic Buffer Overflow
- MITRE CWE-89: SQL Injection
- MITRE CWE-79: Cross-site Scripting
- CISA: Secure by Design
Omsätt det på din Mac
Prova alla funktioner gratis i 17 dagar, inget kort behövs.