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

Free lesson

OS 7.1 (4 of 4): Security testing

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

The open window

A shop locks its front door every night, but a back window stays open.

A thief needs only one open window. Software is the same. One weak place is enough.

Security testing looks for the weak places before a thief does.

Key words

WordWhat it meansExample
Security testingTests that look for weak places a thief could use.Is the password safe?
VulnerabilityA weak place in software that an attacker could use.A password in the code
Vulnerability scanningUsing a tool to check software for known weak places.A scanner finds an old part with a known fault
Static analysisChecking code by reading it, without running it.Search the code for a password
Dynamic analysisChecking the software while it runs.Type strange text into a form
Integration analysisChecking the joins between parts are safe.Is data checked when it moves from one part to another?
DatabaseA store of organised data.A list of users
AttackerA person who tries to break in or do harm.A thief online
Hard-codedWritten inside the code, not kept safely elsewhere.password = "abc"

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

Four tests look for weak places

TestHow it worksDoes the code run?Example
Vulnerability scanningA tool checks for known weak placesSometimesFinds an old part with a known fault
Static analysisA person or tool reads the codeNoFinds a password written in the code
Dynamic analysisA tester uses the running softwareYesTypes strange text into the login box
Integration analysisA tester checks where parts joinYesIs data checked when it goes from the page to the database?

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

Match each test to its story

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Static analysis: spot the problems

Code: python
password = "letmein123"

def check(text):
    return eval(text)

site = "http://my-shop.example"
print("This code was only read, not trusted.")

# Read the code. There are 3 problems. Do not run it to find them.
# Line 1: a password in the code.
# Line 4: eval runs any text as code.
# Line 6: http is not secure.

Make a free account to save your work and track your progress.

Never write a password in code

Warning

Many people think a password in the code is safe because only the team reads it. Code is copied and shared, so anyone who reads it can see the password. Keep passwords in a safe place outside the code.

Write a tiny analyser

Write a tiny static analyser. It gets the lines of a program as a list of text. It gives back the line numbers that have a problem. The first line is number 1. A problem is a line with password =, eval( or http:// in it. A line with two problems is listed only once. Press Run Tests.

Code: python
# A tiny static analyser. It reads code as text. It never runs it.
# Each line of the code is one string in the list "lines".

def find_problems(lines):
    # Give back a list of the line numbers (the first line is 1) that have a problem.
    # A line has a problem if it contains any of these:
    #   a password written in the code: the text  password =
    #   the dangerous function: the text  eval(
    #   an address that is not secure: the text  http://
    return []

Make a free account to save your work, or press Start lab to work through the whole lesson.

Dynamic analysis uses the running software

With dynamic analysis you use the running software, as an attacker would.

You type unusual things into forms. You try very long text. You try to open a page without logging in. You watch what the software does.

SQL injection changes the question

A database answers questions written in SQL (the language of databases).

Some programs put what you typed straight into the question. Then you can change the question. SQL injection means that your own text becomes part of the question. Run the code and see how it works.

Try it: why odd input matters

Code: python
name = "Sam"
query = "SELECT * FROM users WHERE name = '" + name + "'"
print(query)

# Now change the name to:  x' OR '1'='1
# Run again. The question now matches every user.
# This is why a tester tries odd input.

Make a free account to save your work and track your progress.

Attack only your own lab

Warning

The next lab runs a shop that is weak on purpose. Attack it, and nothing else. It is your own private copy. Never try these things on a site that is not yours. In the UK that is a crime under the Computer Misuse Act 1990.

Start the lab first

Note

Press Start the lab on the bar at the top of this lesson. Wait until it says it is running. Each Run button types its command into your lab terminal.

Lab 1. Static analysis: read the code

The shop is a small program. Show its code and read it. Do not run anything yet. Find the line that starts with rows. It joins your search text to the SQL question with a plus sign (+). That is a weak place, and you found it by reading.

Command
docker exec app cat /tmp/app/app.py

A command for the lesson’s lab terminal. It is shown here and not run.

Lab 2. Dynamic analysis: a normal search

Now use the running shop. Search for pen. A good shop gives one line: the name and the price.

Command
curl -s -G --data-urlencode "q=pen" http://app:8000/search

A command for the lesson’s lab terminal. It is shown here and not run.

Lab 3. Dynamic analysis: odd input

Now type odd text, like an attacker. The quote mark (') ends the search text early. OR 1=1 is true for every row. The two dashes (--) make the computer ignore the rest of the question. Look at the answer: the shop lists products that are not for sale.

Command
curl -s -G --data-urlencode "q=x' OR 1=1 --" http://app:8000/search

A command for the lesson’s lab terminal. It is shown here and not run.

Check: name the test you used

Have a go

Check: name the test you used

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

Integration analysis checks every join

flowchart LR
  user[User types in a form] -->|1 check here| page[Web page]
  page -->|2 check here| api[API]
  api -->|3 check here| db[(Database)]

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

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.