Categories
Development

2captcha service to solve reCaptcha v2.0 (python)

In this post we want to show you the code for an automatic connection to 2captcha service for solving google reCaptcha v2.0. Not long ago, google drastically complicated the user-behavior reCaptcha (v2.0). This online service provides a method for solving it.

The simple algorithm this service uses is as follows:

  1. The target site open credentials (recaptcha’s “site key“, site url, optional: proxy IP) are copied by you (client) and submitted to the 2captcha service. You find them using simple web developer tools.
  2. A worker at the service’s end solves reCaptcha with the provided credentials.
  3. In 10-30 seconds you request an answer as a g-recaptcha-response token.
  4. You use this g-recaptcha-response token inside of the target site [submit] form with recaptcha.
All these steps can be done without imitating a browser, just by pure http GET and POST requests, and we show how.

For the purpose of illustration, we made a testing ground with recaptcha.

Get credentials

2captcha service requires us to provide it with the following parameters:

 Request parameter Value
key SERVICE_KEY (2 captchas service key)
googlekey data-sitekey attribute value in g-recaptcha block element
pageurl https://testing-ground.webscraping.pro/recaptcha (url of a target page with recaptcha)
method userrecaptcha

 

Now we go to the site page and inspect the recaptcha html code in web developer tools (hit F12). We find and get the data-sitekey attribute value in the g-recaptcha block. Its value is a constant for a single site, the site_key value provided by google. See a value highlighted in blue on the shot below:2captcha-get-site-key

So we select it and right-click to copy.

Now we have gotten the googlekey parameter (site’s google site key ): 6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8

SERVICE_KEY for the following requesting is taken from the 2captcha account settings:2captcha-get-captcha-api-key

 

Submit to service a request for recaptcha solution

Now we make a GET or POST request to the 2captcha service (in.php endpoint) with the above-mentioned parameters:

http://2captcha.com/in.php?key=SERVICE_KEY&method=userrecaptcha&googlekey=6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8&pageurl=https://testing-ground.webscraping.pro/recaptcha

Python code:

import requests 
from time import sleep, time
service_key = 'xxxxxxxxxxxxxx' # 2captcha service key 
google_site_key = '6LfxxxxxxxxxxxxxxxxxxxxxFMz856JY' 
pageurl = 'https://testing-ground.webscraping.pro/recaptcha' 
url = "http://2captcha.com/in.php?key=" + service_key + "&method=userrecaptcha&googlekey=" + google_site_key + "&pageurl=" + pageurl 
resp = requests.get(url) 
if resp.text[0:2] != 'OK': 
    quit('Service error. Error code:' + resp.text) 
captcha_id = resp.text[3:]

The 2captcha service renders a response in the form of: OK|Captcha_ID where Captcha_ID – is the id of the recaptcha in the system.

Receive valid token

Now we need to wait till a worker solves the recaptcha and google returns a valid token to the service. For this we can make a request to the 2captcha service every 5 seconds until we get a valid token. See a request to res.php endpoint with parameters:

http://2captcha.com/res.php?key=SERVICE_KEY&action=get&id=Captcha_ID

Python code

fetch_url = "http://2captcha.com/res.php?key="+ service_key + "&action=get&id=" + captcha_id

for i in range(1, 10):	
	sleep(5) # wait 5 sec.
	resp = requests.get(fetch_url)
	if resp.text[0:2] == 'OK':
		break
		 	
print('Google response token: ', resp.text[3:])

Submit google’s token in form

Now we submit the form with the g-recaptcha-response token.

At the target site (server-side), this token is checked. The site’s script sends a request to google to check the  g-recaptcha-response token’s validity: if it is true or not, pertaining to that site or not, etc. At our Captcha testing ground this token is checked before the form submission. It is done by passing a token through ajax (xhr) request to proxy.php which, in turn, inquires of google if the site is verified and returns google’s response.

proxy.php

header('Content-type: application/json'); 
$response = $_GET['response'];
$secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  
$json = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secret . '&response=' . $response);
echo $json;

See the post on how to insert and configure reCaptcha code in php.

Python code to send g-recaptcha-response to proxy.php for site verification by google

verify_url = "https://testing-ground.webscraping.pro/proxy.php?response=" + resp.text[3:]
resp = requests.get(verify_url)
print(resp.text)

The script should result in a json:

{ “success”: true,
“challenge_ts”: “2016-09-29T09:25:55Z”,
“hostname”: “testing-ground.webscraping.pro”

}

Python code of a form submitting with g-recaptcha-response:

submit_url = "https://testing-ground.webscraping.pro/recaptcha
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
payload = {'submit': 'submit', 'g-recaptcha-response': resp.test[3:] }
resp = requests.post(submit_url, headers=headers, data=payload)

The whole code

import requests 
from time import sleep, time
start_time = time()

# send credentials to the service to solve captcha
# returns service's captcha_id of captcha to be solved
url="http://2captcha.com/in.php?key=1069c3052adead147d1736d7802fabe2&method=userrecaptcha&googlekey=6Lf5CQkTAAAAAKA-kgNm9mV6sgqpGmRmRMFJYMz8&pageurl=https://testing-ground.webscraping.pro/recaptcha"
resp = requests.get(url) 
if resp.text[0:2] != 'OK':
    quit('Error. Captcha is not received')
captcha_id = resp.text[3:]

# fetch ready 'g-recaptcha-response' token for captcha_id  
fetch_url = "http://2captcha.com/res.php?key=1069c3052adead147d1736d7802fabe2&action=get&id=" + captcha_id
for i in range(1, 20):	
	sleep(5) # wait 5 sec.
	resp = requests.get(fetch_url)
	if resp.text[0:2] == 'OK':
		break
		
print('Time to solve: ', time() - start_time) 

# final submitting of form (POST) with 'g-recaptcha-response' token
submit_url = "https://testing-ground.webscraping.pro/recaptcha"
    # spoof user agent
headers = {'user-agent': 'Mozilla/5.0 Chrome/52.0.2743.116 Safari/537.36'} 
    # POST parameters, might be more, depending on form content
payload = {'submit': 'submit', 'g-recaptcha-response': resp.text[3:]  }
resp = requests.post(submit_url, headers=headers, data=payload)

Limitations

The received g-recaptcha-response token (from 2captcha service) is valid for only 120 seconds (2 min), so you are responsible to apply it in the target site [submit] form within that time limit.

Other language solutions

You might also look at other language options for how to apply 2captcha service:

  • C# code  (code for the same testing-ground page)

Fast recaptcha solutions for bulk form submit

Now the average service solution time is 25 seconds. In the following post we share how to optimize the recaptcha solution with regards to buck solving of the recaptchas with this service.

8 replies on “2captcha service to solve reCaptcha v2.0 (python)”

Is there any way to include captcha on the site and at the same time prevent services like 2captcha from resolving it?

Hi,

I’m looking to bypass the google’s recaptcha to automate a small registration form with selenium.

Is it possible to fill the form using selenium, and then for the captcha part use the 2Captcha’s api to get the response from 2Captcha and submit the form?

Thanks,
Ronnie

Hi , this code work now? i am ok untill Receive valid token step ! but after it i tested all code and captcha cant be ok !

i have problem for send valid token to captcha.

is there any ready selenium code for this ?

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.