locked
Can a VBS file edit itself? RRS feed

  • Question

  • Hi guys,

    I've searched for the above and all I can find are people wanting to hack their global IT policy.

    In a nutshell, I've written a login script using VBS. The username and password therefore either have to be saved in the file itself, or input using an inputbox in plain text.

    I've personally opted for a combination, so my loginId is saved in the VBS file, my password is partially saved and I am only prompted for a "password number". So for example, if my username was "jeffsimply" and password was "password1a". I would most likely increment my password for the relevant system so that next month when asked for a new password I would choose "password 2a" and so on.

    What I would like to work toward, is a script with various usernames and passwords, with the ability to update the usernames and or passwords without needing to edit the script. I don't know the syntax if it's even possible but it would be something like

    If booFirst = 1 Then
    Clear Line 5
    Set Line 5: strUser = "jeffsimpler"
    booFirst = 0
    Else
    End If

    So that the first time the code is run, the strUser is set (I would most likely implement this with an inputbox and a further input box to confirm before setting the first time run check to false.

    Any help would be grand!

    This is my current code:

    Dim ObjectIE Set ObjectIE = CreateObject("InternetExplorer.Application") ObjectIE.Navigate("www.loginpage.com") strUser = "jeffsimply" strPasswordPre = "password" strPasswordPost = "a" Title = "Password number" message = "Please input password number and click ok" Default = "" Question = Inputbox(message,title,Default) strPassword = Question ObjectIE.visible = True Wait objectIE, 500 set doc = ObjectIE.document doc.getelementbyID("txtUser").value = strUser doc.getelementbyID("txtPassword").value = strPasswordPre & strPassword & strPasswordPost doc.getelementbyID("butLogin").click Sub Wait(ObjectIE, SleepInterval) Do WScript.Sleep SleepInterval Loop While ObjectIE.ReadyState < 4 And ObjectIE.Busy End

    Tuesday, March 24, 2015 12:12 PM

Answers

  • I don't suppose you would share an example of how you might get the VBS file to edit itself? Even if it requires a 'restart' to take effect, that is much better than nothing!

    Here is how you could do it if you chose to ignore Bill's highly relevant advice. You have been warned.

    Create a subroutine called "Modify_Script". It should contain code for Steps 1 . . 6 below.

    1. Use the FSO OpenTextFile method to open the script file. Its name will be WScript.ScriptFullName.
    2. Read its contents into the string sText.
    3. Use the Split function to split sText into an array contain the script lines.
    4. Locate the password area and apply your change.
    5. Write the array back to the script file.
    6. Use the WshShell object to invoke your script file, using a command line parameter such as "Rerun".

    The main part of the script would look like so:

    if wscript.Arguments.count > 0 then
       if wscript.Arguments(0) = "Rerun" then Modify_Script
       wscript.quit
    end if
    '(your existing code goes here)

    sub Modify_Script
     . . .
    end sub

    • Marked as answer by Jeff simply Tuesday, March 24, 2015 5:15 PM
    Tuesday, March 24, 2015 4:26 PM

All replies

  • You should not be storing passwords of any kind in a script.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 24, 2015 2:13 PM
    Moderator
  • Batch files can modify themselves so that the change becomes effective immediately. VB script files cannot do this because the interpreter reads the whole script into memory before executing it. Any change becomes effective only when you invoke the script next time. You could, of course, store the passwords in a text file that you keep modifying. Either way your scheme breaks the basic security rule that one must not store passwords in a text file.
    Tuesday, March 24, 2015 2:19 PM
  • consider storing the <strike>password</strike> data in a file external to the script. If there is no file, it's the first time the script ran. If the file is there, it contains the password.

    <strike></strike>


    MCP/MCSA/MCTS/MCITP

    Tuesday, March 24, 2015 2:43 PM
  • Hi Frederik,

    Completely understand this isn't security best practice, if it makes you feel any better, anyone that has physical access to the machine will already have their own access (which in turn has the ability to impersonate anyone else on the team) so there isn't much at stake with regards to knowing each others passwords anyway. Of course logging on from another location is something else, but the idea is this is just a convenience tool when in the office and will only be used here. Anyone without physical access to the machines will have been doing very well to get as far as my C:/ and I'd suspect there are bigger issues than being able to read my passwords.

    Ignoring the above, it is IT policy here to lock your computer before leaving it unattended so it's unlikely that anyone would be able to access the physical file anyway.

    I don't suppose you would share an example of how you might get the VBS file to edit itself? Even if it requires a 'restart' to take effect, that is much better than nothing!


    • Edited by Jeff simply Tuesday, March 24, 2015 3:12 PM
    Tuesday, March 24, 2015 3:11 PM
  • To quote Raymond Chen: "This idea is fraught with peril, and I fear that my answers to your questions will be interpreted as approval rather than reluctant assistance."

    I don't think it's worth figuring out a good way to do what you're asking, since you shouldn't be doing it in the first place.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 24, 2015 3:33 PM
    Moderator
  • I don't suppose you would share an example of how you might get the VBS file to edit itself? Even if it requires a 'restart' to take effect, that is much better than nothing!

    Here is how you could do it if you chose to ignore Bill's highly relevant advice. You have been warned.

    Create a subroutine called "Modify_Script". It should contain code for Steps 1 . . 6 below.

    1. Use the FSO OpenTextFile method to open the script file. Its name will be WScript.ScriptFullName.
    2. Read its contents into the string sText.
    3. Use the Split function to split sText into an array contain the script lines.
    4. Locate the password area and apply your change.
    5. Write the array back to the script file.
    6. Use the WshShell object to invoke your script file, using a command line parameter such as "Rerun".

    The main part of the script would look like so:

    if wscript.Arguments.count > 0 then
       if wscript.Arguments(0) = "Rerun" then Modify_Script
       wscript.quit
    end if
    '(your existing code goes here)

    sub Modify_Script
     . . .
    end sub

    • Marked as answer by Jeff simply Tuesday, March 24, 2015 5:15 PM
    Tuesday, March 24, 2015 4:26 PM
  • Thanks I'll get on this later.

    Tuesday, March 24, 2015 5:15 PM
  • As I have detailed in another reply, password security in the environment this will be used is irrelevant. Thanks for your concern.
    Tuesday, March 24, 2015 5:16 PM