-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.py
295 lines (277 loc) · 10.8 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import argparse
import os
import time
import random
import requests
import re
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.firefox import GeckoDriverManager
from openai import OpenAI
load_dotenv()
def average_of_array(arr):
if not arr:
return 0 # Handle edge case of empty array
sum_elements = sum(arr)
average = sum_elements / len(arr)
return average - 5
def upload_image_to_imgur(image_path):
url = "https://api.imgur.com/3/image"
client_id = os.getenv("IMGUR_CLIENT_ID")
headers = {
"Authorization": f"Client-ID {client_id}"
}
with open(image_path, "rb") as image_file:
payload = {
"image": image_file.read(),
"type": "file"
}
response = requests.post(url, headers=headers, files=payload)
if response.status_code == 200:
data = response.json()
return data['data']['link']
else:
print("Failed to upload image", response.text)
return None
def ask_recaptcha_to_chatgpt(image_url):
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
short_prompt = ("You are an object detection assistant. Understand what is asked on the top instruction of the "
"image. For Example if instruction says select all squares with motorcycle. There are 16 squares "
"give number for each square 0 to 15. The numbers starts from top left to right. Then answer only "
"with the square numbers like 1-3-5-6."
"Understand the instruction and give me the highest probability squares as answer. Give only the correct square numbers.")
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": short_prompt},
{"role": "user", "content": image_url}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
def ask_text_to_chatgpt(image_url):
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
short_prompt = ("Act as a blind person assistant. Read the text from the image and give me only the text answer.")
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": short_prompt
}
]
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url
}
},
{
"type": "text",
"text": "Understand what is asked on the top instruction of the image. Give me the only correct squares with highest probability"
}
]
},
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
def ask_slide_to_chatgpt(image_url):
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
short_prompt = ("There is an slider button in the page and there is an empty gray space on puzzle. You should give "
"give me how many pixels should i slide to complete the puzzle. You can simulate the slider for "
"the exact fit before giving the answer.The total width is 210 pixels."
"Give me only number of pixels in integer 50 up to 210 pixels.")
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": short_prompt},
{"role": "user", "content": image_url}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].message.content
def puzzle_test(driver):
driver.get("https://2captcha.com/demo/geetest")
time.sleep(5)
button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "geetest_radar_tip"))
)
button.click()
box = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "geetest_embed"))
)
time.sleep(1)
box.screenshot('geetest_box.png')
file_url = upload_image_to_imgur('geetest_box.png')
time.sleep(1)
all_results = []
while True:
slider = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "geetest_slider_button"))
)
time.sleep(2)
action = ActionChains(driver)
input_string = ask_slide_to_chatgpt(file_url)
numbers = re.findall(r'\d+', input_string)
if numbers:
result = int(numbers[0])
else:
result = 0
if result < 110:
result = 90
all_results.append(result)
action.click_and_hold(slider).perform()
time.sleep(random.uniform(0.8, 1.2))
total_offset = average_of_array(all_results)
num_steps = 5
step_offset = total_offset / num_steps
for _ in range(num_steps):
action.move_by_offset(step_offset, 0).perform()
time.sleep(random.uniform(0.05, 0.1))
action.release().perform()
def complicated_text_test(driver):
driver.get("https://2captcha.com/demo/mtcaptcha")
time.sleep(5)
iframe = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "mtcaptcha-iframe-1"))
)
iframe.screenshot('captcha_image.png')
file_url = upload_image_to_imgur('captcha_image.png')
print(file_url)
response = ask_text_to_chatgpt(file_url)
print(response)
driver.switch_to.frame(iframe)
input_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "mtcap-noborder.mtcap-inputtext.mtcap-inputtext-custom"))
)
input_field.send_keys(response)
time.sleep(2)
driver.switch_to.default_content()
submit_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "_actionsItem_1f3oo_41._buttonPrimary_d46vc_44._button_d46vc_1._buttonMd_d46vc_34"))
)
submit_button.click()
def text_test(driver):
driver.get("https://2captcha.com/demo/normal")
time.sleep(5)
captcha_image = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "_captchaImage_rrn3u_9"))
)
time.sleep(2)
captcha_image.screenshot('captcha_image.png')
file_url = upload_image_to_imgur('captcha_image.png')
print(file_url)
response = ask_text_to_chatgpt(file_url)
print(response)
input_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "_inputInner_ws73z_12"))
)
input_field.send_keys(response)
submit_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "_actionsItem_1f3oo_41._buttonPrimary_d46vc_44._button_d46vc_1._buttonMd_d46vc_34"))
)
submit_button.click()
time.sleep(5)
driver.quit()
def recaptcha_test(driver):
driver.get("https://patrickhlauke.github.io/recaptcha/")
def handle_recaptcha():
number_of_challenges = 0
recaptcha_frame = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//iframe[@title='reCAPTCHA']"))
)
recaptcha_frame.screenshot('recaptcha_checkbox.png')
driver.switch_to.frame(recaptcha_frame)
checkbox = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "recaptcha-checkbox-border"))
)
checkbox.click()
driver.switch_to.default_content()
time.sleep(2)
array = []
while True:
try:
challenge_iframe = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.XPATH, "//iframe[contains(@title, 'recaptcha challenge expires in two minutes')]"))
)
number_of_challenges += 1
filename = 'recaptcha_challenge_' + str(number_of_challenges) + '.png'
challenge_iframe.screenshot(filename)
image_url = upload_image_to_imgur(filename)
if image_url:
answer = ask_recaptcha_to_chatgpt(image_url)
if "," in answer:
array = answer.split(", ")
elif "-" in answer:
array = answer.split("-")
else:
exit(-1)
driver.switch_to.frame(challenge_iframe)
table = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "rc-imageselect-table-33"))
)
time.sleep(1)
all_images = table.find_elements(By.CLASS_NAME, "rc-image-tile-33")
for each_element in array:
all_images[int(each_element)].click()
submit_button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "recaptcha-verify-button"))
)
submit_button.click()
driver.switch_to.default_content()
time.sleep(5)
if len(driver.find_elements(By.CLASS_NAME, "recaptcha-checkbox-checkmark")) != 0:
break
except Exception as ex:
print(ex)
handle_recaptcha()
time.sleep(5)
driver.quit()
def main():
parser = argparse.ArgumentParser(description="Test various captcha types.")
parser.add_argument('captcha_type', choices=['puzzle', 'text', 'complicated_text', 'recaptcha'],
help="Specify the type of captcha to test")
args = parser.parse_args()
service = FirefoxService(GeckoDriverManager().install())
driver = webdriver.Firefox(service=service)
try:
if args.captcha_type == 'puzzle':
puzzle_test(driver)
elif args.captcha_type == 'text':
text_test(driver)
elif args.captcha_type == 'complicated_text':
complicated_text_test(driver)
elif args.captcha_type == 'recaptcha':
recaptcha_test(driver)
finally:
driver.quit()
if __name__ == "__main__":
main()