Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2567
  • Last Modified:

change password in autoit script

Hello

I have an AutoIT script to set folder attributes.  The script requires a password at the beginning.  The problem is this password is coded into the script meaning the user can't change it.

Also The script writes some commands in a DOS window, which I would like to run in the background, ie not be visible on screen.  I am very new to AutoIT so can anybody help?

My code is shown below.

Many Thanks

$bLoop = 1
While $bLoop = 1
    $text = InputBox("Folder Lock", "Please type your password and click OK.", "", "*")
    If @error = 1 Then
        Exit
    Else
        ; They clicked OK, but did they type the right thing?
        If $text <> "rabbit2011" Then
            MsgBox(4096, "Error", "Incorrect Password - try again!")
        Else
            $bLoop = 0    ; Exit the loop - ExitLoop would have been an alternative too :)
        EndIf
    EndIf
WEnd
; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer = MsgBox(1, "Folder Lock", "To LOCK your folder press OK"  & @CRLF & "To UNLOCK your folder press Cancel")



; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 1 Then
   ; Run Command Prompt
Run("cmd.exe")


; Wait for the Command Prompt to become active
WinWaitActive("[CLASS:ConsoleWindowClass]")


; Now that the Command Prompt window is active type some text
Send("attrib {+}s {+}h {+}r c:\confidential {Enter} cacls c:\confidential /e /c /d %username% {Enter}")
sleep (1000)
Send ("exit {Enter}")
$End = msgBox(0, "Folder Lock", "Your folder has been LOCKED")

If $End = 1 Then
	
	Exit
	
	EndIf
    
EndIf
If $answer = 2 Then
   ; Run Command Prompt
Run("cmd.exe")


; Wait for the Command Prompt to become active
WinWaitActive("[CLASS:ConsoleWindowClass]")


; Now that the Command Prompt window is active type some text
Send("cacls c:\confidential /e /c /g %username%:f {Enter} attrib {-}s {-}h {-}r c:\confidential {Enter} ")
sleep (1000)
Send ("exit {Enter}")
$End = msgBox(0, "Folder Lock", "Your folder has been UNLOCKED")

If $End = 1 Then
	
	Exit
	
	EndIf
    
EndIf

Open in new window

0
JakeSpencer
Asked:
JakeSpencer
  • 13
  • 7
1 Solution
 
TheGorbyCommented:
So, you just want to change the password that's in the script? The password is on line 8 of the script, it is "rabbit2011".

As far as hiding the DOS window, that's easy to do but it will take me 30 minutes or so because the 2 sections where it runs the DOS commands need to be rewritten.
0
 
TheGorbyCommented:
This should run the DOS commands in a hidden window:
$bLoop=1
While $bLoop=1
    $text=InputBox("Folder Lock","Please type your password and click OK.","","*")
    If @error=1 Then
        Exit
    Else
        ; They clicked OK, but did they type the right thing?
        If $text<>"rabbit2011" Then
            MsgBox(4096,"Error","Incorrect Password - try again!")
        Else
            $bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
        EndIf
    EndIf
WEnd
; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer=MsgBox(1,"Folder Lock","To LOCK your folder press OK" & @CRLF & "To UNLOCK your folder press Cancel")

; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer=1 Then
	; Run Command Prompt
	RunWait(@ComSpec & " /c attrib +s +h +r C:\confidential",@UserProfileDir,@SW_HIDE)
	RunWait(@ComSpec & " /c cacls c:\confidential /e /c /d " & @UserName,@UserProfileDir,@SW_HIDE)

	MsgBox(0,"Folder Lock","Your folder has been LOCKED")
	
EndIf

If $answer=2 Then
	; Run Command Prompt
	RunWait(@ComSpec & " /c cacls c:\confidential /e /c /g " & @UserName & ":f",@UserProfileDir,@SW_HIDE)
	RunWait(@ComSpec & " /c attrib -s -h -r c:\confidential",@UserProfileDir,@SW_HIDE)

	MsgBox(0,"Folder Lock","Your folder has been UNLOCKED")
    
EndIf

Open in new window

0
 
JakeSpencerAuthor Commented:
Hello

Many thanks that hides the command box windows.  Would you mind explaining how this new code works?  Does it still write to a command prompt, allbeit hidden?

With regards to the password, I know I can change it in the script.  I need for the user to be able to change it themselves.  So perhaps an extra button called change password?

I am familiar with creating a GUI and buttons, I just dont know how to store the user's password if they wish to change it.

Does that make sense?

Many Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TheGorbyCommented:
Ok yes I get it now, they need to be able to change their passwords without your involvement (duh - it's been a long week for me). The best way this is possible is if the password is stored in a separate file, such as an .ini file. This file could be hidden deep in the folder structure, and the users would never know it's being used by the program. They would never know where that file is either, unless they happened to be exploring in that directory and randomly clicking on unknown files. Would that work for you? There is another way to do it, but it would involve allowing the users access to the AutoIT program files on some remote computer.

How the script works:

Yes it still running the same commands in a command prompt, but hidden. The difference is that the original script was sending keystrokes to the PC that simulated what an actual person would do to run the commands. My script invokes cmd.exe with the needed program and switches, and it's hidden by using the @SW_HIDE flag.

This line here:
@ComSpec & " /c cacls c:\confidential /e /c /g " & @UserName & ":f"

Is equal to this line:
"cmd.exe /c cacls c:\confidential /e /c /g %username%:f"

And if you were to enter "cmd.exe /c cacls c:\confidential /e /c /g %username%:f" into the Run prompt on a computer it would run corrrectly (but not hidden). Check out the AutoIT help file for more info about using the RunWait function.
0
 
JakeSpencerAuthor Commented:
Many thanks for your help.

Is reading, writing a password to the ini file something you can help me with, or is it a lot of work?

Many Many thanks for yoyur help.
0
 
TheGorbyCommented:
It's very easy. I'm going to finish up my lunch and then I'll have some code for you real soon.
0
 
TheGorbyCommented:
I think you'll be very happy with this:
#include <GUIConstantsEx.au3>

Global $ini=@SystemDir & "\FldLock.ini"

If Not FileExists($ini) Then
	$hnd=FileOpen($ini,1)
	FileWrite($hnd,"[Password]" & @CRLF)
	FileWriteLine($hnd,"Password=")
	FileClose($hnd)
	MsgBox(0+64,"Folder Lock","This is the first time running Folder Lock on this PC;" & @LF & @LF & "You will now be prompted to create a password.")
	_ChangePW()
EndIf

Opt("GUIOnEventMode",1)
$prompt=GUICreate("Folder Lock",330,125)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
GUICtrlCreateLabel("Welcome to the Folder Lock program.",75,15)
GUICtrlCreateLabel("Please choose an option below:",85,38)
$modify=GUICtrlCreateButton("Lock/Unlock",20,75,80)
$chgpw=GUICtrlCreateButton("Change Password",120,75,100)
$exit=GUICtrlCreateButton("Exit",240,75,60)
GUICtrlSetOnEvent($modify,"_Modify")
GUICtrlSetOnEvent($chgpw,"_ChangePW")
GUICtrlSetOnEvent($exit,"_Exit")
GUISetState(@SW_SHOW)

While 1
	Sleep(1000)
WEnd

Func _ChangePW()
	$pw1=InputBox("Folder Lock","Please create a new password and click OK.","","*")
	If @error=1 Then Exit
	$pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
	If @error=1 Then Exit
	While $pw1<>$pw2
		$pw1=InputBox("Folder Lock","Passwords do not match or are blank. Please try again.","","*")
		If @error=1 Then Exit
		$pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
		If @error=1 Then Exit
		If $pw2="" Then $pw2="ThisPreventsBlankPasswords"
	WEnd
	IniWrite($ini,"Password","Password",$pw2)
	MsgBox(0+64,"Folder Lock","Your new password has been set.")
EndFunc

Func _Modify()
	$bLoop=1
	While $bLoop=1
		$text=InputBox("Folder Lock","Please type your password and click OK.","","*")
		If @error=1 Then Exit
		; They clicked OK, but did they type the right thing?
		If $text<>IniRead($ini,"Password","Password","failed") Then
			MsgBox(4096,"Error","Incorrect Password - try again!")
		Else
			$bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
		EndIf
	WEnd
	GUIDelete($prompt)
	; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
	$answer=MsgBox(1,"Folder Lock","To LOCK your folder press OK" & @CRLF & "To UNLOCK your folder press Cancel")
	; Check the user's answer to the prompt (see the help file for MsgBox return values)
	Switch $answer
		Case 1
			; Run Command Prompt
			RunWait(@ComSpec & " /c attrib +s +h +r C:\confidential",@UserProfileDir,@SW_HIDE)
			RunWait(@ComSpec & " /c cacls c:\confidential /e /c /d " & @UserName,@UserProfileDir,@SW_HIDE)
			MsgBox(0,"Folder Lock","Your folder has been LOCKED")
		Case 2
			; Run Command Prompt
			RunWait(@ComSpec & " /c cacls c:\confidential /e /c /g " & @UserName & ":f",@UserProfileDir,@SW_HIDE)
			RunWait(@ComSpec & " /c attrib -s -h -r c:\confidential",@UserProfileDir,@SW_HIDE)
			MsgBox(0,"Folder Lock","Your folder has been UNLOCKED")
	EndSwitch
	Exit
EndFunc

Func _Exit()
	GUIDelete($prompt)
	Exit
EndFunc

Open in new window

0
 
TheGorbyCommented:
Oh and by the way, that .ini file is located in the \Windows\System32 folder as "FldLock.ini". This way, if a user forgets their password you can manually change it by editing the INI. In thinking about this post I realized I forgot to require entry of the password in order to change it, so I'll fix that real quick and post the code.
0
 
TheGorbyCommented:
Here we go, now if a password has been set you have to actually know it in order to change it (or you can manually edit the INI file as I mentioned):
#include <GUIConstantsEx.au3>

Global $ini=@SystemDir & "\FldLock.ini"

If Not FileExists($ini) Then
	$hnd=FileOpen($ini,1)
	FileWrite($hnd,"[Password]" & @CRLF)
	FileWriteLine($hnd,"Password=")
	FileClose($hnd)
	MsgBox(0+64,"Folder Lock","This is the first time running Folder Lock on this PC;" & @LF & @LF & "you will now be prompted to create a password.")
	$new=1
	_ChangePW()
EndIf
$new=0

Opt("GUIOnEventMode",1)
$prompt=GUICreate("Folder Lock",330,125)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
GUICtrlCreateLabel("Welcome to the Folder Lock program.",75,15)
GUICtrlCreateLabel("Please choose an option below:",85,38)
$modify=GUICtrlCreateButton("Lock/Unlock",20,75,80)
$chgpw=GUICtrlCreateButton("Change Password",120,75,100)
$exit=GUICtrlCreateButton("Exit",240,75,60)
GUICtrlSetOnEvent($modify,"_Modify")
GUICtrlSetOnEvent($chgpw,"_ChangePW")
GUICtrlSetOnEvent($exit,"_Exit")
GUISetState(@SW_SHOW)

While 1
	Sleep(1000)
WEnd

Func _ChangePW()
	If $new=0 Then
		$bLoop=1
		While $bLoop=1
			$text=InputBox("Folder Lock","Please type your password and click OK.","","*")
			If @error=1 Then Exit
			; They clicked OK, but did they type the right thing?
			If $text<>IniRead($ini,"Password","Password","failed") Then
				MsgBox(4096,"Error","Incorrect Password - try again!")
			Else
				$bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
			EndIf
		WEnd
	EndIf
	$pw1=InputBox("Folder Lock","Please create a new password and click OK.","","*")
	If @error=1 Then Exit
	$pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
	If @error=1 Then Exit
	While $pw1<>$pw2
		$pw1=InputBox("Folder Lock","Passwords do not match or are blank. Please try again.","","*")
		If @error=1 Then Exit
		$pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
		If @error=1 Then Exit
		If $pw2="" Then $pw2="ThisPreventsBlankPasswords"
	WEnd
	IniWrite($ini,"Password","Password",$pw2)
	MsgBox(0+64,"Folder Lock","Your new password has been set.")
EndFunc

Func _Modify()
	$bLoop=1
	While $bLoop=1
		$text=InputBox("Folder Lock","Please type your password and click OK.","","*")
		If @error=1 Then Exit
		; They clicked OK, but did they type the right thing?
		If $text<>IniRead($ini,"Password","Password","failed") Then
			MsgBox(4096,"Error","Incorrect Password - try again!")
		Else
			$bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
		EndIf
	WEnd
	GUIDelete($prompt)
	; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
	$answer=MsgBox(1,"Folder Lock","To LOCK your folder press OK" & @CRLF & "To UNLOCK your folder press Cancel")
	; Check the user's answer to the prompt (see the help file for MsgBox return values)
	Switch $answer
		Case 1
			; Run Command Prompt
			RunWait(@ComSpec & " /c attrib +s +h +r C:\confidential",@UserProfileDir,@SW_HIDE)
			RunWait(@ComSpec & " /c cacls c:\confidential /e /c /d " & @UserName,@UserProfileDir,@SW_HIDE)
			MsgBox(0,"Folder Lock","Your folder has been LOCKED")
		Case 2
			; Run Command Prompt
			RunWait(@ComSpec & " /c cacls c:\confidential /e /c /g " & @UserName & ":f",@UserProfileDir,@SW_HIDE)
			RunWait(@ComSpec & " /c attrib -s -h -r c:\confidential",@UserProfileDir,@SW_HIDE)
			MsgBox(0,"Folder Lock","Your folder has been UNLOCKED")
	EndSwitch
	Exit
EndFunc

Func _Exit()
	GUIDelete($prompt)
	Exit
EndFunc

Open in new window

0
 
JakeSpencerAuthor Commented:
Many thanks for this.  Something seems to be going wrong.

The program runs and asks for a password.  However it does not create the ini file and prompts for a password again next time.

I am on Windows 7 if that makes a difference?

Thanks for your help.  If you can't make it work, don't worry.  I appreciate you trying.

0
 
TheGorbyCommented:
Ah, Windows 7... my guess is that since local admin privileges are required to write to the system32 folder, thats where the script fails. We'll just use a directory deep within the current user profile instead; how about

\Documents and Settings\[username]\Application Data\FldLck\FldLock.ini

If that sounds good to you then just change line 3 of the script as shown below:
Global $ini=@AppDataDir & "\FldLck\FldLock.ini"

Open in new window

0
 
TheGorbyCommented:
Oh nuts, I forgot that Win7 uses the Users folder instead of Documents and Settings... the script will still work if you change line 3 like I said, but to find out where it's saving the INI file just go to Start->Run and type in

%appdata%

and then hit enter.
0
 
TheGorbyCommented:
Sorry, one last thing to change in addition to line 3; change line 6 as shown below
$hnd=FileOpen($ini,9)

Open in new window

0
 
JakeSpencerAuthor Commented:
Hello again

That still doesn't work.  However if I change it to:

Global $ini=@AppDataDir & "\FldLock.ini"

Open in new window


Then this works and creates the ini file in:

C:\Users\[USERNAME]\AppData\Roaming

It does not create a FldLock folder or even write to a folder that already exists there.  If we cannot get the file to write to a FldLock folder, can we create the file in the path above, and set the file attributes to hidden?

Many Thanks
0
 
TheGorbyCommented:
Yes I caught that as well, the change to line 6 that I mentioned should fix that. It probably doesn't make much of a difference though, the AppData folder itself is hidden so really if you want to use the FldLck folder it's up to you. I only put the folder in there to keep in line with the way other programs utilize the AppData folder.
0
 
TheGorbyCommented:
If you would like to make the INI hidden as well though, just add this line after the 'FileClose' on line 9 as below
FileClose($hnd)
FileSetAttrib($ini,"+H")

Open in new window

0
 
JakeSpencerAuthor Commented:
Sorry I missed your post above.

That works perfectly thank you so so much.  One more thing if you don't mind?

I see you use FileExists.  Is there an equivalent to check if a Directory exists and if not create it?  Many Many thanks once again.
0
 
TheGorbyCommented:
You can use FileExists for directories as well

FileExists("C:\Test\Folder")

and I think you can even have a trailing backslash on there and it will still work

FileExists("C:\Test\Folder\")

Don't be afraid to dig into the help file that comes with AutoIT, it's definitely the most in-depth help file I've ever seen with a program. 8 months ago I'd never even heard of AutoIT, and look at me now! I have no problem helping you out though, either :)
0
 
JakeSpencerAuthor Commented:
Thank you very very much.  I started learning C++ but found it too difficult.  I am hoping AutoIt will be easier to pick up.

Thanks again :)
0
 
JakeSpencerAuthor Commented:
The EXPERT in this thread was extremely helpful.  Thank you so much.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now