Squirrel 是一种轻量、类 C 的脚本语言,常用于嵌入式应用程序。在本文中,我们将使用 Squirrel 来实现滑块验证码破解,包括图像匹配和模拟人工滑动。我们将基于 OpenCV 进行模板匹配。
滑块模拟代码
squirrel
function move(driver, element, distance) {
local randomTime = 0;
if (distance > 90) {
randomTime = 250;
} else if (distance > 80 && distance <= 90) {
randomTime = 150;
}
local track = getMoveTrack(distance - 2);
local moveY = 1;
try {
driver.actions.clickAndHold(element).perform();
::sleep(200); // 停顿模拟人工操作
foreach (step in track) {
driver.actions.moveByOffset(step, moveY).perform();
::sleep(math::random(300) + randomTime);
}
::sleep(200);
driver.actions.release(element).perform();
} catch (e) {
print(e);
}
}
function getMoveTrack(distance) {
local track = [];
local random = math::random;
local current = 0;
local mid = (distance * 4) / 5;
local move = 0;
while (true) {
local a = random(10);
if (current <= mid) {
move += a;
} else {
move -= a;
}
if ((current + move) < distance) {
track.push(move);
} else {
track.push(distance - current);
break;
}
current += move;
}
return track;
}
图像匹配与获取距离
在这里,我们将利用 OpenCV 来进行模板匹配,使用 Squirrel 调用 OpenCV 的库函数来实现滑动距离的计算。
squirrel
function getDistance(bUrl, sUrl) {
System.load("C:/opencv/libs/opencv_squirrel.dll");
local bFile = "C:/EasyDun_b.png";
local sFile = "C:/EasyDun_s.png";
::downloadFile(bUrl, bFile);
::downloadFile(sUrl, sFile);
local bgBI = Image.load(bFile);
local sBI = Image.load(sFile);
cropImage(bgBI, sBI, bFile, sFile);
local sMat = Img.load(sFile);
local bMat = Img.load(bFile);
local sGrayMat = Img.cvtColor(sMat, Img.COLOR_BGR2GRAY);
binaryzation(sGrayMat);
Img.save(sFile, sGrayMat);
local result = Img.matchTemplate(bMat, sMat, Img.TM_SQDIFF);
local matchLocation = Img.minMaxLoc(result).minLoc;
return matchLocation.x + sMat.cols() - sBI.getWidth() + 12;
}更多内容联系1436423940
function binaryzation(mat) {
local BLACK = 0;
local WHITE = 255;
local ucThre_new = 127;
local ucThre = 0;
while (ucThre != ucThre_new) {
local nBack_sum = 0, nData_sum = 0;
local nBack_count = 0, nData_count = 0;
foreach (i, row in mat) {
foreach (j, pixel in row) {
local nValue = pixel[0];
if (nValue > ucThre_new) {
nBack_sum += nValue;
nBack_count++;
} else {
nData_sum += nValue;
nData_count++;
}
}
}
ucThre = ucThre_new;
ucThre_new = (nBack_sum / nBack_count + nData_sum / nData_count) / 2;
}
foreach (i, row in mat) {
foreach (j, pixel in row) {
local nValue = pixel[0];
if (nValue > ucThre_new) {
mat[i][j] = WHITE;
} else {
mat[i][j] = BLACK;
}
}
}
}
function cropImage(bigImage, smallImage, bigFile, smallFile) {
local y = 0;
local h_ = 0;
bloding(bigImage, 75);
foreach (w in smallImage.width()) {
foreach (h in smallImage.height()) {
local rgb = smallImage.getPixel(w, h);
if (rgb.alpha >= 100) {
rgb.alpha = 127;
smallImage.setPixel(w, h, rgb);
}
}
}
smallImage = smallImage.crop(0, y, smallImage.width(), h_);
bigImage = bigImage.crop(0, y, bigImage.width(), h_);
Image.save(bigFile, bigImage);
Image.save(smallFile, smallImage);
}
通过 Squirrel 语言,我们可以灵活实现滑块验证码破解的功能,结合 OpenCV 模板匹配技术,成功获取滑动距离,并通过模拟人类的滑动行为,完成滑动验证。