Skip to content

Commit

Permalink
Fix js evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
LiteracyFanatic committed Aug 23, 2020
1 parent 6a6bf49 commit 6c3fe34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
18 changes: 9 additions & 9 deletions anime_downloader/extractors/mp4upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def _get_data(self):
logger.warning('File not found (Most likely deleted)')
return {'stream_url':''}

regex = r">\s*(eval\(function[\W\w]*?)</script>"
script = re.search(regex,soup).group(1)
script = util.deobfuscate_packed_js(script)

url = ''
if re.search(r'player\.src\("([^"]*)',script):
url = re.search(r'player\.src\("([^"]*)',script).group(1)
elif re.search(r'src:"([^"]*)',script):
url = re.search(r'src:"([^"]*)',script).group(1)
regex = r"eval\(function[\W\w]*?</script>"
script = re.search(regex,soup).group().replace('</script>','')
script = script + "player.source.sources[0].src;"
sandbox = """{
player: {},
document: { getElementById: x => x },
$: () => ({ ready: x => x })
}"""
url = util.eval_in_node(script, sandbox=sandbox)
return {
'stream_url': url,
'referer': self.url
Expand Down
17 changes: 11 additions & 6 deletions anime_downloader/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import platform
import re
import os
from os import path
import json
import errno
import time
Expand Down Expand Up @@ -390,16 +391,20 @@ def get_hcaptcha_cookies(url):
return pickle.load(open(COOKIE_FILE, 'rb'))

def deobfuscate_packed_js(packedjs):
return eval_in_node('eval=console.log; ' + packedjs)
return eval_in_node(packedjs)

def eval_in_node(js: str):
def eval_in_node(js: str, sandbox: str = "{}"):
js = base64.b64encode(js.encode('utf-8')).decode()
sandboxedScript ="""
const {VM} = require('vm2');
const js = Buffer.from('%s','base64').toString()
console.log(new VM().run(js))
"""%js
node_path = path.join(path.dirname(__file__), 'node_modules')
const js = Buffer.from('%s','base64').toString();
const vm = new VM({
sandbox: %s
});
const res = vm.run(js);
console.log(res);
"""%(js,sandbox)
node_path = os.path.join(path.dirname(__file__), 'node_modules')
output = subprocess.check_output(['node', '-e', sandboxedScript], env={'NODE_PATH': node_path})
return output.decode('utf-8')

Expand Down

0 comments on commit 6c3fe34

Please sign in to comment.