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

Free lesson

Finding secrets in code and history

After this lesson: I can find a secret in a repository's history and stop the next one.

60 min lesson12 commandsDevSecOps lab: build a security pipeline and a gate

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

Already have an account? Sign in

Deleted, but still there

In January a developer put the shop's payments key in a file, pushed, then noticed. The next day they replaced it with a line that reads the key from the environment.

The file looks clean now. Is the key gone?

By the end of this lesson

Note

I can find a secret in a repository's history and stop the next one.

Key words

WordWhat it means
Secreta key or password that grants access
Historyevery commit a repository has ever had
Entropyhow random a string looks
Rotatereplace a key and cancel the old one
Fingerprintthe id of one finding, to accept it

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

Starter

Have a go

Starter

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

Scan the files, then the history

Gitleaks is an open-source secret scanner. gitleaks dir reads the files as they are now. gitleaks git reads every commit in the history.

--redact hides the value in the output, so the scan does not leak the secret a second time.

Bring the lab's copy up to date

Command
docker exec gitleaks git -C /work/tuckshop pull -q

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

List the commits

Command
docker exec gitleaks git -C /work/tuckshop log --oneline

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the files as they are now

Command
docker exec gitleaks gitleaks dir /work/tuckshop --no-banner --redact

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan every commit

Command
docker exec gitleaks gitleaks git /work/tuckshop --no-banner --redact -v

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

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

A common mistake

Warning

Many people think deleting the line in a new commit removes the secret. Every clone still holds the old commit. Once a key is pushed, rotate it first: make a new one and cancel the old. Rewriting history comes after, if at all.

How a scanner spots a key

Gitleaks has rules for many providers' key formats. Its generic rule looks for a name such as KEY or TOKEN and a value with high entropy.

Entropy measures how mixed the characters are. A word scores low; a random key scores high. The shop's key scored about 4.9.

Try it: measure entropy

Code: python
import math

def entropy(text):
    counts = {}
    for ch in text:
        counts[ch] = counts.get(ch, 0) + 1
    total = len(text)
    return sum(n / total * math.log2(total / n) for n in counts.values())

for value in ["password1", "aaaaaaaaaaaa", "7Gq2Rk9Xw4Lm8Tz1Vb5Nc3Hd"]:
    print(round(entropy(value), 2), value)

# Add a value of your own. Which kind of string scores highest?

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

A rule for your own key format

The generic rule can miss a key, for example one whose value contains the word "fake". Your own keys have a known shape, so describe it.

The shop's keys start tuck_live_ and 24 letters or digits follow. The rule extends the built-in ones.

Write the shop's own rule

Command
docker exec -i gitleaks sh -c 'cat > /work/rules/gitleaks.toml' <<'EOF'
[extend]
useDefault = true

[[rules]]
id = "tuckshop-payments-key"
description = "Tuck shop payments key"
regex = '''tuck_live_[A-Za-z0-9]{24}'''
keywords = ["tuck_live_"]
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the history with it

Command
docker exec gitleaks gitleaks git /work/tuckshop --config /work/rules/gitleaks.toml --no-banner --redact -v

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

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

Accept a rotated key, stop a new one

The old key was rotated, so the team accepts that finding. Its fingerprint goes in an ignore file, and the scan passes.

Then a pre-commit hook scans what you are about to commit. A hook can be skipped, so the pipeline scans again on the server.

Accept the old finding by its fingerprint

Command
docker exec gitleaks sh -c 'gitleaks git /work/tuckshop --no-banner -v 2>/dev/null | grep Fingerprint | cut -d" " -f2 > /work/rules/.gitleaksignore'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan again with the ignore file

Command
docker exec gitleaks gitleaks git /work/tuckshop --gitleaks-ignore-path /work/rules/.gitleaksignore --no-banner

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Clone the shop in your terminal

Command
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.git

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Add a pre-commit hook

Command
printf '#!/bin/sh\ngit diff --cached | docker exec -i gitleaks gitleaks stdin --no-banner --redact\n' > tuckshop/.git/hooks/pre-commit && chmod +x tuckshop/.git/hooks/pre-commit

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try to commit a made-up key

Command
echo 'PAYMENTS_KEY = "tuck_live_Zz9Yy8Xx7Ww6Vv5Uu4Tt3Ss2"' >> tuckshop/settings.py && git -C tuckshop -c user.name=Student -c [email protected] commit -qam 'Try a key'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Throw the change away

Command
git -C tuckshop checkout settings.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: a small secret scanner

Write a small secret scanner in three functions.

entropy(text) returns the Shannon entropy of a string in bits per character, rounded to 2 places, and 0 for an empty string: for each character, take its share p of the string and add up -p * log2(p). is_suspect(line) returns True when the line has an =, the name to its left contains KEY, TOKEN or PASSWORD (in any case), and the value to its right, with spaces and double quotes stripped, is at least 16 characters long with an entropy of at least 3.5. redact(line) returns the line with that value replaced by REDACTED inside the quotes, keeping the first 4 characters, such as KEY = "tuckREDACTED".

Code: python
import math


def entropy(text):
    return 0


def is_suspect(line):
    return False


def redact(line):
    return line

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

Exit quiz

Have a go

Exit quiz

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

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.