Contents

2D Spectrogram for CTF Stego Challenges

Understanding Spectrograms

A spectrogram is a visual representation of the spectrum of frequencies in a signal as it varies over time. It provides a way to analyse how the frequency content of a signal changes, which is particularly useful in fields such as audio analysis, speech processing, and also seismology (the study of earthquakes).

https://upload.wikimedia.org/wikipedia/commons/c/c5/Spectrogram-19thC.png

Source: Spectrogram Wikipedia

How Spectrograms Work

So how do spectrograms work? A spectrogram displays time on the x-axis and frequency on the y-axis. The intensity (in dBs) or colour of each point in the spectrogram represents the amplitude or energy of the frequency at that particular time. This allows for the observation of patterns, harmonics, and other features that are not easily detectable in the raw signal.

How Spectrograms are Made

To create a spectrogram, we follow these steps:

  1. Break the Signal into Small Pieces: First, we divide the audio signal into small, overlapping segments. This is like cutting a long recording into many short clips.

  2. Analyse Each Piece: For each small segment, we figure out the different frequencies (like musical notes) that are present. We do this using a mathematical tool called the Fast Fourier Transform (FFT). If you’d like to learn more about Fourier Transforms I recommend checking out this video explanation.

  3. Plot the Frequencies Over Time: Finally, we take all the frequency information from each segment and plot it on a graph. Time runs along the bottom (horizontal axis) and frequency runs up the side (vertical axis). The colours or brightness in the plot show how strong each frequency is at each moment in time.

This process creates a visual map of how the sound’s frequencies change over time, making it easier to see patterns and details in the audio.

Applications of Spectrograms

Spectrograms are widely used in various applications:

  • Audio Analysis: Understanding musical recordings, identifying instruments, and studying the acoustic properties of sounds. If you’re interested in audio analysis check out this amazing free course from 🤗 HF Audio Course.
  • Speech Recognition: Converting spoken words into text by analysing the frequency patterns of speech, see Whisper
  • Medical Diagnostics: Used in techniques such as ultrasound to visualise the frequency content of biological signals.

Interpreting Spectrograms

Reading a spectrogram involves identifying the patterns and changes in frequency over time. For example, harmonics in a musical note appear as parallel lines, while noise appears as a more random, scattered pattern. Understanding these visual cues can provide valuable insights into the nature of the signal.

CTF Challenge Solution:

My challenge created for ENUSEC’s LTDH'24 CTF was titled “RecordedTransmission” with the following description “Our advanced radios have picked up this transmission, can you see if you can decode it?”. Players were provided with a link to the following wav file.

Of course, there are a number of different ways to use audio files to hide information. Including hiding text in the binary, using tools like MP3Stego, or even just using high and low frequencies to represent binary.

However, as you may have expected from the post title, this challenge instead uses frequencies to ‘draw’ the flag inside a spectrogram. Therefore, in order to solve this challenge we must plot the audio data as a spectrogram. There are a number of ways this can be achieved, I will demonstrate two.

Python Solution:

Of course, the hacker’s first idea is to code up a solution. It’s actually pretty simple to plot a spectrogram using python. In fact, Matplotlib’s pyplot has a handy .specgram function to do the heavy lifting for us. The first step is to extract the sample_rate and samples from the .wav file - to do this I use scipy.io’s wavfile module, but there are other options. Then with that we can use plt.specgram() to generate our spectrogram. And we get…

https://lnwatson.co.uk/images/posts/ltdh24/spectrogram-python.png

Code:

import matplotlib.pyplot as plt  
from scipy.io import wavfile  
  
  
# Function to plot the spectrogram  
def plot_spectrogram(audio_file):  
    # Read the audio file  
    sample_rate, samples = wavfile.read(audio_file)  
  
    # Generate the spectrogram  
    plt.figure(figsize=(10, 6))  
    plt.specgram(samples, Fs=sample_rate, NFFT=1024, noverlap=512, cmap='viridis')  
    plt.title('Spectrogram of Transmission.wav')  
    plt.xlabel('Time [s]')  
    plt.ylabel('Frequency [Hz]')  
    plt.colorbar(label='Intensity [dB]')  
    plt.show()  
  
  
# Path to the audio file  
audio_file_path = './transmission.wav'  
  
# Plot the spectrogram  
plot_spectrogram(audio_file_path)

Alternative solution

A more exciting visual way to solve this challenge is to use a tool like friture - a realtime audio analyser. Open the application, select 2D spectrogram and using the same laptop play the audio recording and you can watch as the flag appears before your own eyes.

Or just watch this recording…

How was the challenge wav file made?

I would love to claim credit for the code used to generate the audio transmission file but actually it was created by ZachNagengast. There are likely many ways to automate generating files like this but I came across Zach’s reply/issue python implementation on a gitrepo that uses matlab to create similar spectrogram art.