Created
January 27, 2021 19:51
-
-
Save sambshapiro/e8253619703f5462683adaae04f391f5 to your computer and use it in GitHub Desktop.
Decrypting an HLS Stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import Crypto | |
from hashlib import md5 | |
from Crypto.Cipher import AES | |
from Crypto import Random | |
import hexdump | |
from chardet import detect | |
source_directory = '' | |
target_directory = '' | |
### Inspiration: https://gist.github.com/delimitry/053db410f7fdd4e1d6034a0a7fd45d9b | |
def decrypt(data, key, iv): | |
"""Decrypt using AES CBC""" | |
decryptor = AES.new(key, AES.MODE_CBC, IV=iv) | |
return decryptor.decrypt(data) | |
# iterate through a specific stream, e.g. "hd", "sd", "low" | |
def decryptStream(directory, key): | |
print("decrypting " + directory) | |
for filename in os.listdir(directory): | |
if filename.endswith(".ts"): | |
out_file = os.path.join(target_directory, os.path.basename(directory), filename) | |
# 'rb' is read-as-binary: https://stackoverflow.com/questions/8710456/reading-a-binary-file-with-python | |
enc_data = open(os.path.join(directory, filename), 'rb').read() | |
iv = os.urandom(16) | |
with open(out_file, 'wb') as f: | |
dec_data = decrypt(enc_data, key, iv) | |
f.write(dec_data) | |
elif filename.endswith(".m3u8"): | |
out_file = os.path.join(target_directory, os.path.basename(directory), filename) | |
m3u8 = open(os.path.join(directory, filename), 'rb').read() | |
with open(out_file, 'wb') as f: | |
f.write(m3u8) | |
else: | |
continue | |
# get file encoding type | |
# https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c | |
def get_encoding_type(file): | |
with open(file, 'rb') as f: | |
rawdata = f.read() | |
return detect(rawdata)['encoding'] | |
# get key | |
def getKey(source_directory): | |
for filename in os.listdir(source_directory): | |
if filename.endswith(".key"): | |
# 'rb' is read-as-binary: https://stackoverflow.com/questions/8710456/reading-a-binary-file-with-python | |
key = open(os.path.join(source_directory, filename), 'rb').read() | |
return key | |
# iterate through parent source directory | |
key = getKey(source_directory) | |
for filename in os.listdir(source_directory): | |
if os.path.isdir(os.path.join(source_directory, filename)): | |
# if (stream) directory (e.g. "hd", "sd", "low") | |
# first create the stream directory in the target folder | |
os.mkdir(os.path.join(target_directory, filename)) | |
# now decrypt the stream | |
decryptStream(os.path.join(source_directory, filename), key) | |
elif filename.endswith(".m3u8"): | |
out_file = os.path.join(target_directory, filename) | |
encoding_type = get_encoding_type(os.path.join(source_directory, filename)) | |
m3u8 = open(os.path.join(source_directory, filename), 'rb').read() | |
with open(out_file, 'wb') as f: | |
f.write(m3u8) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment