Categories
Development

How to Write a Captcha Solver that uses DeathByCaptcha service

Let’s look at a practical example on how to solve CAPTCHAs using the DeathByCaptcha service. This example is written in C#, but you can get it in Java as well.

I use the Web Scraper Testing Ground as an example of a CAPTCHA-protected web page, but I recognize the first  CAPTCHA only as the most difficult one. Also since CAPTCHA images are generated dynamically I use Selenium WebDriver with Firefox to capture them.

If you don’t want to use the DeathByCaptcha service you can substitute it with some OCR software like GSA Capture Breaker. It simulates the service locally, allowing you to not pay per each captcha recognition.

I want to start explaining the code portion by portion, but if you don’t have time to read all the explanations you can find the complete code at the end of the post or download a ready project here.

OK, let’s start!

First, we need to initialize the WebDriver and open the target webpage with captcha:

using (var driver = new FirefoxDriver())
{
    driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/captcha");

Then we need to obtain the CAPTCHA image. Since the WebDriver doesn’t allow us to explicitly extract dynamically created images from the page, let’s create a screenshot of the whole page and then crop the captcha image basing on image position on the web page:

var arrScreen = driver.GetScreenshot().AsByteArray;
using (var msScreen = new MemoryStream(arrScreen))
{
    var bmpScreen = new Bitmap(msScreen);
    var cap = driver.FindElementById("captcha");
    var rcCrop = new Rectangle (cap.Location, cap.Size);
    Image imgCap = bmpScreen.Clone(rcCrop, bmpScr.PixelFormat);

Now, since we have the CAPTCHA image, we can send it to the DeathByCaptcha service for further recognition:

using (var msCaptcha = new MemoryStream())
{
    imgCap.Save(msCaptcha, ImageFormat.Png);
    // put your DeathByCaptcha credentials here
    var client = new SocketClient("user", "password");
    var res = client.Decode(msCaptcha.GetBuffer(), 20);

After we receive a response from the service and the captcha is recognized, we can type its code into the textbox and click the “submit” button:

if (res!=null && res.Solved && res.Correct)
{
    driver.FindElementByXPath("//input[@name='captcha_code']")
    .SendKeys(res.Text);

    driver.FindElementByXPath("//input[@name='submit']")
    .Click ();

Then after the page reloads we can check whether the server accepted our captcha or not. If the captcha was recognized incorrectly, we need to send this report back to the DeathByCaptcha service:

var h4 = driver.FindElementByXPath("//div[@id='case_captcha']//h4");
if (!h4.Text.Contains("SUCCESSFULLY"))
{
    Console.WriteLine("The captcha has been solved incorrectly!");
    client.Report(res);
}
else
    Console.WriteLine("The captcha has been solved correctly!");

That’s it! Here is the complete code of the application:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using OpenQA.Selenium.Firefox;
using DeathByCaptcha;
using System.IO;

namespace CaptchaSolver
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Initializing the Firefox webdriver...");
            using (var driver = new FirefoxDriver())
            {
                Console.WriteLine("Opening the page...");
                driver.Navigate().GoToUrl("http://testing-ground.scraping.pro/captcha");

                var arrScreen = driver.GetScreenshot().AsByteArray;
                using (var msScreen = new MemoryStream(arrScreen))
                {
                    var bmpScreen = new Bitmap(msScreen);
                    var cap = driver.FindElementById("captcha");
                    var rcCrop = new Rectangle (cap.Location, cap.Size);
                    Image imgCap = bmpScreen.Clone(rcCrop, bmpScreen.PixelFormat);

                    using (var msCaptcha = new MemoryStream())
                    {
                        imgCap.Save(msCaptcha, ImageFormat.Png);

                        // put your DeathByCaptcha credentials here
                        var client = new SocketClient("user", "password");

                        Console.WriteLine("Sending request to DeathByCaptcha...");
                        var res = client.Decode(msCaptcha.GetBuffer(), 20);
                        if (res!=null && res.Solved && res.Correct)
                        {
                            driver.FindElementByXPath ( "//input[@name='captcha_code']" ).SendKeys(res.Text);

                            driver.FindElementByXPath ( "//input[@name='submit']" ).Click ();

                            var h4 = driver.FindElementByXPath("//div[@id='case_captcha']//h4");
                            if (!h4.Text.Contains("SUCCESSFULLY"))
                            {
                                Console.WriteLine("The captcha has been solved incorrectly!");
                                client.Report(res);
                            }
                            else
                                Console.WriteLine("The captcha has been solved correctly!");
                        }
                        else
                            Console.WriteLine("Captcha recognition error occured");
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

Don’t forget that you can  download the complete VS Project. And feel free to ask any questions or give any responses!

37 replies on “How to Write a Captcha Solver that uses DeathByCaptcha service”

HI,
Can you provide me the code in java for crop the captcha image basing on image position on the web page.

Or can you provide me the whole above code in JAVA as it is on C# ..it would be very helpful to me..

Thanks in Advance

I’m a complete nubie – how do I use this file? Are you saying that I need to sign up for death by captcha – run this file (how) and I won’t have to pay a price for captcha solving?

Please Advise

How to do this with Chrome driver? I try to do it with chrome but I got this error. System.OutOfMemoryException was unhandled
Message=Out of memory.
Source=System.Drawing
StackTrace:
at System.Drawing.Bitmap.Clone(Rectangle rect, PixelFormat format)
at CaptchaSolver.Program.Main(String[] args) in C:\realcaptcha\Program.cs:line 35
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

How to substitute it with GSA Capture Breaker ?
I know I have to change it here but no idea how to.
using (var msCaptcha = new MemoryStream())
{
imgCap.Save(msCaptcha, ImageFormat.Png);
// put your DeathByCaptcha credentials here
var client = new SocketClient(“user”, “password”);
var res = client.Decode(msCaptcha.GetBuffer(), 20);

How can I make this code work with Bypass CAPTCHA service?
What changes I need to do to the code?
Is this the only line I need to change ?
var client = new SocketClient(“user”, “password”);
Please help me 🙂

Hi,

The program is stuck after console display ‘Initializing the Firefox webdriver…’. It does open the firefox browser but then nothing happen.

Please do guide me to right direction 😀

Thank you.

Hi

I am new to c# selenium.

Can you please tell how to set up the DeathByCaptcha in a C# project ?
its written as a API in death by captcha site.

How to use the command and load the dll ?

using DeathByCaptcha;

Hi,

I tried to run this but i get a pop up to download or search for a file called DriverServices.cs. But there is no such file in my system.

Could you please help me on this?

Thanks
Kiran

Hi, I cannot crop the captcha successfully using the code below.

var bmpScreen = new Bitmap(msScreen);
var cap = driver.FindElementById(“captcha”);
var rcCrop = new Rectangle (cap.Location, cap.Size);
Image imgCap = bmpScreen.Clone(rcCrop, bmpScreen.PixelFormat);

Its working on many sites including yours in example code, but not working on my target site. I saved the image cropped, its empty. What could be the problem?

Is there a downloadable script written in javascript for any of the captcha services, other than 9kw? DBC, Imagetyperz? Any?? The .js sample from 9kw works perfectly in imacros, however its so slow, it usually times out before solving the new recaptcha 2.0’s. (usually solves 50% of the time) I know ZERO about programing or script writing and would love to have a javascript that I could just plop my username and password into (like 9kw) and let it run! Any help would be greatly appreciated! I tried editing the 9kw js file to suit DBC and I failed because I do not understand how to write code/scripts

Hi, I am trying to break the new nocaptcha/recaptcha with python and dbc. Do you have the code for python?

I was wondering if you could help me write a program that solves the 9by9 caps. And then fills out of a form and submits the data.

Leave a Reply to Tiok Cancel reply

Your email address will not be published.

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