From 93a3a5fadab3f8accb475a80aa8f32dad6c8b92a Mon Sep 17 00:00:00 2001 From: Christopher Rosell Date: Wed, 26 Oct 2011 03:03:19 +0200 Subject: Add SWF verification. --- src/livestreamer/compat.py | 4 ++-- src/livestreamer/plugins/justintv.py | 6 +++++- src/livestreamer/utils.py | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/livestreamer/compat.py b/src/livestreamer/compat.py index 142f39c..d3d6bdc 100644 --- a/src/livestreamer/compat.py +++ b/src/livestreamer/compat.py @@ -4,7 +4,7 @@ import sys orig_str = str -def str(s, enc=None): +def str(s, enc="ascii"): if sys.version_info[0] == 3: return orig_str(s, enc) else: @@ -13,7 +13,7 @@ def str(s, enc=None): orig_bytes = bytes -def bytes(s, enc=None): +def bytes(s, enc="ascii"): if sys.version_info[0] == 3: return orig_bytes(s, enc) else: diff --git a/src/livestreamer/plugins/justintv.py b/src/livestreamer/plugins/justintv.py index e9631a1..dacfd05 100644 --- a/src/livestreamer/plugins/justintv.py +++ b/src/livestreamer/plugins/justintv.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from livestreamer.plugins import Plugin, register_plugin -from livestreamer.utils import CommandLine +from livestreamer.utils import CommandLine, swfverify from livestreamer.compat import urllib, str import xml.dom.minidom, re, sys, random @@ -66,9 +66,13 @@ class JustinTV(object): return streams def stream_cmdline(self, stream, filename): + swfhash, swfsize = swfverify(self.SWFURL) + cmd = CommandLine("rtmpdump") cmd.arg("rtmp", ("{0}/{1}").format(stream["connect"], stream["play"])) cmd.arg("swfUrl", self.SWFURL) + cmd.arg("swfhash", swfhash) + cmd.arg("swfsize", swfsize) cmd.arg("live", True) cmd.arg("flv", filename) diff --git a/src/livestreamer/utils.py b/src/livestreamer/utils.py index 6dd13f4..bfb59cc 100644 --- a/src/livestreamer/utils.py +++ b/src/livestreamer/utils.py @@ -1,5 +1,10 @@ #!/usr/bin/env python3 +from livestreamer.compat import urllib, bytes +import hmac, hashlib, zlib + +SWF_KEY = b"Genuine Adobe Flash Player 001" + class CommandLine(object): def __init__(self, command): self.command = command @@ -15,10 +20,23 @@ class CommandLine(object): if value == True: args.append(("--{0}").format(key)) else: - escaped = value.replace('"', '\\"').replace("$", "\$").replace("`", "\`") + escaped = str(value).replace('"', '\\"').replace("$", "\$").replace("`", "\`") args.append(("--{0} \"{1}\"").format(key, escaped)) args = (" ").join(args) cmdline = ("{0} {1}").format(self.command, args) return cmdline + + +def swfverify(url): + fd = urllib.urlopen(url) + swf = fd.read() + fd.close() + + if swf[:3] == b"CWS": + swf = b"F" + swf[1:8] + zlib.decompress(swf[8:]) + + h = hmac.new(SWF_KEY, swf, hashlib.sha256) + + return h.hexdigest(), len(swf) -- cgit v1.2.3