| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import ast |
| 4 |
import re |
| 5 |
import operator as op |
| 6 |
import pyparsing |
| 7 |
|
| 8 |
from ..exceptions import CloudflareSolveError |
| 9 |
from . import JavaScriptInterpreter |
| 10 |
|
| 11 |
# ------------------------------------------------------------------------------- # |
| 12 |
|
| 13 |
_OP_MAP = { |
| 14 |
ast.Add: op.add, |
| 15 |
ast.Sub: op.sub, |
| 16 |
ast.Mult: op.mul, |
| 17 |
ast.Div: op.truediv, |
| 18 |
ast.Invert: op.neg, |
| 19 |
} |
| 20 |
|
| 21 |
# ------------------------------------------------------------------------------- # |
| 22 |
|
| 23 |
|
| 24 |
class Calc(ast.NodeVisitor): |
| 25 |
|
| 26 |
def visit_BinOp(self, node): |
| 27 |
return _OP_MAP[type(node.op)](self.visit(node.left), self.visit(node.right)) |
| 28 |
|
| 29 |
# ------------------------------------------------------------------------------- # |
| 30 |
|
| 31 |
def visit_Num(self, node): |
| 32 |
return node.n |
| 33 |
|
| 34 |
# ------------------------------------------------------------------------------- # |
| 35 |
|
| 36 |
def visit_Expr(self, node): |
| 37 |
return self.visit(node.value) |
| 38 |
|
| 39 |
# ------------------------------------------------------------------------------- # |
| 40 |
|
| 41 |
@classmethod |
| 42 |
def doMath(cls, expression): |
| 43 |
tree = ast.parse(expression) |
| 44 |
calc = cls() |
| 45 |
return calc.visit(tree.body[0]) |
| 46 |
|
| 47 |
# ------------------------------------------------------------------------------- # |
| 48 |
|
| 49 |
|
| 50 |
class Parentheses(object): |
| 51 |
|
| 52 |
def fix(self, s): |
| 53 |
res = [] |
| 54 |
self.visited = set([s]) |
| 55 |
self.dfs(s, self.invalid(s), res) |
| 56 |
return res |
| 57 |
|
| 58 |
# ------------------------------------------------------------------------------- # |
| 59 |
|
| 60 |
def dfs(self, s, n, res): |
| 61 |
if n == 0: |
| 62 |
res.append(s) |
| 63 |
return |
| 64 |
for i in range(len(s)): |
| 65 |
if s[i] in ['(', ')']: |
| 66 |
s_new = s[:i] + s[i + 1:] |
| 67 |
if s_new not in self.visited and self.invalid(s_new) < n: |
| 68 |
self.visited.add(s_new) |
| 69 |
self.dfs(s_new, self.invalid(s_new), res) |
| 70 |
|
| 71 |
# ------------------------------------------------------------------------------- # |
| 72 |
|
| 73 |
def invalid(self, s): |
| 74 |
plus = minus = 0 |
| 75 |
memo = {"(": 1, ")": -1} |
| 76 |
for c in s: |
| 77 |
plus += memo.get(c, 0) |
| 78 |
minus += 1 if plus < 0 else 0 |
| 79 |
plus = max(0, plus) |
| 80 |
return plus + minus |
| 81 |
|
| 82 |
# ------------------------------------------------------------------------------- # |
| 83 |
|
| 84 |
|
| 85 |
class ChallengeInterpreter(JavaScriptInterpreter): |
| 86 |
|
| 87 |
def __init__(self): |
| 88 |
super(ChallengeInterpreter, self).__init__('native') |
| 89 |
|
| 90 |
# ------------------------------------------------------------------------------- # |
| 91 |
|
| 92 |
def eval(self, body, domain): |
| 93 |
|
| 94 |
operators = { |
| 95 |
'+': op.add, |
| 96 |
'-': op.sub, |
| 97 |
'*': op.mul, |
| 98 |
'/': op.truediv |
| 99 |
} |
| 100 |
|
| 101 |
# ------------------------------------------------------------------------------- # |
| 102 |
|
| 103 |
def flatten(lists): |
| 104 |
return sum(map(flatten, lists), []) if isinstance(lists, list) else [lists] |
| 105 |
|
| 106 |
# ------------------------------------------------------------------------------- # |
| 107 |
|
| 108 |
def jsfuckToNumber(jsFuck): |
| 109 |
# "Clean Up" JSFuck |
| 110 |
jsFuck = jsFuck.replace('!+[]', '1').replace('!![]', '1').replace('[]', '0') |
| 111 |
jsFuck = jsFuck.lstrip('+').replace('(+', '(').replace(' ', '') |
| 112 |
jsFuck = Parentheses().fix(jsFuck)[0] |
| 113 |
|
| 114 |
# Hackery Parser for Math |
| 115 |
stack = [] |
| 116 |
bstack = [] |
| 117 |
|
| 118 |
for i in flatten(pyparsing.nestedExpr().parseString(jsFuck).asList()): |
| 119 |
if i == '+': |
| 120 |
stack.append(bstack) |
| 121 |
bstack = [] |
| 122 |
continue |
| 123 |
bstack.append(i) |
| 124 |
stack.append(bstack) |
| 125 |
|
| 126 |
return int(''.join([str(Calc.doMath(''.join(i))) for i in stack])) |
| 127 |
|
| 128 |
# ------------------------------------------------------------------------------- # |
| 129 |
|
| 130 |
def divisorMath(payload, needle, domain): |
| 131 |
jsfuckMath = payload.split('/') |
| 132 |
if needle in jsfuckMath[1]: |
| 133 |
expression = re.findall(r"^(.*?)(.)\(function", jsfuckMath[1])[0] |
| 134 |
|
| 135 |
expression_value = operators[expression[1]]( |
| 136 |
float(jsfuckToNumber(expression[0])), |
| 137 |
float(ord(domain[jsfuckToNumber(jsfuckMath[1][ |
| 138 |
jsfuckMath[1].find('"("+p+")")}') + len('"("+p+")")}'):-2 |
| 139 |
])])) |
| 140 |
) |
| 141 |
else: |
| 142 |
expression_value = jsfuckToNumber(jsfuckMath[1]) |
| 143 |
|
| 144 |
expression_value = jsfuckToNumber(jsfuckMath[0]) / float(expression_value) |
| 145 |
|
| 146 |
return expression_value |
| 147 |
|
| 148 |
# ------------------------------------------------------------------------------- # |
| 149 |
|
| 150 |
def challengeSolve(body, domain): |
| 151 |
jschl_answer = 0 |
| 152 |
|
| 153 |
try: |
| 154 |
jsfuckChallenge = re.search( |
| 155 |
r"setTimeout\(function\(\){\s+var.*?f,\s*(?P<variable>\w+).*?:(?P<init>\S+)};" |
| 156 |
r".*?\('challenge-form'\);.*?;(?P<challenge>.*?a\.value)\s*=\s*\S+\.toFixed\(10\);", |
| 157 |
body, |
| 158 |
re.DOTALL | re.MULTILINE |
| 159 |
).groupdict() |
| 160 |
except AttributeError: |
| 161 |
raise CloudflareSolveError('There was an issue extracting "jsfuckChallenge" from the Cloudflare challenge.') |
| 162 |
|
| 163 |
kJSFUCK = re.search(r'(;|)\s*k.=(?P<kJSFUCK>\S+);', jsfuckChallenge['challenge'], re.S | re.M) |
| 164 |
if kJSFUCK: |
| 165 |
try: |
| 166 |
kJSFUCK = jsfuckToNumber(kJSFUCK.group('kJSFUCK')) |
| 167 |
except IndexError: |
| 168 |
raise CloudflareSolveError('There was an issue extracting "kJSFUCK" from the Cloudflare challenge.') |
| 169 |
|
| 170 |
try: |
| 171 |
kID = re.search(r"\s*k\s*=\s*'(?P<kID>\S+)';", body).group('kID') |
| 172 |
except IndexError: |
| 173 |
raise CloudflareSolveError('There was an issue extracting "kID" from the Cloudflare challenge.') |
| 174 |
|
| 175 |
try: |
| 176 |
r = re.compile(r'<div id="{}(?P<id>\d+)">\s*(?P<jsfuck>[^<>]*)</div>'.format(kID)) |
| 177 |
|
| 178 |
kValues = {} |
| 179 |
for m in r.finditer(body): |
| 180 |
kValues[int(m.group('id'))] = m.group('jsfuck') |
| 181 |
|
| 182 |
jsfuckChallenge['k'] = kValues[kJSFUCK] |
| 183 |
except (AttributeError, IndexError): |
| 184 |
raise CloudflareSolveError('There was an issue extracting "kValues" from the Cloudflare challenge.') |
| 185 |
|
| 186 |
jsfuckChallenge['challenge'] = re.finditer( |
| 187 |
r'{}.*?([+\-*/])=(.*?);(?=a\.value|{})'.format( |
| 188 |
jsfuckChallenge['variable'], |
| 189 |
jsfuckChallenge['variable'] |
| 190 |
), |
| 191 |
jsfuckChallenge['challenge'] |
| 192 |
) |
| 193 |
|
| 194 |
# ------------------------------------------------------------------------------- # |
| 195 |
|
| 196 |
if '/' in jsfuckChallenge['init']: |
| 197 |
val = jsfuckChallenge['init'].split('/') |
| 198 |
jschl_answer = jsfuckToNumber(val[0]) / float(jsfuckToNumber(val[1])) |
| 199 |
else: |
| 200 |
jschl_answer = jsfuckToNumber(jsfuckChallenge['init']) |
| 201 |
|
| 202 |
# ------------------------------------------------------------------------------- # |
| 203 |
|
| 204 |
for expressionMatch in jsfuckChallenge['challenge']: |
| 205 |
oper, expression = expressionMatch.groups() |
| 206 |
|
| 207 |
if '/' in expression: |
| 208 |
expression_value = divisorMath(expression, 'function(p)', domain) |
| 209 |
else: |
| 210 |
if 'Element' in expression: |
| 211 |
expression_value = divisorMath(jsfuckChallenge['k'], '"("+p+")")}', domain) |
| 212 |
else: |
| 213 |
expression_value = jsfuckToNumber(expression) |
| 214 |
|
| 215 |
jschl_answer = operators[oper](jschl_answer, expression_value) |
| 216 |
|
| 217 |
# ------------------------------------------------------------------------------- # |
| 218 |
|
| 219 |
# if not jsfuckChallenge['k'] and '+ t.length' in body: |
| 220 |
# jschl_answer += len(domain) |
| 221 |
|
| 222 |
# ------------------------------------------------------------------------------- # |
| 223 |
|
| 224 |
return '{0:.10f}'.format(jschl_answer) |
| 225 |
|
| 226 |
# ------------------------------------------------------------------------------- # |
| 227 |
|
| 228 |
return challengeSolve(body, domain) |
| 229 |
|
| 230 |
# ------------------------------------------------------------------------------- # |
| 231 |
|
| 232 |
|
| 233 |
ChallengeInterpreter() |