Signed in on the library computer
Amira signs in to her college noticeboard on a library computer, reads a message and presses Log out. Ten minutes later someone else sits down at the same computer.
Whether they can get into her account depends on how the app keeps her session: the record that says this browser belongs to Amira. This lesson looks at that record and three ways to get it wrong.
Only your own lab
Warning
Every attack here is against the weak copy of the Noticeboard in your own lab, which has no way out to the internet. Trying it on a site you do not own, without written permission, is a crime in the UK under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can judge whether a web app keeps its sessions safely.
Starter
Have a go
Starter
3 questionsKey words
| Word | What it means | Example |
|---|---|---|
| Session id | a random value that names one signed-in browser | |
| Cookie flag | an attribute that limits where a cookie goes | HttpOnly |
| Session fixation | making a victim sign in on an id you already know | |
| Rotation | issuing a new session id at sign-in | |
| Idle timeout | ending a session nobody has used for a while |
The cells students fill in are left blank in this preview.
Three flags limit where the key goes
A session cookie is sent with every request, so its flags decide where the key can travel. Secure sends it over HTTPS only. HttpOnly hides it from script, so a cross-site scripting bug cannot read it. SameSite=Lax keeps it off most requests started by other sites.
A name starting __Host- makes the browser insist on Secure, Path=/ and no Domain. Then no other site under the same domain can set it. The lab runs the app twice: port 8000 is weak, port 8001 safe.
Read both copies' session cookies
docker exec tools python3 steps/cookies.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: what cookies.py printed
| Copy | Set-Cookie line | Missing |
|---|---|---|
| weak, port 8000 | sid=<random>; Path=/ | Secure, HttpOnly, SameSite |
| safe, port 8001 | __Host-sid=<random>; Path=/; Secure; HttpOnly; SameSite=Lax | nothing |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsTry it: spot a missing flag
# Which of the three flags does this Set-Cookie line carry?
line = "sid=Zq81; Path=/; HttpOnly"
parts = [one.strip() for one in line.split(";")]
names = [one.split("=")[0].lower() for one in parts[1:]]
for flag in ["Secure", "HttpOnly", "SameSite"]:
if flag.lower() in names:
print(flag, "present")
else:
print(flag, "MISSING")
# Add "; Secure; SameSite=Lax" to the line and run it again.
Make a free account to save your work and track your progress.
A new id at sign-in stops fixation
Many apps hand every visitor a session before they sign in, for a basket or a form. Session fixation abuses that. Dan visits and gets an id, then gets that same id into Amira's browser, through a shared computer or a page that sets cookies. When Amira signs in, a weak app marks that session as hers, and Dan holds it.
The fix is rotation: throw the old id away at sign-in and issue a new one. Do the same whenever a user gains rights.
Fix a session on the weak copy
docker exec tools python3 steps/fixation.py weakShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try the same on the safe copy
docker exec tools python3 steps/fixation.py safeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a random session id is safe whoever chose it. Randomness stops guessing, not planting: if the id existed before sign-in, whoever knew it then knows it now. Issue a new id at every sign-in.
Log out on the server, and time out
Pressing Log out must end the session on the server. Telling the browser to forget the cookie is not enough: a copy taken earlier, from a shared computer or a stolen laptop, would still work.
Two timers limit what a stolen id is worth. An idle timeout ends a session nobody has used, often after 15 to 30 minutes. An absolute limit ends every session after a fixed time, such as 8 hours. The safe copy ends a session after 15 idle minutes.
Log out, then reuse the cookie
docker exec tools python3 steps/logout.py weakShown here, not run. Press Start lab to type it into your own lab’s terminal.
The same on the safe copy
docker exec tools python3 steps/logout.py safeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsFixing it and spotting it
The fixes are all in the safe copy. It sets the three flags and a __Host- name and issues a new id at sign-in. It deletes the session at Log out and ends idle sessions.
To spot an attack, log each sign-in with a short prefix of the session id. One id that was handed to one browser and signed in from another is a warning. The app's log shows every decision both copies made, each line starting with a time.
Read the app's log
docker logs appShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: check a Set-Cookie line
A Set-Cookie line is a name and value, then attributes separated by ;. Write two functions.
parse_cookie(line) returns a dictionary with name and value, plus each attribute in lower case: a flag such as Secure becomes "secure": True, and a pair such as SameSite=Lax becomes "samesite": "Lax". Strip the spaces round each part.
problems(line) returns a list of what a session cookie lacks: "Secure", "HttpOnly" and "SameSite", in that order, for each that is missing. Then, if the name starts with __Host-, add "__Host- rules" when the cookie is not Secure, its path is not /, or it has a Domain.
def parse_cookie(line):
return {}
def problems(line):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go