HTB: The secret of a Queen Challenge
The secret of a Queen - HackTheBox Challenge Writeup
Challenge Information
| Field | Value |
|---|---|
| Challenge Name | The secret of a Queen |
| Category | Misc |
| Difficulty | Easy |
| Author | d3vn0mi |
Description
In this challenge, we’re given an image file containing encrypted symbols and tasked with decrypting it to reveal the Queen’s secret. The challenge requires identifying the cipher method, extracting visual symbols from the image, and matching them against a known alphabet to decode the message.
Solution Overview
The solution involved three main phases:
- Identifying the cipher: Recognizing that the symbols correspond to Mary, Queen of Scots’ cipher alphabet
- Extracting symbols: Parsing the image to isolate individual encrypted characters
- Decryption: Matching extracted symbols against a reference alphabet to decode the plaintext
Key Steps
Step 1: Challenge Analysis and File Extraction
# Locate and examine the challenge filesfind <workdir> -maxdepth 3
# Extract the challenge archivecd <workdir>mkdir -p workunzip -o archive.zip -d work/
# Examine the main image filefile "The secret of a Queen.png"Step 2: Identifying the Cipher Type
Through research and cross-referencing, we determined the cipher was the Mary Stuart (Mary, Queen of Scots) cipher, also known as the Mary Queen of Scots alphabet. This historical cipher was used during the Babington Plot conspiracy.
# Research the cipher using web resources# Mary Stuart cipher alphabet reference from dcode.fr and historical sourcescurl -sL -A "Mozilla/5.0" "https://www.dcode.fr/mary-stuart-cipher" -o dcode_mary.htmlStep 3: Image Processing and Symbol Extraction
We cropped the image to isolate the top section containing the encrypted symbols:
from PIL import Image
# Load and convert the challenge image to grayscaleim = Image.open('The secret of a Queen.png').convert('L')w, h = im.size
# Crop the top portion containing the cipher texttop = im.crop((0, 0, w, 100)) # Adjust coordinates based on actual imagetop.save('work/top.png')
# Further segment the image into individual character regions# Example segments extracted from analysissegs = [(72, 208), (208, 288), (292, 354), ...] # Bounding boxes for each symbolStep 4: Building the Reference Alphabet
We obtained the Mary Stuart cipher alphabet from dcode.fr and extracted individual character images:
from PIL import Image
# Download the reference alphabetimport urllib.requesturllib.request.urlretrieve( "https://www.dcode.fr/tools/mary-stuart/images/alphabet.png", "/tmp/dcode_alphabet.png")
# Crop and separate individual alphabet charactersim = Image.open('dcode_alphabet.png').convert('RGB')w, h = im.size
# Extract each letter's symbol representationrow1 = im.crop((0, 0, w, h//2)) # Adjust based on alphabet layoutStep 5: Symbol Matching and Decryption
We compared each extracted symbol from the challenge image against the reference alphabet using binary image matching:
import numpy as npfrom PIL import Image
def load_bbox_bin(path, is_dcode=False): """Load and binarize an image region""" im = Image.open(path).convert('L') arr = np.array(im) # Binarize with threshold binary = (arr > 128).astype(np.uint8) return binary
def match_symbol(extracted_symbol, alphabet_dict): """Find best matching letter from reference alphabet""" best_match = None best_score = -1
for letter, ref_binary in alphabet_dict.items(): # Calculate similarity using binary correlation similarity = np.sum(extracted_symbol == ref_binary) if similarity > best_score: best_score = similarity best_match = letter
return best_match
# Extract symbols from challenge imagechallenge_symbols = []for bbox in segment_boxes: symbol_binary = load_bbox_bin(f'work/ttop_{idx:02d}.png') challenge_symbols.append(symbol_binary)
# Match each symbol and build decrypted messagedecrypted_message = []for symbol in challenge_symbols: letter = match_symbol(symbol, reference_alphabet) decrypted_message.append(letter)
flag = ''.join(decrypted_message)Step 6: Flag Retrieval
The decrypted message revealed the flag:
HTB{REDACTED}The flag was saved to the evidence directory upon successful decryption.
Tools Used
| Tool | Purpose |
|---|---|
| Python 3 | Image processing and symbol matching |
| PIL (Pillow) | Image manipulation and cropping |
| NumPy | Binary array operations and similarity calculations |
| curl | Downloading reference materials and alphabet images |
| Bash | File management and command execution |
| dcode.fr | Mary Stuart cipher reference alphabet |
| Wikipedia | Historical context on Babington Plot and Mary, Queen of Scots |
Key Learnings
-
Historical Cipher Recognition: The Mary Stuart cipher is a lesser-known historical cipher with distinct visual symbols. Identifying the cipher type is crucial before attempting decryption.
-
Image Processing Techniques: Extracting individual symbols from a composite image requires careful bounding box identification and binary conversion for reliable matching.
-
Template Matching: When dealing with symbol-based ciphers, creating a reference alphabet and using binary correlation for matching is more reliable than attempting character recognition.
-
Research-Driven Approach: Leveraging OSINT tools and web resources to identify cipher types significantly accelerates the challenge-solving process.
-
Binarization for Comparison: Converting grayscale images to binary (black/white) before comparison improves matching accuracy by removing noise and lighting variations.
Conclusion
“The secret of a Queen” challenges solvers to recognize historical ciphers, extract visual data from images, and apply image processing techniques for cryptanalysis. The key to success was identifying the Mary Stuart cipher alphabet and systematically matching extracted symbols against the reference to decrypt the message. This demonstrates the intersection of cryptography, computer vision, and historical knowledge in modern cybersecurity challenges.