Skip to main content

Unit 4 · Loop-de-Loop · Lesson 38 of 57

Project: Cracking Alex's Lock

Project time!

Alex locked the admin panel with a 3 digit PIN, and then he "forgot" it. (Suspicious.)

There's a function called check_pin that tells you whether a PIN is right. It gives back True or False, and that's all it'll tell you.

A 3 digit PIN is somewhere from 0 to 999. That's only 1000 possible PINs... and computers are REALLY good at doing things 1000 times.

So let's just try all of them.

What it should do

  1. Try every PIN from 0 to 999 with check_pin
  2. When you find the right one, stop looking
  3. Print which PIN it was, and how many tries it took:
Cracked it! The PIN is 427.
It took 428 tries.

(Don't print every wrong guess, or you'll be scrolling for a while.)

The first line of the code is how we set Alex's PIN when we check your work. Pretend you can't see it, and don't use secret yourself! Just ask check_pin. When you press Run, type any PIN from 0 to 999 to play Alex.

Crack Alex's PIN!

Python exercise

Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.

You just did a brute force attack!

Trying every possible answer until one works is called a brute force attack, and it's a real thing attackers do. Your program got through up to 1000 PINs in a blink.

So how do real systems stop it?

  • Lock people out. Remember Three Strikes? Only 3 tries, and brute force is dead in the water.
  • Make it longer. 3 digits is 1,000 PINs. A 12 character password with letters, numbers and symbols has more possibilities than there are grains of sand on Earth.
  • Slow it down. Make every guess take a second. 1000 guesses is no big deal, but billions are.

Same lesson as always: if your security depends on nobody bothering to try, it's not security.

Do this lesson for real

With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.