Sign in to follow this  
Followers 0
cambradford

TrueCrypt Volume Creation Automation

4 posts in this topic

Hey guys, I am very unfamiliar with AutoIt3 and scripting in general. I've been playing with a piece of code I found on these forums a while back trying to automate TrueCrypt volume creation on Windows. The script performs as it should but I have two problems that I could use guidance on.

1) Specifying where the TrueCrypt volume is stored at. Right now it goes into the directory of the script.

2) Getting around the iniReader, storing the password in a settings.ini file is not the best approach, are there ways to prompt the user for this input? (as well as other configs)

Here is what I'm working with 

#include <winapi.au3>
#include <Constants.au3>

AutoItSetOption("WinDetectHiddenText", 1)
Opt("WinTitleMatchMode", 3)
Global $PID_TC_Format = 0

Global $Encryption = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "EncryptionAlgorithm", "AES")
Global $Hash = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "HashAlgorithm", "RIPEMD-160")
Global $Size = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "SizeInKB", "10000")
Global $FileSystemType = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "FileSystemType", "NTFS")
Global $Password = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "Password", "password")
Global $Location = iniRead("C:\Users\Cameron\workspace\settings.ini", "settings", "Location", "location")
    ConsoleWrite("_TC_Container_Create("") returned " & _TC_Container_Create("") & @TAB & @error & @CRLF)

Func _TC_Container_Create($TC_Container_Location = $Location, $TC_Container_Encryption = $Encryption, $TC_Container_Hash = $Hash, $TC_Container_Size_KB = $Size, $TC_Container_Password = $Password, $TC_Container_Filesystem = $FileSystemType)

    Local $b_TC_Volume_Creation_Wizard_failed = False, $hWnd_TC_Volume_Creation_Wizard = 0, $TC_Volume_Creation_Wizard_Error_Code = 0

    ; NTFS min Size = 2829 KB
    ; FAT min Size = 275 KB
    ; ControlSetText() if it's just text you're sending. It's much faster.

    If Not $TC_Container_Location Then $TC_Container_Location = @ScriptDir & "\Test.tc"
    If $TC_Container_Size_KB < 275 Then $TC_Container_Size_KB = 275
    If $TC_Container_Size_KB < 2829 Then $TC_Container_Filesystem = "FAT"

    If FileExists($TC_Container_Location) Then
        If MsgBox(262420, "TCMyFiles", "The Target File" & @CRLF & @CRLF & $TC_Container_Location & @CRLF & @CRLF & "already exists. Do you want to overwrite the file?") <> 6 Then
            SetError(99)
            Return 0
        EndIf
        FileDelete($TC_Container_Location)
    Else
        $TC_Check_Location = FileOpen($TC_Container_Location, 10)
        If $TC_Check_Location <> -1 Then
            FileClose($TC_Check_Location)
            FileDelete($TC_Container_Location)
        Else
            SetError(96)
            Return 0
        EndIf
    EndIf

    $PID_TC_Format = Run('"C:\Program Files\TrueCrypt\TrueCrypt Format.exe"', @ScriptDir, @SW_HIDE)
    ;$PID_TC_Format = Run('"' & @ScriptDir & '\TrueCrypt\TrueCrypt Format.exe"', @ScriptDir, @SW_SHOW)

    ProcessWait($PID_TC_Format, 5)

    If $PID_TC_Format = 0 Then
        MsgBox(0, 'Error', '"TrueCrypt Format.exe" new PID could not be detected.')
        SetError(98)
        Return 0
    Else
        If WinWait("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:CustomDlg]", "", 10) = 0 Then
            MsgBox(0, 'Error', '"TrueCrypt Volume Creation Wizard" window not found...')
            SetError(97)
            Return 0
        Else

            $hWnds = WinList("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:CustomDlg]", "")

            For $I = 0 To UBound($hWnds) - 1
                If WinGetProcess($hWnds[$I][1]) = $PID_TC_Format Then $hWnd_TC_Volume_Creation_Wizard = $hWnds[$I][1]
            Next

            If $hWnd_TC_Volume_Creation_Wizard <> 0 Then
                ConsoleWrite('Step 01: Found TrueCrypt Volume Creation Wizard hWnd: ' & $hWnd_TC_Volume_Creation_Wizard & @CRLF)
                $TC_Volume_Creation_Wizard_Error_Code = 1
            Else
                $b_TC_Volume_Creation_Wizard_failed = True
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Encrypt the system partition or entire system drive") Then
                        ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                        ConsoleWrite('Step 02: Create File Container' & @CRLF)
                        $TC_Volume_Creation_Wizard_Error_Code = 2
                        ExitLoop
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Type") Then
                        ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                        ConsoleWrite('Step 03: Select Standard Type' & @CRLF)
                        $TC_Volume_Creation_Wizard_Error_Code = 3
                        ExitLoop
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Location") Then
                        ControlSetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]', $TC_Container_Location, 1)
                        If ControlGetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]') = $TC_Container_Location Then
                            ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                            ConsoleWrite('Step 04: Set Container location to ' & $TC_Container_Location & @CRLF)
                            $TC_Volume_Creation_Wizard_Error_Code = 4
                            ExitLoop
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Encryption Options") Then
                        ControlCommand($hWnd_TC_Volume_Creation_Wizard, "", "[CLASS:ComboBox; INSTANCE:1]", "SelectString", $TC_Container_Encryption)
                        If ControlCommand($hWnd_TC_Volume_Creation_Wizard, "", "[CLASS:ComboBox; INSTANCE:1]", "GetCurrentSelection", "") = $TC_Container_Encryption Then
                            ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                            ConsoleWrite('Step 05: Set Encryption Algorithm to ' & $TC_Container_Encryption & @CRLF)
                            $TC_Volume_Creation_Wizard_Error_Code = 5
                            ExitLoop
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Size") Then
                        If _WinAPI_GetWindowLong(ControlGetHandle($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:6]'), $GWL_STYLE) <> 1342373897 Then
                            ControlClick($hWnd_TC_Volume_Creation_Wizard, "", ControlGetHandle($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:6]'))
                        ElseIf _WinAPI_GetWindowLong(ControlGetHandle($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:6]'), $GWL_STYLE) = 1342373897 Then
                            ConsoleWrite('Step 06: Select KB as Container Size' & @CRLF)
                            $TC_Volume_Creation_Wizard_Error_Code = 6
                            ExitLoop
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Size") Then
                        ControlSetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]', $TC_Container_Size_KB, 1)
                        If ControlGetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]') = $TC_Container_Size_KB Then
                            ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                            ConsoleWrite('Step 07: Set Container Size to ' & $TC_Container_Size_KB & ' KB' & @CRLF)
                            $TC_Volume_Creation_Wizard_Error_Code = 7
                            ExitLoop
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Password") Then
                        ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:6]')
                        ControlSetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]', $TC_Container_Password, 1)
                        If ControlGetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:1]') = $TC_Container_Password Then
                            ControlSetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:2]', $TC_Container_Password, 1)
                            If ControlGetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Edit; INSTANCE:2]') = $TC_Container_Password Then
                                ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                                ConsoleWrite('Step 08: Set Container Password to "' & $TC_Container_Password & '"' & @CRLF)
                                $TC_Volume_Creation_Wizard_Error_Code = 8
                                ExitLoop
                            EndIf
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
                ControlClick("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:#32770]", '', '[CLASS:Button; INSTANCE:1]')
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While TimerDiff($TC_Timer) < 5000
                    If WinExists("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:#32770]", "") = 1 Then ControlClick("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:#32770]", '', '[CLASS:Button; INSTANCE:1]')
                    If StringInStr(WinGetText($hWnd_TC_Volume_Creation_Wizard), "Volume Format") Then
                        ControlCommand($hWnd_TC_Volume_Creation_Wizard, "", "[CLASS:ComboBox; INSTANCE:1]", "SelectString", $TC_Container_Filesystem)
                        If ControlCommand($hWnd_TC_Volume_Creation_Wizard, "", "[CLASS:ComboBox; INSTANCE:1]", "GetCurrentSelection", "") = $TC_Container_Filesystem Then
                            ConsoleWrite('Step 09: Set Container File Format to ' & $TC_Container_Filesystem & @CRLF)
                            $TC_Volume_Creation_Wizard_Error_Code = 9
                            ControlClick($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Button; INSTANCE:3]')
                            ExitLoop
                        EndIf
                    Else
                        $b_TC_Volume_Creation_Wizard_failed = True
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Container_Creation_Progress = ""
                $TC_Container_Creation_Progress_Save = ""
                $TC_Timer = TimerInit()
                While 1
                    $TC_Container_Creation_Progress = StringReplace(ControlGetText($hWnd_TC_Volume_Creation_Wizard, '', '[CLASS:Static; INSTANCE:12]'), "%", "")
                    If ($TC_Container_Creation_Progress <> $TC_Container_Creation_Progress_Save) And $TC_Container_Creation_Progress <> 100 Then
                        $TC_Timer = TimerInit()
                        $TC_Container_Creation_Progress_Save = $TC_Container_Creation_Progress
                        ConsoleWrite("Step 10: Container creation progress: " & $TC_Container_Creation_Progress & "%" & @CRLF)
                    EndIf
                    If $TC_Container_Creation_Progress = 100.000 Then
                        ConsoleWrite("Step 10: Container creation progress: " & $TC_Container_Creation_Progress & "%" & @CRLF)
                        $TC_Volume_Creation_Wizard_Error_Code = 10
                        ExitLoop
                    EndIf
                    If TimerDiff($TC_Timer) > 5000 Then
                        ConsoleWrite("Step 10: No progress in 5 seconds... failure?" & @CRLF)
                        $b_TC_Volume_Creation_Wizard_failed = True
                        ExitLoop
                    EndIf
                WEnd
            EndIf

            If $b_TC_Volume_Creation_Wizard_failed = False Then
                $TC_Timer = TimerInit()
                While 1
                    If WinExists("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:#32770]", "The TrueCrypt volume has been successfully created.") = 1 Then
                        ControlClick("[TITLE:TrueCrypt Volume Creation Wizard; CLASS:#32770]", '', '[CLASS:Button; INSTANCE:1]')
                        ConsoleWrite("Step 11: Container creation finished successfully" & @CRLF)
                        $TC_Volume_Creation_Wizard_Error_Code = 11
                        ExitLoop
                    EndIf
                    If TimerDiff($TC_Timer) > 15000 Then
                        ConsoleWrite("Step 11: Progress at 100% but TC failed to notify about success for 15 seconds...failure?" & @CRLF)
                        $b_TC_Volume_Creation_Wizard_failed = True
                        ExitLoop
                    EndIf
                WEnd
            EndIf

        EndIf
    EndIf

    SetError($TC_Volume_Creation_Wizard_Error_Code)
    If $b_TC_Volume_Creation_Wizard_failed = True Then
        Return 0
    Else
        Return 1
    EndIf

EndFunc   ;==>_TC_Container_Create

Func OnAutoItExit()
    ProcessClose($PID_TC_Format)
EndFunc   ;==>OnAutoItExit

Share this post


Link to post
Share on other sites



first, TrueCrypt supports command-line switches; the major part of your code involves manipulating the GUI, which is unnecessary. when a single line would do the job. ref: http://www.truecrypt.org/docs/command-line-usage

for the container location - obviously you have much to learn on how to call functions, as you can tell the function where to put it, but you don't. you should read the AutoIt help, section "Language Reference" the page about functions.

for user input see the help for function InputBox.

also, just for my curiosity, how did you come about to need automation for volume creation? this is usually a one-time task.

Share this post


Link to post
Share on other sites

Volume creation is not yet supported on Windows cmd, only mac/linux, therefore that is why I am trying to automate the GUI. 

What does not make sense, is I should be able to edit the $TC_Container_Location variable like I edited the other variables and it should place it wherever I set the path. 

Thank you for the inputBox reference, also I am doing a project of spinning up multiple truecrypt volumes and want to automate the process to increase speed.

Share this post


Link to post
Share on other sites

"Volume creation is not yet supported on Windows cmd" - true, my bad.

as for the location, note that you call this command if your script:

_TC_Container_Create("")

this command is telling the function _TC_Container_Create that the first parameter, which is the location, is an empty string.

because of this, the following line is in effect:

If Not $TC_Container_Location Then $TC_Container_Location = @ScriptDir & "Test.tc"

hence the volume is created in the script folder.

if you want to use only defaults, the function call should be like this:

_TC_Container_Create()

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0