Edek Posted August 1, 2008 Share Posted August 1, 2008 Hi.This is a script I created to automate mounting TrueCrypt encrypted volumes.The idea is to keep keyfiles of encrypted volumes on USB pendrive only.Only after plugging pendrive to computer it is possible to mount encrypted volumes stored on this computer.Pendrive may but don't have to be encrypted.To automate volume mounting simply compile this script an put executable on your pendrive.You must also edit configuration file. This file have INI format. Configuration file have the same name as script/executable with INI extension. For example if your script is named MountCryptedDrive.au3 then configuration file is MountCryptedDrive.ini. In [Automount] section you specify volumes to mount at script startup. This script may create simple tray menu if [TrayMenu] section is present in configuration file. See sample configuration file below.To mount TrueCrypt volume this script by default uses TrueCrypt that is installed on your computer. If TrueCrypt is not installed this script assumes that TrueCrypt.exe is located on App sub directory.When you set Portable=1 in [General] section of configuration file then this script doesn't look for TrueScript installation on your computer and assumes that TrueCrypt.exe is located on App sub directory directly.This compiled script works fine for me with PortableApps.This is script:expandcollapse popup#NoTrayIcon Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown. #include <File.au3> #include <Array.au3> #include <Constants.au3> Global Const $APP_TITLE = "TrueCrypt volume (auto)mounter" Global Const $APP_VERSION = "0.1" Global Const $TC_UNINSTALL_KEY = "\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\TrueCrypt" ; uninstall key Func get_app_uninstall_string($appkey) Dim $app_dir = RegRead("HKEY_LOCAL_MACHINE" & $appkey, "UninstallString") If $app_dir == "" Then $app_dir = RegRead("HKEY_CURRENT_USER" & $appkey, "UninstallString") EndIf Return $app_dir EndFunc ;==>get_app_uninstall_string ; parse uninstall script Func get_app_uninstall_exe($sUninstallString) Local $aExe = StringRegExp($sUninstallString, """(.+)"" /u", 1) If Not @error Then Return SetError(0, @extended, $aExe[0]) Else Return SetError(@error, @extended, @error) EndIf EndFunc ;==>get_app_uninstall_exe Func extract_dir($sFullPath) Local $sDrive, $sDir, $sFName, $sExt _PathSplit($sFullPath, $sDrive, $sDir, $sFName, $sExt) Return _PathMake($sDrive, $sDir, "TrueCrypt", "exe") EndFunc ;==>extract_dir Func get_tc_executable_from_registry() Local $sUninstallString = get_app_uninstall_string($TC_UNINSTALL_KEY) If StringLen($sUninstallString) = 0 Then Return SetError(1, 0, "") EndIf Local $sExe = get_app_uninstall_exe($sUninstallString) If @error Then Return SetError(1, $sExe, "") EndIf Local $sTC = extract_dir($sExe) Return SetError(0, 0, $sTC) EndFunc ;==>get_tc_executable_from_registry Func get_tc_portable() Return _PathFull(@ScriptDir & "\App\TrueCrypt.exe") EndFunc ;==>get_tc_portable Func get_tc_executable($sConfigFile) Local $sTC = "" Local $nPortable = IniRead($sConfigFile, "General", "Portable", 0) If $nPortable = 1 Then $sTC = get_tc_portable() Else $sTC = get_tc_executable_from_registry() If @error Then ; second chance, portable $sTC = get_tc_portable() EndIf EndIf Return $sTC EndFunc ;==>get_tc_executable Func get_script_name() Local $sName = @ScriptName Local $n = StringInStr($sName, ".", 0, -1) If @error Or $n = 0 Then Return $sName Else Return StringLeft($sName, $n - 1) EndIf EndFunc ;==>get_script_name Func create_config_file($sConfigFile) IniWriteSection($sConfigFile, "General", "Portable=0") IniWriteSection($sConfigFile, "Automount", "Example1=MyMount1" & @LF & "Example2=MyMount2") IniWriteSection($sConfigFile, "TrayMenu", "Example1=MyMount1" & @LF & "Example2=MyMount2" & @LF & "Tray=TrayMountOnly") IniWriteSection($sConfigFile, "MyMount1", "Volume=exvolume1.tc" & @LF & "KeyFile=kf1.key" & @LF & "Drive=W") IniWriteSection($sConfigFile, "MyMount2", "Volume=exvolume2.tc" & @LF & "KeyFile=mykeyfile.key" & @LF & "Drive=K") IniWriteSection($sConfigFile, "TrayMountOnly", "Volume=\Device\Harddisk0\Partition0" & @LF & "KeyFile=traymount.key") EndFunc ;==>create_config_file ; Config file (INI) Global Const $CONFIG_FILE = @ScriptDir & "\" & get_script_name() & ".ini" If Not FileExists($CONFIG_FILE) Then If MsgBox(0x14, $APP_TITLE, "Configuration file doesn't exists." & @CRLF & "Do you want to create sample configuration file?") = 6 Then create_config_file($CONFIG_FILE) ShellExecute($CONFIG_FILE) EndIf Exit 100 EndIf ; TrueCrypt executable Global Const $TRUE_CRYPT = get_tc_executable($CONFIG_FILE) If Not FileExists($TRUE_CRYPT) Then Exit 200 EndIf Func read_tt_data($sConfigFile, $sSection, _ ByRef $sVolume, ByRef $sDrive, ByRef $sKeyFile) $sKeyFile = IniRead($sConfigFile, $sSection, "KeyFile", "") If StringLen($sKeyFile) = 0 Then Return SetError(1, "KeyFile", False) EndIf $sVolume = IniRead($sConfigFile, $sSection, "Volume", "") If StringLen($sVolume) = 0 Then Return SetError(1, "Volume", False) EndIf $sDrive = IniRead($sConfigFile, $sSection, "Drive", "") Return True EndFunc ;==>read_tt_data Func run_tt($sVolume, $sDrive, $sKeyFile) Local $sCmdParams = "/b /c n /h n /q" $sCmdParams &= " /v " & $sVolume ; volume $sCmdParams &= " /k """ & $sKeyFile & """" ; keyfile If StringLen($sDrive) = 0 Then $sCmdParams &= " /a" Else $sCmdParams &= " /l " & $sDrive EndIf Local $nExitCode = RunWait("""" & $TRUE_CRYPT & """ " & $sCmdParams, @ScriptDir) If @error Then ClipPut($sCmdParams) Return SetError(@error, "Cannot run TrueCrypt", $nExitCode) Else Return SetError(0, False, $nExitCode) EndIf EndFunc ;==>run_tt ; Automount section Dim $aAutoMount = IniReadSection($CONFIG_FILE, "Automount") Dim $i, $nExitCode Dim $sVolume, $sDrive, $sKeyFile If Not @error Then For $i = 1 To $aAutoMount[0][0] If read_tt_data($CONFIG_FILE, $aAutoMount[$i][1], $sVolume, $sDrive, $sKeyFile) Then $nExitCode = run_tt($sVolume, $sDrive, $sKeyFile) If @error Or $nExitCode > 0 Then MsgBox(0x10, $APP_TITLE & " : " & $aAutoMount[$i][0], "Fail to mount volume " & $sVolume & " ." & @CRLF & "Exit code: " & $nExitCode & ".", 10) EndIf EndIf Next EndIf ; TrayMenu section Dim $aTrayMenu = IniReadSection($CONFIG_FILE, "TrayMenu") Dim $aTrayMenuItems[1] = [0] Dim $exititem, $aboutitem, $tcitem, $dismountitem If Not @error Then For $i = 1 To $aTrayMenu[0][0] Dim $mt[2] = [TrayCreateItem($aTrayMenu[$i][0]), $i] _ArrayAdd($aTrayMenuItems, $mt) $aTrayMenuItems[0] += 1 Next TrayCreateItem("") $tcitem = TrayCreateItem("Show TrueCrypt") $dismountitem = TrayCreateItem("Dismount all volumes") TrayCreateItem("") $aboutitem = TrayCreateItem("&About") $exititem = TrayCreateItem("E&xit") If Not @Compiled Then TraySetIcon(@ScriptDir & "\TrueCrypt.ico") EndIf TraySetState() TraySetToolTip($APP_TITLE) While True $msg = TrayGetMsg() Select Case $msg = 0 ContinueLoop Case $msg = $exititem ExitLoop Case $msg = $aboutitem MsgBox(0x40, $APP_TITLE, _ "Simple TrueCrypt (auto)mounter." & @CRLF & _ "TrueCrypt executable: " & $TRUE_CRYPT & @CRLF & _ "Configuration file: " & $CONFIG_FILE & @CRLF & _ "Version: " & $APP_VERSION & "." & @CRLF & _ "AutoIt version: " & @AutoItVersion & "." & @CRLF & _ "Author: Edmunt Pienkowsky." & @CRLF & _ "E-mail: roed@onet.eu." & @CRLF & _ "Icon: http://rocketdock.com/addons/icons/2773") Case $msg = $tcitem Or $msg = $TRAY_EVENT_PRIMARYDOUBLE Run($TRUE_CRYPT, @ScriptDir) Case $msg = $dismountitem Run("""" & $TRUE_CRYPT & """ /d /q") Case Else For $i = 1 To $aTrayMenuItems[0] $mt = $aTrayMenuItems[$i] If $msg = $mt[0] Then If read_tt_data($CONFIG_FILE, $aTrayMenu[$mt[1]][1], $sVolume, $sDrive, $sKeyFile) Then $nExitCode = run_tt($sVolume, $sDrive, $sKeyFile) If Not @error And $nExitCode = 0 Then TrayTip($APP_TITLE, "Volume " & $sVolume & " mounted successfully.", 10, 1) Else TrayTip($APP_TITLE, "Fail to mount volume " & $sVolume & " ." & @CRLF & "Exit code: " & $nExitCode & ".", 10, 3) EndIf EndIf EndIf Next EndSelect WEnd EndIfThis is a simple configuration file:[General] Portable=0 [TrayMenu] Example=ExampleVolume TT=TrueCrypt [Automount] Example=ExampleVolume [ExampleVolume] Volume=ExampleVolume.tc KeyFile=mykey.mp3 Drive=N [TrueCrypt] Volume=\Device\Harddisk0\Partition2 KeyFile=pkey.mp3Icon file you can find at: http://rocketdock.com/addons/icons/2773. Link to comment Share on other sites More sharing options...
TheSaint Posted October 4, 2008 Share Posted October 4, 2008 Hi Edek, you might like to check out my Open Projects program, available here. Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
TheSaint Posted October 4, 2008 Share Posted October 4, 2008 Hi Edek, you might like to check out my Open Projects program, available here. Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now