I figured I better ask this particular issue in it's own thread instead of my thread where I'm trying to get my script finished here
http://www.autohotkey.com/forum/viewtop ... sc&start=0
I'm looking for help to encrypt a password that is currently saved in a text file. The password is needed to log into a switch using the Putty program. The script asks for the password in the beginning and saves it so it can be retrieved later by the script and sent to Putty when logging onto our network switches.
Code:
InputBox, vPrsnPwd, Please enter your password, Enter your tacacs password. If you enter this wrong you can open your putty_conf.txt file to correct it later. ,Hide
FileAppend, %vPrsnPwd%`n, putty_conf.txt
This works fine for testing but for security reasons it's a vulnerability I would like reduced or removed. Now since the script needs to decrypt the password before it gets sent to Putty, I need to figure out a way to encrypt the saved password and then to have the script decrypt it before it's sent. So I guess the script would also need to be able to use a key for the encryption and decryption process. In the beggining I guess I could just use a simple key to test the encryption decryption process, but in the end having a simple key viewable is just as bad as having the password in plain text. So eventually I would need a way to use tough encryption. It would be great if I could figure out a way to use the the users computer crediatls as the key for the encryption and decryption. This would make each key different for each user and probably be the best security meassure but I'm not even sure if something like that is possible.
Anyway, below is my entire script so feel free to disect it and I sure hope someone can lend a hand with my password encryption issue.
Code:
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
; Author: A.N.Other <myemail@nowhere.com>
;
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
;==========================================================================================================================================
; Credit 1 (create config files) goes to http://www.ghisler.ch/wiki/index.php/AutoHotkey:_Launch_putty_or_RDP_sessions_via_a_tree-driven_selection_menu for this first bit of code that creats the configuration files.
;==========================================================================================================================================
; Uncomment the "FileInstall" line below if you want to include an image in a compiled AutoHotKey executable
; This makes the GUI look a little more polished and professional but in no way changes it's essential functionality
; FileInstall, abcd.jpg, abcd.jpg, 1
;================================================================================================================================
; Begin checks and create configuration files if needed.
; First section simply checks if conf file for the utility exists and reads it's contents into variables
; If it doesn't exist it prompts users for the information and writes the values into the conf file
;=================================================================================================================================
; =========================
; Check for putty_conf.txt file =====
; =========================
PuttyFound = false
IfExist, %A_WorkingDir%\putty_conf.txt
{
PuttyFound = true
FileReadLine, PuttyLoc, putty_conf.txt, 1
FileReadLine, PrsnId, putty_conf.txt, 2
FileReadLine, PrsnPwd, putty_conf.txt, 3
FileReadLine, TftpserverIP, putty_conf.txt, 4
FileReadLine, AorName, putty_conf.txt, 5
}
if PuttyFound != true
{
MsgBox Please indicate location of Putty or program will not function correctly, Browse your program folder and select the Putty.exe file
FileSelectFile, PuttyLoc, 1, %A_MyDocument%, Please indicate location of Putty or program will not function correctly, *.exe
FileAppend, %PuttyLoc%`n, putty_conf.txt
InputBox, vPrsnId, Please enter your userid, Enter your tacacs user id. If you enter this wrong you can open your putty_conf.txt file to correct it later.
FileAppend, %vPrsnId%`n, putty_conf.txt
InputBox, vPrsnPwd, Please enter your password, Enter your tacacs password. If you enter this wrong you can open your putty_conf.txt file to correct it later. ,Hide
FileAppend, %vPrsnPwd%`n, putty_conf.txt
InputBox, vTftpserverIP, Please enter IP address of TFTP server for Baselines, Enter the IP address of the TFTP server used to retrieve the baseline. If you enter this wrong you can open your putty_conf.txt file to correct it later.
FileAppend, %vTftpserverIP%`n, putty_conf.txt
InputBox, vAorName, Enter your unit number, Please enter the only the 2 digit number for your organization (i.e. 23 for 23rd Sig or 62 for 62nd Sig). This is used to determine which AOR to run the baseline for.
FileAppend, %vAorName%`n, putty_conf.txt
MsgBox The configuration file with the location of putty, your userid, aor, and tftp server IP has now been created.`n`nThe name of the file is puttytree_conf.txt and it can be found in the same directory as this utility.`n`nThe utility will now exit - please rerun it and it should no longer ask for the Putty location or your userid.`nThis should also be the last time you see this message.
}
; ====================================================
; Check for Security Banner file used in switch for switch configuration ==
; ====================================================
BannerFound = false
IfExist, %A_WorkingDir%\Banner_conf.ini
{
BannerFound = true
FileRead, BannerLoc, Banner_conf.ini
}
if BannerFound != true
{
Gui, Add, Text,, Copy and Paste Banner text below:
Gui, Add, Edit, r30 w500 vBanner ; The ym option starts a new column of controls.
Gui, Add, Button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed.
Gui, Show,, Updated Banner
return ; End of auto-execute section. The script is idle until the user does something.
GuiClose:
ButtonOK:
Gui, Submit ; Save the input from the user to each control's associated variable.
MsgBox "The Banner is now updated. You will no longer receive this message. If you need to update the Banner again, please delete the banner_conf.ini file".
FileAppend, %Banner%`n, Banner_conf.ini
ExitApp
}
;================================================================================================================================
; Custom Message Box --Credit http://www.autohotkey.com/forum/topic32367.html
; Custom Message Box for Baseline and Banner Choices
;================================================================================================================================
Pressed := CMsgbox( "Banner and Baseline Script", "Run Baseline, Banner, or Both", "*Baseline|Banner|Both","",1 )
banner_flag=0 ; default to 'no'
baseline_flag=0 ; default to 'no'
;----msgbox >>>%Pressed% ; just for diagnostics, not final script
If Pressed = Both
{
banner_flag=1
baseline_flag=1
}
else if Pressed = Banner
{
banner_flag=1
baseline_flag=0
}
else If Pressed = Baseline
{
banner_flag=0
baseline_flag=1
}
else ; the default is 'Closed'
{
exitapp ; end the script
}
; ================================================================================================================================
; Custom Message Box --Credit http://www.autohotkey.com/forum/topic32367.html
; Message Box for Baseline Type
; ================================================================================================================================
if baseline_flag=1
{
Basetype := CMsgbox( "FTP Server file location", "Run Foundry Baseline Type", "*ls nolb|ls full|ls notac|bb nolb|bb full|bb notac","",2 )
ls_flag=0 ;default to 'no'
lsnolb_flag=0 ; default to 'no'
lsfull_flag=0 ; default to 'no'
lsnotac_flag=0 ; default to 'no'
bbnolb_flag=0 ; default to 'no'
bbfull_flag=0 ; default to 'no'
bbnotac_flag=0 ; default to 'no'
;-----msgbox >>>%Basetype% ; just for diagnostics, not final script
If Basetype = ls nolb
{
lsnolb_flag=1
ls_flag=1
Looporbase := "ls"
Looptacfull := "nolb"
}
else if Basetype = ls full
{
lsfull_flag=1
ls_flag=1
Looporbase := "ls"
Looptacfull := "full"
}
else If Basetype = ls notac
{
lsnotac_flag=1
ls_flag=1
Looporbase := "ls"
Looptacfull := "notac"
}
else if Basetype = bb nolb
{
bbnolb_flag=1
bb_flag=1
Looporbase := "bb"
Looptacfull := "nolb"
}
else If Basetype = bb full
{
bbfull_flag=1
bb_flag=1
Looporbase := "bb"
Looptacfull := "full"
}
else If Basetype = bb notac
{
bbnotac_flag=1
bb_flag=1
Looporbase := "bb"
Looptacfull := "notac"
}
else ; the default is 'Closed'
{
exitapp ; end the script
}
}
else
{
baseline_flag=0
bb_flag=0
ls_flag=0
}
;===================================================
;First Input to get device name or IP address and saved as a variable==
;===================================================
InputBox, vHostname, Please enter device name or IP address
Hostname :=vHostname
; ======================================================================================================================================
; Begining run of Putty and execute commands
; ======================================================================================================================================
SetKeyDelay, 0, 5
Run, %PuttyLoc%
WinWait, PuTTY Configuration, ,20
IfWinNotActive, PuTTY Configuration, , WinActivate, PuTTY Configuration,
WinWaitActive, PuTTY Configuration, ,
ControlSend, edit1,{Raw}%Hostname% , ahk_class PuTTYConfigBox
ControlSend, edit1,{Enter}, ahk_class PuTTYConfigBox
Sleep, 1000
; ======================================================================================================================================
; Watch putty.exe process. Putty needs to be watched in case it is accidently closed on error or user closes before script finishes.
; ======================================================================================================================================
process = putty.exe ; putty.exe is to be monitored.
setTimer, watchProc, 100
; ====================================================================================
; Connecting with SSH may bring up a security Window. If It does the script will Tab over and hit Enter to press Yes.
; If not, it will wait a few seconds and assume the security Window will not popup and continue.
; ====================================================================================
WinWait, PuTTY Security Alert, ,2
if !errorlevel
{
IfWinNotActive, PuTTY Security Alert, , WinActivate, PuTTY Security Alert,
WinWaitActive, PuTTY Security Alert, ,5
ifWinActive, PuTTY Security Alert, ,Send, {TAB}{ENTER}
}
SetKeyDelay, 0, 5, ahk_class PuTTY
Sleep, 500
IfWinNotActive, ahk_class PuTTY, , WinActivate, ahk_class PuTTY
WinWait, ahk_class PuTTY, ,2
WinWaitActive, ahk_class PuTTY
SetKeyDelay, 0, 5, ahk_class PuTTY
ControlSend, , {Raw}%PrsnId%, ahk_class PuTTY
Sleep, 500
ControlSend, , {Enter}, ahk_class PuTTY
Sleep, 1000
SetKeyDelay, 0, 20, ahk_class PuTTY
ControlSend, , {Raw}%PrsnPwd%, ahk_class PuTTY
Sleep, 500
ControlSend, , {Enter}, ahk_class PuTTY
Sleep, 500
if banner_flag=1 ; When banner flag =1 the script will send the contents of the banner_conf.ini file created into the active putty Window.
{
SetKeyDelay, 0, 5
Sleep, 500
ControlSend, ,{Raw}conf t, ahk_class PuTTY
ControlSend, ,{Enter}, ahk_class PuTTY
Sleep, 1000
ControlSend, ,{Raw}banner motd $, ahk_class PuTTY
ControlSend, ,{Enter}, ahk_class PuTTY
FileRead, BannerText, Banner_conf.ini
clipboard =
clipboard = %BannerText%
ControlSend, ,% RegExReplace(Clipboard, "`r"), ahk_class PuTTY
Sleep 500
clipboard =
ControlSend, ,{Enter}${Enter}, ahk_class PuTTY
Sleep, 1000
ControlSend, , end{Enter}, ahk_class PuTTY
}
if baseline_flag=1 ; When the baseline flag = 1 the script will send the copy tftp command along with the proper format for the baseline config stored on the tftp server to the active putty Window.
{
SetKeyDelay, 0, 10
Sleep, 500
ControlSend, ,{Raw}copy tftp run %TftpserverIP% fbase/%Looporbase%/aor%AorName%/%AorName%-fbase-%Looporbase%-%Looptacfull%, ahk_class PuTTY
ControlSend, ,{Enter}, ahk_class PuTTY
Sleep, 15000
ControlSend, , {Enter}, ahk_class PuTTY
}
Sleep, 1000
ControlSend, , wr mem{Enter}, ahk_class PuTTY
Sleep, 5000
ControlSend, , exit{Enter}, ahk_class PuTTY
Sleep, 500
ControlSend, , exit{Enter}, ahk_class PuTTY
If Pressed = Both
{
MsgBox, Banner and Baseline Updated
}
else if Pressed = Banner
{
MsgBox, Banner Updated
}
else if Pressed = Baseline
{
MsgBox, Baseline Updated
}
else
{
MsgBox, Script Closed
}
exitApp
Sleep, 1500
; ==================================================================
; Ending portion of watch putty.exe process. This is what kills the script if putty.exe closes.
; ==================================================================
; place this at or near to bottom of script.
watchProc:
process, exist, % process
if !errorlevel {
msgbox, 0x10, Fatal Error!, % process " has been closed, stopped working, or crashed."
. "`nThis script cannot run without " process
. "`n`nPlease press OK to exit."
exitApp
return
}
; =================================================================
; End portion needed for Custom message box at the begging of script
; =================================================================
CMsgBox( title, text, buttons, icon="", owner=0 ) {
Global _CMsg_Result
GuiID := 9 ; If you change, also change the subroutines below
StringSplit Button, buttons, |
If( owner <> 0 ) {
Gui %owner%:+Disabled
Gui %GuiID%:+Owner%owner%
}
Gui %GuiID%:+Toolwindow +AlwaysOnTop
MyIcon := ( icon = "I" ) or ( icon = "" ) ? 222 : icon = "Q" ? 24 : icon = "E" ? 110 : icon
Gui %GuiID%:Add, Picture, Icon%MyIcon% , Shell32.dll
Gui %GuiID%:Add, Text, x+12 yp w180 r8 section , %text%
Loop %Button0%
Gui %GuiID%:Add, Button, % ( A_Index=1 ? "x+12 ys " : "xp y+3 " ) . ( InStr( Button%A_Index%, "*" ) ? "Default " : " " ) . "w100 gCMsgButton", % RegExReplace( Button%A_Index%, "\*" )
Gui %GuiID%:Show,,%title%
Loop
If( _CMsg_Result )
Break
If( owner <> 0 )
Gui %owner%:-Disabled
Gui %GuiID%:Destroy
Result := _CMsg_Result
_CMsg_Result := ""
Return Result
}
9GuiEscape:
9GuiClose:
_CMsg_Result := "Close"
Return
CMsgButton:
StringReplace _CMsg_Result, A_GuiControl, &,, All
Return
; ===================================================================
; End Script.
; Thanks to all that helped specially user Leef_me and TLM on AutoHotkey forum.
; ===================================================================