Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

8.2 (2 of 7): Hackers and attacks on websites

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

50 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
HackerA person who gets into a system. A malicious one has no permission.a criminal
HacktivistA hacker who attacks to support a cause.a protest attack
Nation stateHackers working for a country's government.spying on another country
Brute forceTrying every possible password until one works.0000, 0001, 0002 ...
Cross-site scriptingXSS: getting a page to run an attacker's code in other people's browsers.a harmful comment
DatabaseAn organised store of data that programs can question.a table of users
QueryA question a program asks a database.find the user called sam
SQL injectionTyped text that changes what a database query does.a search box that shows secrets
Buffer overflowToo much data for a small space, so it spills into the next.20 letters in a 10-letter box
Man-in-the-middleAn attacker who sits secretly between two people and reads their messages.a fake Wi-Fi hotspot
DNSThe internet's phone book. It turns a name into an address.shop.com becomes 203.0.113.5
APIA 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Who attacks?

AttackerWantsExample
HacktivistTo make a pointTakes down a site of a company they dislike
Nation stateSecrets and powerSpies on another country's government
Organised crimeMoneySteals card numbers and sells them
IndividualMoney, fun or revengeA 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 typeChoices per placePlacesWorkingCombinations
4-digit PIN10410 x 10 x 10 x 1010,000
6-digit PIN10610 x 10 x 10 x 10 x 10 x 101,000,000
3 lowercase letters26326 x 26 x 2617,576

The cells students fill in are left blank in this preview.

Try it: count the combinations

Code: python
# 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 typeChoices per placePlacesCombinations
3 lowercase letters26317,576
6 lowercase letters266308,915,776
8 lowercase letters268208,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

Code: sql
-- 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

Code: python
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?

Code: web
<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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Buffer overflow: a box that is too small

Memory boxSizeBeforeAfter a name of 11 letters is written
Box 1: name10 letterssamAAAAAAAAAA (full)
Box 2: is_admin1 letterNA (letter 11 spilled over. N is gone)
FixCheck the length first. Refuse anything over 10 letters.

The cells students fill in are left blank in this preview.

Four more threats

ThreatWhat happensHow to reduce it
Man-in-the-middleAn attacker secretly reads or changes messages between two peopleUse encrypted connections (HTTPS). Avoid public Wi-Fi for private tasks
DNS attackThe phone book is changed so a real name leads to a fake siteUse trusted DNS. Check the padlock and the address
Insecure APIA program door that gives out data to anyone who asksRequire a login key. Limit what each key can do
Open Wi-FiAnyone can join. Others on it may watch your trafficUse 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

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

Code: python
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 &amp;   < becomes &lt;   > becomes &gt;
    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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

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.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.