驗證碼破解

我们将使用Python编程语言以及Selenium库来模拟人类操作,以破解极验滑动验证码。极验滑动验证码通常用于防止机器人访问,但我们将展示如何绕过它。

第一步:导入必要的库和依赖项

首先,确保你已经安装了Python和Selenium库。如果没有安装,你可以使用pip进行安装。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
from io import BytesIO
import time
第二步:打开目标网站和输入登录信息

我们首先需要打开目标网站并输入登录信息。请将下面的URL和登录信息替换为你自己的。

url = 'https://account.geetest.com/login'
email = 'your_email@example.com'
password = 'your_password'
第三步:模拟点击验证按钮

我们模拟点击验证按钮以触发极验滑动验证码的出现。

browser = webdriver.Chrome()
browser.get(url)

# 输入邮箱和密码
email_field = browser.find_element(By.ID, 'email')
password_field = browser.find_element(By.ID, 'password')
email_field.send_keys(email)
password_field.send_keys(password)

# 查找验证按钮并点击
verify_button = browser.find_element(By.CLASS_NAME, 'geetest_radar_tip')
verify_button.click()
第四步:获取验证码图片

接下来,我们需要获取不带缺口的验证码图片和带缺口的验证码图片。我们将使用Selenium截取屏幕快照,并从中提取验证码图片。

wait = WebDriverWait(browser, 10)

# 等待验证码图片加载完成
wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img')))

# 获取验证码图片的位置和大小
captcha_img = browser.find_element(By.CLASS_NAME, 'geetest_canvas_img')
location = captcha_img.location
size = captcha_img.size

# 截取屏幕快照
screenshot = browser.get_screenshot_as_png()

# 将屏幕快照转换为Image对象
screenshot = Image.open(BytesIO(screenshot))

# 根据位置和大小裁剪出验证码图片
left = int(location['x'])
top = int(location['y'])
right = int(location['x'] + size['width'])
bottom = int(location['y'] + size['height'])
captcha = screenshot.crop((left, top, right, bottom))

# 保存不带缺口的验证码图片
captcha.save('captcha_without_gap.png')
第五步:点击滑块并获取带缺口的验证码图片

现在,我们需要点击滑块来显示带缺口的验证码图片。

# 查找滑块并点击
slider = browser.find_element(By.CLASS_NAME, 'geetest_slider_button')
slider.click()

# 再次截取屏幕快照
screenshot = browser.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))

# 根据位置和大小裁剪出带缺口的验证码图片
captcha = screenshot.crop((left, top, right, bottom))

# 保存带缺口的验证码图片
captcha.save('captcha_with_gap.png')
第六步:识别缺口位置

接下来,我们需要识别验证码图片中的缺口位置。我们可以通过对比不带缺口和带缺口的图片,找到像素点的差异。


def is_pixel_different(image1, image2, x, y):
    pixel1 = image1.getpixel((x, y))
    pixel2 = image2.getpixel((x, y))
    threshold = 60  # 设置一个像素差异阈值
    return abs(pixel1[0] - pixel2[0]) > threshold or abs(pixel1[1] - pixel2[1]) > threshold or abs(
        pixel1[2] - pixel2[2]) > threshold

image_without_gap = Image.open('captcha_without_gap.png')
image_with_gap = Image.open('captcha_with_gap.png')

gap = 0
for x in range(100, size['width']):
    for y in range(size['height']):
        if is_pixel_different(image_without_gap, image_with_gap, x, y):
            gap = x
            break
    if gap > 0:
        break
第七步:模拟滑动滑块

现在我们知道了缺口的位置,接下来需要模拟滑动滑块来通过验证。


# 获取滑块元素
slider = browser.find_element(By.CLASS_NAME, 'geetest_slider_button')

# 计算滑块需要滑动的距离
track = []
distance = gap - BORDER
current = 0

while current < distance:
    if distance - current < 10:
        track.append(distance - current)
        current = distance
    else:
        track.append(10)
        current += 10

# 使用ActionChains来模拟滑动
action = ActionChains(browser)
action.click_and_hold(slider)
for x in track:
    action.move_by_offset(x, 0)
action.release()
action.perform()
第八步:完成验证和登录

最后,我们需要等待验证完成,并进行登录。


# 等待验证完成
wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))

# 登录
login_button = browser.find_element(By.CLASS_NAME, 'login-btn')
login_button.click()
以上是使用Python和Selenium破解极验滑动验证码的示例。请注意,这个方法仅供学习和研究之用,不应用于非法活动。同时,请尊重网站的使用政策,不要滥用资源。
如果上述代码遇到问题或已更新无法使用等情况可以联系Q:2633739505或直接访问www.ttocr.com测试对接(免费得哈)
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于数字加减验证码的规则比较简单,可以通过手动识别的方式来破解。 首先,我们需要通过 Python 的 PIL 库来读取验证码图片,并将其转换为灰度图像。然后,我们可以使用图像处理技术来识别数字和符号,并计算出验证码的结果。 以下是一个示例代码,可以对数字加减验证码进行破解: ```python from PIL import Image # 读取验证码图片 im = Image.open('captcha.png') # 转换为灰度图像 im = im.convert('L') # 图像处理函数,用于处理数字和符号 def process_digit(im, x, y): digits = { (0, 1, 0, 1, 0, 1, 1, 1, 1, 1): 0, (0, 0, 0, 1, 0, 0, 0, 1, 0, 0): 1, (0, 1, 0, 0, 1, 1, 1, 0, 1, 1): 2, (0, 1, 0, 0, 1, 1, 0, 1, 1, 1): 3, (0, 0, 0, 1, 1, 1, 0, 1, 0, 0): 4, (0, 1, 0, 1, 1, 0, 0, 1, 1, 1): 5, (0, 1, 0, 1, 1, 0, 1, 1, 1, 1): 6, (0, 1, 0, 0, 0, 1, 0, 1, 0, 0): 7, (0, 1, 0, 1, 1, 1, 1, 1, 1, 1): 8, (0, 1, 0, 1, 1, 1, 0, 1, 1, 1): 9, (0, 0, 1, 0, 0, 0, 0, 0, 0, 0): '+', (0, 0, 1, 0, 0, 0, 0, 1, 0, 0): '-', } # 提取数字和符号的像素 pixels = [] for i in range(5): for j in range(7): pixel = im.getpixel((x + i, y + j)) pixels.append(pixel) # 识别数字和符号 digit = digits.get(tuple(pixels)) return digit # 获取验证码的数字和符号 digits = [] for i in range(4): digit = process_digit(im, 13 + i * 18, 2) digits.append(digit) # 计算验证码的结果 result = eval(''.join(map(str, digits))) print('验证码:', digits) print('结果:', result) ``` 在上面的代码中,我们定义了一个图像处理函数 `process_digit`,该函数用于提取数字和符号的像素,并识别其对应的数字或符号。我们还定义了一个字典 `digits`,其中包含了数字和符号对应的像素值,用于识别验证码中的数字和符号。 在主函数中,我们使用 `process_digit` 函数来获取验证码的数字和符号,并使用 Python 内置函数 `eval` 来计算验证码的结果。 这个方法的缺点是如果验证码中包含了噪点或干扰线,可能会影响识别的准确性。但是对于一般的数字加减验证码来说,这种方法已经足够有效了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值