What if you type code, not a name?
A website asks for your name. You type "Sam". The site says "Hello Sam".
Now a different person types something strange. The site shows every name in its list.
The site made a mistake: it trusted what a stranger typed. Many attacks start like this.
Key words
| Word | What it means | Example |
|---|---|---|
| Hacker | A person who gets into a system. A malicious one has no permission. | a criminal |
| Hacktivist | A hacker who attacks to support a cause. | a protest attack |
| Nation state | Hackers working for a country's government. | spying on another country |
| Brute force | Trying every possible password until one works. | 0000, 0001, 0002 ... |
| Cross-site scripting | XSS: getting a page to run an attacker's code in other people's browsers. | a harmful comment |
| Database | An organised store of data that programs can question. | a table of users |
| Query | A question a program asks a database. | find the user called sam |
| SQL injection | Typed text that changes what a database query does. | a search box that shows secrets |
| Buffer overflow | Too much data for a small space, so it spills into the next. | 20 letters in a 10-letter box |
| Man-in-the-middle | An attacker who sits secretly between two people and reads their messages. | a fake Wi-Fi hotspot |
| DNS | The internet's phone book. It turns a name into an address. | shop.com becomes 203.0.113.5 |
| API | A way for one program to ask another program for data. | a weather app asking a weather service |
The cells students fill in are left blank in this preview.
Quick check: who attacks and why?
Have a go
Quick check: who attacks and why?
3 questionsWho attacks?
| Attacker | Wants | Example |
|---|---|---|
| Hacktivist | To make a point | Takes down a site of a company they dislike |
| Nation state | Secrets and power | Spies on another country's government |
| Organised crime | Money | Steals card numbers and sells them |
| Individual | Money, fun or revenge | A student who tries to change their own marks |
The cells students fill in are left blank in this preview.
Match the attacker to the story
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Each extra character multiplies the guesses
A computer can try millions of passwords a second. Brute force tries all of them.
The more characters you use, the more combinations there are. Each new character multiplies the number.
See the table. A 4-digit PIN has 10 choices for each of 4 places.
Worked example: how many guesses?
| Password type | Choices per place | Places | Working | Combinations |
|---|---|---|---|---|
| 4-digit PIN | 10 | 4 | 10 x 10 x 10 x 10 | 10,000 |
| 6-digit PIN | 10 | 6 | 10 x 10 x 10 x 10 x 10 x 10 | 1,000,000 |
| 3 lowercase letters | 26 | 3 | 26 x 26 x 26 | 17,576 |
The cells students fill in are left blank in this preview.
Try it: count the combinations
# In Python, ** means "to the power of".
# 10 ** 4 means 10 x 10 x 10 x 10.
print(10 ** 4)
# A computer that tries 1000 guesses a second:
guesses_per_second = 1000
seconds = (10 ** 4) / guesses_per_second
print("A 4-digit PIN takes", seconds, "seconds")
# Change 10 ** 4 to 10 ** 6 in both places. How long now?
Make a free account to save your work and track your progress.
Extra: Going further: longer passwords
| Password type | Choices per place | Places | Combinations |
|---|---|---|---|
| 3 lowercase letters | 26 | 3 | 17,576 |
| 6 lowercase letters | 26 | 6 | 308,915,776 |
| 8 lowercase letters | 26 | 8 | 208,827,064,576 |
The cells students fill in are left blank in this preview.
Typed text can change a query
A website often asks a database a question. The question is built from the text you type.
In SQL injection, the attacker types text that changes the question. The database answers a different question, and may show secret rows.
The cause: the site mixed your text into the query and trusted it. The fix is to check typed text and keep it apart from the question.
Extra: Try it: a changed query
-- This is a toy table of your own. Nothing real can be harmed.
-- A normal search for the name sam:
SELECT name, role FROM users WHERE name = 'sam';
-- Now the typed text is x' OR '1'='1 and the query becomes:
SELECT name, role FROM users WHERE name = 'x' OR '1'='1';
-- Run both. Why did the second one show every user?
Make a free account to save your work and track your progress.
Extra: Try it: the safe way
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE users (name TEXT, role TEXT)")
db.execute("INSERT INTO users VALUES ('sam', 'student')")
db.execute("INSERT INTO users VALUES ('admin', 'administrator')")
typed = "x' OR '1'='1"
# Unsafe: the typed text is pasted into the question.
unsafe = "SELECT name FROM users WHERE name = '" + typed + "'"
print("Unsafe:", db.execute(unsafe).fetchall())
# Safe: the ? is a hole. The typed text can only fill the hole.
print("Safe:", db.execute("SELECT name FROM users WHERE name = ?", [typed]).fetchall())
Make a free account to save your work and track your progress.
The one rule
Warning
Only try attacks on a lab copy that is yours. The Computer Misuse Act 1990 makes it a crime to try them on a system that is not yours, even for fun.
Typed code can run in other browsers
A comment box shows what people type. What if a person types a bit of code and the page runs it?
That is cross-site scripting (XSS). The code runs in other visitors' browsers. It could steal their login.
The fix: show typed text as text. Never as code.
Extra: Try it: text or code?
<p>Press each button. Then type your own text and try again.</p>
<input id="box" value="<b>hello</b>">
<button id="unsafe">Show (unsafe)</button>
<button id="safe">Show (safe)</button>
<div id="out"></div>Make a free account to save your work and track your progress.
Quick check: text or code?
Have a go
Quick check: text or code?
3 questionsBuffer overflow: a box that is too small
| Memory box | Size | Before | After a name of 11 letters is written |
|---|---|---|---|
| Box 1: name | 10 letters | sam | AAAAAAAAAA (full) |
| Box 2: is_admin | 1 letter | N | A (letter 11 spilled over. N is gone) |
| Fix | Check the length first. Refuse anything over 10 letters. |
The cells students fill in are left blank in this preview.
Four more threats
| Threat | What happens | How to reduce it |
|---|---|---|
| Man-in-the-middle | An attacker secretly reads or changes messages between two people | Use encrypted connections (HTTPS). Avoid public Wi-Fi for private tasks |
| DNS attack | The phone book is changed so a real name leads to a fake site | Use trusted DNS. Check the padlock and the address |
| Insecure API | A program door that gives out data to anyone who asks | Require a login key. Limit what each key can do |
| Open Wi-Fi | Anyone can join. Others on it may watch your traffic | Use a password on the Wi-Fi. Use a VPN away from home |
The cells students fill in are left blank in this preview.
Man-in-the-middle
flowchart LR
you([You]) -->|message| attacker[Attacker on fake Wi-Fi]
attacker -->|copy or changed message| bank[Your bank]
bank -->|reply| attacker
attacker -->|reply| you
Scenario: what went wrong?
Have a go
Scenario: what went wrong?
4 questionsYour turn: make input safe
Fill in three functions. The first counts the combinations in a password. The second makes typed text safe to show on a page. The third checks a username. Press Run Tests.
def combinations(symbols, length):
# symbols is how many different characters can be used. length is how many are typed.
# A 4-digit PIN has 10 symbols and a length of 4, so 10000 combinations.
return 0
def escape_html(text):
# Swap the dangerous characters so a browser shows them as text and does not run them.
# & becomes & < becomes < > becomes >
return text
def username_ok(name):
# A good username is 3 to 12 characters long, and only letters and digits.
return True
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Final check
Have a go
Final check
5 questionsRemember
Important
Many attacks work because a site trusts what a stranger sends. Check input, use safe queries, encrypt traffic and use long passwords. Practise only on your own lab copy.