
You may have solved millions of captchas for verification, which can be a tedious chore when you need to authenticate your identity as a human. These websites offer an additional layer of security by utilizing captcha services to prevent bots and automation scripts from accessing their website.
What is CAPTCHA?
CAPTCHA (Completely Automated Public Turing Test to Tell Computers and Humans Apart) is a form of challenge-response test used to assess whether or not a user is human.
CAPTCHAs display jumbled, scrambled, or distorted text, images, numbers, and audio that is tough for computers to solve but relatively easy for people to solve. These are used to protect websites and applications against harmful actions.
2captcha API
There are numerous applications and services available to assist us in solving a captcha in seconds.2captcha is a website that offers captcha-solving services.
2captcha provides API for various programming languages, including Python, and we'll utilise the2captcha-python
library to solve the captcha in a few steps.
Installing dependencies
The primary work is to install the Python library called 2captcha-python. Run the following command in your terminal.
pipinstall2captcha-python
Please keep in mind that we are using pip to install the library. If you are not using pip, the installation command will be different.
Solving reCAPTCHA
We'll use a reCAPTCHA testing website and here's the URL of the websitehttps://patrickhlauke.github.io/recaptcha/. It provides a reCAPTCHA widget for testing purposes.
Code
First, we must import the library and create an instance of the classTwoCaptcha
, which we will initialise with the API key. To make aPOST request to the target site, we also need therequests
library.
# Imported the required libsfromtwocaptchaimportTwoCaptchaimportrequests# Instance of TwoCaptchasolver=TwoCaptcha('YOUR_API_KEY')
After we've built the instance, the next step is to obtain the token from the 2captcha server.
# Target website URLsite_url="https://patrickhlauke.github.io/recaptcha/"# Getting the tokentry:# For solving reCAPTCHAtoken=captcha_solver.recaptcha(sitekey='6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5',url=site_url)# Handling the exceptionsexceptExceptionase:raiseSystemExit('Error: CAPTCHA token not recieved.')# Print the token and exit the programelse:SystemExit('Token-'+str(token))
We called therecaptcha
method from the instancecaptcha_solver
within thetry
block and passed the reCAPTCHA data-sitekey to thesitekey
parameter and the URL of the target site to theurl
parameter.
To get thesitekey from the website, open it in inspect mode and look for the tag<iframe>
withtitle="reCAPTCHA"
and you'll find the key within thesrc
attribute.
If any exceptions are triggered within theexcept
block, the program will display an error message and exit. If the program is successfully executed, it will return the token that will be used to solve reCAPTCHA and then quit.
The final step will be to transfer the token to the target site, which will be accomplished by sending aPOST request to the target site via therequests
library.
# Imported the required libsfromtwocaptchaimportTwoCaptchaimportrequests# Instance of TwoCaptchacaptcha_solver=TwoCaptcha('YOUR_API_KEY')# Target website URLsite_url="https://patrickhlauke.github.io/recaptcha/"# Getting the tokentry:# For solving reCAPTCHAtoken=captcha_solver.recaptcha(sitekey='6Ld2sf4SAAAAAKSgzs0Q13IZhY02Pyo31S2jgOB5',url=site_url)# Handling the exceptionsexceptExceptionase:raiseSystemExit('Error: CAPTCHA token not recieved.')# Sending the token to the websiteelse:# Sending spoof user-agentheaders={'user-agent':'Mozilla/5.0 Chrome/52.0.2743.116 Safari/537.36'}# Sending recieved tokendata={'recaptcha-token':str(token)}# Making POST request to the target sitetoken_response=requests.post(site_url,headers=headers,data=data)print('Token sent')
We changed theelse
part of the code, and the result is the code seen above. Before performing thePOST request to the target site, we've established certain configurations to send along with the request within theelse
block.
We used therequests
library'spost
function to send aPOST request to the target site, passing thesite_url
(target site URL),headers
(containing the user-agent), anddata
(containing token). The data can contain more parameters depending on the form.
Note: The received token from the 2captcha servers will only be valid for 120 seconds, so you need to submit the token to the target site within the time limit.
Solving hCAPTCHA
The URL of the hCAPTCHA demo site ishttps://accounts.hcaptcha.com/demo.
The procedure will be the same as with reCAPTCHA, with the exception that we must modify the method to hcaptcha from the2captcha-python
package.
# Imported the required libsfromtwocaptchaimportTwoCaptchaimportrequests# Instance of TwoCaptchacaptcha_solver=TwoCaptcha('YOUR_API_KEY')# Target website URLsite_url="https://accounts.hcaptcha.com/demo"# Getting the tokentry:# For solving hCAPTCHAtoken=captcha_solver.hcaptcha(sitekey='a5f74b19-9e45-40e0-b45d-47ff91b7a6c2',url=site_url)# Handling the exceptionsexceptExceptionase:raiseSystemExit('Error: CAPTCHA token not recieved.')# Sending the token to the websiteelse:# Sending spoof user-agentheaders={'user-agent':'Mozilla/5.0 Chrome/52.0.2743.116 Safari/537.36'}# Sending recieved tokendata={'hcaptcha-token':str(token)}# Making POST request to the target sitetoken_response=requests.post(site_url,headers=headers,data=data)print('Token sent')
We'll get thesitekey
of the hCAPTCHA within thesrc
attribute of the<iframe>
tag.
Conclusion
We've used the2captcha-python
package for solving the reCAPTCHA and hCAPTCHA. The2captcha-python
package provided by 2captcha provides captcha-solving services and almost all types of captcha can be solved using their APIs. They provide a browser extension for solving CAPTCHAs.
Top comments(2)
Some comments may only be visible to logged-in visitors.Sign in to view all comments.
For further actions, you may consider blocking this person and/orreporting abuse