forked from VeNoMouS/cloudscraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
390 additions
and
719 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exclude_paths: | ||
- tests/* | ||
- README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
[run] | ||
source = cloudscraper | ||
omit = | ||
*test* | ||
cloudscraper/interpreters/jsfuck.py | ||
omit = tests/*,cloudscraper/interpreters/jsfuck.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[pytest] | ||
addopts = -p no:warnings | ||
timeout = 2000 | ||
env = PYTHONHASHSEED=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,118 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import hashlib | ||
import responses | ||
import pytest | ||
import re | ||
|
||
from requests.compat import urlencode | ||
from collections import OrderedDict | ||
from os import path | ||
from io import open | ||
|
||
try: | ||
from urlparse import parse_qsl | ||
except ImportError: | ||
from urllib.parse import parse_qsl | ||
|
||
# Fake URL, network requests are not allowed by default when using the decorator | ||
url = 'https://example-site.dev' | ||
url = 'http://www.evildomain.com' | ||
|
||
# These kwargs will be passed to tests by the decorator | ||
cloudscraper_kwargs = dict( | ||
delay=0.01, | ||
debug=False | ||
) | ||
cloudscraper_kwargs = dict(delay=0.01, debug=False) | ||
|
||
# Cloudflare challenge fixtures are only read from the FS once | ||
cache = {} | ||
|
||
|
||
class ChallengeResponse(responses.Response): | ||
"""Simulates a standard IUAM JS challenge response from Cloudflare | ||
This would be the first response in a test. | ||
Kwargs: | ||
Keyword arguments used to override the defaults. | ||
The request will error if it doesn't match a defined response. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
defaults = ( | ||
('method', 'GET'), | ||
('status', 503), | ||
('headers', {'Server': 'cloudflare'}), | ||
('content_type', 'text/html') | ||
) | ||
|
||
for k, v in defaults: | ||
kwargs.setdefault(k, v) | ||
|
||
super(ChallengeResponse, self).__init__(**kwargs) | ||
|
||
|
||
class RedirectResponse(responses.CallbackResponse): | ||
"""Simulate the redirect response that occurs after sending a correct answer | ||
This would be the second response in a test. | ||
It will call the provided callback when a matching request is received. | ||
Afterwards, the default is to redirect to the index page "/" aka fake URL. | ||
Kwargs: | ||
Keyword arguments used to override the defaults. | ||
The request will error if it doesn't match a defined response. | ||
""" | ||
|
||
def __init__(self, callback=lambda request: None, **kwargs): | ||
defaults = ( | ||
('method', 'GET'), | ||
('status', 302), | ||
('headers', {'Location': '/'}), | ||
('content_type', 'text/html'), | ||
('body', '') | ||
) | ||
|
||
for k, v in defaults: | ||
kwargs.setdefault(k, v) | ||
|
||
args = tuple(kwargs.pop(k) for k in ('status', 'headers', 'body')) | ||
kwargs['callback'] = lambda request: callback(request) or args | ||
|
||
super(RedirectResponse, self).__init__(**kwargs) | ||
|
||
|
||
class DefaultResponse(responses.Response): | ||
"""Simulate the final response after the challenge is solved | ||
This would be the last response in a test and normally occurs after a redirect. | ||
Kwargs: | ||
Keyword arguments used to override the defaults. | ||
The request will error if it doesn't match a defined response. | ||
""" | ||
|
||
def __init__(self, **kwargs): | ||
defaults = ( | ||
('method', 'GET'), | ||
('status', 200), | ||
('content_type', 'text/html') | ||
) | ||
|
||
for k, v in defaults: | ||
kwargs.setdefault(k, v) | ||
|
||
super(DefaultResponse, self).__init__(**kwargs) | ||
# ------------------------------------------------------------------------------- # | ||
|
||
|
||
def fixtures(filename): | ||
"""Read and cache a challenge fixture | ||
""" | ||
Read and cache a challenge fixture | ||
Returns: HTML (bytes): The HTML challenge fixture | ||
""" | ||
if not cache.get(filename): | ||
with open(path.join(path.dirname(__file__), 'fixtures', filename), 'rb') as fp: | ||
print('reading...') | ||
with open(path.join(path.dirname(__file__), 'fixtures', filename), 'r') as fp: | ||
cache[filename] = fp.read() | ||
return cache[filename] | ||
|
||
|
||
# This is the page that should be received after bypassing the JS challenge. | ||
requested_page = fixtures('requested_page.html') | ||
# ------------------------------------------------------------------------------- # | ||
|
||
|
||
# This fancy decorator wraps tests so the responses will be mocked. | ||
# It could be called directly e.g. challenge_responses(*args)(test_func) -> wrapper | ||
def challenge_responses(filename, jschl_answer): | ||
# This function is called with the test_func and returns a new wrapper. | ||
def challenge_responses_decorator(test): | ||
def mockCloudflare(fixture, payload): | ||
def responses_decorator(test): | ||
@responses.activate | ||
def wrapper(self, interpreter): | ||
html = fixtures(filename).decode('utf-8') | ||
|
||
params = OrderedDict(re.findall(r'name="(s|jschl_vc|pass)"\svalue="(\S+)"', html)) | ||
params['jschl_answer'] = jschl_answer | ||
|
||
submit_uri = '{}/cdn-cgi/l/chk_jschl?{}'.format(url, urlencode(params)) | ||
|
||
responses.add(ChallengeResponse(url=url, body=fixtures(filename))) | ||
|
||
def onRedirect(request): | ||
# We don't register the last response unless the redirect occurs | ||
responses.add(DefaultResponse(url=url, body=requested_page)) | ||
|
||
responses.add(RedirectResponse(url=submit_uri, callback=onRedirect)) | ||
|
||
return test(self, interpreter=interpreter, **cloudscraper_kwargs) | ||
# The following causes pytest to call the test wrapper once for each interpreter. | ||
return pytest.mark.parametrize('interpreter', ['js2py', 'nodejs'])(wrapper) | ||
|
||
return challenge_responses_decorator | ||
def wrapper(self): | ||
def post_callback(request): | ||
postPayload = dict(parse_qsl(request.body)) | ||
postPayload['r'] = hashlib.sha256(postPayload.get('r', '').encode('ascii')).hexdigest() | ||
|
||
for param in payload: | ||
if param not in postPayload or postPayload[param] != payload[param]: | ||
return ( | ||
503, | ||
{'Server': 'cloudflare'}, | ||
fixtures(fixture) | ||
) | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
return ( | ||
200, | ||
[ | ||
( | ||
'Set-Cookie', '__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; Domain=.evildomain.com; path=/' | ||
), | ||
( | ||
'Set-Cookie', | ||
'__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; domain=.evildomain.com; path=/' | ||
), | ||
('Server', 'cloudflare') | ||
], | ||
'Solved OK' | ||
) | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
def challengeCallback(request): | ||
status_code = 503 | ||
|
||
if 'reCaptcha' in fixture or '1020' in fixture: | ||
status_code = 403 | ||
return ( | ||
status_code, | ||
[ | ||
( | ||
'Set-Cookie', | ||
'__cfduid=d5927a7cbaa96ec536939f93648e3c08a1576098703; Domain=.evildomain.com; path=/' | ||
), | ||
('Server', 'cloudflare') | ||
], | ||
fixtures(fixture) | ||
) | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
responses.add_callback( | ||
responses.POST, | ||
url, | ||
callback=post_callback, | ||
content_type='text/html', | ||
) | ||
|
||
responses.add_callback( | ||
responses.GET, | ||
url, | ||
callback=challengeCallback, | ||
content_type='text/html', | ||
) | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
return test(self, **cloudscraper_kwargs) | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
return wrapper | ||
|
||
# ------------------------------------------------------------------------------- # | ||
|
||
return responses_decorator |
Oops, something went wrong.