Join And Get Free Trial!

How to bypass reCAPTCHA v2 in Nodejs#

logoNextCaptcha
March 15,2024

About reCAPTCHA v2#

reCAPTCHA v2 is a Captcha system developed by Google to help websites distinguish between human users and automated programs (such as bots). It verifies the user's human identity by requiring them to click a checkbox before completing an action such as form submission or login. reCAPTCHA v2 determines whether the user is a real human by analyzing information such as the user's clicking behavior and browsing patterns.

Type of reCAPTCHA v2#

  • Checkbox

As shown, the "I'm not a robot" checkbox requires the user to click the checkbox that indicates the user is not a robot. This will immediately verify by the user (without a Captcha) or challenge them that they are human

newCaptchaAnchor

  • Invisible

As shown, the invisible reCAPTCHA badge does not require the user to click a checkbox, Yes is instead called directly when the user clicks an existing button on the site, or it can be called via a JavaScript API call. The integration requires a JavaScript callback after reCAPTCHA verification is complete. By default, only the most suspicious traffic will be prompted to solve a Captcha

Untitled

In this article, We will demystify Captcha and walk through the process of building a simple reCAPTCHA v2 solver using nodejs.

Environmental preparation#

First, make sure Nodejs is installed on your system. Additionally, install the required libraries:
npm install axios
npm install cheerio
When the target web page is a static page, We directly use `axios` to download the relevant web page, and then use `cheerio` to parse the web page and extract the relevant data needed to bypass reCAPTCHA v2 Before we start the official bypass, We need to register the website NextCaptcha. After registering an account, you can get an account key in the background, which is the clientKey, and save it for later use. After obtaining the relevant clientKey, We officially begin the journey to bypass reCAPTCHA v2. `getMessageFallback` called for how-to-bypass-captcha-with-nodejs.head3Content5

Get the HTML of a web page#

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');

Get 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;
    }
}
 

Build to bypass reCAPTCHA v2 requests#

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;
    }
}

Get reCAPTCHA v2 bypass task results#

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;
    }
}

put them together#

The gRecaptchaResponse in the obtained `result` is the token solved by reCAPTCHA v2. We can use this key to submit it to the relevant interface of the website.
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()