# Rev - Somewhat Linear

## Description

Deep in the alien cave system, you find a strange device. It seems to be some sort of communication cipherer, but only a couple of recordings are still intact. Can you figure out what the aliens were trying to say? The flag is all lower case

## Downloads

{% file src="<https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2Fz9kCCscHxr5F0A2VvGTG%2Frev_somewhat_linear.zip?alt=media&token=13e9cb5a-ddb3-4502-b626-211877c04417>" %}

## Solution

For this challenge, we're given a really short python script and 2 '.wav' sound files.

{% code lineNumbers="true" %}

```python
import numpy as np
import soundfile as sf

flag, rate = sf.read('../htb/flag.wav')


# randomly shuffle the frequencies
freqs = np.fft.rfftfreq(len(flag), 1.0/rate)
filter_frequency_response = np.random.uniform(-10, 10, len(freqs))

# get the amplitudes
def filter(sample):
    amplitudes = np.fft.rfft(sample)
    shuffled_amplitudes = amplitudes * filter_frequency_response

    return np.fft.irfft(shuffled_amplitudes)

impulse = np.zeros(len(flag))
impulse[0] = 1

shuffled_signal = filter(impulse)
sf.write('impulse_response.wav', shuffled_signal, rate)

shuffled_flag = filter(flag)
sf.write('shuffled_flag.wav', shuffled_flag, rate)
```

{% endcode %}

What this code does is that it first generates a random sound wave and multiplies it with a very simple impulse wave that is generated with the following code:

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FDxRABK8Cmt6eKY7cKhD4%2Fimage.png?alt=media&#x26;token=ba39d237-5bee-44cd-b5a5-77d7e5dabc96" alt=""><figcaption><p>Generated impulse</p></figcaption></figure>

The result is then multiplied with the amplitude of the `flag.wav` sound file.

We can add print statements to see the before and after of the multiplication of the impulse.

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2F3X3RDANyLNcJxa0f6ISX%2Fimage.png?alt=media&#x26;token=a18ed7c3-da3e-4f58-b68d-91344524b1bb" alt=""><figcaption><p>Before and after multiplication</p></figcaption></figure>

Since the impulse simply adds a complex component, we can use the `real` keyword to get back the generated random noise.&#x20;

<figure><img src="https://4077916634-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9J6tnLQGbY0Or10P4xUT%2Fuploads%2FzhWB9WmN1TUpTV3YiCvE%2Fimage.png?alt=media&#x26;token=017cc73d-0aa0-4218-ba78-c7bc7455e325" alt=""><figcaption><p>Getting back the random noise</p></figcaption></figure>

After getting back the randomized noise, we simply need to use the shuffled flag sound waves and divide the amplitudes with the random noise. The solve script is as follows:

{% code lineNumbers="true" %}

```python
import numpy as np
import soundfile as sf

impulse, rate = sf.read('impulse_response.wav')
shuffled_flag, rate = sf.read('shuffled_flag.wav')

# Get the randomized filter_frequency_response
impulse_response = np.fft.rfft(impulse).real
print(impulse_response)

# Get the amplitudes of the shuffled flag
flag_amplitudes = np.fft.rfft(shuffled_flag)
print(flag_amplitudes)

# Divide the amplitudes with the random noise
amplitudes = flag_amplitudes / impulse_response
print(amplitudes)

# Inverse fourier transform
flag = np.fft.irfft(amplitudes)
sf.write('flag.wav', flag, rate)
```

{% endcode %}

&#x20;Flag: `htb{th1s_w@s_l0w_eff0rt}`
