2 min read

[CTF] WU - Alice and Bob

CTF InterCampus Ynov 2024

Difficulty Level : Medium

Challenge Category : Crypto

Description :

Conducting a thorough investigation into a Cybercrime, we discovered this file on a suspect's device. Can you help us understand its contents?

Solution Steps

Step 1: Analyze the Ciphertext

  • Begin by analyzing the ciphertext for any patterns, repetitions, or clues.
  • Determine the character count in the ciphertext. In this challenge, the ciphertext contains 29 characters.

Step 2: Perform Frequency Analysis

Character frequency analysis is a key step in solving substitution ciphers. By comparing the frequency of characters in the ciphertext with known letter frequency distributions in English, we can map cipher characters to their plaintext equivalents.

Useful Resources


Step 3: Use Python for Analysis

The following Python script helps analyze the ciphertext for character and word frequencies:

from collections import Counter
import re
import pandas as pd

# Path to the encoded file
file_path = "/tmp/encoded_text.txt"

# Read the encoded text
with open(file_path, 'r', encoding='utf-8') as file:
    encoded_text = file.read()

# Clean up the encoded text
encoded_text_cleaned = re.sub(r'[^\S\n]+', '', encoded_text)

# Character frequency analysis
char_counts = Counter(encoded_text_cleaned)
total_chars = sum(char_counts.values())
char_frequencies = {char: (count / total_chars) * 100 for char, count in char_counts.items()}

# Word frequency analysis
words = re.findall(r'\S+', encoded_text)
word_counts = Counter(words)
total_words = sum(word_counts.values())
word_frequencies = {word: (count / total_words) * 100 for word, count in word_counts.items()}

# Sort frequencies
sorted_char_frequencies = dict(sorted(char_frequencies.items(), key=lambda x: x[1], reverse=True))
sorted_word_frequencies = dict(sorted(word_frequencies.items(), key=lambda x: x[1], reverse=True))

# Convert to DataFrames for better visualization
char_freq_df = pd.DataFrame(list(sorted_char_frequencies.items()), columns=["Character", "Frequency (%)"])
word_freq_df = pd.DataFrame(list(sorted_word_frequencies.items()), columns=["Word", "Frequency (%)"])

# Output the frequency analysis
print("Character Frequency Analysis:")
print(char_freq_df.to_string(index=False))
print("\nWord Frequency Analysis:")
print(word_freq_df.to_string(index=False))

Step 4: Replace Encoded Characters

Using the reverse substitution mappings provided, replace each encoded character with its corresponding plaintext character. Here's the substitution mapping:

Reversed Substitution Mappings

reversed_substitutions = {
    '⊲': 'A', 'π': 'B', 'ϕ': 'C', 'ϵ': 'D', 'λ': 'E', '÷': 'F',
    '±': 'G', '%': 'H', '≦': 'I', '≧': 'J', '⊓': 'K', 'ρ': 'L',
    'σ': 'M', '∑': 'N', '∂': 'O', '∫': 'P', 'Ω': 'Q', 'ω': 'R',
    '∧': 'S', '⊕': 'T', '#': 'U', '≡': 'V', '⊻': 'W', '∀': 'X',
    '∐': 'Y', '⫛': 'Z', '≅': '{', '≇': '}', '⊊': '_'
}

Decoding Script

# Replace characters with reversed substitutions
for char, substitution in reversed_substitutions.items():
    encoded_text = encoded_text.replace(char, substitution)

# Save the decoded text to a file
output_file_path = "decoded_text.txt"
with open(output_file_path, "w") as output_file:
    output_file.write(encoded_text)

print(f"The decoded text has been saved to {output_file_path}.")