Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eval #491

Open
wants to merge 1 commit into
base: javascript_sandbox
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "{}"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be transparent? We can't assume the next developer (who's going to use eval_in_node for implementing a provider) to know how to construct a sandbox string.

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