WORK! fetch token, start and stop server

This commit is contained in:
loveyousomuch 2021-10-28 00:06:18 +03:00
parent 671a71d5cd
commit 414f63b40b
4 changed files with 65 additions and 86 deletions

44
js2py_test.py Executable file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env python3
import base64
import js2py
# Emulate 'atob' function
#print(base64.standard_b64decode('MmlYaDVXNXVFWXE1ZldKSWF6UTY='))
# Test cases
tests = [
"""(() => {window[("A" + "J" + "AX_T" + "OKE" + "N")]=("2iXh5W5u" + "EYq" + "5fWJIa" + "zQ6");})();""",
""" (() => {window[["N","TOKE","AJAX_"].reverse().join('')]=["IazQ6","fWJ","h5W5uEYq5","2iX"].reverse().join('');})();""",
"""(() => {window["AJAX_TOKEN"] = atob("SGVsbG8sIHdvcmxk")})();""",
"""(() => {window[atob('QUpBWF9UT0tFTg==')]=atob('MmlYaDVXNXVFWXE1ZldKSWF6UTY=');})();""",
"""(() => {window["AJAX_TOKEN"] = "1234" })();""",
"""(() => {window[atob('QUpBWF9UT0tFTg==')]="2iXh5W5uEYq5fWJIazQ6";})();""",
]
# Array function to ECMAScript 5.1
def code(f):
return "(function() { " + f[f.index("{")+1 : f.index("}")] + "})();"
# Emulation atob V8
def atob(arg):
return base64.standard_b64decode(str(arg)).decode("utf-8")
presettings = """
let window = {};
"""
ctx = js2py.EvalJs({ 'atob': atob })
'''
ctx.execute(presettings + code(tests[3]))
print(ctx.window)
'''
for f in tests:
try:
c = code(f)
ctx.execute(presettings + c)
print(ctx.window['AJAX_TOKEN'])
except Exception as e:
print(c, '\n', e)

View file

@ -8,18 +8,22 @@ from typing import Optional, Union
from . import aterrors
#TEST
from py_mini_racer import MiniRacer
# TEST
import js2py
import base64
presettings = """
let window = {1: null, 2: null, AJAX_TOKEN: null};
let i = 1;
function __log() { return {win_var: window["AJAX_TOKEN"], 1: window[1], 2: window[2]} };
function atob(arg) {window[i++] = arg;};
"""
postsettings = """__log();"""
# Set obj for js
presettings = """
let window = {};
"""
# Convert array function to CMAScript 5 function
def toECMAScript5Function(f):
return "(function() { " + f[f.index("{")+1 : f.index("}")] + "})();"
# Emulation of atob - https://developer.mozilla.org/en-US/docs/Web/API/atob
def atob(s):
return base64.standard_b64decode(str(s)).decode("utf-8")
REQGET = 0
REQPOST = 1
@ -45,41 +49,17 @@ class AternosConnect:
# fetch text
pagehead = pagetree.head
text = pagehead.text_content()
#print(text)
#search
token_js_func = text[
text.index("const COOKIE_PREFIX = \"ATERNOS\";") +
len("const COOKIE_PREFIX = \"ATERNOS\";") :
text.index("(function(i,s,o,g,r,a,m)")
].strip()
print(token_js_func)
js_funcs = re.findall(r"\(\(\)(.*?)\)\(\);", text)
token_js_func = js_funcs[1] if len(js_funcs) > 1 else js_funcs[0]
# run js
ctx = MiniRacer()
result = ctx.eval(presettings + token_js_func)
result = ctx.call('__log')
print(result)
if 'win_var' in result and result['win_var']:
result = result['win_var']
elif '1' in result and ('2' in result and not result['2']):
result = base64.standard_b64decode(result['1'])
else:
result = base64.standard_b64decode(result['2'])
print(result)
self.token = result
"""
self.token = re.search(
r'const\s+AJAX_TOKEN\s*=\s*["\'](\w+)["\']',
text
)[1]
"""
ctx = js2py.EvalJs({ 'atob': atob })
jsf = toECMAScript5Function(token_js_func)
ctx.execute(presettings + jsf)
self.token = ctx.window['AJAX_TOKEN']
except (IndexError, TypeError):
raise aterrors.AternosCredentialsError(
'Unable to parse TOKEN from the page'

View file

@ -1,45 +0,0 @@
#!/usr/bin/env python3
from py_mini_racer import MiniRacer
import base64
# Set function for manage global vars
presettings = """
let window = {1: null, 2: null, AJAX_TOKEN: null};
let i = 1;
function __log() { return {win_var: window["AJAX_TOKEN"], 1: window[1], 2: window[2]} };
function atob(arg) {window[i++] = arg;};
"""
# Test cases
tests = [
"""(() => {window[("A" + "J" + "AX_T" + "OKE" + "N")]=("2iXh5W5u" + "EYq" + "5fWJIa" + "zQ6");})();""",
""" (() => {window[["N","TOKE","AJAX_"].reverse().join('')]=["IazQ6","fWJ","h5W5uEYq5","2iX"].reverse().join('');})();""",
"""(() => {window["AJAX_TOKEN"] = atob("SGVsbG8sIHdvcmxk")})();""",
"""(() => {window[atob('QUpBWF9UT0tFTg==')]=atob('MmlYaDVXNXVFWXE1ZldKSWF6UTY=');})();""",
"""(() => {window["AJAX_TOKEN"] = "1234" })();""",
"""(() => {window[atob('QUpBWF9UT0tFTg==')]="2iXh5W5uEYq5fWJIazQ6";})();""",
]
# Emulate 'atob' function
#print(base64.standard_b64decode('MmlYaDVXNXVFWXE1ZldKSWF6UTY='))
for js in tests:
ctx = MiniRacer()
result = ctx.eval(presettings + js)
result = ctx.call('__log')
print(result)
'''
if 'win_var' in result and result['win_var']:
result = result['win_var']
elif '1' in result and ('2' in result and not result['2']):
result = base64.standard_b64decode(result['1'])
else:
result = base64.standard_b64decode(result['2'])
'''
print('Case:\n', js, '\n')
print('Result: \n', result, '\n')
print('-' * 30, '\n')

View file

@ -1,4 +1,4 @@
lxml==4.6.2
requests==2.25.1
cloudscraper==1.2.58
py-mini-racer==0.6.0
Js2Py==0.71