使用JavaScript和Puppeteer破解滑动验证码

1. 安装Puppeteer
首先,需要安装Node.js和npm。然后,安装Puppeteer库:

bash

npm install puppeteer
2. 编写代码
下面是一个使用Puppeteer破解滑动验证码的完整示例代码:

javascript

const puppeteer = require('puppeteer');
const fs = require('fs');
const pixelmatch = require('pixelmatch');
const { PNG } = require('pngjs');

async function getScreenshot(page, selector) {
    const element = await page.$(selector);
    const boundingBox = await element.boundingBox();
    const screenshot = await page.screenshot({
        clip: {
            x: boundingBox.x,
            y: boundingBox.y,
            width: boundingBox.width,
            height: boundingBox.height
        }
    });
    return screenshot;
}

function getDistance(image1, image2) {
    const img1 = PNG.sync.read(image1);
    const img2 = PNG.sync.read(image2);
    const { width, height } = img1;
    const diff = new PNG({ width, height });

    pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });

    for (let x = 0; x < width; x++) {
        for (let y = 0; y < height; y++) {
            const idx = (y * width + x) * 4;
            if (diff.data[idx] !== 0) {
                return x;
            }
        }
    }
    return 0;
}

function generateTracks(distance) {
    const tracks = [];
    let current = 0;
    let mid = distance * 3 / 5;
    let t = 0.2;
    let v = 0;

    while (current < distance) {
        let a;
        if (current < mid) {
            a = 2;
        } else {
            a = -3;
        }
        let v0 = v;
        v = v0 + a * t;
        let move = v0 * t + 0.5 * a * (t ** 2);
        current += move;
        tracks.push(Math.round(move));
    }
    return tracks;
}

async function main() {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto('https://example.com/login');

    await page.type('#username', 'your_username');
    await page.type('#password', 'your_password');
    await page.click('#login_button');

    await page.waitForSelector('.geetest_slider_button');

    const fullBg = await getScreenshot(page, '.geetest_canvas_fullbg');
    fs.writeFileSync('full_bg.png', fullBg);

    await page.evaluate(() => {
        document.querySelector('.geetest_canvas_fullbg').style.display = 'none';
    });

    const bg = await getScreenshot(page, '.geetest_canvas_bg');
    fs.writeFileSync('bg.png', bg);

    const distance = getDistance(fullBg, bg);
    const tracks = generateTracks(distance);

    const slider = await page.$('.geetest_slider_button');
    const sliderBox = await slider.boundingBox();

    await page.mouse.move(sliderBox.x + sliderBox.width / 2, sliderBox.y + sliderBox.height / 2);
    await page.mouse.down();

    let accumulated = 0;
    for (let track of tracks) {
        accumulated += track;
        await page.mouse.move(sliderBox.x + sliderBox.width / 2 + accumulated, sliderBox.y + sliderBox.height / 2, { steps: 5 });
    }

    await page.mouse.up();

    // 添加适当的等待时间,以观察结果
    await page.waitForTimeout(5000);

    await browser.close();
}

main().catch(console.error);
3. 运行代码
将上述代码保存为一个JavaScript文件(如crackCaptcha.js),然后在终端中运行:

bash

node crackCaptcha.js

更多内容联系1436423940

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值