#####################
# MPEG4 format tuto #
#####################

Let's start with some manipulation of the MPEG4 container format.
For this, I will work with the SSTIC 2011 challenge, available here:
http://static.sstic.org/challenge2011/challenge

The challenge was quite complex, with different parts. The first one consisted 
to analyze an MPEG4 video that requires reverse-engineering. The format is defined 
in the specification ISO IEC 14496-12:2008. It is possible to get a free copy of 
the specification here: 
http://standards.iso.org/ittf/PubliclyAvailableStandards/c051533_ISO_IEC_14496-12_2008.zip

Anyway, MPEG4 container format is made of successive "atoms", each composed of a 
size field, a type field, and a data field. Atoms can be imbricated together, 
that will make a hierarchy of atoms.
But let's understand it with the MPEG4 format descriptor:


[mich@Mitcher libmich]$ ipython
Python 2.7.2 (default, Jun 29 2011, 11:10:00) 
Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from libmich.formats.MPEG4 import *

In [2]: fd=open('../../sstic2011/challenge', 'rb'); buf=fd.read(); fd.close()

In [3]: len(buf)
Out[3]: 4638867

In [4]: m = MPEG4()

In [6]: m.parse?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:<bound method MPEG4.parse of [[MPEG4] <MPEG4 atom[atom]: size():8, type():'ftyp', data():''> [MPEG4]]>
Namespace:  Interactive
File:       /home/mich/github/libmich/libmich/formats/MPEG4.py
Definition: m.parse(self, s='', recursive=True)
Docstring:  <no docstring>

In [7]: m.parse(buf) # errors are because 'recursive' bruteforces encapsulated atoms
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for type
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data
[ERR] String buffer not long enough for data

In [8]: len(m) == len(buf)
Out[8]: True

In [9]: str(m) == buf
Out[9]: True

In [10]: show(m)
[[[ MPEG4 ]]]
### MPEG4 atom [atom] ###
<[size] : 24>
<[type] : 'ftyp'>
<[data] : 'mp42\x00\x00\x00\x00mp42isom'>
### MPEG4 atom [atom] ###
<[size] : 4170138>
<[type] : 'mdat'>
<[data] : '\x1f\x8b\x08\x08@\x06\x82M\x02\x00introduction.txt\x00\xadRAn\xd4@\x10\xbc\xcf+\xfa\xb6\x17\xe2\xc5 \x85$\x17$@ \xa2=D,<`\xd6n\xefN4\x9eq\xba\\.\x7f?\x00\xab\xd7\xceP+3\x1avQ\xcd\xab\xef\x83\x93p]\x04B\x14\xe7\x0f\x13\'\xf7\x94Jl\xfc\xcf?\xa2\x1fHh^\xc9\x1f\xb6E\x99$\x12\x99\n\x0f/\x16\x97\xd7\xaa\r\x1a\xe7\xc3\x06}\xa3.\x8dHP\xf9\xad\xb8\x1c\x81\xaasdvLN7\xf7\xb1W\x92h\xf1\xe5v\x0e<\xa6\x01\xdb\n\xda?\xc8o\x13\xffmh\xb5\x81\x18@\x8b\xe6\n\xa4\x8c\xdc\x7f\xbd\x18\xf7\xb7\xe3-7.\x99\xc1~M\xfa\x84\xb1\xcf\xec\xea\xec@\x15\xcb\xb6\xa00\xb2\x81@\xfa\x132N\x12\xb8)\xae.,\xd4\xf4\xca6?\xe6U\xb6\xe7\x03\xd8&\xd0g\xc8d\x99H#\xbd\xdb\x156\xd1\n\xf5\x07\xd12\xe7\xa5S]\x8eY\x8cK\xbf\xbde0<\x14\xca,@\xe4\x14d\xb8\x0cS\xed`P\x1f\xd2\xa6,/\xf2\xa890\x17$\xd7\x9d\xe7)\x1f:\x91E%\x87\xe4\xc63AR\x17\x8b;\x12*\xdf\x8b\x89r)\xe4\x8e\xf6\xb7\x9a\xe1\xbb\x1c\tU\x00\x00\xdf\xfd\xb2t\x1d\x9f|\x82m\xeaIf?\x16\x06Rb\x93Q\xbbeX\xd2j\xcc\xbf\xf1\x9e\xb8\x1a\x13\xe2\xf4\xa1\xdf\xc9\x9a\xc1\n\xfe>^\xf8\x9b\xd60\x87\x84\xfc\xac\xce\xb...>
### MPEG4 atom [atom] ###
<[size] : 277450>
<[type] : 'mdat'>
<[data] : '\xde\x04\x00\x00libfaac 1.28\x00\x00B\x00\x93 \x042\x00G!\x00I\x90\x02\x19\x00#\x80!\x00I\x90\x02\x19\x00#\x80! I\x90\x02\x19\x00#\x80!N\xfe\xff\xff\x1de\xcc\x97jN\x0b{U\xcb\xfcIS(\x00\x00\x00\x00\x00"\x8a\x93 g\x9a$/ \x07#\xac\x9f\xbf\xfb\xcf0\x01\xa1\xa9\xea=?\x1f\xbb\x80\x1a\x1c\x8e\xa7\xa8\xe7\xf7\x80\x07Q\xd6W\xbf\xd0\xe3\x80\x00\x16\xd0\xe4h?3\xae\xc6}\x1b\x10\x00\x155\xb5\xff\x8b\x8e}\xdd\x99\xc9[\x0c\xe4\x90\xff\x9f\xfa\xbfk.?\xbf\x80\x02\x7fdh{\xde\xfd\xba\xee\xdf\x0e\xcf\xd4\x80\x10C\xbfN\x0f\x06\xd0\x8c4\x14\xe4\x80\x04\xefXd\xe0<y\xb6@\x00(\x9a\x9e\xa4\xb1\xa6\xca\xce\x00\x00@\x9e\xb0m\xd3\x10\x00\x02\xe1.\xf4\x9a\xab\xa8d\x0f\xc0\xe0!N\xfe\xff\xff\x1bf\x8c&.Q)21\xa9\x1a\xfd\xc9\x85\n\xae?s?\x801F\x8e\xf59\xce\xa7\xfa\x05\xae\x1b\x01\x9dX\xf2@Q\x10:\xe0\x8a\x03\xd7\xeb\xa0H\xe4X4\x039\xb3\xac\xd2(n\'v\xb0?\x87\x85\x19\xca@\x06T\xc4\xe9\xeaag\xe7\xdf\x8fX\x01]\x8aA\xe5\x8cy4\xe6])\x07\x0f(\x9a\xc6\'\xde{\xcd\x07[\x8a{\xcc\xae"\xe4\x8cc\xb0\xe6p\x149\x00CXP\x1a \x9b\xa7\xbbu\x86\xb2\x92\x02=\xa5...>
### MPEG4 atom [atom] ###
<[size] : 178748>
<[type] : 'mdat'>
<[data] : '\x00\x00\x00xr\x02\x00\x08\x00\x00\x00\x80r\x02\x00\x08\x00\x00\x00\x88r\x02\x00\x08\x00\x00\x00\x90r\x02\x00\x08\x00\x00\x00\x98r\x02\x00\x08\x00\x00\x00\xa0r\x02\x00\x08\x00\x00\x00\xa8r\x02\x00\x08\x00\x00\x00\xb0r\x02\x00\x08\x00\x00\x00\xb8r\x02\x00\x08\x00\x00\x00\xc0r\x02\x00\x08\x00\x00\x00\xc8r\x02\x00\x08\x00\x00\x00\xd0r\x02\x00\x08\x00\x00\x00\xd8r\x02\x00\x08\x00\x00\x00\xe0r\x02\x00\x08\x00\x00\x00\xe8r\x02\x00\x08\x00\x00\x00\xf0r\x02\x00\x08\x00\x00\x00\xf8r\x02\x00\x08\x00\x00\x00\x00s\x02\x00\x08\x00\x00\x00\x08s\x02\x00\x08\x00\x00\x00\x10s\x02\x00\x08\x00\x00\x00\x18s\x02\x00\x08\x00\x00\x00 s\x02\x00\x08\x00\x00\x00(s\x02\x00\x08\x00\x00\x000s\x02\x00\x08\x00\x00\x008s\x02\x00\x08\x00\x00\x00@s\x02\x00\x08\x00\x00\x00Hs\x02\x00\x08\x00\x00\x00Ps\x02\x00\x08\x00\x00\x00Xs\x02\x00\x08\x00\x00\x00`s\x02\x00\x08\x00\x00\x00hs\x02\x00\x08\x00\x00\x00ps\x02\x00\x08\x00\x00\x00xs\x02\x00\x08\x00\x00\x00\x80s\x02\x00\x08\x00\x00\x00\x88s\x02\x00\x08\x00\x00\x00\x90s\x02\x00\x08\x00\x00\x00\x9...>
### MPEG4 atom [atom] ###
<[size] : 12507>
<[type] : 'moov'>
<[data] : ''>
	### MPEG4 atom [atom] ###
	<[size] : 108>
	<[type] : 'mvhd'>
	<[data] : '\x00\x00\x00\x00\xc9\xaa\xa2\x01\xc9\xaa\xa2\x02\x00\x01_\x90\x00\x17\xc1\xaa\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'>
	### MPEG4 atom [atom] ###
	<[size] : 5982>
	<[type] : 'trak'>
	<[data] : ''>
		### MPEG4 atom [atom] ###
		<[size] : 92>
		<[type] : 'tkhd'>
		<[data] : '\x00\x00\x00\x01\xc9\xaa\xa2\x01\xc9\xaa\xa2\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00\x17\xc1\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x02\x80\x00\x00\x01\xe0\x00\x00'>
		### MPEG4 atom [atom] ###
		<[size] : 12>
		<[type] : 'ssti'>
		<[data] : '\x00\x00\x00\x00'>
		### MPEG4 atom [atom] ###
		<[size] : 5822>
		<[type] : 'mdia'>
		<[data] : ''>
			### MPEG4 atom [atom] ###
			<[size] : 32>
			<[type] : 'mdhd'>
			<[data] : '\x00\x00\x00\x00\xc9\xaa\xa2\x01\xc9\xaa\xa2\x02\x00\x01_\x90\x00\x17\xc1\xaa\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 33>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00vide\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 5749>
			<[type] : 'minf'>
			<[data] : ''>
				### MPEG4 atom [atom] ###
				<[size] : 20>
				<[type] : 'vmhd'>
				<[data] : '\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00'>
				### MPEG4 atom [atom] ###
				<[size] : 36>
				<[type] : 'dinf'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 28>
					<[type] : 'dref'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0curl \x00\x00\x00\x01'>
				### MPEG4 atom [atom] ###
				<[size] : 5685>
				<[type] : 'stbl'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 181>
					<[type] : 'stsd'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\xa5mp4v\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x80\x01\xe0\x00H\x00\x00\x00H\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\xff\xff\x00\x00\x00Oesds\x00\x00\x00\x00\x03\x80\x80\x80>\x00\x00\x00\x04\x80\x80\x800 \x11\x00\x8a\x84\x00(\xd5\xa0\x00\x1dlR\x05\x80\x80\x80\x1e\x00\x00\x01\xb0\x01\x00\x00\x01\xb5\x89\x13\x00\x00\x01\x00\x00\x00\x01 \x00\xc4\x8d\x88t\xfd\x14\x04<\x14C\x06\x80\x80\x80\x01\x02'>
					### MPEG4 atom [atom] ###
					<[size] : 3248>
					<[type] : 'stts'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x01\x94\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x01\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b\xbd\x00\x00\x00\x02\x00\x00\x0b\xbe\x00\x00\x00\x01\x00\x00\x0b...>
					### MPEG4 atom [atom] ###
					<[size] : 2092>
					<[type] : 'stsz'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06\x00\x00V\xcd\x00\x00\x1dZ\x00\x00\x1b;\x00\x00\x12\x13\x00\x00\x17f\x00\x00\r\xc4\x00\x00\x15_\x00\x00\x1e>\x00\x00!]\x00\x00$o\x00\x00\x1b\xa2\x00\x00\x1b\xe0\x00\x00\x13x\x00\x00\x0f\xfc\x00\x00\x12J\x00\x00#\xda\x00\x00\x109\x00\x00\x0b\x05\x00\x00\x0c\xe3\x00\x00\x12\xde\x00\x00\x16\x8e\x00\x00\x13^\x00\x00\x18\x8a\x00\x00\x0bJ\x00\x00\x07[\x00\x00\x0c\x87\x00\x0001\x00\x00\x16\xea\x00\x00\x10)\x00\x00\x15\x82\x00\x00\x16e\x00\x00\r\x98\x00\x00\t\xbd\x00\x00\x14\xbe\x00\x00\x0bN\x00\x00\tH\x00\x00\x0f\x80\x00\x00\x0c\xd0\x00\x00\x0f]\x00\x00\ns\x00\x00\x0c\xbe\x00\x00\x13\xde\x00\x00\x15\xb7\x00\x00\x141\x00\x00\x14\'\x00\x00\x17\x99\x00\x00\x13\x9b\x00\x00\x12q\x00\x00\x11W\x00\x00\x14O\x00\x00\rz\x00\x00\x0c\xbd\x00\x00\x0e\xef\x00\x00\x15\x00\x00\x00\x0f\x0e\x00\x00\x0b\xf5\x00\x00,\x95\x00\x00\x15\xa1\x00\x00\x161\x00\x00\x14\x9a\x00\x00\x0eA\x00\x00\x14Q\x00\x00\n\x03\x00\x00\x18\x05\x00\x00\x12R\x00\x00\x13\xd2\x00\x00\x14\xd6\x00\x00\x14\xcb\x00\...>
					### MPEG4 atom [atom] ###
					<[size] : 40>
					<[type] : 'stsc'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x1e\x00\x00\x00\x01\x00\x00\x00\x12\x00\x00\x00\x08\x00\x00\x00\x01'>
					### MPEG4 atom [atom] ###
					<[size] : 88>
					<[type] : 'stco'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00_\x00\x02\xdb\x07\x00\x04\xf0_\x00\x08\x0c\xb7\x00\x0c\x02d\x00\x0f\xf6\xd9\x00\x14\x03\x97\x00\x17\x8b\t\x00\x1a\x9a\xf8\x00\x1f\x83\xb0\x00$\x1b%\x00(o\xbb\x00,\x9b\xbf\x001\x97\xb8\x005\xd5\x0e\x009s\xa5\x00;\xdcM\x00>\xe9\x16'>
					### MPEG4 atom [atom] ###
					<[size] : 28>
					<[type] : 'stss'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\xfb\x00\x00\x01\xf5'>
		### MPEG4 atom [atom] ###
		<[size] : 48>
		<[type] : 'edts'>
		<[data] : ''>
			### MPEG4 atom [atom] ###
			<[size] : 40>
			<[type] : 'elst'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x02\x00\x17\xc1\xaa\x00\x00\x00\x00\x00\x01\x00\x00\x00\x17\xc1\xaa\x00\x00\x00\x00\x00\x01\x00\x00'>
	### MPEG4 atom [atom] ###
	<[size] : 3492>
	<[type] : 'trak'>
	<[data] : ''>
		### MPEG4 atom [atom] ###
		<[size] : 92>
		<[type] : 'tkhd'>
		<[data] : '\x00\x00\x00\x01\xc9\xaa\xa2\x02\xc9\xaa\xa2\x02\x00\x00\x00\x02\x00\x00\x00\x00\x00\x17\x96\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
		### MPEG4 atom [atom] ###
		<[size] : 3372>
		<[type] : 'mdia'>
		<[data] : ''>
			### MPEG4 atom [atom] ###
			<[size] : 32>
			<[type] : 'mdhd'>
			<[data] : '\x00\x00\x00\x00\xc9\xaa\xa2\x02\xc9\xaa\xa2\x02\x00\x00\xacD\x00\x0b\x90\x00\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 33>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00soun\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 3299>
			<[type] : 'minf'>
			<[data] : ''>
				### MPEG4 atom [atom] ###
				<[size] : 16>
				<[type] : 'smhd'>
				<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00'>
				### MPEG4 atom [atom] ###
				<[size] : 36>
				<[type] : 'dinf'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 28>
					<[type] : 'dref'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0curl \x00\x00\x00\x01'>
				### MPEG4 atom [atom] ###
				<[size] : 3239>
				<[type] : 'stbl'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 103>
					<[type] : 'stsd'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00Wmp4a\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x10\x00\x00\x00\x00\xacD\x00\x00\x00\x00\x003esds\x00\x00\x00\x00\x03\x80\x80\x80"\x00\x00\x00\x04\x80\x80\x80\x14@\x15\x00\x02\xe5\x00\x02\xe4\xd0\x00\x01\xf8\x95\x05\x80\x80\x80\x02\x12\x10\x06\x80\x80\x80\x01\x02'>
					### MPEG4 atom [atom] ###
					<[size] : 24>
					<[type] : 'stts'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x02\xe4\x00\x00\x04\x00'>
					### MPEG4 atom [atom] ###
					<[size] : 2980>
					<[type] : 'stsz'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\xe4\x00\x00\x00\x1a\x00\x00\x00\t\x00\x00\x00\t\x00\x00\x00\t\x00\x00\x00\xa6\x00\x00\x00\xd7\x00\x00\x023\x00\x00\x02G\x00\x00\x025\x00\x00\x01\x1d\x00\x00\x00\xe4\x00\x00\x00\xe2\x00\x00\x00\xe8\x00\x00\x00\xee\x00\x00\x00\xd0\x00\x00\x00\xdc\x00\x00\x00\xf3\x00\x00\x01\n\x00\x00\x01\x06\x00\x00\x01\x17\x00\x00\x01\x13\x00\x00\x02\n\x00\x00\x02\xcc\x00\x00\x02\xe5\x00\x00\x02\xd3\x00\x00\x02\xdf\x00\x00\x02\xce\x00\x00\x02\xcf\x00\x00\x02\xdf\x00\x00\x02\xd5\x00\x00\x02\xcd\x00\x00\x02\xcc\x00\x00\x02\xc4\x00\x00\x02\xbc\x00\x00\x02\xb0\x00\x00\x02\x91\x00\x00\x02}\x00\x00\x02d\x00\x00\x02U\x00\x00\x02F\x00\x00\x02:\x00\x00\x02\x1b\x00\x00\x02\x18\x00\x00\x01\xdd\x00\x00\x01\xd3\x00\x00\x01\xc9\x00\x00\x01\xac\x00\x00\x01\x96\x00\x00\x01\xa0\x00\x00\x01\x93\x00\x00\x01\xa0\x00\x00\x01\x92\x00\x00\x01\xa3\x00\x00\x01\x99\x00\x00\x01\x98\x00\x00\x01\x8c\x00\x00\x01\x94\x00\x00\x01\x90\x00\x00\x01\x89\x00\x00\x01\x8e\x00\x00\x01\x93\x00\x00\x01\x96\x00\x00\x01\x...>
					### MPEG4 atom [atom] ###
					<[size] : 40>
					<[type] : 'stsc'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00,\x00\x00\x00\x01\x00\x00\x00\x11\x00\x00\x00$\x00\x00\x00\x01'>
					### MPEG4 atom [atom] ###
					<[size] : 84>
					<[type] : 'stco'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x11\x00?\xa1\xba\x00?\xf0\x8b\x00@+\x7f\x00@nR\x00@\xa0p\x00@\xd9\xe8\x00A\x18\xce\x00A[\xb6\x00A\xa0~\x00A\xe1\xba\x00B&(\x00Bj\xc8\x00B\xb0Z\x00B\xf3\x83\x00C4\xad\x00Cl\x17\x00C\xa8\xf6'>
		### MPEG4 atom [atom] ###
		<[size] : 20>
		<[type] : 'udta'>
		<[data] : ''>
			### MPEG4 atom [atom] ###
			<[size] : 12>
			<[type] : 'name'>
			<[data] : '\x00\x00\x00\x00'>
	### MPEG4 atom [atom] ###
	<[size] : 2917>
	<[type] : 'trak'>
	<[data] : ''>
		### MPEG4 atom [atom] ###
		<[size] : 92>
		<[type] : 'tkhd'>
		<[data] : '\x00\x00\x00\x0f\xc9\xaa\xa2\x01\xc9\xaa\xa2\x01\x00\x00\x00\x03\x00\x00\x00\x00\x00\x17\x96\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
		### MPEG4 atom [atom] ###
		<[size] : 2817>
		<[type] : 'mdia'>
		<[data] : ''>
			### MPEG4 atom [atom] ###
			<[size] : 32>
			<[type] : 'mdhd'>
			<[data] : '\x00\x00\x00\x00\xc9\xaa\xa2\x01\xc9\xaa\xa2\x01\x00\x01_\x90\x00\x17\xc1\xaa\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 45>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00data\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00SsticHandler\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 2732>
			<[type] : 'minf'>
			<[data] : ''>
				### MPEG4 atom [atom] ###
				<[size] : 12>
				<[type] : 'nmhd'>
				<[data] : '\x00\x00\x00\x00'>
				### MPEG4 atom [atom] ###
				<[size] : 36>
				<[type] : 'dinf'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 28>
					<[type] : 'dref'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x0curl \x00\x00\x00\x01'>
				### MPEG4 atom [atom] ###
				<[size] : 2676>
				<[type] : 'stbl'>
				<[data] : ''>
					### MPEG4 atom [atom] ###
					<[size] : 32>
					<[type] : 'stsd'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x10elf \x00\x00\x00\x00\x00\x00\x00\x01'>
					### MPEG4 atom [atom] ###
					<[size] : 24>
					<[type] : 'stts'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x80\x00\x00\x137'>
					### MPEG4 atom [atom] ###
					<[size] : 1552>
					<[type] : 'stsc'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x08\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\t\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\n\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\r\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x0e\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x0f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x10\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x11\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x12\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x13\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x14\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x15\x00\x00\x00\x01\x...>
					### MPEG4 atom [atom] ###
					<[size] : 532>
					<[type] : 'stsz'>
					<[data] : "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x03\xab\x00\x00\x08\xca\x00\x00\x08\xb3\x00\x00\x01\xe7\x00\x00\x05\x9e\x00\x00\x00\x00\x00\x00\x01\xe7\x00\x00\x02\xb8\x00\x00\x00\xe8\x00\x00\x04\xfc\x00\x00\x00\xb9\x00\x00\x05\xcc\x00\x00\x02\x15\x00\x00\x02\x15\x00\x00\x00\xe8\x00\x00\x00\xd0\x00\x00\x07\x85\x00\x00\x01\xfe\x00\x00\x03\xe5\x00\x00\x07\xe2\x00\x00\t\x9b\x00\x00\x03\xa0\x00\x00\x0bk\x00\x00\x00\\\x00\x00\x05\xb5\x00\x00\x00\xd0\x00\x00\x06\x12\x00\x00\x08\xca\x00\x00\x02\xa0\x00\x00\tU\x00\x00\x0b%\x00\x00\x06X\x00\x00\x08\x10\x00\x00\x01\xe7\x00\x00\nT\x00\x00\x07\xcb\x00\x00\x02\xcf\x00\x00\x01\xe7\x00\x00\x06o\x00\x00\x03,\x00\x00\t'\x00\x00\x04\xe4\x00\x00\x03\xfc\x00\x00\x02\xa0\x00\x00\n&\x00\x00\x02[\x00\x00\x06\xb4\x00\x00\n\x9a\x00\x00\x0b\x0e\x00\x00\x00.\x00\x00\x07\x85\x00\x00\x01D\x00\x00\nl\x00\x00\t\xc9\x00\x00\x04\xfc\x00\x00\t'\x00\x00\x02\xb8\x00\x00\x06\xcc\x00\x00\x03\xa0\x00\x00\x02,\x00\x00\x00\xa2\x00\x00\x05\xe4\x00\x00\x03,\x00\x00\x05X\x00\x00\x05X\x00\x...>
					### MPEG4 atom [atom] ###
					<[size] : 528>
					<[type] : 'stco'>
					<[data] : '\x00\x00\x00\x00\x00\x00\x00\x80\x00E\x11\xec\x00D\x8dx\x00C\xdd\x84\x00F\x85T\x00E\xbf\xea\x00D\xa6\xd6\x00F\r\x04\x00D\xd2\xb0\x00D\xa6\xd6\x00E\xec\xab\x00Dv\x18\x00F:Q\x00E\xb8\xf1\x00DP\xf3\x00E\xf1\xbe\x00Dd\xf9\x00E\xfd\xfa\x00E\x05T\x00D[0\x00C\xfe\xad\x00F]I\x00DI?\x00D\xb5\x83\x00E\xd7\x04\x00E \x03\x00E5\xc1\x00E\x8b\xe9\x00D\x84\xae\x00C\xfc\r\x00Fo\xad\x00F@\x1d\x00E\xe6S\x00E-\xb1\x00DF\x14\x00E\xc5\x88\x00D\x16S\x00E\xd45\x00D\x96B\x00D6~\x00E6\x91\x00C\xeaK\x00E|\xc8\x00FKB\x00F\x10\x01\x00E\xac\xfb\x00C\xf3r\x00F\x87;\x00E\x07R\x00Eq\xba\x00E9\xbd\x00F\x05\x7f\x00DG\xfb\x00E\x15\x97\x00F\x8d\xef\x00D\x11W\x00D<\xed\x00D3\xc6\x00E%\xb8\x00D\r\xb7\x00D\xa7\xbe\x00D\xdf0\x00F4m\x00Ec\x80\x00E\xd7`\x00E9\xeb\x00E,\x84\x00E\\o\x00F(\x8e\x00D\x9d\xde\x00Dn\x08\x00D\xea\x0f\x00D\xa9\xea\x00D\x98)\x00EPc\x00F\x82@\x00F[\xd6\x00D\xdew\x00D\xccp\x00FT\x96\x00Fni\x00D_\x15\x00DL\xdf\x00D\x06\x8f\x00F \xdb\x00E\x95\xc9\x00E\xdc\xb8\x00E\xa4\x8e\x00F--\x00F\x0e\xeb\x00Dq\xd6\x00E\xf1\xa7\x00D\xf6\xd5\...>

In [11]: for atom in m: print('type %s, size %i' % (atom.type(), atom.size()))
type ftyp, size 24
type mdat, size 4170138
type mdat, size 277450
type mdat, size 178748
type moov, size 12507
type mvhd, size 108
type trak, size 5982
type tkhd, size 92
type ssti, size 12
type mdia, size 5822
type mdhd, size 32
type hdlr, size 33
type minf, size 5749
type vmhd, size 20
type dinf, size 36
type dref, size 28
type stbl, size 5685
type stsd, size 181
type stts, size 3248
type stsz, size 2092
type stsc, size 40
type stco, size 88
type stss, size 28
type edts, size 48
type elst, size 40
type trak, size 3492
type tkhd, size 92
type mdia, size 3372
type mdhd, size 32
type hdlr, size 33
type minf, size 3299
type smhd, size 16
type dinf, size 36
type dref, size 28
type stbl, size 3239
type stsd, size 103
type stts, size 24
type stsz, size 2980
type stsc, size 40
type stco, size 84
type udta, size 20
type name, size 12
type trak, size 2917
type tkhd, size 92
type mdia, size 2817
type mdhd, size 32
type hdlr, size 45
type minf, size 2732
type nmhd, size 12
type dinf, size 36
type dref, size 28
type stbl, size 2676
type stsd, size 32
type stts, size 24
type stsc, size 1552
type stsz, size 532
type stco, size 528

In [12]: m.num()
Out[12]: 57

There is a total of 57 atoms.
From here, it is needed to check the ISO specification. We learn that:
- 'ftyp' is a magic with mpeg4 version used for the file
- 'mdat' is containing an effective data stream. Here we have 3 mdat atoms, so 3 streams
- 'moov' is containing extended information for the mpeg4 file
- within 'moov', we find one 'trak' atom per stream
- each 'trak' contains descriptors for the corresponding stream
As a first approach, we can try so check if 'mdat' contents are recognized by the 
Linux file command:


In [17]: fd=open('t1', 'wb').write(m[1].data())

In [18]: !file t1
t1: gzip compressed data, was "introduction.txt", from FAT filesystem (MS-DOS, OS/2, NT), last modified: Thu Mar 17 14:01:52 2011, max compression

In [19]: !unzip t1
Archive:  t1
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of t1 or
        t1.zip, and cannot find t1.ZIP, period.

In [20]: fd=open('t2', 'wb').write(m[2].data())

In [21]: !file t2
t2: data

In [22]: fd=open('t3', 'wb').write(m[3].data())

In [23]: !file t3
t3: data


It's a beginning, but not very conclusive indeed!
So let's check deeper in the atoms contained in the 'moov' part. We can see a specific
'ssti' atom, which is not standard from the ISO specification (and do not forget 
the challenge is the `SSTIC` one). It is possible to retrieve successive headers
of this atom: we have the 'trak' and 'moov' parts.


In [35]: for atom in m:
   ....:     if atom.type()=='ssti':
   ....:         show(atom)
   ....:         ssti=atom
   ....:         
		### MPEG4 atom [atom] ###
		<[size] : 12>
		<[type] : 'ssti'>
		<[data] : '\x00\x00\x00\x00'>

In [37]: ssti
Out[37]: <MPEG4 atom[atom]: size():12, type():'ssti', data():'\x00\x00\x00\x00'>

In [38]: ssti.get_header()
Out[38]: <MPEG4 atom[atom]: size():5982, type():'trak', data():''>

In [39]: _.get_header()
Out[39]: <MPEG4 atom[atom]: size():12507, type():'moov', data():''>

In [40]: for atom in m:
   ....:     if atom.type()=='trak':
   ....:         print('bip')
   ....:         if atom==ssti.get_header():
   ....:             print 'good'
   ....:             
bip
good
bip
bip


OK, the 'ssti' atom is contained into the 1st 'trak' atom. So it might be that the
interesting stream is the 1st one: but do not speculate too much.
From the 'tkhd' atoms, we can see in the specification that some useful information 
on media streams may appear. Let's look at them:


In [43]: for atom in m:
   ....:     if atom.type()=='hdlr':
   ....:         show(atom)
   ....:         
			### MPEG4 atom [atom] ###
			<[size] : 33>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00vide\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 33>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00soun\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'>
			### MPEG4 atom [atom] ###
			<[size] : 45>
			<[type] : 'hdlr'>
			<[data] : '\x00\x00\x00\x00\x00\x00\x00\x00data\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00SsticHandler\x00'>


Nice, if we trust these atoms, we can guess the 1st stream is a video media, the 
2nd is a sound media, the 3rd corresponds to some SsticHandler that is actually not 
any media (think audio / video) streams...
Now it is time to dive deeper in the ISO spec to see how a stream is organized. 
Some important atoms are descriptor for media streams: 'stsd' for sample description 
(e.g. codec needed), 'stco' for chunk offsets, 'stsc' that provides sample-to-chunk 
partial data offset and 'stsz' for samples size.
Actually, stco provides the global file offset for a given chunk of stream. stsc and stsz 
will help in rebuilding completely the streams.

In [19]: for atom in m:
    if atom.type()=='stco': print('stco: %i' % atom.get_index())
    elif atom.type()=='stsz': print('stsz: %i' % atom.get_index())
    elif atom.type()=='stsc': print('stsc: %i' % atom.get_index())
   ....:     
stsz: 19
stsc: 20
stco: 21
stsz: 37
stsc: 38
stco: 39
stsc: 54
stsz: 55
stco: 56

It is possible to access each atom by simply calling the correct one:
From the ISO spec, we have the following structure:

aligned(8) class SampleToChunkBox
extends FullBox(‘stsc’, version = 0, 0) {
    unsigned int(32) entry_count;
    for (i=1; i <= entry_count; i++) {
        unsigned int(32) first_chunk;
        unsigned int(32) samples_per_chunk;
        unsigned int(32) sample_description_index;
    }
}

In [27]: m[20]
Out[27]: <MPEG4 atom[atom]: size():40, type():'stsc', data():'\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x1e\x00\x00\x00\x01\x00\x00\x00\x12\x00\x00\x00\x08\x00\x00\x00...>

In [28]: stsc1=m[20].data()

In [29]: stsc1
Out[29]: '\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x1e\x00\x00\x00\x01\x00\x00\x00\x12\x00\x00\x00\x08\x00\x00\x00\x01'

The 4 first bytes are just null, 
then we have the entry count: 2, and then 2 SampleToChunk tables with 3 values each.

It is possible to access all other atoms the same way to extract useful information
from the MPEG4 file. In the case of our challenge, we have the following:
1) In the 1st stream (the video one), by following video chunk offsets and sizes from the
descripting atoms, we can extract a video stream, that is actually ciphered.
2) Remember the 1st media stream was however recognized as a zip file. If we check the
preceeding extraction, we find that some parts are not covered by the stream descriptors.
We can extract all the bytes that are not corresponding to the video, this way we retrieve
a compressed text that explains we need to go on a server to try to exploit an sql server.
3) In the 3rd stream (the data one), again by following chunk offsets and sizes from the 
descripting atoms, we can extract a library (.so) that needs to be loaded within VLC. The
library is in charge of deciphering the video stream. Encryption keys are to be found 
in the sql server, and decryption stage requires some more reverse engineering of the 
library.


