Solved

change password in autoit script

Posted on 2011-03-18
20
2,103 Views
Last Modified: 2013-11-13
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
Comment
Question by:JakeSpencer
  • 13
  • 7
20 Comments
 
LVL 11

Expert Comment

by:TheGorby
ID: 35165986
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35166085
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
 

Author Comment

by:JakeSpencer
ID: 35166168
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35166363
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
 

Author Comment

by:JakeSpencer
ID: 35166409
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35167352
It's very easy. I'm going to finish up my lunch and then I'll have some code for you real soon.
0
 
LVL 11

Expert Comment

by:TheGorby
ID: 35167910
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35167928
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
 
LVL 11

Accepted Solution

by:
TheGorby earned 500 total points
ID: 35167988
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
 

Author Comment

by:JakeSpencer
ID: 35168660
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:TheGorby
ID: 35168726
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35168759
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35168872
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
 

Author Comment

by:JakeSpencer
ID: 35168927
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35168959
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35169012
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
 

Author Comment

by:JakeSpencer
ID: 35169015
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
 
LVL 11

Expert Comment

by:TheGorby
ID: 35169047
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
 

Author Comment

by:JakeSpencer
ID: 35169063
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
 

Author Closing Comment

by:JakeSpencer
ID: 35169074
The EXPERT in this thread was extremely helpful.  Thank you so much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now