How to assign password to secure string in VB Script

依然范特西╮ 提交于 2019-12-01 14:41:49

You can use this small code with powershell and batch

@ECHO OFF
Title Type a password with powershell and batch
:CheckPassword
Mode con cols=50 lines=3
cls & color 0A & echo.
set MyPassword=Hackoo
set "psCommand=powershell -Command "$pword = read-host 'Enter your password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
if %MyPassword%==%password% (Goto:Good) else (Goto:Bad)
exit/b
::***********************************************************************************************
:Good
Cls & Color 0A
echo(
echo                   Good Password
TimeOut /T 2 /NoBreak>nul
Exit
::***********************************************************************************************
:Bad
Cls & Color 0C
echo(
echo                   Bad password
TimeOut /T 1 /NoBreak>nul
Goto:CheckPassword
::***********************************************************************************************

I think this function PasswordBox can help you, just give a try ;)

' Just an example of how to use the function
 '
 wsh.echo "You entered: ", _
          Join(PasswordBox("Enter UID and password", _
               "Testing"), ", ")

 ' A function to present a Password dialog in a VBS (WSF) 
 ' script
 ' Requires WScript version 5.1+
 ' Tom Lavedas <tlavedas@hotmail.com>
 ' with help from and thanks to Joe Ernest and 
 ' Michael Harris
 '
 ' modified 1/2008 to handle IE7
 '
 Function PasswordBox(sPrompt,sDefault)
   set oIE = CreateObject("InternetExplorer.Application")
   With oIE
 ' Configure the IE window
     .RegisterAsDropTarget = False
     .statusbar = false : .toolbar    = false
     .menubar   = false : .addressbar = false
     .Resizable = False 
     .Navigate "about:blank"
     Do Until .ReadyState = 4 : WScript.Sleep 50 : Loop
 ' Test for IE 7 - cannot remove 'chrome' in that version
     sVersion  = .document.parentWindow.navigator.appVersion  
     if instr(sVersion, "MSIE 7.0") = 0 Then .FullScreen = True 
     .width = 400       : .height = 270
 ' Create the password box document
     With .document
       oIE.left = .parentWindow.screen.width \ 2 - 200
       oIE.top  = .parentWindow.screen.height\ 2 - 100
       .open
       .write "<html><head><" & "script>bboxwait=true;</" _
            & "script><title>Password _</title></head>"_
            & "<body bgColor=silver scroll=no " _
            & "language=vbs style='border-" _ 
            & "style:outset;border-Width:3px'" _
            & " onHelp='window.event.returnvalue=false" _
            & ":window.event.cancelbubble=true'" _
            & " oncontextmenu=" _ 
            & "'window.event.returnvalue=false" _
            & ":window.event.cancelbubble=true'" _
            & " onkeydown='if ((window.event.keycode>111)"_
            & " and  (window.event.keycode<117)) or" _
            & " window.event.ctrlkey then" _
            & " window.event.keycode=0" _
            & ":window.event.cancelbubble=true" _
            & ":window.event.returnvalue=false'" _
            & " onkeypress='if window.event.keycode=13" _
            & " then bboxwait=false'><center>" _
            & "<div style='padding:10px;background-color:lightblue'>" _
            & "<b>&nbsp" & sPrompt & "<b>&nbsp</div><p>" _
            & "<table bgcolor=cornsilk cellspacing=10><tr><td>" _
            & " <b>User:</b></td><td>" _
            & "<input type=text size=10 id=user value='" _
            & sDefault & "'>" _
            & "</td><tr><td> <b>Password:</b></td><td>" _
            & "<input type=password size=12 id=pass>" _ 
            & "</td></tr></table><br>" _
            & "<button onclick='bboxwait=false;'>" _
            & "&nbsp;Okay&nbsp;" _
            & "</button> &nbsp; <button onclick=" _
            & "'document.all.user.value=""CANCELLED"";" _
            & "document.all.pass.value="""";" _
            & "bboxwait=false;'>Cancel" _
            & "</button></center></body></html>"
       .close
       Do Until .ReadyState = "complete" : WScript.Sleep 100 : Loop
       .all.user.focus
       .all.user.select ' Optional
       oIE.Visible = True
       CreateObject("Wscript.Shell")_
         .Appactivate "Password _"
       PasswordBox = Array("CANCELLED")
       On Error Resume Next
       Do While .parentWindow.bBoxWait
         if Err Then Exit Function
         WScript.Sleep 100
       Loop
       oIE.Visible = False
       PasswordBox = Array(.all.user.value, _
                           .all.pass.value)
     End With ' document
   End With   ' IE
 End Function

If you are executing the VBScript via cscript.exe something like;

cscript.exe /nologo "test.vbs"

You can use the WScript object to access the StdIn (for input) and StdOut (for output) streams to the command window using a script like this;

Function PromptForInput(prompt)
  Dim prog : prog = WScript.Fullname
  If LCase(Right(prog, 12)) = "\cscript.exe" Then
    Call WScript.StdOut.WriteLine(prompt & " ")
    PromptForInput = WScript.StdIn.ReadLine()
  Else
    Call Err.Raise(vbObjectError + 5, "Must be called from cscript.exe")
  End If
End Function

Dim input
input = PromptForInput("Did you wish to continue? [Y/N]")
Select Case UCase(input)
Case "Y", "N"
  Call WScript.StdOut.Writeline("You chose: " & UCase(input))
Case Else
  Call WScript.StdOut.Writeline("Invalid option!")
End Select

Output:

Did you wish to continue? [Y/N]
y
You chose: Y

You can adapt it to prompt for passwords but be aware that the input is not hidden so all the characters typed are visible in the command window until it's closed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!