def __initJSEngine(self): """ Sets up the javascript context and loads it up with objects and functions defined in our *.js files. This is how we 'expose' our Reader-esque functions and structures to javascript in a PDF to potentially call. """ self.logger.debug("[JavaScript] Initializing Engine") self.javascript_global = Global(self.logger, DEBUG) with PyV8.JSContext(self.javascript_global) as ctx: self.ctx = ctx if not USE_PURE_JS: self.logger.debug("[JavaScript] Using PyV8 Engine") js_bootstrap = open('adobe_bootstrap.js').read() ctx.eval(js_bootstrap) else: self.logger.debug("[JavaScript] Using Pure JavaScript Engine") # [Trevor] find the dir containing the adobe JS files adobeDir = PATH_PURE_JS if adobeDir[-1] != '/': adobeDir += '/' files = [i for i in os.listdir(adobeDir) if i.endswith('.js')] files.sort() try: for js_file in files: self.logger.debug('[JavaScript] Loading %s......' % (adobeDir + js_file)) data = open(adobeDir + js_file).read() ctx.eval(data) except Exception, e: self.logger.exception('[%s] %s' % (js_file, e)) exit(-1)
def run(self): with PyV8.JSLocker(),\ PyV8.JSContext(Global()) as ctxt,\ open('dsl.js', 'r') as dsl_file,\ open('spec/example.js', 'r') as driver_file: print "*************%s PyV8 Begins*************" % self.name ctxt.eval(dsl_file.read(), 'dsl') ctxt.eval(driver_file.read(), 'driver') ctxt.eval("var meta_data = driver.validate()", 'validate') meta_data = PyV8.convert(ctxt.locals.meta_data) print "meta data:" print meta_data["fields"]["name"] print meta_data["fields"]["version"] print "\nerrors:" print meta_data.get("errors") print "\nactions:" print meta_data["actions"] print "\nstates:" print meta_data["states"] devices_dict = {'343dsadfas': {'device_type': 'adsfas'}} ctxt.eval("var devices_dict = %s" % json.dumps(devices_dict)) ctxt.eval("log(10, 'devices_dict = ' + JSON.stringify(devices_dict));") print "\nlog:" ctxt.eval("driver.action('test', 'test', function() { debug('debug msg') });") ctxt.eval("driver.validate()") result = ctxt.eval("driver.translate_action('test')") print "*************%s start for real work*************" % self.name while True: print '\n***************' + self.name print "process_data returns:" result = ctxt.eval( "var result = driver.process_data('8D4B6F_1', 1101, '2014-07-25 09:41:43', '00');", 'process_data') print PyV8.convert(ctxt.locals.result) print "\ntranslate_action:" result = ctxt.eval("driver.translate_action('move')", 'translate_action') print PyV8.convert(result) with PyV8.JSUnlocker(): print "%s wait for data..........." % self.name time.sleep(1) print "**************%s PyV8 Ends**************" % self.name
def kuaidaili_type_com(self, t, *args): logger.info('kuaidaili.com %s start' % t) i = 1 self.THREAD_ID += 1 rq = requests.Session() headers = self.http_headers() rq.get('http://www.kuaidaili.com/', headers=headers) while(1): gevent.sleep(3) url = 'http://www.kuaidaili.com/free/%s/%d/' % (t, i) r = rq.get(url, headers=headers) if 'qo=eval;qo(po);' in r.text: c = PyV8.JSContext() c.enter() f = c.eval(r.text) print(f) # exit() print(r.text) logger.debug('bypass...') continue if r.status_code == 404: break if r.status_code == 503: logger.error('%s return <%d>' % (url, r.status_code)) continue try: html = BeautifulSoup(r.text, 'lxml') tbody = html.tbody if tbody is None: print(html) continue for tr in tbody.find_all('tr'): # print(tr) p = proxy() p.ip = tr.find_all('td', {'data-title':"IP"})[0].text p.port = int(tr.find_all('td', {'data-title':"PORT"})[0].text) p.safe = tr.find_all('td', {'data-title':"匿名度"})[0].text p.type = tr.find_all('td', {'data-title':"类型"})[0].text p.place = tr.find_all('td', {'data-title':"位置"})[0].text # print(tr.find_all('td', {'data-title':"响应速度"})[0].text) # print(tr.find_all('td', {'data-title':"最后验证时间"})[0].text) logger.debug('<get>%s' % p) self.wait_for_verify.put(p) self.THREAD_ID += 1 self.add_thread(self.verify_proxy_thread, self.THREAD_ID) logger.debug('%s ok' % url) gevent.sleep(1) except AttributeError as e: print(e) # print(r.text) logger.error('%s Error, sleep 10s' % url) gevent.sleep(10) continue # exit() i += 1
def encrypt(password, publicKey): with PyV8.JSContext() as ctxt: with open('rsa.js') as f: js = f.read() ctxt.locals.publicKey = publicKey ctxt.locals.password = password ctxt.eval(js) res = ctxt.locals.res return res
def run(self): self.locker = PyV8.JSLocker() self.locker.enter() self.ctxt = PyV8.JSContext(Global()) self.ctxt.enter() with open('dsl.js', 'r') as dsl_file,\ open('spec/example.js', 'r') as driver_file: while True: print "*************%s PyV8 Begins*************" % self.name self.ctxt.eval(dsl_file.read(), 'dsl') self.ctxt.eval(driver_file.read(), 'driver') self.ctxt.eval("var meta_data = driver.validate()", 'validate') devices_dict = {'343dsadfas': {'device_type': 'adsfas'}} self.ctxt.eval("var devices_dict = %s" % json.dumps(devices_dict)) self.ctxt.eval("log(10, 'devices_dict = ' + JSON.stringify(devices_dict));") print "\nlog:" self.ctxt.eval("driver.action('test', 'test', function() { debug('debug msg') });") self.ctxt.eval("driver.validate()") result = self.ctxt.eval("driver.translate_action('test')") print "*************%s start for real work*************" % self.name print '\n***************' + self.name print "process_data returns:" result = self.ctxt.eval( "var result = driver.process_data('8D4B6F_1', 1101, '2014-07-25 09:41:43', '00');", 'process_data') print PyV8.convert(self.ctxt.locals.result) print "\ntranslate_action:" result = self.ctxt.eval("driver.translate_action('move')", 'translate_action') print PyV8.convert(result) with PyV8.JSUnlocker(): print "%s wait for data..........." % self.name time.sleep(1) print "**************%s PyV8 Ends**************" % self.name self.locker.leave() self.ctxt.leave()
def run(self): print "*************%s start for real work*************" % self.name while True: print "*************%s.locker.enter()*************" % self.name self.locker.enter() print "%s process_data returns:" % self.name result = self.ctxt.eval( "var result = driver.process_data('8D4B6F_1', 1101, '2014-07-25 09:41:43', '00');", 'process_data') print PyV8.convert(self.ctxt.locals.result) print "\ntranslate_action:" result = self.ctxt.eval("driver.translate_action('move')", 'translate_action') print PyV8.convert(result) print "*************%s.locker.leave()*************" % self.name self.locker.leave() print "%s wait for data..........." % self.name time.sleep(1) self.ctxt.leave() print "**************%s PyV8 Ends**************" % self.name
def parse_video_info(self, response): selector = scrapy.Selector(response) script = selector.xpath( '//div[@class="media-parent"]/div/video/script') params = re.findall('strencode\((.*)\)', script.extract()[0])[0] params = params.split(',') js = u""" function strencode(input, key) { input = atob(input); len = key.length; code = ''; for (i = 0; i < input.length; i++) { k = i %% len; code += String.fromCharCode(input.charCodeAt(i) ^ key.charCodeAt(k)); } return atob(code); } let source = strencode(%s, %s) """ % (params[0], params[1]) # JS ENCODE CONVERT PYTHON # def aencode(self, input, key): # input = input.decode('base64') # _len = len(key) # # code = '' # for i in range(_len): # _key = i % _len # # code += chr(ord(input[i]) ^ ord(key[_key])) # # return code.decode('base64') ctxt = PyV8.JSContext() ctxt.enter() ctxt.eval(js) vars = ctxt.locals print(vars.source)
def tx_pwd_encode_by_js(self, pwd, salt, verifycode): """ 调用V8引擎,直接执行TX的登陆JS中的加密方法,不用自己实现其中算法。 """ # pwd, salt, verifycode, undefined # with PyV8.JSContext() as ctxt: # with open("qq.login.encrypt.js") as jsfile: # ctxt.eval(jsfile.read()) # encrypt_pwd = ctxt.eval("window.$pt.Encryption.getEncryption('%s', '%s', '%s', undefined)" # %(pwd, salt, verifycode) ) # return encrypt_pwd # # self.jsRT.require("xlogin") # self.jsRT.require("c_login_2_src") print "OK" str = u"Encryption.getEncryption('%s', '%s', '%s', undefined)" % ( pwd.encode('utf-8'), salt.encode('utf-8'), verifycode.encode('utf-8')) print str # encrypt_pwd = self.jsRT.execute(str.encode("utf-8")) # print "Encrypt1:" # print encrypt_pwd # return encrypt_pwd with PyV8.JSContext() as ctxt: with open("c_login_2.js") as jsfile: ctxt.eval(jsfile.read()) str = u"Encryption.getEncryption('%s', '%s', '%s', undefined)" % ( pwd.encode('utf-8'), salt.encode('utf-8'), verifycode.encode('utf-8')) print str # encrypt_pwd = ctxt.eval(str.encode("utf-8")) # print "Encrypt2:" # print encrypt_pwd encrypt_pwd = ctxt.eval(str.encode("utf-8")) # print "Encrypt3:" print encrypt_pwd return encrypt_pwd
def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.name = name self.ctxt = PyV8.JSContext(Global()) self.locker = PyV8.JSLocker() with open('dsl.js', 'r') as dsl_file,\ open('spec/example.js', 'r') as driver_file: print "*************%s PyV8 Begins*************" % self.name self.ctxt.enter() print "*************%s __init__.locker.enter()*************" % self.name self.locker.enter() self.ctxt.eval(dsl_file.read(), 'dsl') self.ctxt.eval(driver_file.read(), 'driver') self.ctxt.eval("var meta_data = driver.validate()", 'validate') meta_data = PyV8.convert(self.ctxt.locals.meta_data) print "meta data:" print meta_data["fields"]["name"] print meta_data["fields"]["version"] print "\nerrors:" print meta_data.get("errors") print "\nactions:" print meta_data["actions"] print "\nstates:" print meta_data["states"] devices_dict = {'343dsadfas': {'device_type': 'adsfas'}} self.ctxt.eval("var devices_dict = %s" % json.dumps(devices_dict)) self.ctxt.eval("log(10, 'devices_dict = ' + JSON.stringify(devices_dict));") print "\nlog:" self.ctxt.eval("driver.action('test', 'test', function() { debug('debug msg') });") self.ctxt.eval("driver.validate()") self.ctxt.eval("driver.translate_action('test')") print "*************%s __init__.locker.leave()*************" % self.name self.locker.leave()
pass from pyv8 import PyV8 TEMPLATE_FOLDER = 'templates/' env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_FOLDER)) env.add_extension('jsjinja.ext.JsJinjaExtension') class Global(PyV8.JSClass): def log(self, *args): print args ctx = PyV8.JSContext(Global()) ctx.enter() ctx.eval(jsjinja.lib()) # templates = env.list_templates() def test_extension(): js = env.jsjinja.generate_source(source='{{a}}') ex = ctx.eval('(new (%s))' % js).render({"a": "test"}) assert ex == "test" def test_extensiontag(): template = '''{% jsjinja %}{% macro x(s) %}{{s}}{% endmacro %}{% endjsjinja %}'''
import PyV8 import json class Global(PyV8.JSClass): def log(self, level, msg): print "level %d: %s" % (level, msg) ctxt = PyV8.JSContext(Global()) ctxt.enter() driver = open('dsl.js', 'r').read() ctxt.eval(driver) example = open('spec/example.js', 'r').read() ctxt.eval(example) ctxt.eval("var meta_data = driver.validate()") meta_data = PyV8.convert(ctxt.locals.meta_data) print "meta data:" print meta_data["fields"] print "errors:" print meta_data.get("errors") print "actions:" print meta_data["actions"] print "states:" print meta_data["states"] print "data fields:" print meta_data["data_fields"] print "passive?:" print meta_data["passive"] print "init:" devices_dict = {'343dsadfas': {'device_type': 'adsfas'}}
def __init__(self): self.js_env = PyV8.JSContext() self.js_env.enter() code = JSource.all_code self.js_env.eval(code)
contexts.append(PyV8.JSContext(Global())) for ctxt in contexts: ctxt.enter() driver = open('dsl.js', 'r').read() ctxt.eval(driver) example = open('spec/example.js', 'r').read() ctxt.eval(example) ctxt.leave() for i in range(3): contexts[i].enter() print "init:" devices_dict = {str(i): {'device_type': 'adsfas'}} contexts[i].eval("var devices_dict = %s" % json.dumps(devices_dict)) contexts[i].eval("log(10, JSON.stringify(devices_dict));") contexts[i].leave() for i in range(3): contexts[i].enter() print "fetch_data:" print PyV8.convert(contexts[i].eval("driver.fetch_data();")) for j in range(3): print "process_data:" print PyV8.convert(contexts[j].eval("driver.process_data('" + str(j) + "', 1101, '2014-07-25 09:41:43', '" + str(i*100 + j) + "');")) print "translate_action:" result = contexts[j].eval("driver.translate_action('move')") print PyV8.convert(result) contexts[i].leave()
if __name__ == '__main__': s = requests.Session() s.get('http://yun.baidu.com') js = ''' function callback(){ return 'bd__cbs__'+Math.floor(2147483648 * Math.random()).toString(36) } function gid(){ return 'xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (e) { var t = 16 * Math.random() | 0, n = 'x' == e ? t : 3 & t | 8; return n.toString(16) }).toUpperCase() } ''' ctxt = PyV8.JSContext() ctxt.enter() ctxt.eval(js) ###########获取gid#############################3 gid = ctxt.locals.gid() ###########获取callback#############################3 callback1 = ctxt.locals.callback() ###########获取token#############################3 tokenUrl="https://passport.baidu.com/v2/api/?getapi&tpl=netdisk&subpro=netdisk_web&apiver=v3" \ "&tt=%d&class=login&gid=%s&logintype=basicLogin&callback=%s"%(time.time()*1000,gid,callback1) token_response = s.get(tokenUrl) pattern = re.compile(r'"token"\s*:\s*"(\w+)"') match = pattern.search(token_response.text) if match: token = match.group(1)
""" Some debugging code. """ DIR = 'adobe/' import os import sys if len(sys.argv) < 1: data = '' else: print 'Using %s' % (sys.argv[1]) script = open(sys.argv[1]).read() files = os.listdir(DIR) files.sort() js = [] with PyV8.JSContext(Global()) as ctx: try: for file in files: if file.endswith('.js'): print 'Loading %s......' % (DIR + file), data = open(DIR + file).read() ctx.eval(data) print 'Done!' except Exception, e: print e raise try: ctx.eval(script) except Exception, e: print e pass
js = """ function callback(){ var h = "bd__cbs__"; return h + Math.floor(Math.random() * 2147483648).toString(36); } function gid() { return "xxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (e) { var t = 16 * Math.random() | 0, n = "x" == e ? t : 3 & t | 8; return n.toString(16) }).toUpperCase() } """ js_driver = PyV8.JSContext() js_driver.__enter__() js_driver.eval(js) gid = ctxt.locals.gid() # print(gid) callback1 = js_driver.locals.callback() callback2 = js_driver.locals.callback() callback3 = js_driver.locals.callback() token_url = "https://passport.baidu.com/v2/api/?getapi&tpl=nx&apiver=v3&tt=%s&class=login&gid=%s&" \ "logintype=basicLogin&traceid=&callback=%s" % (time.time()*1000,gid,callback1) token_result = session.get(token_url, verify=False).text token_pattern = re.compile('"token"\s*:\s*"(.*?)",')
def execute(code): if isinstance(code, unicode): code = code.encode("utf-8") with PyV8.JSContext() as c: c.enter() return c.eval(code)
def context(self): if not hasattr(self, "_context"): self._context = PyV8.JSContext(self) return self._context
def execute_JS(self, js_func_string, arg): ctxt = PyV8.JSContext() ctxt.enter() func = ctxt.eval("({js})".format(js=js_func_string)) return func(arg)