使用Dart语言破解滑块验证码的流程解析

在这篇文章中,我们将使用 Dart 语言来破解滑块验证码,详细讲解如何下载验证码图片、计算滑块移动距离、生成滑动轨迹,并发送验证请求。

  1. 下载验证码图片
    首先,我们需要下载滑块验证码的前景和背景图片。在 Dart 中,我们可以使用 http 包来实现。

dart

import 'dart:io';
import 'package:http/http.dart' as http;
import 'dart:typed_data';
import 'dart:convert';

Future downloadImage(String url, String filePath) async {
final response = await http.get(Uri.parse(url));

if (response.statusCode == 200) {
File(filePath).writeAsBytesSync(response.bodyBytes);
print('Downloaded image from $url');
} else {
throw Exception('Failed to load image');
}
}

void main() async {
await downloadImage("https://captcha.com/bg.png", "background.png");
await downloadImage("https://captcha.com/fg.png", "foreground.png");
}
2. 计算滑块移动距离
接下来,我们需要计算滑块的移动距离。这一步需要对前景和背景图片进行对比。Dart 语言中的 image 包可以帮助我们处理图像。

dart

import 'package:image/image.dart' as img;

int calculateDistance(String bgPath, String fgPath) {
final bgImg = img.decodeImage(File(bgPath).readAsBytesSync())!;
final fgImg = img.decodeImage(File(fgPath).readAsBytesSync())!;

for (int x = 0; x < bgImg.width; x++) {
for (int y = 0; y < bgImg.height; y++) {
if (bgImg.getPixel(x, y) != fgImg.getPixel(x, y)) {
print('Slide distance is ${x}');
return x; // 返回滑动距离
}
}
}

throw Exception('No difference found');
}

void main() async {
await downloadImage("https://captcha.com/bg.png", "background.png");
await downloadImage("https://captcha.com/fg.png", "foreground.png");
int distance = calculateDistance("background.png", "foreground.png");
print('Calculated distance: $distance');
}
3. 生成滑动轨迹
我们需要生成一个模拟的滑动轨迹,轨迹应该包含每一步的坐标和时间延迟。

dart

import 'dart:math';

List<List> generateTrack(int distance) {
List<List> track = [];
int current = 0;
Random rng = Random();

while (current < distance) {
int moveDistance = rng.nextInt(3) + 1; // 每次移动 1-3 像素
current += moveDistance;
if (current > distance) {
current = distance;
}
int timeDelay = rng.nextInt(21) + 10; // 随机生成 10-30 毫秒延迟
track.add([current, 0, timeDelay]);
}

return track;
}

void main() {
int distance = 100; // 假设距离为100
List<List> track = generateTrack(distance);
print('Generated track: $track');
}
4. 发送验证请求
最后,我们需要使用 http 包发送带有滑动距离和轨迹的验证请求。

dart

Future sendVerification(int distance, List<List> track) async {
final uri = Uri.parse("https://captcha.com/verify");
List trackData = track.map((point) => '[${point[0]}, ${point[1]}, ${point[2]}]').toList();

String body = 'distance=$distance&track=${trackData.join(",")}';

final response = await http.post(uri, body: body, headers: {'Content-Type': 'application/x-www-form-urlencoded'});

print('Verification request sent. Response status: ${response.statusCode}');
}

void main() async {
int distance = 100; // 假设距离为100
List<List> track = generateTrack(distance);
await sendVerification(distance, track);
}
5. 主程序整合
最后,我们将所有步骤整合起来,形成一个完整的破解流程。

dart
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:image/image.dart' as img;
import 'dart:math';更多内容联系1436423940

Future downloadImage(String url, String filePath) async {
final response = await http.get(Uri.parse(url));

if (response.statusCode == 200) {
File(filePath).writeAsBytesSync(response.bodyBytes);
print('Downloaded image from $url');
} else {
throw Exception('Failed to load image');
}
}

int calculateDistance(String bgPath, String fgPath) {
final bgImg = img.decodeImage(File(bgPath).readAsBytesSync())!;
final fgImg = img.decodeImage(File(fgPath).readAsBytesSync())!;

for (int x = 0; x < bgImg.width; x++) {
for (int y = 0; y < bgImg.height; y++) {
if (bgImg.getPixel(x, y) != fgImg.getPixel(x, y)) {
print('Slide distance is ${x}');
return x; // 返回滑动距离
}
}
}

throw Exception('No difference found');
}

List<List> generateTrack(int distance) {
List<List> track = [];
int current = 0;
Random rng = Random();

while (current < distance) {
int moveDistance = rng.nextInt(3) + 1; // 每次移动 1-3 像素
current += moveDistance;
if (current > distance) {
current = distance;
}
int timeDelay = rng.nextInt(21) + 10; // 随机生成 10-30 毫秒延迟
track.add([current, 0, timeDelay]);
}

return track;
}

Future sendVerification(int distance, List<List> track) async {
final uri = Uri.parse("https://captcha.com/verify");
List trackData = track.map((point) => '[${point[0]}, ${point[1]}, ${point[2]}]').toList();

String body = 'distance=$distance&track=${trackData.join(",")}';

final response = await http.post(uri, body: body, headers: {'Content-Type': 'application/x-www-form-urlencoded'});

print('Verification request sent. Response status: ${response.statusCode}');
}

void main() async {
// 1. 下载验证码图片
await downloadImage("https://captcha.com/bg.png", "background.png");
await downloadImage("https://captcha.com/fg.png", "foreground.png");

// 2. 计算滑动距离
int distance = calculateDistance("background.png", "foreground.png");

// 3. 生成滑动轨迹
List<List> track = generateTrack(distance);

// 4. 发送验证请求
await sendVerification(distance, track);
}

posted @ 2024-10-12 13:15  啊飒飒大苏打  阅读(8)  评论(0编辑  收藏  举报