DEV Community

Cover image for Solving CAPTCHA Using 2Captcha
Ethan
Ethan

Posted on

Solving CAPTCHA Using 2Captcha

Introduction

Hello! In this tutorial I will be showing how to bypass the captcha recognition process using a service called 2Captcha.


What is 2Captcha?

2Captcha is a service created to automate the captcha recognition process.

All captchas are recognized by humans which means that 2Captcha can recognise all humanly readable types of captchas.

If you're like me and sometimes have trouble reading reading the letters etc in a captcha, then you may also benefit from trying the service out.

I was surprised to know it even handles reCAPTCHA v2. ๐Ÿ˜Ž


Getting the 2Captcha API Key

In order to start using the service we will need an API Key.

First you will need to register an account at:
https://2captcha.com/auth/register

Account Registration

Once you have created an account simply navigate your browser to following URL and make a note of your API Key.
https://2captcha.com/setting


Setting up the project

For this example I will be using nodejs.

First we need to create the project.

npm i -y
Enter fullscreen mode Exit fullscreen mode

Then the need to install the 2captcha module, this can be done via:

npm i 2captcha
Enter fullscreen mode Exit fullscreen mode

Next we need to create a config file that holds the 2Captcha API key:

mkdir src
Enter fullscreen mode Exit fullscreen mode

Open "src/config.js" and add the following, make sure to replace the API key with your own, the API key can be accessed at:
https://2captcha.com/setting

const API_KEY = 'secret';

module.exports = { 
  API_KEY
}
Enter fullscreen mode Exit fullscreen mode

Using 2Captcha to solve a simple Captcha

Normal captcha is an image that contains distorted but human-readable text. Though sometimes I can't make sense of some of the letters used. ๐Ÿ˜…

So let's try using 2Captcha to solve it.

For this example I will use the following normal captcha image:

Normal Captcha

First create the "src/normal.js" file and add the following:

const { Solver } = require('2captcha');
const { readFileSync } = require('fs');

const { API_KEY } = require('./config');

const solver = new Solver(API_KEY);

(async () => {
  try {
    const imageBase64File = await readFileSync('./captcha-image.png', 'base64');
    const response = await solver.imageCaptcha(imageBase64File);

    console.log(response);
  } catch (error) {
    console.error(error);
  }
})();
Enter fullscreen mode Exit fullscreen mode

The source itself is pretty simple, we import the 2Captcha module, read the image file as Base64 data and then pass the data to the 2Captcha module to solve it.

Next we can try running it via:

node src/normal.js
Enter fullscreen mode Exit fullscreen mode

Normal data

Amazing! ๐Ÿ˜ƒ Feel free to try the sample with a variety of images.


Using 2Captcha to solve reCAPTCHA

Next we can try using 2Captcha to solve the more advanced reCAPTCHA. ๐Ÿ‘€
Personally I always have issues with this type of captcha as I always seem to be wrong. ๐Ÿ˜…

For this example I will be using the following site:
https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php

In order to solve this captcha we will need to get the sitekey variable, this can be found by opening the "Developer Console" and simply filtering the word "sitekey" like the following:

Sitekey

Make a note of this variable as we will be needing it.

Next create the "src/recaptcha.js" file and add the following:

const { Solver } = require('2captcha');

const { API_KEY } = require('./config');

const solver = new Solver(API_KEY);

(async () => {
  try {
    const response = await solver.recaptcha(
      '6LfW6wATAAAAAHLqO2pb8bDBahxlMxNdo9g947u9',
      'https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php'
    );  

    console.log(response);
  } catch (error) {
    console.error(error);
  }
})();
Enter fullscreen mode Exit fullscreen mode

Again the source is very simple, this time instead of an image we just pass the sitekey and the url of the page to the 2Captcha module.

reCAPTCHA is obviously harder than a simple image file so the request my take some time.
If all goes well you should see the following output:

reCaptcha image

Amazing. ๐Ÿ˜Ž


Conclusion

Here I have shown how to use the 2Captcha service to solve simple and hard captchas, I was amazed at how it can be done so simply. ๐Ÿ‘€

Since it's better at solving them better than me I sometimes use it for personal reasons. ๐Ÿ˜…

I can definetely see something like this being used for automation purposes etc. ๐Ÿ˜€

The source can be found here:
https://github.com/ethand91/2captcha-sample


Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

โ€œBuy Me A Coffeeโ€

Latest comments (5)

Collapse
 
stuffedparrrot profile image
sTuffedParrrot

I have been working with recaptchaV2 2captcha code in VB.net, i can get the results but cannot get the webpage to respond to anything,
program clicks the button, i sent the webpage url and sitekey to 2captcha and get the response, but getting the web page to use the response is where i am stuck. there is a textarea id="g-recaptcha-response and i load the response within the textarea, and send it back to the web page. but the web page does not seem to see it, i also tried creating a text box and loaded it with the response, still nothing, there is no submit button on the response only the check mark that brings images up. i tried to create a submit button, but still nothing.
appreciate any idea. thanks

Collapse
 
lschiavini profile image
Lucas Schiavini

Hahahahaha I wonder what the next level captcha will be.

Collapse
 
ethand91 profile image
Ethan

I dread thinking about it. xD

Some comments may only be visible to logged-in visitors. Sign in to view all comments.