469 words
2 minutes
πŸ” PicoGym - spelling-quiz

πŸ“‚ Download challenge file.

Description: I found the flag, but my brother wrote a program to encrypt all his text files. He has a spelling quiz study guide too, but I don’t know if that helps.
Difficulty: Medium
Author: BrownieInMotion

Summary#

It’s an encryption of all text files using a substitution cipher. We have three things to work with: the encryption program, an encrypted flag, and an encrypted spelling quiz study guide. The key advantage is that the study guide and flag were encrypted using the same cipher key. By analyzing the encrypted study guide with frequency analysis, we can discover the substitution mapping and decrypt the hidden flag.

Analysis#

Challenge Files#

The challenge provides a public.zip file containing three items:

encrypt.py β†’ The encryption program
flag.txt β†’ The encrypted flag
study-guide.txt β†’ Encrypted spelling quiz words

The Encrypted Flag#

Looking at the encrypted flag:

Terminal window
$ cat flag.txt
brcfxba_vfr_mid_hosbrm_iprc_exa_hoav_vwcrm

This looks like random letters - but it’s actually a substitution cipher.

Understanding the Encryption#

Here’s the encryption program:

import random
import os
files = [
os.path.join(path, file)
for path, dirs, files in os.walk('.')
for file in files
if file.split('.')[-1] == 'txt'
]
alphabet = list('abcdefghijklmnopqrstuvwxyz')
random.shuffle(shuffled := alphabet[:])
dictionary = dict(zip(alphabet, shuffled))
for filename in files:
text = open(filename, 'r').read()
encrypted = ''.join([
dictionary[c]
if c in dictionary else c
for c in text
])
open(filename, 'w').write(encrypted)

What this does:

  1. Finds all .txt files in the directory
  2. Creates a shuffled alphabet (randomized order)
  3. Creates a dictionary mapping original letters to shuffled letters
  4. For each file, replaces every letter using the dictionary
  5. Saves the encrypted text back to the file

Key insight: The same shuffled alphabet is used to encrypt ALL files, including the study guide and flag!

The Study Guide#

The encrypted study guide contains words from a spelling quiz:

Terminal window
$ head study-guide.txt
gocnfwnwtr
sxlyrxaic
dcrrtfrxcv
uxbvwavcq
lwvicwtiwm
pwtmwnxvicq
avingciisa
ylwtmrcawx
mwaxdcrrxuwlwvq
yciflwnf

Identifying the Cipher Type#

Using cipher identification tools like dcode.fr, these encrypted words are strongly identified as Monoalphabetic Substitution.

alt text

Solution#

How Frequency Analysis Works#

In English, certain letters appear more frequently than others:

  • β€˜e’ appears most often (~12.7%)
  • β€˜t’, β€˜a’, β€˜o’, β€˜i’ are also very common
  • β€˜z’, β€˜q’, β€˜x’ are rare

If we encrypt English text, the most common encrypted letter should correspond to β€˜e’, the second most common to β€˜t’, etc.

Cracking the Cipher#

  1. Use frequency analysis tools on the encrypted texts
  2. Upload to dcode.fr Monoalphabetic Substitution
  3. Feed it the encrypted study guide - the tool analyzes letter frequencies
  4. The tool outputs the original alphabet mapping
  5. Use the mapping to decrypt the flag

Results#

After running frequency analysis on the encrypted study guide and flag:

alt text

The decrypted flag:

PERHAPS_THE_DOG_JUMPED_OVER_WAS_JUST_TIRED
⚑ Raikiri

πŸŽ‰ Flag pwned!

Wrap it as: picoCTF{PERHAPS_THE_DOG_JUMPED_OVER_WAS_JUST_TIRED}

πŸ’‘ TL;DR / Lesson Learned

βœ… Substitution Cipher - Each letter maps to exactly one other letter
βœ… Monoalphabetic - The mapping never changes (unlike polyalphabetic ciphers)
βœ… Frequency Analysis - Using letter frequency patterns to break encryption
βœ… Ciphertext-only attack - We don’t need the key, just encrypted text and patterns
βœ… Using known plaintext - The spelling quiz study guide is likely real English words!