DEV Community

gurakuqienrik6
gurakuqienrik6

Posted on

How to Solve Captcha with Nodejs

About reCAPTCHA v2

reCAPTCHA v2 is a CAPTCHA system developed by Google that aims to help websites distinguish between human users and automated programs (such as bots). It verifies a user's human identity by requiring them to click a checkbox before completing actions like submitting a form or logging in. reCAPTCHA v2 determines if a user is a genuine human by analyzing information such as their clicking behavior and browsing patterns.

Type of reCAPTCHA v2

  1. Checkbox

    As shown in the image, the "I'm not a robot" checkbox requires the user to click the checkbox indicating that the user is not a robot. This will either immediately pass the user (without a CAPTCHA) or challenge them to verify they are human.

reCAPTCHA v2 checkbox type

  1. Invisible

    As shown in the image, the invisible reCAPTCHA badge does not require the user to click a checkbox. Instead, it is directly invoked when the user clicks an existing button on the site, or it can be invoked via the JavaScript API. After the reCAPTCHA verification is completed, the integration requires a JavaScript callback. By default, only the most suspicious traffic will be prompted to solve a CAPTCHA.

reCAPTCHA v2 Invisible type

In this article, we will unveil the mystery of CAPTCHAs and walk through the process of building a simple reCAPTCHA v2 solver using Node.js, step by step.

Environment Setup

First, make sure you have Node.js installed on your system. Additionally, install the required libraries:

npm install axios
npm install cheerio
Enter fullscreen mode Exit fullscreen mode

When the target webpage is a static page, we directly use axios to download the relevant webpage, and then use cheerio to parse the webpage and extract the relevant data needed to bypass reCAPTCHA v2.

Before we start the actual bypass, we need to register on the NextCaptcha website. After registering an account, you can obtain an account key from the backend, which is the clientKey. Save it for later use.

After obtaining the relevant clientKey, we will formally begin our journey of bypassing reCAPTCHA v2.

Here we will use the official Demo as an example, and the target URL is https://www.google.com/recaptcha/api2/demo

Obtain the HTML of the webpage

const axios = require('axios');

async function getPageData(url) {
    try {
        const data = await axios.get(url)
        return data.data;  
    }  catch (e) {
        console.error('getPageData error', e.message);
        return null;
    }
}

getPageData('https://www.google.com/recaptcha/api2/demo');
Enter fullscreen mode Exit fullscreen mode

Obtain the reCAPTCHA related parameters

const cheerio = require('cheerio');

function parserData(html) {
    try {
        const $ = cheerio.load(html);

        return $('[data-sitekey]').data('sitekey')

    } catch (e) {
        console.error('parserData error', e.message);
        return null;
    }
}

Enter fullscreen mode Exit fullscreen mode

Construct the bypass CAPTCHA request

const axios = require('axios');

async function createCaptchaTask(url, siteKey, isInvisible) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/createTask', {
            "clientKey": "clientKey", // clientKey from NextCaptcha dashboard
            "task": {
                type: "RecaptchaV2TaskProxyless",
                websiteURL: url,
                websiteKey: siteKey,
                isInvisible
            }
        });
        return data.data;
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}
Enter fullscreen mode Exit fullscreen mode

Obtain the CAPTCHA solver task result

const axios = require('axios');

async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}
async function getTaskResult(taskId) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/getTaskResult', {
            "clientKey": "clientKey", // clientKey from NextCaptcha
            taskId
        });
        if (data.data.status === 'processing') {
            await sleep();
            return getTaskResult(taskId)
        } else {
            console.error('createCaptchaTask errorCode', data.data.errorCode);
            console.error('createCaptchaTask errorDescription', data.data.errorDescription);
            return null;
        }
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}
Enter fullscreen mode Exit fullscreen mode

Final Code

const axios = require('axios');
const cheerio = require('cheerio');

async function getPageData(url) {
    try {
        const data = await axios.get(url)
        return data.data;  
    }  catch (e) {
        console.error('getPageData error', e.message);
        return null;
    }
}

function parserData(html) {
    try {
        const $ = cheerio.load(html);

        return $('[data-sitekey]').data('sitekey')

    } catch (e) {
        console.error('parserData error', e.message);
        return null;
    }
}

async function createCaptchaTask(url, siteKey, isInvisible) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/createTask', {
            "clientKey": "clientKey", // clientKey from NextCaptcha dashboard
            "task": {
                type: "RecaptchaV2TaskProxyless",
                websiteURL: url,
                websiteKey: siteKey,
                isInvisible
            }
        });
        return data.data;
    } catch (e) {
        console.error('createCaptchaTask error', e.message);
        return null;
    }
}

async function sleep(time = 500) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve();
        }, time)
    })
}

async function getTaskResult(taskId, tryTimes = 60) {
    try {
        const data = await axios.post('https://api.nextcaptcha.com/getTaskResult', {
            "clientKey": "clientKey", // clientKey from NextCaptcha
            taskId
        });
         if (data.data.status === 'ready') {
            return data.data;
        } else if (data.data.status === 'processing' && tryTimes >= 0) {
            await sleep();
            return getTaskResult(taskId)
        } else {
            if (tryTimes < 0) {
                console.error('getTaskResult out of time');
            } else {
                console.error('getTaskResult errorCode', data.data.errorCode);
                console.error('getTaskResult errorDescription', data.data.errorDescription);
            }
            return null;
        }
    } catch (e) {
        console.error('getTaskResult error', e.message);
        return null;
    }
}

async function mian() {
    const url = 'https://www.google.com/recaptcha/api2/demo'
    const html = await getPageData(url);
    const sitekey = parserData(html);
    console.log(sitekey)
    const task = await createCaptchaTask(url, sitekey, false);
    const result = await getTaskResult(task.taskId);
    console.log(result)
}

mian()

Enter fullscreen mode Exit fullscreen mode

The gRecaptchaResponse in the result obtained in the last step is the token solved by reCAPTCHA v2. We can use this token to submit it to the relevant interface of the website, so that we have completed the entire process of CAPTCHA bypass.

Conclusion

Although the verification code service of NextCaptcha is charged and each verification requires a certain amount of points, its price is relatively affordable compared to other similar services on the market. More importantly, its recognition accuracy is very high and it can crack the verification codes of many websites.

Currently, the website has recharge benefits. You can contact customer service to get discounts https://t.me/nextcaptcha0

Top comments (0)