-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
100 lines (93 loc) · 3.81 KB
/
main.py
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
import asyncio
import os
import json
PYPPETEER_CHROMIUM_REVISION = '1263111'
os.environ['PYPPETEER_CHROMIUM_REVISION'] = PYPPETEER_CHROMIUM_REVISION
from pyppeteer import launch
import time
import requests
apikey = "2captcha api key"
async def main():
browser = await launch(headless=False, devtools=True, autoClose=False, args=['--no-sandbox', '--disable-setuid-sandbox'])
page = await browser.newPage()
# Executing the javascript code on the page
await page.evaluateOnNewDocument(
"""
() => {
console.clear = () => console.log('Console was cleared')
const i = setInterval(() => {
if (window.turnstile) {
clearInterval(i)
window.turnstile.render = (a, b) => {
let params = {
sitekey: b.sitekey,
pageurl: window.location.href,
data: b.cData,
pagedata: b.chlPageData,
action: b.action,
userAgent: navigator.userAgent,
json: 1
}
// we will intercept the message in puppeeter
console.log('intercepted-params:' + JSON.stringify(params))
window.cfCallback = b.callback
return
}
}
}, 50)
}
"""
)
# Intercept console messages to catch a message containing 'intercepted-params:'
async def console_message_handler(msg):
print(f"Dialog message: {msg}")
txt = msg.text
print(txt)
if 'intercepted-params:' in txt:
params = json.loads(txt.replace('intercepted-params:', ''))
print(params)
try:
# Captcha params
payload = {
"key": apikey,
"method": "turnstile",
"sitekey": params["sitekey"],
"pageurl": params["pageurl"],
"data": params["data"],
"pagedata": params["pagedata"],
"action": params["action"],
"useragent": params["userAgent"],
"json": 1,
}
# Send captcha to 2captcha
response = requests.post(f"https://2captcha.com/in.php?", data=payload)
print("Captcha sent")
print(response.text)
captcha_id = response.json()["request"]
time.sleep(2)
# Getting a captcha response
while True:
solution = requests.get(f"https://2captcha.com/res.php?key={apikey}&action=get&json=1&id={captcha_id}").json()
if solution["request"] == "CAPCHA_NOT_READY":
print(solution["request"])
time.sleep(1)
elif "ERROR" in solution["request"]:
print(solution["request"])
else:
print(solution)
break
# Use the received captcha response. Pass the answer to the configured callback function `cfCallback`
await page.evaluate('cfCallback', solution["request"])
except Exception as e:
print(e)
await browser.close()
else:
return
# Watch the console
page.on('console', lambda msg: asyncio.ensure_future(console_message_handler(msg)))
# Open target page
# Just change this URL to the target page you want
await page.goto('https://2captcha.com/demo/cloudflare-turnstile-challenge')
# Create an asyncio event loop and run function main()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())