近年来,极验验证码作为一种高级验证技术被广泛应用。然而,对于爬虫开发者来说,破解验证码始终是一大挑战。本文将详细介绍如何使用C++结合图像处理和自动化技术,破解极验第三代滑块验证码。
一、前置准备
1. 安装依赖库
为了实现验证码破解,我们需要安装libcurl、OpenCV以及Selenium WebDriver。在Ubuntu上可以通过以下命令安装这些库:
sh
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev libopencv-dev
pip install selenium
二、实现步骤
1. 获取验证码图片
首先,我们使用libcurl库来获取验证码图片并保存到本地。这些图片将用于后续的图像处理操作。
cpp
#include <iostream>
#include <fstream>
#include <curl/curl.h>
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
std::ofstream *ofs = static_cast<std::ofstream*>(userp);
ofs->write(static_cast<char*>(contents), size * nmemb);
return size * nmemb;
}
void download_image(const std::string &url, const std::string &filename) {
CURL *curl;
CURLcode res;
std::ofstream ofs(filename, std::ios::binary);
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ofs);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
int main() {
const std::string bg_url = "https://static.geetest.com/pictures/gt/3999642ae/3999642ae.webp";
const std::string full_bg_url = "https://static.geetest.com/pictures/gt/3999642ae/bg/fbdb18152.webp";
download_image(bg_url, "bg_image.webp");
download_image(full_bg_url, "full_bg_image.webp");
return 0;
}
2. 图像处理
接下来,使用OpenCV加载和处理图片,找到滑块缺口的位置。
cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int find_gap(const Mat &bg_image, const Mat &full_bg_image) {
for (int x = 0; x < bg_image.cols; x++) {
for (int y = 0; y < bg_image.rows; y++) {
Vec3b bg_pixel = bg_image.at<Vec3b>(y, x);
Vec3b full_bg_pixel = full_bg_image.at<Vec3b>(y, x);
if (bg_pixel != full_bg_pixel) {
return x;
}
}
}
return -1;
}
int main() {
Mat bg_image = imread("bg_image.webp", IMREAD_COLOR);
Mat full_bg_image = imread("full_bg_image.webp", IMREAD_COLOR);
if (bg_image.empty() || full_bg_image.empty()) {
cout << "Could not open or find the images!" << endl;
return -1;
}
int gap_position = find_gap(bg_image, full_bg_image);
cout << "Gap position: " << gap_position << endl;
return 0;
}
3. 模拟拖动滑块
为了模拟人类拖动滑块行为,我们可以先使用Python生成拖动轨迹,然后在C++中调用Python脚本。这里使用Selenium来控制浏览器执行拖动操作。
python
# generate_tracks.py
import numpy as np
def bezier_curve(t):
return 3 * t * (1 - t)**2 + 3 * (1 - t) * t**2 + t**3
def generate_tracks(distance):
tracks = []
for i in range(101):
t = i / 100
x = int(bezier_curve(t) * distance)
tracks.append(x)
return tracks
if __name__ == "__main__":
import sys
distance = int(sys.argv[1])
tracks = generate_tracks(distance)
print(tracks)
python
# simulate_drag.py
from selenium import webdriver
import time
import subprocess
import json
# 获取 gap_position
gap_position = 100 # 假设值,实际应从 C++ 程序获取
# 生成拖动轨迹
result = subprocess.run(['python3', 'generate_tracks.py', str(gap_position)], stdout=subprocess.PIPE)
tracks = json.loads(result.stdout.decode('utf-8'))
# 使用 Selenium 模拟拖动滑块
browser = webdriver.Chrome()
browser.get('https://account.ch.com/NonRegistrations-Regist')
knob = browser.find_element_by_class_name('gt_slider_knob')
actions = webdriver.ActionChains(browser)
actions.click_and_hold(knob).perform()
for track in tracks:
actions.move_by_offset(track, 0).perform()
time.sleep(0.02) # 模拟人类行为
actions.release().perform()
browser.quit()
4. 调用Python脚本生成轨迹并拖动滑块
在C++中使用system函数调用Python脚本,生成拖动轨迹并控制浏览器完成滑块拖动。
cpp
#include <iostream>
#include <cstdlib>
int main() {
int gap_position = 100; // 从图像处理步骤获取
std::string command = "python3 generate_tracks.py " + std::to_string(gap_position) + " > tracks.json";
system(command.c_str());
std::cout << "Tracks generated and saved to tracks.json" << std::endl;
system("python3 simulate_drag.py");
return 0;
}
更多内容联系1436423940