← Security lab: audit a Pi with Kali

What guessing looks like in the log

A computer records every login attempt. A long run of failures for one account from one address in a short time is someone guessing passwords. Reading the log is the defender's view of an attack, and the fix is to make guessing harder, not to hope it fails.

Someone has been trying the target's login. How do you tell, and how bad is it?

This is authentication (who are you?) from the defender's side. Every login attempt, good or bad, is written in /var/log/auth.log. Only an administrator can read it, so you log in to the target as the lab's admin account and use sudo. grep picks out lines, and grep -c counts them.

How this lesson works: Predict, Try, Investigate, Modify, Practise, Make
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

A run of failures

The log shows 14 'Failed password for root' lines in two minutes, all from 10.0.0.9. What does that most likely mean?

TryTry it on the board and compare the result with your prediction.

Try: read the log as the admin

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

You are on the target as admin. Find the failed logins: sudo grep Failed /var/log/auth.log.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: Counting and comparing

Worked exampleBefore each step, say what you think comes next.
  1. sudo grep Failed /var/log/auth.log\text{sudo grep Failed /var/log/auth.log}every failed login attempt.

Now try it yourself.

Which pair of facts makes a log pattern look like guessing?

Sam reads the log. Click the line where it first goes wrong.

Which change makes guessing a password hardest?

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

Count the failed logins: sudo grep -c Failed /var/log/auth.log.

Use the board. This ticks when you get there.

Find the logins that worked: sudo grep Accepted /var/log/auth.log.

Use the board. This ticks when you get there.

Count only the failures for root: sudo grep -c "Failed password for root" /var/log/auth.log.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Practise

Why do guessers try 'root' first?

Which of these slows a guesser down the most?

Review: telnet shows version 2.0 (new). Is it a finding?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Count and keep it

Then: cat fails.txt. In the next lesson you fix the login.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

You need the count of failures from 10.0.0.9.

Another hint

sudo grep -c 10.0.0.9 /var/log/auth.log counts the lines.

Another hint

Send it to a file with > fails.txt.

Save the number of log lines from 10.0.0.9 into a file called fails.txt in your home folder.

Use the board. This ticks when you get there.

Now you can
Next lesson5. Close the doors: harden the targetFixing means closing what is not needed, updating what is old and tightening the login, in that order of thought: fewer doors, stronger locks. Each fix does one job and none of them does all of them, so you layer them and then check again.
For teachers

A computer records every login attempt. A long run of failures for one account from one address in a short time is someone guessing passwords. Reading the log is the defender's view of an attack, and the fix is to make guessing harder, not to hope it fails. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Try the boards yourselves.

The gate (read first). This course teaches security as audit and defence: you scan, read and fix a lab computer your class owns. The pretend Kali and target in the lessons are safe anywhere. The real-lab version is different, so it needs your sign-off before anyone starts: school policy and IT's agreement, a written lab agreement every student signs (lab-rules.txt on the board is a model), and a lab that is cut off from every other network: a Pi running Kali and a second Pi or a virtual machine as the target, joined by one switch or one ethernet cable, with no router and no internet. Download the Kali image and any tools once, at home or at IT's request. Scan only the target named on the lab sheet. This course does not teach breaking into anything: it stops at find, understand and fix. It relaxes the 'defensive only' rule of the other courses only in that the real lab uses real scanning tools on a computer the class owns.

Simplifications: the log on the board is made up for the lesson (14 failures from one address, one success). The pretend admin can read it with sudo; on a real Pi the first account can read it without. The addresses are private lab addresses. This lesson stays on the defender's side: it reads and counts, and does not teach how guessing is done.

Real lab, if you run one: Kali Linux has a Raspberry Pi image (Pi 4 or Pi 5; the Pi 5 wants its 27 W supply); flash it with Raspberry Pi Imager or the Kali image instructions and boot it with a screen and keyboard, or ssh in. The target can be a second Pi (an older Raspberry Pi OS with a few extra services) or a purpose-made practice target your IT team approves. Start from the same state every time: re-flash, or snapshot a virtual machine. nmap is already on Kali. Give each pair a printed lab sheet naming the target's address, the one account they may use and what they are allowed to change.

Other options: fail2ban blocks addresses automatically after repeated failures; journalctl -u ssh is the same story on systems that use the journal; a central log server keeps the record safe from the computer being attacked.

With internet: a server on the open internet is guessed at within minutes, all day, by automated scanners. That is why exposing a server raises the stakes (see System admin on a Raspberry Pi).

Running the lesson

  1. Predict: wrong answers treat failure as success of the defence.
  2. Try: ask which user the failures target.
  3. Worked example: stop at Sam and ask what the 'Accepted' line means a week later.
  4. Vary: ask what the single success might be.
  5. Make: ask what number the report should lead with.

The hard part (on purpose)

Seeing the pattern rather than the lines. Ask what a real person who forgot a password looks like.

When students get stuck

Stretch and support

Stretch: Find out what fail2ban does and what it cannot protect against.

Support: A card of the three grep commands.

Where it leads

Links: Security and privacy › Sign-in and permissions; Cyber safety and privacy › Passwords; next: Close the doors.

What each step is for