def test_command_line_code(): """Test that command line code is executed.""" proc = Spidermonkey(code='print("Hello")') stdout, stderr = proc.communicate() assert (stdout, stderr) == ('Hello\n', '') assert proc.returncode == 0
def test_command_line_code(): """Test that command line code is executed.""" proc = Spidermonkey(code='print("Hello")') stdout, stderr = proc.communicate() assert (stdout, stderr) == (b'Hello\n', b'') assert proc.returncode == 0
def test_script_file(): """Test that a script file is executed as expected.""" proc = Spidermonkey(script_file='-') stdout, stderr = proc.communicate(b'print("World")') assert (stdout, stderr) == (b'World\n', b'') assert proc.returncode == 0
def test_script_file(): """Test that a script file is executed as expected.""" proc = Spidermonkey(script_file='-') stdout, stderr = proc.communicate('print("World")') assert (stdout, stderr) == ('World\n', '') assert proc.returncode == 0
def test_early_script_file(file): """Test that an early script file is executed as expected.""" proc = Spidermonkey(early_script_file=file) stdout, stderr = proc.communicate('print("World")') assert (stdout, stderr) == ('World\n', '') assert proc.returncode == 0
def test_extra_flags(): """Test that extra flags are processed as expected.""" proc = Spidermonkey(extra_flags=('-e', 'print(scriptArgs)'), script_args=('Hello', 'World')) stdout, stderr = proc.communicate(b'') assert (stdout, stderr) == (b'Hello,World\n', b'') assert proc.returncode == 0
def test_script_args(): """Test that script args are set as expected.""" proc = Spidermonkey(code='print(scriptArgs)', script_args=('Hello', 'World')) stdout, stderr = proc.communicate(b'') assert (stdout, stderr) == (b'Hello,World\n', b'') assert proc.returncode == 0
def test_multi_scripts(): """Test that when multiple scripts are passed, they're all executed.""" CODE = ('print(version())', 'version(185)', 'print(version())', 'print("Hello")') proc = Spidermonkey(early_script_file='-', code=CODE) stdout, stderr = proc.communicate('print("World")') assert (stdout, stderr) == ('0\n185\nHello\nWorld\n', '') assert proc.returncode == 0
def test_compileonly(): """Test that compileonly returns a status but no output.""" # Valid code: no output, returns 0. proc = Spidermonkey(compile_only=True, script_file='-') stdout, stderr = proc.communicate(b'print("Hello")') assert not stdout assert not stderr assert proc.returncode == 0 # Invalid code: only stderr output, returns > 0. proc = Spidermonkey(compile_only=True, script_file='-') stdout, stderr = proc.communicate(b'print("Hello"') assert stdout == b'' assert stderr != b'' assert proc.returncode > 0 # The compile-only flag is not applied to code passed on the # command line, so it should not be accepted. with pytest.raises(AssertionError): Spidermonkey(compile_only=True, code="print('Hello')")
def test_compileonly(): """Test that compileonly returns a status but no output.""" # Valid code: no output, returns 0. proc = Spidermonkey(compile_only=True, script_file='-') stdout, stderr = proc.communicate('print("Hello")') assert not stdout assert not stderr assert proc.returncode == 0 # Invalid code: only stderr output, returns > 0. proc = Spidermonkey(compile_only=True, script_file='-') stdout, stderr = proc.communicate('print("Hello"') assert stdout == '' assert stderr != '' assert proc.returncode > 0 # The compile-only flag is not applied to code passed on the # command line, so it should not be accepted. with pytest.raises(AssertionError): Spidermonkey(compile_only=True, code="print('Hello')")
def _cloudflare(self, request, response, spider): """Resolve the CloudFlare challenge.""" # Extract the URL from the form xp = '//form/@action' url = response.xpath(xp).extract_first() url = response.urljoin(url) domain = spider.allowed_domains[0] # Extract the parameters from the form xp = '//form/input[@name="jschl_vc"]/@value' jschl_vc = response.xpath(xp).extract_first() xp = '//form/input[@name="pass"]/@value' pass_ = response.xpath(xp).extract_first() if jschl_vc and pass_: # Extract the JavaScript snippets that can be evaluated xp = '//script/text()' init = response.xpath(xp).re_first(r'var s,t,o,p.*') challenge = response.xpath(xp).re_first(r'(.*;)a.value') variable = response.xpath(xp).re_first(r'\s+;(\w+\.\w+).=') result = 'print((%s+%s).toFixed(10))' % (variable, len(domain)) code = (init, challenge) proc = Spidermonkey(early_script_file='-', code=code) stdout, stderr = proc.communicate(result) jschl_answer = stdout.strip() logger.debug('Challenge response: %s', jschl_answer) # Generate the new request formdata = { 'jschl_vc': jschl_vc, 'pass': pass_, 'jschl_answer': jschl_answer, } original_url = request.url request = scrapy.FormRequest.from_response(response, formdata=formdata) request.headers['Referer'] = original_url # XXX TODO - Is there a way to delay this single request? time.sleep(4) return request else: # The challenge changed and the code is outdated logger.error('CloudFlare challenge changed. Please update') return response
def _unencrypt_images(self, response): """Build the JavaScript progran to unencrypt the image URLs.""" xp = '//script' # Get the scripts that will build the program to unencrypt the # URLs for the images ca = response.meta['ca'] lo = response.meta['lo'] # Get the list of keys keys = response.xpath(xp).re(r'var _0x.*') # Generate the JavaScript program to show the URLs wrapKA = response.xpath(xp).re(r'lstImages.push\((.*)\);') wrapKA = '\n'.join(('print(%s);' % line for line in wrapKA)) code = [ca, lo] code.extend(keys) proc = Spidermonkey(early_script_file='-', code=code) stdout, stderr = proc.communicate(wrapKA) return stdout.strip().split('\n')
def test_multi_scripts(): """Test that when multiple scripts are passed, they're all executed.""" CODE = (""" function makeSave() { let save = 0; function clo(val) { if (val === undefined) return save; else save = val; } return clo; } test = makeSave(); """ 'print(test())', 'test(185)', 'print(test())', 'print("Hello")') proc = Spidermonkey(early_script_file='-', code=CODE) stdout, stderr = proc.communicate(b'print("World")') print(stderr) assert (stdout, stderr) == (b'0\n185\nHello\nWorld\n', b'') assert proc.returncode == 0
exit(-1) signal.signal(signal.SIGALRM, handler) for target_program in get_seed(target_dir): with target_program.open('r') as f: Mutator = code_mutator(f.read()) for i in range(10): try: m_code = Mutator.gen_mutant() except: break sm = Spidermonkey(early_script_file='-', code=m_code) signal.alarm(2) try: sm_out, sm_err = sm.communicate() except Exception as x: sm_out = b"" sm_err = b"Timeout" signal.alarm(0) signal.alarm(2) v8 = V8bridge() try: v8.eval_source(m_code) except Exception as x: v8_out = "" if x.args[0] == "bTimeout": v8_err = b"Timeout" elif isinstance(x.args[0], bytes): v8_err = x.args[0]