一、前置准备
1. 安装依赖库
为了实现验证码破解,我们需要安装reqwest、opencv和selenium-webdriver库。在Cargo.toml中添加这些依赖:
toml
[dependencies]
reqwest = { version = "0.11", features = ["blocking"] }
opencv = { version = "0.55", features = ["opencv-4"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
二、实现步骤
1. 获取验证码图片
首先,我们使用reqwest库来获取验证码图片并保存到本地。这些图片将用于后续的图像处理操作。
rust
use std::fs::File;
use std::io::copy;
use reqwest::blocking::get;
fn download_image(url: &str, path: &str) -> Result<(), Box<dyn std::error::Error>> {
let response = get(url)?;
let mut dest = File::create(path)?;
copy(&mut response.bytes()?.as_ref(), &mut dest)?;
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let bg_url = "https://static.geetest.com/pictures/gt/3999642ae/3999642ae.webp";
let 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")?;
Ok(())
}
2. 图像处理
接下来,使用OpenCV加载和处理图片,找到滑块缺口的位置。
rust
use opencv::{
core,
imgcodecs,
imgproc,
prelude::*,
types,
};
fn find_gap(bg_image: &Mat, full_bg_image: &Mat) -> Result<i32, Box<dyn std::error::Error>> {
for x in 0..bg_image.cols() {
for y in 0..bg_image.rows() {
let bg_pixel = bg_image.at_2d::<core::Vec3b>(y, x)?;
let full_bg_pixel = full_bg_image.at_2d::<core::Vec3b>(y, x)?;
if bg_pixel != full_bg_pixel {
return Ok(x);
}
}
}
Ok(-1)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let bg_image = imgcodecs::imread("bg_image.webp", imgcodecs::IMREAD_COLOR)?;
let full_bg_image = imgcodecs::imread("full_bg_image.webp", imgcodecs::IMREAD_COLOR)?;
if bg_image.empty() || full_bg_image.empty() {
eprintln!("Could not open or find the images!");
return Ok(());
}
let gap_position = find_gap(&bg_image, &full_bg_image)?;
println!("Gap position: {}", gap_position);
Ok(())
}
3. 模拟拖动滑块
为了模拟人类拖动滑块行为,我们可以先使用Python生成拖动轨迹,然后在Rust中调用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 # 假设值,实际应从 Rust 程序获取
# 生成拖动轨迹
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脚本生成轨迹并拖动滑块
在Rust中使用std::process::Command调用Python脚本,生成拖动轨迹并控制浏览器完成滑块拖动。
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let gap_position = 100; // 从图像处理步骤获取
let output = Command::new("python3")
.arg("generate_tracks.py")
.arg(gap_position.to_string())
.output()
.expect("Failed to execute command");
println!("Tracks generated: {}", String::from_utf8_lossy(&output.stdout));
Command::new("python3")
.arg("simulate_drag.py")
.status()
.expect("Failed to execute command");
Ok(())
更多内容联系1436423940