# NcN CTF 2k14: OST (400 points)

Can you hear it? We neither ;)

# cat pattern.py
#!/usr/bin/python

import scipy.io.wavfile
import sys
import wave
from collections import Counter

inputfile = sys.argv[1]

wave_read_object = wave.open(inputfile, 'rb')
print 'Number of audio channels = ',  wave_read_object.getnchannels()
print 'Sample width = ', wave_read_object.getsampwidth(), '(bytes)'
print 'Sampling frequency = ', wave_read_object.getframerate(), '(Hz)'
frames = wave_read_object.getnframes()
print 'Number of audio frames = ', frames
wave_read_object.close()

rate, data = scipy.io.wavfile.read(inputfile)

r = Counter()
for frames in data:
 r[str(frames[1])] += 1
print r

word = ''
last_bit = ''
binary = ''

for frames in data:
 if frames[1] == 0:
  bit = '0'
 else:
  bit = '1'
 if last_bit != bit:
  sys.stdout.write(word + '\n')
  word = ''
 word += bit
 last_bit = bit
print binary

# ./pattern.py ost.wav
Number of audio channels =  2
Sample width =  2 (bytes)
Sampling frequency =  44100 (Hz)
Number of audio frames =  661522
Counter({'0': 342450, '328': 319072})

0000000000000
11111111111111111111111111
0000000000000
1111111111111
0000000000000
1111111111111
00000000000000000000000000
11111111111111111111111111
0000000000000
1111111111111
00000000000000000000000000
11111111111111111111111111
00000000000000000000000000
11111111111111111111111111
00000000000000000000000000
11111111111111111111111111
0000000000000
1111111111111
0000000000000
1111111111111
0000000000000
1111111111111
0000000000000
1111111111111
00000000000000000000000000
11111111111111111111111111
0000000000000
1111111111111
...

# cat solution.py
#!/usr/bin/python

import scipy.io.wavfile
import sys
import wave
from collections import Counter

inputfile = sys.argv[1]
outputfile = sys.argv[2]

wave_read_object = wave.open(inputfile, 'rb')
print 'Number of audio channels = ',  wave_read_object.getnchannels()
print 'Sample width = ', wave_read_object.getsampwidth(), '(bytes)'
print 'Sampling frequency = ', wave_read_object.getframerate(), '(Hz)'
frames = wave_read_object.getnframes()
print 'Number of audio frames = ', frames
wave_read_object.close()

rate, data = scipy.io.wavfile.read(inputfile)

r = Counter()
for frames in data:
 r[str(frames[1])] += 1
print r

word = ''
binary = ''

for frames in data:
 if frames[1] == 0:
  bit = '0'
 else:
  bit = '1'
 if word == '0000000000000':
  binary += '0'
  sys.stdout.write(word + '\n')
  word = ''
 if word == '1111111111111':
  binary += '1'
  sys.stdout.write(word + '\n')
  word = ''
 word += bit
print binary

decoded = ''

for i in xrange(0, len(binary), 2):
 print i, i + 2
 symbol = binary[i : i + 2]
 if symbol == '01':
  digit = '1'
 else:
  digit = '0'
 decoded += digit

f = open(outputfile, 'wb')

for i in xrange(0, len(decoded), 8):
 print i, i + 8
 byte = decoded[i : i + 8]
 print chr(int(byte,2))
 f.write(chr(int(byte,2)))
f.close()

# ./solution.py ost.wav output
Number of audio channels =  2
Sample width =  2 (bytes)
Sampling frequency =  44100 (Hz)
Number of audio frames =  661522
Counter({'0': 342450, '328': 319072})
0000000000000
1111111111111
1111111111111
0000000000000
1111111111111
0000000000000
1111111111111
0000000000000
0000000000000
1111111111111
1111111111111
0000000000000
1111111111111
0000000000000
0000000000000
1111111111111
1111111111111
0000000000000
0000000000000
1111111111111
1111111111111
0000000000000
...
011010100110100110011001101010101001101001010110100110101001010110101010010110011010101001100110101010010110011010101010011001101010101010101010101010101010101010101010101010101010101001011001100110100110100110011010011010101001101010011010100110011010011010101010101010101010101010101010101010101010011010101001100101101010101010101010101010101010101010101010101010101010010110101010101010100110101010101010100110101010101010101010101010101010101010101010101010100101011010010101101001101001101001010101011001011001100101100110101010101010101010101010101010101010101010101010101010101010011010010110101001101001101001100101100110101001010110011010100110101010101010101010101010101010101001100110011001100110101001011001101001101010010110100101101001101010101010101010
...
0 2
2 4
4 6
6 8
8 10
10 12
12 14
14 16
16 18
18 20
...
0 8
�
8 16
P
16 24
N
24 32
G
32 40

40 48

...
# file output 
output: PNG image data, 534 x 48, 8-bit gray+alpha, non-interlaced
# tesseract output flag && cat flag.txt
NcN_d787634fa059f9a009f539a18526666208a3e960