Keypad tones as text

Backdoor CTF 2016 DTune: Decoding a DTMF Recording into Text

A short audio recording turns out to be a vault keypad session. Each tone is a DTMF digit, and runs of the same digit spell letters the way old mobile phones did.

Hexfield editorial

EventBackdoor CTF 2016
CategoryForensics, sound
Points70
EncodingDTMF plus multi-tap
Forensics70 pts
  1. 01

    Inspect

    The audio is a sequence of touch-tone beeps in groups, and the hint says the message consists of uppercase letters.

  2. 02

    Hypothesize

    DTMF digits carry text through the old phone keypad, where pressing a key several times selects a letter.

  3. 03

    Test

    An online decoder returns digits but merges the closely spaced repeats, so the repetitions are counted by hand in a waveform editor.

  4. 04

    Solve

    Pairing each digit with its repeat count and indexing into the keypad letters gives a readable sentence ending in a garbled hash name.

Byte histogram of a PCM WAV file with a narrow peak around the silence value and a spread of sample bytes from sine tones
Pure tones recorded as 8-bit or 16-bit PCM leave a recognizable byte distribution, quite unlike speech or noise.

The file and the clue

DTune, from Backdoor CTF 2016, is a 70-point forensics task in the sound subcategory. The description tells a story about a recording of someone opening a vault, and one clue: the hidden message consists of uppercase letters. The attachment is a WAV file, and listening to it is enough to identify it. It sounds exactly like a phone dialing, a series of beeps of equal length grouped into short bursts.

WAV is a thin wrapper around raw PCM samples, so there is little to analyze structurally. The 44-byte RIFF header gives sample rate, bit depth and channel count, followed by the data chunk. Everything interesting is in the waveform, which for a dialing recording is a row of pure sine pairs separated by silence. A spectrogram makes the structure visible at once: rows of two horizontal lines per beep.

How DTMF works

Dual-tone multi-frequency signaling assigns every key of a 4 by 4 grid one low frequency and one high frequency. The rows use 697, 770, 852 and 941 Hz. The columns use 1209, 1336, 1477 and 1633 Hz, where the fourth column belongs to the A to D keys that most handsets do not have. Key 8 is 852 plus 1336 Hz, key 4 is 770 plus 1209 Hz, key 3 is 697 plus 1477 Hz and key 0 is 941 plus 1336 Hz. Detectors usually apply the Goertzel algorithm, which measures the energy at one chosen frequency over a short window without a full FFT, and report a key when exactly one tone from each group stands above the rest.

import math

def goertzel(samples, rate, freq):
    k = 2 * math.cos(2 * math.pi * freq / rate)
    s1 = s2 = 0.0
    for x in samples:
        s0 = x + k * s1 - s2
        s2, s1 = s1, s0
    return s1 * s1 + s2 * s2 - k * s1 * s2

Any web-based DTMF decoder does the same job. Feeding the challenge file to one returned a list of digits starting 8, 4, 3, 0, 3, 5, 2, 4, 0.

From digits to letters

Digits alone are not a message, and the hint about uppercase letters suggests letters are encoded some other way. Telephone keypads follow the ITU E.161 layout: key 2 carries ABC, 3 DEF, 4 GHI, 5 JKL, 6 MNO, 7 PQRS, 8 TUV and 9 WXYZ. Before touchscreens, people typed text messages by pressing a key once for the first letter, twice for the second and so on. The digit 8 pressed once is T. The key 4 pressed twice is H. Key 3 pressed twice is E. Those three presses already read as the opening of “the”, and key 0 as a separator then acts as a space.

That confirms the scheme, but it exposes the weak point in the first decode. Each letter is a run of identical digits, and a run of two or three presses is delivered as several short beeps with brief pauses between them. The decoder used in the first pass collapsed or dropped those repeats, so the digit list was right and the letters were not.

Counting the repeats

The fix is manual and does not need clever tooling. Open the file in Audacity, zoom into the waveform and count the beeps per group. Gaps within a run are visibly shorter than the gaps between different keys. Writing the counts down gives a second list parallel to the digit list, one repeat count per distinct tone. Pairing the two lists and indexing into the key’s letter string, with count minus one as the index, yields the text.

keys = {0: " ", 2: "abc", 3: "def", 4: "ghi", 5: "jkl",
        6: "mno", 7: "pqrs", 8: "tuv", 9: "wxyz"}
text = "".join(keys[d][n - 1] for d, n in zip(digits, counts))

The decoded sentence begins “the flag is” and then continues with a word that looks like noise: “shaajm”. That word is the signal that the decode is nearly right. The intended word is “sha256”, and its digits sit on keys 2, 5 and 6, where a letters-only table has no entry for them. On many handsets the digit itself was the last character of the key’s cycle, so a table with only the letters gives a wrong character for those presses. Recognizing the near-miss as a hash name is enough: the flag is the SHA-256 digest of the text that follows in the message.

Where this can go wrong

Several traps appear in audio challenges of this type. The window length matters when running your own detector, because a window shorter than one beep misses it and a window spanning two keys blends them. A recording with background noise or clipped peaks can produce a third frequency, which makes decoders reject a key. Two consecutive letters on the same key, as in “ee”, need a longer pause than the one between repeats, so a decoder must distinguish a short intra-letter gap from a longer inter-letter one. Here the sender avoided that situation or used a clear pause, which is why a plain run-length count worked.

What transfers to other challenges

Short, regular bursts in a WAV file point to a keyed signaling scheme: DTMF, Morse, or a simple frequency-shift code. The first step is a spectrogram, not a hex editor. When a tool returns almost-right output, compare it with the structure you can see, and correct the stage that loses information. The same pattern of an automated decode followed by a manual pass appears in image and network challenges, such as the difference-image steganography task from the same contest. Raw sample bytes of such recordings look very different from speech or compressed audio, as the explanation of binary visualization shows with byte histograms.

Questions readers ask

What is DTMF and why does it use two tones?

Dual-tone multi-frequency signaling sends each key as the sum of one low-group and one high-group sine wave. Using two simultaneous frequencies from separate groups makes an accidental match with speech or noise very unlikely, so telephone equipment can detect keys reliably.

Why did the online decoder lose information?

Repeated presses of one key come as short beeps separated by brief gaps. A decoder tuned to ignore contact bounce can treat those beeps as one tone, which loses the repeat count that the multi-tap scheme depends on.

How does multi-tap texting map digits to letters?

Keys 2 to 9 carry three or four letters each in alphabetical order. Pressing a key once gives the first letter, twice the second, and so on. Key 0 is a space on the layout this challenge uses, and key 7 and key 9 carry four letters.

New to capture the flag? Formats, teams and a first-month plan in one place.

Read the CTF guide