-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcloudflare.class.php
198 lines (176 loc) · 4.67 KB
/
cloudflare.class.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Stack\Bypass;
/**
* Require the composer packages
*/
require "vendor/autoload.php";
/**
* @package CloudFlare
* @author Stack
*/
class CloudFlare
{
/**
* $target stores the string of the URL in question
* @var string
*/
private $target;
/**
* $client stores an instance of \GuzzleHttp\Client
* @var [type]
*/
private $client;
/**
* $client stores an instance of \GuzzleHttp\Request
* @var \GuzzleHttp\Request
*/
private $request;
/**
* $cookieJar stores an instance of \GuzzleHttp\Cookie\FileCookieJar
* @var \GuzzleHttp\Cookie\FileCookieJar
*/
private $cookieJar;
/**
* $cookieExists for use when calculating if we should get the cookie again
* @var [type]
*/
private $cookieExists = false;
/**
* WAIT_RESPONSE_CODE this is the response code which CloudFlare throws when UAM is active
* @var int
*/
const WAIT_RESPONSE_CODE = 503;
/**
* SERVER_NAME name of the server which CloudFlare uses
* @var string
*/
const SERVER_NAME = "cloudflare-nginx";
/**
* USER_AGENT user agent to use in the requests
* @var string
*/
const USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36";
/**
* REFRESH_EXPRESSION regular expression used to parse the "Refresh" header
* @var string
*/
const REFRESH_EXPRESSION = "/8;URL=(\/cdn-cgi\/l\/chk_jschl\?pass=[0-9]+\.[0-9]+-.*)/";
/**
* __construct - constructor for the class
* @param \GuzzleHttp\Cookie\FileCookieJar $response instance of response class
* @return bool protected or not
*/
public function __construct($targetSite, $cookies = [false, "cookie.txt"])
{
$this->target = $targetSite;
$config = [
"cookies" => true,
"http_errors" => false
];
if(isset($cookies[0]) && $cookies[0])
{
if(empty($cookies[1]))
return;
$file = $cookies[1];
$this->cookieJar = new \GuzzleHttp\Cookie\FileCookieJar($file);
$config = [
"cookies" => $this->cookieJar,
"http_errors" => false
];
if(file_exists($file))
{
$this->cookieExists = true;
$this->cookieJar->load($file);
}
}
$this->client = new \GuzzleHttp\Client($config);
$this->initialRequest();
}
/**
* verifyPage - this will verify that the page is protected by CloudFlare
* @param \GuzzleHttp\Client $response instance of response class
* @return bool protected or not
*/
private function verifyPage($response)
{
return ($response->getHeader("Server")[0] == self::SERVER_NAME && $response->getStatusCode() == self::WAIT_RESPONSE_CODE);
}
/**
* getCookie will retrive the cookie for bypassing
*/
private function getCookie()
{
$refreshHeader = $this->request->getHeader("Refresh")[0];
$followLocation = $this->target.$this->parseRefresh($refreshHeader);
$data = $this->getHeaderData();
$data['Referer'] = $this->target;
$this->request = $this->client->request(
"GET",
$followLocation,
$data
);
}
/**
* initialRequest this does the inital request, makes sure the page is CloudFlare et.c
*/
private function initialRequest()
{
$this->request = $this->client->request(
"GET",
$this->target,
$this->getHeaderData()
);
if(!$this->cookieExists)
{
if(!$this->verifyPage($this->request))
{
throw new \Exception("This website is not protected by CloudFlare or the UAM is not enabled", 1);
}
sleep(8);
$this->getCookie();
}
}
/**
* parseRefresh parses the "Refresh" header
* @param string $header "Refresh: X"
* @return string parsed URI
*/
private function parseRefresh($header)
{
$matchURL = preg_match(self::REFRESH_EXPRESSION, $header, $match);
if($matchURL)
{
return $match[1];
}
throw new \Exception("Can not seem to parse the refresh header", 2);
}
/**
* get does a GET request to the specified URI
* @param string $uri location
* @return string body of the request
*/
public function get($uri)
{
$this->request = $this->client->request(
"GET",
$this->target,
$this->getHeaderData(),
[
'allow_redirects' => true
]
);
return $this->request->getBody();
}
/*
* getHeaderData returns the header data for the requests
*/
private function getHeaderData()
{
return [
"User-Agent" => self::USER_AGENT,
"Accept" => "*/*",
"Accept-Encoding" => "gzip, deflate, sdch",
"Accept-Language" => "en-GB,en-US;q=0.8,en;q=0.6"
];
}
}