def solve_challenge(body, domain): try: js = re.search( r'setTimeout\(function\(\){\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\r?\n[\s\S]+?a\.value =.+?)\r?\n', body).group(1) except Exception: raise ValueError( 'Unable to identify Cloudflare IUAM Javascript on website. {}'. format(BUG_REPORT)) js = re.sub(r'\s{2,}', ' ', js, flags=re.MULTILINE | re.DOTALL).replace( '\'; 121\'', '').replace('; ;', ';') js += '\na.value;' js_env = ''' String.prototype.italics=function(str) {{return "<i>" + this + "</i>";}}; var document = {{ createElement: function () {{ return {{ firstChild: {{ href: "https://{domain}/" }} }} }}, getElementById: function () {{ return {{"innerHTML": "{innerHTML}"}}; }} }}; ''' try: inner_html = re.search( r'<div(?: [^<>]*)? id="([^<>]*?)">([^<>]*?)</div>', body, re.MULTILINE | re.DOTALL) inner_html = inner_html.group(2) if inner_html else '' except: logging.error( 'Error extracting Cloudflare IUAM Javascript. {}'.format( BUG_REPORT)) raise try: js_env = re.sub(r'\s{2,}', ' ', js_env.format(domain=domain, innerHTML=inner_html), flags=re.MULTILINE | re.DOTALL) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() with LOCK: context = js2py.EvalJs({'atob': atob}) result = context.eval('{}{}'.format(js_env, js)) float(result) except Exception: raise ValueError( "Cloudflare IUAM challenge returned unexpected answer. {}". format(BUG_REPORT)) return result
def __init__(self, pac_js, recursion_limit=ARBITRARY_HIGH_RECURSION_LIMIT): """ Load a PAC file from a given string of JavaScript. Errors during parsing and validation may raise a specialized exception. :param str pac_js: JavaScript that defines the FindProxyForURL() function. :param int recursion_limit: Python recursion limit when executing JavaScript. PAC files are often complex enough to need this to be higher than the interpreter default. :raises MalformedPacError: If the JavaScript could not be parsed, does not define FindProxyForURL(), or is otherwise invalid. :raises PyImportError: If the JavaScript tries to use Js2Py's `pyimport` keyword, which is not legitimate in the context of a PAC file. :raises PacComplexityError: If the JavaScript was complex enough that the Python recursion limit was hit during parsing. """ self._recursion_limit = recursion_limit if 'pyimport' in pac_js: raise PyimportError() # Disallow parsing of the unsafe 'pyimport' statement in Js2Py. js2py.disable_pyimport() try: with _temp_recursion_limit(self._recursion_limit): context = js2py.EvalJs(function_injections) context.execute(pac_js) # A test call to weed out errors like unimplemented functions. context.FindProxyForURL('/', '0.0.0.0') self._context = context self._func = context.FindProxyForURL except PyJsException: # as e: raise MalformedPacError() # from e except RuntimeError: # RecursionError in PY >= 3.5. raise PacComplexityError()
def findvideos(item): logger.info() itemlist = [] data = httptools.downloadpage(item.url).data _sa = scrapertools.find_single_match(data, 'var _sa = (true|false);') _sl = scrapertools.find_single_match(data, 'var _sl = ([^;]+);') sl = eval(_sl) #buttons = scrapertools.find_multiple_matches(data, '<button href="" class="selop" sl="([^"]+)">') buttons = [0, 1, 2] for id in buttons: new_url = golink(int(id), _sa, sl) data_new = httptools.downloadpage(new_url).data matches = scrapertools.find_multiple_matches( data_new, 'javascript">(.*?)</script>') js = "" for part in matches: js += part #logger.info("test before:" + js) try: matches = scrapertools.find_multiple_matches( data_new, '" id="(.*?)" val="(.*?)"') for zanga, val in matches: js = js.replace( 'var %s = document.getElementById("%s");' % (zanga, zanga), "") js = js.replace('%s.getAttribute("val")' % zanga, '"%s"' % val) #logger.info("test1 after:" +js) except: pass js = re.sub('(document\[.*?)=', 'prem=', js) import js2py js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval(js) url = scrapertools.find_single_match(result, 'src="(.*?)"') title = '%s' itemlist.append( Item(channel=item.channel, title=title, url=url, action='play', language='latino', infoLabels=item.infoLabels)) itemlist = servertools.get_servers_itemlist( itemlist, lambda i: i.title % i.server.capitalize()) # Requerido para FilterTools itemlist = filtertools.get_links(itemlist, item, list_language) # Requerido para AutoPlay autoplay.start(itemlist, item) return itemlist
def eval(self, jsEnv, js): if js2py.eval_js('(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]') == '1': logging.warning('WARNING - Please upgrade your js2py https://github.com/PiotrDabkowski/Js2Py, applying work around for the meantime.') js = jsunfuck(js) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval('{}{}'.format(jsEnv, js)) return result
def solve_cf(self, body, domain): # Adaptado de: https://github.com/VeNoMouS/cloudflare-scrape-js2py js = re.search( r"setTimeout\(function\(\){\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\r?\n[\s\S]+?a\.value =.+?)\r?\n", body).group(1) js = re.sub(r"a\.value = ((.+).toFixed\(10\))?", r"\1", js) js = re.sub(r'(e\s=\sfunction\(s\)\s{.*?};)', '', js, flags=re.DOTALL | re.MULTILINE) js = re.sub(r"\s{3,}[a-z](?: = |\.).+", "", js).replace("t.length", str(len(domain))) js = js.replace('; 121', '') js = re.sub(r"[\n\\']", "", js) jsEnv = """ var t = "{domain}"; var g = String.fromCharCode; o = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; e = function(s) {{ s += "==".slice(2 - (s.length & 3)); var bm, r = "", r1, r2, i = 0; for (; i < s.length;) {{ bm = o.indexOf(s.charAt(i++)) << 18 | o.indexOf(s.charAt(i++)) << 12 | (r1 = o.indexOf(s.charAt(i++))) << 6 | (r2 = o.indexOf(s.charAt(i++))); r += r1 === 64 ? g(bm >> 16 & 255) : r2 === 64 ? g(bm >> 16 & 255, bm >> 8 & 255) : g(bm >> 16 & 255, bm >> 8 & 255, bm & 255); }} return r; }}; function italics (str) {{ return '<i>' + this + '</i>'; }}; var document = {{ getElementById: function () {{ return {{'innerHTML': '{innerHTML}'}}; }} }}; {js} """ innerHTML = re.search( '<div(?: [^<>]*)? id="([^<>]*?)">([^<>]*?)<\/div>', body, re.MULTILINE | re.DOTALL) innerHTML = innerHTML.group(2).replace("'", r"\'") if innerHTML else "" import js2py from jsc import jsunc js = jsunc(jsEnv.format(domain=domain, innerHTML=innerHTML, js=js)) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval(js) return float(result)
def eval(self, jsEnv, js): if js2py.eval_js( '(+(+!+[]+[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+[!+[]+!+[]]+[+[]])+[])[+!+[]]' ) == '1': logging.warning( 'WARNING - Please upgrade your js2py https://github.com/PiotrDabkowski/Js2Py, applying work around for the meantime.' ) js = jsunfuck(js) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval('{}{}'.format(jsEnv, js)) return result
assert js2py.eval_js('1 + "1"') == '11' assert js2py.eval_js('function (r) {return r}')(5) == 5 x, c = js2py.run_file('examples/esprima.js') assert c.esprima.parse('var abc = 40').to_dict() == {'type': 'Program', 'body': [{'type': 'VariableDeclaration', 'kind': 'var', 'declarations': [{'id': {'type': 'Identifier', 'name': 'abc'}, 'type': 'VariableDeclarator', 'init': {'type': 'Literal', 'raw': '40', 'value': 40}}]}], 'sourceType': 'script'} try: assert js2py.eval_js('syntax error!') and 0 except js2py.PyJsException as err: assert str(err).startswith('SyntaxError: ') assert js2py.eval_js('pyimport time; time.time()') <= time.time() js2py.disable_pyimport() try: assert js2py.eval_js('pyimport time') and 0 except js2py.PyJsException as err: assert str(err).startswith('SyntaxError: ') # Simple PyWrapper test class Foo: def __init__(self, bar): self.bar = bar self.bar_history = [] def set_bar(self, x): import copy self.bar_history.append(copy.deepcopy(self))
def __init__(self): self._lock = Lock() self._libraries = VDOM_javascript_libraries(self) js2py.disable_pyimport()
def solve_challenge(self, body, domain): try: js = re.search( r"setTimeout\(function\(\){\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\r?\n[\s\S]+?a\.value =.+?)\r?\n", body).group(1) except Exception: raise ValueError( "Unable to identify Cloudflare IUAM Javascript on website. {}". format(BUG_REPORT)) js = re.sub(r"a\.value = ((.+).toFixed\(10\))?", r"\1", js) js = re.sub(r'(e\s=\sfunction\(s\)\s{.*?};)', '', js, flags=re.DOTALL | re.MULTILINE) js = re.sub(r"\s{3,}[a-z](?: = |\.).+", "", js).replace("t.length", str(len(domain))) js = js.replace('; 121', '') # Strip characters that could be used to exit the string context # These characters are not currently used in Cloudflare's arithmetic snippet js = re.sub(r"[\n\\']", "", js) if 'toFixed' not in js: raise ValueError( "Error parsing Cloudflare IUAM Javascript challenge. {}". format(BUG_REPORT)) try: jsEnv = """ var t = "{domain}"; var g = String.fromCharCode; o = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; e = function(s) {{ s += "==".slice(2 - (s.length & 3)); var bm, r = "", r1, r2, i = 0; for (; i < s.length;) {{ bm = o.indexOf(s.charAt(i++)) << 18 | o.indexOf(s.charAt(i++)) << 12 | (r1 = o.indexOf(s.charAt(i++))) << 6 | (r2 = o.indexOf(s.charAt(i++))); r += r1 === 64 ? g(bm >> 16 & 255) : r2 === 64 ? g(bm >> 16 & 255, bm >> 8 & 255) : g(bm >> 16 & 255, bm >> 8 & 255, bm & 255); }} return r; }}; function italics (str) {{ return '<i>' + this + '</i>'; }}; var document = {{ getElementById: function () {{ return {{'innerHTML': '{innerHTML}'}}; }} }}; {js} """ innerHTML = re.search( '<div(?: [^<>]*)? id="([^<>]*?)">([^<>]*?)<\/div>', body, re.MULTILINE | re.DOTALL) innerHTML = innerHTML.group(2).replace("'", r"\'") if innerHTML else "" js = jsunfuck( jsEnv.format(domain=domain, innerHTML=innerHTML, js=js)) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval(js) except Exception: logging.error( "Error executing Cloudflare IUAM Javascript. {}".format( BUG_REPORT)) raise try: float(result) except Exception: raise ValueError( "Cloudflare IUAM challenge returned unexpected answer. {}". format(BUG_REPORT)) return result
def solve_challenge(self, body, domain): try: js = re.search( r"setTimeout\(function\(\){\s+(var s,t,o,p,b,r,e,a,k,i,n,g,f.+?\r?\n[\s\S]+?a\.value =.+?)\r?\n", body ).group(1) except Exception: raise ValueError("Unable to identify Cloudflare IUAM Javascript on website. {}".format(BUG_REPORT)) js = re.sub(r"a\.value = ((.+).toFixed\(10\))?", r"\1", js) js = re.sub(r'(e\s=\sfunction\(s\)\s{.*?};)', '', js, flags=re.DOTALL|re.MULTILINE) js = re.sub(r"\s{3,}[a-z](?: = |\.).+", "", js).replace("t.length", str(len(domain))) js = js.replace('; 121', '') # Strip characters that could be used to exit the string context # These characters are not currently used in Cloudflare's arithmetic snippet js = re.sub(r"[\n\\']", "", js) if 'toFixed' not in js: raise ValueError("Error parsing Cloudflare IUAM Javascript challenge. {}".format(BUG_REPORT)) try: jsEnv = """ var t = "{domain}"; var g = String.fromCharCode; o = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; e = function(s) {{ s += "==".slice(2 - (s.length & 3)); var bm, r = "", r1, r2, i = 0; for (; i < s.length;) {{ bm = o.indexOf(s.charAt(i++)) << 18 | o.indexOf(s.charAt(i++)) << 12 | (r1 = o.indexOf(s.charAt(i++))) << 6 | (r2 = o.indexOf(s.charAt(i++))); r += r1 === 64 ? g(bm >> 16 & 255) : r2 === 64 ? g(bm >> 16 & 255, bm >> 8 & 255) : g(bm >> 16 & 255, bm >> 8 & 255, bm & 255); }} return r; }}; function italics (str) {{ return '<i>' + this + '</i>'; }}; var document = {{ getElementById: function () {{ return {{'innerHTML': '{innerHTML}'}}; }} }}; {js} """ innerHTML = re.search( '<div(?: [^<>]*)? id="([^<>]*?)">([^<>]*?)<\/div>', body, re.MULTILINE | re.DOTALL ) innerHTML = innerHTML.group(2).replace("'", r"\'") if innerHTML else "" js = jsunfuck(jsEnv.format(domain=domain, innerHTML=innerHTML, js=js)) def atob(s): return base64.b64decode('{}'.format(s)).decode('utf-8') js2py.disable_pyimport() context = js2py.EvalJs({'atob': atob}) result = context.eval(js) except Exception: logging.error("Error executing Cloudflare IUAM Javascript. {}".format(BUG_REPORT)) raise try: float(result) except Exception: raise ValueError("Cloudflare IUAM challenge returned unexpected answer. {}".format(BUG_REPORT)) return result