Jump to content

Recommended Posts

Posted (edited)

Hiya!

This is my first time EVER creating such thing, any suggestions, tips, etc. would be helpfull.

I have no idea how hard it is to crack such thing... Oh well.

Syntax

_CaptchaCode($iNumOfTries)

Screenshot

Posted Image

Example

#include <EditConstants.au3>
#include "CaptchaCode.au3"

$GUI = GUICreate("Register Form", 210, 110)
GUICtrlCreateLabel("Userame:", 10, 10)
GUICtrlCreateLabel("Email:", 10, 30)
GUICtrlCreateLabel("Password:", 10, 50)
$Username = GUICtrlCreateInput("", 100, 7, 100)
$Email = GUICtrlCreateInput("", 100, 27, 100)
$Password = GUICtrlCreateInput("", 100, 47, 100, 20, $ES_PASSWORD)
$Register = GUICtrlCreateButton("Register", 10, 75, 90)
$Clear = GUICtrlCreateButton("Clear", 110, 75, 90)

GUISetState()
While 1
    Switch GUIGetMsg()
    Case -3
        Exit
        
    Case $Register
        If GUICtrlRead($Username) <> "" And GUICtrlRead($Email) <> "" And GUICtrlRead($Password) <> "" Then
            If _CaptchaCode(3) Then
                MsgBox(64, Default, "Registration done!")
                $Data = ""
                $Data &= "Username  : " & GUICtrlRead($Username) & @CRLF
                $Data &= "Email     : " & GUICtrlRead($Email) & @CRLF
                $Data &= "Password  : " & GUICtrlRead($Password) & @CRLF
                FileWrite(@DesktopDir & "\register_form.txt", $Data)
            Else
                MsgBox(16, Default, "Tries maxed out, exiting program.")
                Exit
            EndIf
        Else
            MsgBox(16, Default, "Please fill in every information.")
        EndIf
        
    Case $Clear
        GUICtrlSetData($Username, "")
        GUICtrlSetData($Email, "")
        GUICtrlSetData($Password, "")
        
    EndSwitch
WEnd

CaptchaCode.au3

#include <GUIConstantsEx.au3>

; MD5 UDF: http://www.autoitscript.com/forum/index.php?showtopic=10590

$iOptOld = Opt("GUIEventOptions",0)
Opt("GUIEventOptions", $iOptOld)

Global $__CaptchaCode_cCharacter[6], $c__CaptchaCode_Label[5], $c__CaptchaCode_Line[2], $a__CaptchaCode_Font[6]
Global $__CaptchaCode_Tries

Func _CaptchaCode($iNumOfTries)
    Local $sCaptchaCode
    
    $__CaptchaCode_Tries = 0
    
    $hWnd = GUICreate("       * ENTER CODE *", 175, 150, -1, -1, 0x00080000)
    $cBg = GUICtrlCreateLabel("", 10, 10, 150, 50)
    GUICtrlSetState($cBg, 128)
    GUICtrlSetBkColor($cBg, 0x000000)
    $cInput = GUICtrlCreateInput("", 10, 70, 150, 20)
    $cCheck = GUICtrlCreateButton("Check", 10, 95, 70)
    $cNew = GUICtrlCreateButton("New", 90, 95, 70)
    
    $sCaptchaCode = __CaptchaCode_Create()
    
    $cFg = GUICtrlCreateLabel("", 10, 10, 150, 50)
    GUICtrlSetBkColor($cFg, -2)
        
    GUISetState()
    While 1
        Switch GUIGetMsg()
        Case -3
            
        Case $cNew
            __CaptchaCode_Delete()
            $sCaptchaCode = __CaptchaCode_Create()
            GUICtrlSetData($cInput, "")
            
        Case $cCheck
            If $__CaptchaCode_Tries == $iNumOfTries - 1 Then
                __CaptchaCode_Delete()
                GUIDelete($hWnd)
                Return False
                ExitLoop
            Else
                If (GUICtrlRead($cInput) == $sCaptchaCode) Then
                    __CaptchaCode_Delete()
                    GUIDelete($hWnd)
                    Return True
                    ExitLoop
                Else
                    $__CaptchaCode_Tries += 1
                    MsgBox(16, "* ERROR *", "Wrong Code!")
                    __CaptchaCode_Delete()
                    $sCaptchaCode = __CaptchaCode_Create()
                    GUICtrlSetData($cInput, "")
                EndIf
            EndIf
            
        EndSwitch
        
    WEnd
EndFunc

Func __CaptchaCode_Create()
    Local $sCaptcha = __CaptchaCode_Generate()
    Local $iX = Random(15, 25, 1)
    Local $iY = Random(15, 40, 1)
    
    $a__CaptchaCode_Font[0] = "Arial Black"
    $a__CaptchaCode_Font[1] = "Comic Sans MS"
    $a__CaptchaCode_Font[2] = "Impact"
    $a__CaptchaCode_Font[3] = "Tahoma"
    $a__CaptchaCode_Font[4] = "System"
    $a__CaptchaCode_Font[5] = "Verdama"
    
    $aSplit = StringSplit($sCaptcha, "")
    For $x = 0 To 5
        $__CaptchaCode_cCharacter[$x] = GUICtrlCreateLabel($aSplit[$x + 1], $iX, $iY)
        $iX += 20 + Random(1, 5, 1)
        $iY = Random(15, 40, 1)
        
        GUICtrlSetBkColor($__CaptchaCode_cCharacter[$x], -2)
        GUICtrlSetColor($__CaptchaCode_cCharacter[$x], Random(0x808080, 0xC0C0C0))
        
        $iNum = Random(1, 7, 1)
        Switch $iNum
        Case 1
            $iStyle = 2 ;2
        Case 2
            $iStyle = 4 ;4
        Case 3
            $iStyle = 8 ;8
        Case 4
            $iStyle = 2 + 4 ;6
        Case 5
            $iStyle = 4 + 8 ;12
        Case 6
            $iStyle = 2 + 8 ;10
        Case 7
            $iStyle = 2 + 4 + 8 ;16
        EndSwitch
        
        GUICtrlSetFont($__CaptchaCode_cCharacter[$x], 13, Random(350, 450, 1), $iStyle, $a__CaptchaCode_Font[Random(0, 5, 1)])
        
        Sleep(10)
    Next
    
    $iX = 15 + Random(1, 10, 1)
    $iY = 5 + Random(1, 10, 1)
    For $z = 0 To 4
        $c__CaptchaCode_Label[$z] = GUICtrlCreateLabel("", $iX, $iY, 1, 30)
        $iX += 25 + Random(1, 10, 1)
        $iY = 5 + Random(1, 30, 1)
        
        GUICtrlSetBkColor($c__CaptchaCode_Label[$z], Random(0x808080, 0xC0C0C0))
    Next
    
    $iX = 5 + Random(1, 25, 1)
    $iY = 20 + Random(1, 10, 1)
    For $b = 0 To 1
        $c__CaptchaCode_Line[$b] = GUICtrlCreateLabel("", $iX, $iY, 130, 1)
        $iX = 5 + Random(1, 10, 1)
        $iY += 10 + Random(1, 10, 1)
        
        GUICtrlSetBkColor($c__CaptchaCode_Line[$b], Random(0x808080, 0xC0C0C0))
    Next
    
    Return $sCaptcha
EndFunc

Func __CaptchaCode_Delete()
    For $y = 0 To 5
        GUICtrlDelete($__CaptchaCode_cCharacter[$y])
    Next
    
    For $a = 0 To 4
        GUICtrlDelete($c__CaptchaCode_Label[$a])
    Next
    
    For $c = 0 To 1
        GUICtrlDelete($c__CaptchaCode_Line[$c])
    Next
EndFunc

Func __CaptchaCode_Generate()
    Local $sCharacters = StringSplit("BCDFGHJKLMNPQRSTVWXYZ1234567890", "")
    Local $sCode = ""
    
    For $i = 1 To 6
        $sCode &= $sCharacters[Random(1, $sCharacters[0], 1)]
    Next
    
    Return $sCode   
EndFunc

UPDATE 09-04-10: Changed variable names, function names, added a tries system

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Posted (edited)

Looks nice :(...

I recommend that you add

$iOptOld = Opt("GUIEventOptions",0)

Opt("GUIEventOptions",$iOptOld)

at the beginning and end of the _CreateCaptcha() function.

Edit:

And also you should rename the global variables used to a more UDF related name, something like

Global $__CaptchaCode_cCharacter[6], $c__CaptchaCode_Label[5], $c__CaptchaCode_Line[2], $a__CaptchaCode_Font[6]

And maybe the functions too...

_CaptchaCode()

__CaptchaCode_Create()

__CaptchaCode_Delete()

__CaptchaCode_Generate()

Edited by KaFu
Posted

I would make it have a return type of true / false rather than calling a function. Just opinion. I would also work on the spelling of 'success' :( . If you intend to use this properly then lots of labels will not work, as simply going through all the windows children looking for controls with the class 'Static' and reading those will crack it. An easy solution is to give the lines their own text, but that will also be easy enough to figure out from the size. Ultimately, GUICtrlCreateGraphic is the solution... Or disable all the labels and put another label over the top. I haven't tried that but it could work.... (I had to try it out :) see spoiler)

  Reveal hidden contents

In answer to your remark of: 'I have no idea how hard it is to crack such thing... Oh well.', it would be easy enough. But it looks impressive.

Posted

Well, I tinkered around a PHP captcha a while ago. To make that work, one way is to create a captach from a string, then encypt this string with a one-way algo. (e.g. md5()), send that as a cookie, and finally compare the md5() value of the users input with the value of the cookie... might be an issue here too, if you want to make it hacker proof :( (I guess it might be possible to read the unencrypted string directly from memory).

Posted

Nice, there is few similar examples of generating captcha: http://www.autoitscript.com/forum/index.php?showtopic=85143

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

  On 4/8/2010 at 2:12 PM, 'KaFu said:

Looks nice :)...

I recommend that you add

$iOptOld = Opt("GUIEventOptions",0)

Opt("GUIEventOptions",$iOptOld)

at the beginning and end of the _CreateCaptcha() function.

Edit:

And also you should rename the global variables used to a more UDF related name, something like

Global $__CaptchaCode_cCharacter[6], $c__CaptchaCode_Label[5], $c__CaptchaCode_Line[2], $a__CaptchaCode_Font[6]

And maybe the functions too...

_CaptchaCode()

__CaptchaCode_Create()

__CaptchaCode_Delete()

__CaptchaCode_Generate()

Thanks! And, yeah... Probably a good idea.

  On 4/8/2010 at 2:35 PM, 'Mat said:

I would make it have a return type of true / false rather than calling a function.

True... *changes*

  On 4/8/2010 at 3:21 PM, 'KaFu said:

Well, I tinkered around a PHP captcha a while ago. To make that work, one way is to create a captach from a string, then encypt this string with a one-way algo. (e.g. md5()), send that as a cookie, and finally compare the md5() value of the users input with the value of the cookie... might be an issue here too, if you want to make it hacker proof :) (I guess it might be possible to read the unencrypted string directly from memory).

I wanted to encrypt it at first thought, not a good idea though, so I will try your way. :(

  On 4/8/2010 at 10:04 PM, 'MrCreatoR said:

Nice, there is few similar examples of generating captcha: http://www.autoitscript.com/forum/index.php?showtopic=85143

Oh, thanks for showing! :D

  On 4/8/2010 at 11:44 PM, 'Beege said:

Pretty cool! ;)

Thanks! B) Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Posted (edited)

Updated! See first post.

  'KaFu said:

Well, I tinkered around a PHP captcha a while ago. To make that work, one way is to create a captach from a string, then encypt this string with a one-way algo. (e.g. md5()), send that as a cookie, and finally compare the md5() value of the users input with the value of the cookie... might be an issue here too, if you want to make it hacker proof (I guess it might be possible to read the unencrypted string directly from memory).

Eh, I failed at that part. :)

  On 4/8/2010 at 2:35 PM, 'Mat said:

Or disable all the labels and put another label over the top.

Thanks for that! :(

Ouch. I actualy DO need graphics... :)

If WinExists(WinGetTitle("", "New")) Then
    $Text = WinGetText(WinGetTitle("", "New"))
    
    $Split = StringSplit($Text, Chr(10))
    For $i = 3 To $Split[0]
        ControlSend(WinGetTitle("", "New"), "", "[CLASS:Edit; INSTANCE:1]", StringUpper($Split[$i]))
    Next
EndIf
Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Posted

You've put the $iOptOld = Opt("GUIEventOptions", 0) and Opt("GUIEventOptions", $iOptOld) at the wrong place. Additionally I added the md5 crypt I meant...

#include <GUIConstantsEx.au3>
#include <Crypt.au3>

Global $__CaptchaCode_cCharacter[6], $c__CaptchaCode_Label[5], $c__CaptchaCode_Line[2], $a__CaptchaCode_Font[6]
Global $__CaptchaCode_Tries

Func _CaptchaCode($iNumOfTries)
    $iOptOld = Opt("GUIEventOptions", 0)

    Local $sCaptchaCode

    $__CaptchaCode_Tries = 0

    $hWnd = GUICreate("ENTER CODE", 175, 160, -1, -1, 0x00080000)
    $cBg = GUICtrlCreateLabel("", 10, 10, 150, 50)
    GUICtrlSetState($cBg, 128)
    GUICtrlSetBkColor($cBg, 0x000000)
    $cInput = GUICtrlCreateInput("", 10, 70, 150, 20)
    $cCheck = GUICtrlCreateButton("Check", 10, 95, 70)
    $cNew = GUICtrlCreateButton("New", 90, 95, 70)

    $sCaptchaCode = __CaptchaCode_Create()

    $cFg = GUICtrlCreateLabel("", 10, 10, 150, 50)
    GUICtrlSetBkColor($cFg, -2)

    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case -3
                ExitLoop

            Case $cNew
                __CaptchaCode_Delete()
                $sCaptchaCode = __CaptchaCode_Create()
                GUICtrlSetData($cInput, "")

            Case $cCheck
                If $__CaptchaCode_Tries == $iNumOfTries - 1 Then
                    ExitLoop
                Else
                    If (_Crypt_HashData(GUICtrlRead($cInput),$CALG_MD5) == $sCaptchaCode) Then
                        __CaptchaCode_Delete()
                        GUIDelete($hWnd)
                        Opt("GUIEventOptions", $iOptOld)
                        Return True
                    Else
                        $__CaptchaCode_Tries += 1
                        MsgBox(16, "* ERROR *", "Wrong Code!")
                        __CaptchaCode_Delete()
                        $sCaptchaCode = __CaptchaCode_Create()
                        GUICtrlSetData($cInput, "")
                    EndIf
                EndIf
        EndSwitch
    WEnd

    __CaptchaCode_Delete()
    GUIDelete($hWnd)
    Opt("GUIEventOptions", $iOptOld)
    Return False

EndFunc   ;==>_CaptchaCode

Func __CaptchaCode_Create()
    Local $sCaptcha = __CaptchaCode_Generate()
    Local $iX = Random(15, 25, 1)
    Local $iY = Random(15, 40, 1)

    $a__CaptchaCode_Font[0] = "Arial Black"
    $a__CaptchaCode_Font[1] = "Comic Sans MS"
    $a__CaptchaCode_Font[2] = "Impact"
    $a__CaptchaCode_Font[3] = "Tahoma"
    $a__CaptchaCode_Font[4] = "System"
    $a__CaptchaCode_Font[5] = "Verdama"

    $aSplit = StringSplit($sCaptcha, "")
    For $x = 0 To 5
        $__CaptchaCode_cCharacter[$x] = GUICtrlCreateLabel($aSplit[$x + 1], $iX, $iY)
        $iX += 20 + Random(1, 5, 1)
        $iY = Random(15, 40, 1)

        GUICtrlSetBkColor($__CaptchaCode_cCharacter[$x], -2)
        GUICtrlSetColor($__CaptchaCode_cCharacter[$x], Random(0x808080, 0xC0C0C0))

        $iNum = Random(1, 7, 1)
        Switch $iNum
            Case 1
                $iStyle = 2 ;2
            Case 2
                $iStyle = 4 ;4
            Case 3
                $iStyle = 8 ;8
            Case 4
                $iStyle = 2 + 4 ;6
            Case 5
                $iStyle = 4 + 8 ;12
            Case 6
                $iStyle = 2 + 8 ;10
            Case 7
                $iStyle = 2 + 4 + 8 ;16
        EndSwitch

        GUICtrlSetFont($__CaptchaCode_cCharacter[$x], 13, Random(350, 450, 1), $iStyle, $a__CaptchaCode_Font[Random(0, 5, 1)])

        Sleep(10)
    Next

    $iX = 15 + Random(1, 10, 1)
    $iY = 5 + Random(1, 10, 1)
    For $z = 0 To 4
        $c__CaptchaCode_Label[$z] = GUICtrlCreateLabel("", $iX, $iY, 1, 30)
        $iX += 25 + Random(1, 10, 1)
        $iY = 5 + Random(1, 30, 1)
        GUICtrlSetBkColor($c__CaptchaCode_Label[$z], Random(0x808080, 0xC0C0C0))
    Next

    $iX = 5 + Random(1, 25, 1)
    $iY = 20 + Random(1, 10, 1)
    For $b = 0 To 1
        $c__CaptchaCode_Line[$b] = GUICtrlCreateLabel("", $iX, $iY, 130, 1)
        $iX = 5 + Random(1, 10, 1)
        $iY += 10 + Random(1, 10, 1)

        GUICtrlSetBkColor($c__CaptchaCode_Line[$b], Random(0x808080, 0xC0C0C0))
    Next

    Return _Crypt_HashData($sCaptcha,$CALG_MD5)
EndFunc   ;==>__CaptchaCode_Create

Func __CaptchaCode_Delete()
    For $y = 0 To 5
        GUICtrlDelete($__CaptchaCode_cCharacter[$y])
    Next

    For $a = 0 To 4
        GUICtrlDelete($c__CaptchaCode_Label[$a])
    Next

    For $c = 0 To 1
        GUICtrlDelete($c__CaptchaCode_Line[$c])
    Next
EndFunc   ;==>__CaptchaCode_Delete

Func __CaptchaCode_Generate()
    Local $sCharacters = StringSplit("BCDFGHJKLMNPQRSTVWXYZ1234567890", "")
    Local $sCode = ""

    For $i = 1 To 6
        $sCode &= $sCharacters[Random(1, $sCharacters[0], 1)]
    Next

    Return $sCode
EndFunc   ;==>__CaptchaCode_Generate

  On 4/9/2010 at 7:47 AM, 'AlmarM said:

Ouch. I actualy DO need graphics... :)

Definitly, maybe Yashied has something in his vault for you :(...
Posted

  On 4/9/2010 at 9:55 PM, 'JamesBrooks said:

Why would you store such data in a cookie? Sessions are much better for this type of thing.

If I remember correctly I've had some trouble with Session ID at that time. But why not use a one way encrypted / hashed value instead in a cookie? At least I can't see no security risk there, worst possible case is that cookie's blocked... users fault.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...