Website interaction and solve captchas automatically

Inhalt

Automation of web page interaction, solve captchas automatically and sending notifications to cell phones

You may know this: you are waiting for information on a web page and you don’t want to constantly updating  the page manually? You always wanted to know, how to automate a web query and how you can solve captchas automatically? And you would rather prefer an automated process to monitor the website and send you a push notification to your cell phone when the information is available?

A smart solution could be using certain web services such as 2captcha.com . How it works is exemplified here by using a sample page (https://indibit.de/test-page-captcha).

What can be seen in the video? The moving of the cursor, typing into the text boxes and solving the captcha is executed script controlled and not by the users themselves!

What can we learn?

  • How can we automate Windows and the interaction with a web page?
  • How can we solve the unpoplular captchas automatically?
  • How can we send a push notofication to the cell phone by using an automated process?

What do we need?

What is AutoIt?

AutoIt is a software that allows to simulate the interaction with a Windows PC. That means, a standard Windows PC is controlled by software and no real users. The language is very simple and based on the syntax that an average programmer already knows from the Java or .Net world. It even admits to create simple applications.

What is 2captcha.com? How to solve captchas automatically

2captcha.com is a captcha solver service that accepts captchas as image files, solves them for a small fee and provides the characters as normal text via an interface. The result is obtained by a simple web request, which you can even call up the browser. The service  2captcha.com collects the submitted captchas, list them on their platform to be solved. Other registered users call up the images and enter the solutions. The platform then places the result at the customers disposal. The subsequent status message contains the correct string that usually corresponds with the initial captcha.

As requestor you can determine various parameters that charcterise the complexity of the captcha. For instance, is the result expected to be case sensitive or does the captcha contain numbers, letters, special characters or even separate words etc.

Anyone can register on the Platform and solve captchas. If you feel like it, you may try a career as captcha solver – after all, you can earn $ 0.0001 per correct result … ..

The solving of 1000 captchas can cost up to 1 $ – by using Paypal it is just a matter of seconds to get started.

Preparation:

Install AutoIt  (standard installation procedure – Ok -> Ok -> Next .. etc.)

Unpack PycURL zip file and copy the contents from the folder site-package in the python folder to the local installation (eg C:\Python34\Lib\site-package)

It is important to know that I use a Windows7 PC with Google Chrome as a browser and a resolution of 1600 x 900 pixels in my example. You possibly have to adapt the pixel coordinates of your respective resolution and scaling in the script.

Here we go:

Open the AutoIt v3 SciTE Script Editor from the Start menu. You can start in the open document immediately. First, we create a new instance of Chrome along with the web page that you want to query. If you want to use another browser, you only need to specify the relevant path to the browser after the Run() command and adjust the window_title .

#include <Constants.au3>
#include <ScreenCapture.au3>
#include <Date.au3>
#include <Inet.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
; 
; Author: Heiko 
; 

ClipPut("") ;  clear clip board initially 
Run("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe https://indibit.de/test-page-captcha/") ; Call Google Chrome - perhaps you have to change the path to browser executable 

$window_title = "Test-Page-Captcha - indiBit - Google Chrome" 

;get window handle 
$window_handle = WinWait($window_title) 
; maximize window 
WinSetState ( "Test-Page-Captcha - indiBit - Google Chrome", "",@SW_MAXIMIZE) ; window tile of process, which have to maximized

Now we let the cursor work for us a little bit. Click on the first text field to enter the name. After that, the script is pressing the Tab key to jump to the next text fields to fill in the values. In the last step the script saves the captcha image locally. Thus, we have automated the website query:

AutoItSetOption("MouseCoordMode", 0)
BlockInput(1)
;wait one second
Sleep(5000)
MouseClick("left", 300, 300, 1)
Send("John")
Send ("{TAB}")
Send("your-mail-address@test123.123")
Send ("{TAB}")
Send("your subject")
Send ("{TAB}")
Send("your message body")
Sleep(1000)
MouseClick("right", 250,685, 1)
Sleep(2000)
MouseClick("left", 260,505, 1)
Sleep(1000)
;enter filename "captcha" 
Send("captcha")
Sleep(1000)
;press Enter
Send("{Enter}")
Sleep(1000)
;confirm with "y", if the PNG already exists ; y=yes
Send("y")
sleep(1000);

Now the captcha is converted into a PNG file on the desktop. This image now can be sent via Web API to the service 2captcha.com. On average, the solution of captchas is present after 10 seconds and can be pasted into the query window.

Captcha to solve: solve captchas automatically
Result from 2captcha.com: solve captchas automatically

 

 

 

 

To solve the captcha, send the picture via WEB request as a multipart document.

;we need a TCP connection for the webrequest
TCPStartup()
Local $file = "C:\Users\Heiko\Desktop\captcha.png"

$tcp = TCPConnect(TCPNameToIP("2captcha.com"), 80)
Local $boundary = "-----" & Random(10000000, 99999999, 1)

Local $data = "--" & $boundary & @CRLF
$data &= 'Content-Disposition: form-data; name="file"; filename="' & "captcha.PNG" & '"' & @CRLF & 'Content-Type: image/jpeg' & @CRLF & @CRLF

Local $data2 = @CRLF & "--" & $boundary & @CRLF & 'Content-Disposition: form-data; name="method"' & @CRLF & @CRLF & "post" & @CRLF
$data2 &= "--" & $boundary & "--" & @CRLF & @CRLF

Local $data3 = @CRLF & "--" & $boundary & @CRLF & 'Content-Disposition: form-data; name="key"' & @CRLF & @CRLF & "YourAPIKey" & @CRLF
$data3 &= "--" & $boundary & "--" & @CRLF & @CRLF

Local $header = 'POST /in.php HTTP/1.1' & @CRLF
$header &= 'Host: 2captcha.com' & @CRLF
$header &= 'User-Agent: AutoIt/'&@AutoItVersion&' ('&@OSVersion&'; '&@OSServicePack&'; '&@OSArch&')' & @CRLF
$header &= 'Content-Type: multipart/form-data; boundary=' & $boundary & @CRLF
$header &= 'Content-Length: ' & (StringLen($data) + StringLen($data2) + StringLen($data3) + FileGetSize($file)) & @CRLF & @CRLF

TCPSend($tcp, $header)
TCPSend($tcp, $data)
TCPSend($tcp, FileRead(FileOpen($file)))
TCPSend($tcp, $data2)
TCPSend($tcp, $data3)

$r = ""
$i = 1
;write server response in a tooltip
While ($i<10)
$r &= TCPRecv($tcp, 1024)
ToolTip($r, 0, 0, "Debug")
$i = $i+1
if($i = 9) then
Sleep(1000)
EndIf
WEnd

In the next step we take the CaptchaID we got as result for the uploaded picture and request the solution:

;Take the response from web request. In Line 10 you find the CaptchaID. You need this ID to request the captcha solution
 $r &= TCPRecv($tcp, 1024)
 $res = StringSplit($r,@CR) ; String split on every Carriage Return 
 ;_ArrayDisplay($res) ;Show content of string split
 ConsoleWrite("content: " & $res) ;show if you need
$captchaID = $res[10]
sleep(1000)
If (StringInStr($captchaID, "OK|") ) Then
 $captchaID = StringReplace($captchaID,"OK|","") ; remove "OK|"
 ;prepare information to request so solution
 $sPD = 'id=$captchaID'
 $oReceivedResult=""
 ;try 50 times if a solution is available.
 For $k = 50 to 1 Step -1
   Sleep(2000)
   $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
   $oHTTP.Open("GET", "http://2captcha.com/res.php?key=YourAPIKey&action=get&id=" & $captchaID, False)
   $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
   ; send request
   $oHTTP.Send($sPD)
   ; In the result you can see that the captcha is not solved yet or an another error message or the hopefully correct solution.
   $oReceived = $oHTTP.ResponseText
   $oStatusCode = $oHTTP.Status
   $oReceivedResult = $oReceivedResult & $oReceived & @CRLF
   ToolTip($oReceivedResult, 0, 200, "Captcha Check")
   $oReceived = StringReplace($oReceived,"OK|","") ; remove "OK|"
   If ($oReceived= "CAPCHA_NOT_READY") Then
     Sleep(500)
   Else
     $k=1
   EndIf
   If $oStatusCode <> 200 then
   MsgBox(4096, "Response code", $oStatusCode)
   EndIf
 Next

If the hopefully correct solution has been transferred, it can be written in the captcha result textbox.

MouseClick("left", 250,710, 1); click in the text entry box to enter the solution 

 Sleep(1000)

 Send($oReceived)

 Sleep(500)

 MouseClick("left", 250,750, 1); click the ok button

The following function takes the screenshot and saves it in a Dropbox folder. The link to the image will be sent by e-mail afterwards, so you possibly can ceck directly on the phone, if the result meets the specifications.

$filename = @MON&@MDAY&@HOUR&@MIN&@SEC ; filename is timestamp

 _ScreenCapture_SetJPGQuality(70) ; set jpg quality to 70 - should be enough

 _ScreenCapture_Capture("C:\Users\Heiko\Dropbox\Public\kitasucher_" & $filename & ".jpg") ; save screenshot file in a folder

 Return ("https://dl.dropboxusercontent.com/u/XXXXXXX_your_link_XXXXXXXX/kitasucher_" & $filename & ".jpg")

EndFunc ;==>Screen

In the next post i will show how you can send the screenshot via email.

Used Sources:
https://2captcha.com/api-2captcha

https://autoit.de/wiki/index.php/Tutorial

https://www.autoitscript.com/forum/

http://brugbart.com/http-get-request-autoit

http://videos.cctvcamerapros.com/digital-io-alarm-in-out/send-push-notifications-from-raspberry-pi.html#setup-push-apps

Heiko

10 Comments

  1. Antworten Literate Aspects

    Hello, thank you for the AutoIt data you have shared.

    Please, what is the name of the audio music you have on:

    https://www.youtube.com/watch?v=TrTTEx8stUE

    It is amazing, I have been trying to find this song title for a while, and I was very surprised to discover it on you video. It is one of my favorite songs.

  2. Antworten vajjump

    error

    „C:\Documents and Settings\Administrator.VAJRA-A55772675\Desktop\fg.au3“ (122) : ==> „If“ statement has no matching „EndIf“ statement.:
    Func _ScreenCapture_SetJPGQuality(70)

  3. Antworten Marco Broglia

    Hi

    I cant understand line 86=> „$r &amp;= TCPRecv($tcp, 1024)“
    I think is not an Autoit syntax.
    By the way, good job, congratulations.

    • Antworten Sebastian

      Hi Marco,
      sorry for late response. This was just a little „feature“ of WordPress that sometimes crashes copied code.
      I made some corrections and hope, that it’s working fine now.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht.

WordPress Cookie Hinweis von Real Cookie Banner