This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare.php
144 lines (123 loc) · 3.85 KB
/
cloudflare.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
class CloudflareSolver {
private $html;
private $url;
function __construct($url, $html) {
// $url must contain scheme for parse_url
if(preg_match('/^(?:[a-zA-Z0-9+\-\.]+?:)?\/\//', $url, $matches) === 0) {
$url = 'http://' . $url;
}
$this->url = parse_url($url);
$this->html = $html;
}
function getTimeout() {
return (preg_match('/}, (\d+)\);/', $this->html, $matches) === 1) ? $matches[1] : 4000;
}
function getSolvedUrl() {
$query = [];
// Read challenge values from challenge-form
if(preg_match('/name="jschl_vc" value="([^"]+)"/', $this->html, $matches) !== 1) {
return false;
}
$query['jschl_vc'] = $matches[1];
if(preg_match('/name="pass" value="([^"]+)"/', $this->html, $matches) !== 1) {
return false;
}
$query['pass'] = $matches[1];
// Solve the challenge
$query['jschl_answer'] = $this->solveJsChallenge();
return $query['jschl_answer']
? $this->url['scheme'] . '://' . $this->url['host'] . '/cdn-cgi/l/chk_jschl?' . http_build_query($query)
: false;
}
function isValid() {
// Check if page is likely to be a Cloudflare challenge
return strpos($this->html, '/cdn-cgi/l/chk_jschl') !== false
&& strpos($this->html, 'challenge-form') !== false
&& strpos($this->html, 'jschl_vc') !== false
&& strpos($this->html, 'jschl_answer') !== false;
}
function solveJsChallenge() {
// Find the initial value
if(preg_match('/{"\w+":([^}]+)};/', $this->html, $matches) !== 1) {
return false;
}
$challenge = self::decodeJsInt($matches[1]);
// Find all instructions to apply
if(preg_match_all('/([+\-*\/%])=([^ .;]+);/', $this->html, $matches, PREG_SET_ORDER) === FALSE) {
return false;
}
foreach($matches as $match) {
// Decode each expression and apply the instruction
$op = $match[1];
$number = self::decodeJsInt($match[2]);
$challenge = self::jsMath($op, $challenge, $number);
}
// Limit to 10 decimals
$challenge = round($challenge, 10);
// Return challenge response and length of the domain
return $challenge + strlen($this->url['host']);
}
private static function decodeJsInt($jsInt) {
if(preg_match_all("/([\/])?\+\(((?:\([^);\/]+\)\+?)+)\)/", $jsInt, $matches) > 0) {
// Found outer layer, may contain multiple ops
$opCount = count($matches[0]);
$preOps = [];
$ints = [];
for($i = 0; $i < $opCount; $i++) {
$preOps[] = $matches[1][$i];
$ints[] = self::decodeJsInt($matches[2][$i]);
}
$finalNum = '0';
for($i = 0; $i < $opCount; $i++) {
if(preg_match('/[+\-*\/%]/', $preOps[$i]) === 1) {
$finalNum = self::jsMath($preOps[$i], $ints[$i - 1], $ints[$i]);
}
}
return $finalNum;
}
else if(preg_match_all('/\([^);]+\)\+?/', $jsInt, $matches) !== FALSE) {
// Found numbers to concatenate
// Make sure we consume everything we match
$matchLen = 0;
foreach($matches[0] as $match)
$matchLen += strlen($match);
self::checkValid(strlen($jsInt) === $matchLen);
// Concatenate numbers as a string
$finalNum = '';
$opCount = count($matches[0]);
for($i = 0; $i < $opCount; $i++) {
// Only count the expressions that equals '1'
$finalNum .= substr_count($matches[0][$i], '+!![]') + substr_count($matches[0][$i], '!+[]');
}
return $finalNum;
} else {
// ???
self::checkValid(false);
}
}
private static function jsMath($op, $a, $b) {
// Use same precision as JS
$oldPrecision = ini_set('precision', 17);
$a = (float)$a;
$b = (float)$b;
switch($op) {
case '+': $a += $b; break;
case '-': $a -= $b; break;
case '*': $a *= $b; break;
case '/': $a /= $b; break;
case '%': $a %= $b; break;
default: self::checkValid(false);
}
$a = (string)$a;
ini_set('precision', $oldPrecision);
return $a;
}
private static function checkValid($expression) {
if(!$expression) {
echo 'Assertion failed.' . PHP_EOL;
debug_print_backtrace();
die();
}
}
}