How to bypass Captcha in Node.JS using 2Captcha


nodejs node.js captcha 2captcha

In this tutorial, we will learn how to bypass captcha using the 2captcha package for the Node.js platform. There are a lot of tasks in software development where we need to use third-party captcha solving services to do some automation. This service has an API where we send our captchas and the employees resolve them and provide the result. We will focus on the Node.js wrapper for their API, but if you are interested in other programming languages - they have.

Outline

Before We Get Started

This tutorial assumes you have:

Setup the project

In this part, we will create a project for our experiments. Let’s create a folder and init a Node.js project:

mkdir 2captcha-sample
cd 2captcha-sample
npm init -y

The result of execution will be the following:

Npm init

Now we can install the captcha solver package:

npm i 2captcha --save

Decoding image captcha

In this part, we will decode the image with a captcha. Let’s create a separate file in the project folder:

touch image-decode.js

I found an image with a captcha in Google and copied it to the root of a project:

Captcha Example

Now we can copy the following code to the file with the API key:

const Captcha = require('2captcha');
const fs = require('fs');

const solver = new Captcha.Solver('<api-key>');

solver
    .imageCaptcha(fs.readFileSync('./captcha.png', 'base64'))
    .then((res) => { console.log(res); })
    .catch((err) => {  console.log(err); });

In this piece of code, we create an instance of solver with an API key and use the imageCaptcha. We are passing the image being parsed by fs package.

We can run this code in the terminal:

node image-decode.js

In my case I got this response:

roman@Romans-MacBook-Pro-2 2captcha-sample % node image-decode.js
{ data: 'cating fish', id: '68443826626' }

As you see the data has the same text as on mentioned captcha image.

By the way, we can visit the statistics page and see a detailed report about the submitted captcha:

Submitted captcha

Decoding reCapthca

We can decode the reCaptcha using the recaptcha method. We can create a new file for this:

touch recaptcha-decode.js

The source code:

const Captcha = require(`2captcha`);
const solver = new Captcha.Solver(`<api-key>`);

solver
    .recaptcha(
        `6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-`,
        `https://www.google.com/recaptcha/api2/demo`
    )
    .then((res) => console.log(res))
    .catch((err) => console.log(err));

The recaptcha method has sitekey and page URL parameters. The sitekey is some value related to the reCaptcha service and we can find it on the page by searching with siteKey in Developer Tools. For example on the demo page:

Sitekey on Demo page

Copy and paste it to the source along with the URL of the page where the captcha is.

After running the node recaptcha-decode.js we can get the following:

The result of ReCaptcha method

In the submitted captchas page we can see the detailed info about the request:

Recaptcha info

Decoding hCaptcha

hCaptcha is another type of captcha. We can decode it using the hcaptcha method. This chapter has almost the same codebase as in ReCaptcha Chapter besides the hCaptcha method and other demo page and site key. The process of finding the sitekey is the same as in ReCaptcha Chapter

Our new file:

node hcaptcha-decode.js

The source:

const Captcha = require('2captcha');
const solver = new Captcha.Solver(`<api-key>`);

solver
    .hcaptcha(
        `51829642-2cda-4b09-896c-594f89d700cc`,
        `http://democaptcha.com/demo-form-eng/hcaptcha.html`
    )
    .then((res) => console.log(res))
    .catch((err) => console.log(err));

The result:

The hCaptcha result

In the statistics we have:

The hCaptcha statistics

Bonus: Balance method

You can get the balance value of your 2captcha account directly in the code by using the balance method:

solver.balance().then((res)=>console.log(res)).catch((err)=>console.log(err));

The output:

Balance output

Summary

This was interesting to learn how to decode different types of captchas using the captcha solving software from 2captcha for NodeJS. If you are interested in the package for other programming platforms you can check other API wrappers.

comments powered by Disqus