Batch File Command Hide Password [duplicate] - batch-file

This question already has answers here:
Can I mask an input text in a bat file?
(19 answers)
Closed 6 years ago.
I have this batch file I wrote to open putty and want to make it a universal script for others. The script is as follows
#echo off
::Written by Mark Gulick::
::Today's Date 20150316::
set /p U="Enter Username: "
set /p P="Enter Password: "
set /p DC="Enter DC Number: "
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE %U%#b0%DC%db -pw %P%
pause
I would like to make the password not show up and have tried some areas on here and haven't found one that will work. I might be doing it wrong too. I'm a little rusty on my scripting. Am I missing something or should I use something else other then the set command?

You can do something like this :
#echo off & setlocal DisableDelayedExpansion
Title %~n0
Mode 50,5 & Color 0E
set /p U="Enter Username : "
Call:InputPassword "Enter Password" P
set /p DC="Enter DC Number: "
setlocal EnableDelayedExpansion
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE !U!#b0!DC!db -pw !P!
pause
::***********************************
:InputPassword
Cls
echo.
echo.
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p
)
goto :eof
::***********************************

This post on DOSTips references a post here by MC ND, but I can't find the original, so here it is again. Whenever you want to get a password and mask the input, simply call :getPassword target_variable input_prompt where target_variable is the name of the variable you store the password in and input_prompt is whatever you show the user to prompt them to enter their password.
#echo off
setlocal enabledelayedexpansion
set /p "user_name=Enter username here:"
call :getPassword user_password "Enter password here: "
:: The user's password has been stored in the variable %user_password%
exit /b
::------------------------------------------------------------------------------
:: Masks user input and returns the input as a variable.
:: Password-masking code based on http://www.dostips.com/forum/viewtopic.php?p=33538#p33538
::
:: Arguments: %1 - the variable to store the password in
:: %2 - the prompt to display when receiving input
::------------------------------------------------------------------------------
:getPassword
set "_password="
:: We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
:: Prompt the user
set /p "=%~2" <nul
:keyLoop
:: Retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
:: If No keypress (enter), then exit
:: If backspace, remove character from password and console
:: Otherwise, add a character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
set /p "=!BS! !BS!"<nul
)
) else (
set "_password=%_password%%key%"
set /p "="<nul
)
goto :keyLoop
)
echo/
:: Return password to caller
set "%~1=%_password%"
goto :eof

batch-file's cannot do this alone, but if you're running batch that means you're likely on windows, and automatically have VBScript installed. You can use this to get a password with masking:
randomThing.bat
echo Blah blah blah...
:: Call a vbscript file with the outputs being set as a variable
for /f "usebackq tokens=*" %%r in (`wscript "password.vbs"`) do set retPass=%%r
:: Write back the input for example's sake.
echo What you typed in: %retPass%
password.vbs
'Call this script from a batch file to input a masked password
set objPassword = createObject("scriptPW.password")
wScript.stdOut.write "Input Password:"
strPassword = objPassword.getPassword()
wScript.echo strPassword
Note I'm answering this on mobile, and as such cannot test if this method still works. However it gets the main idea across.

Related

How to display text beneath "set /p variable="

In this senario,
echo Write your name below.
echo.
set /p name="Enter here: "
echo.
echo *other text*
is there a way where the set /p name="Enter name: " doesn't pause the screen and also displays the text below.
I figured that since its a top down language it probably cannot, but there are many people on here with more experience who might no otherwise.
If you're using Windows 10, you can use VT100 escape sequences to move the cursor up and down, printing everything first and then getting user input afterwards.
#echo off
:: Generate an escape character. Normally I'd just use ALT+027,
:: but SO doesn't display those properly.
for /f %%A in ('echo prompt $E^| cmd') do set "esc=%%A"
:: The trick here is to write out everything and then move the cursor
:: back to where it needs to be
echo Write your name below.
echo(
echo(
echo(
echo *other text*%ESC%[2A
set /p "name=Enter here: "
:: When we're done, move the cursor to where it would have been if
:: we had written everything top-down
echo %ESC%[1B
You can store new line character in a variable and print it out
#echo off
setlocal EnableDelayedExpansion
(set LF=^
%=empty=%
)
echo Write your name below.
echo.
set /p name="Enter here:!LF!*other text*!LF!"

Test for and remove substring in user input

I want to create a batch file that echoes text that it receives as part of a user-inputted command.
#echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
Asking the user for the command first and then asking the user what to echo, like below, is not an option
#echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
You could use this:
#echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
Note that I also fixed your if, and put a pause after your if so you know whether it goes to :echo or not.
You want to split your Input into a first word (token) and the rest. You can do this with a for:
#echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%

Make cmd window on top of others

I am working on simple batch script to hide the password inputted by user. The solution is to create a popup and change text-color for that popup. Following is the code and it works properly
#echo off
Echo Please enter your password in the popup window and then press enter
set tempbat="%temp%\p.cmd"
REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color 01 >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%
start /wait "" %tempbat%
del %tempbat% 2>NUL
set /p Pwd=<"%temp%\pass.txt"
del "%temp%\pass.txt" 2>NUL
echo %Pwd%
My only concern is that when the popup occurs, can I set it always on top of main cmd window, and even disable access to main cmd window (I expect the behavior like Bootstrap Modal)?
Thank for reading and hope to receive helps from you
Instead of trying to cover the password with something, it is possible to obfuscate user input in batch. See MC ND's code from this answer:
#echo off
setlocal enableextensions disabledelayedexpansion
rem Call the subroutine to get the password
call :getPassword password
rem Echo what the function returns
if defined password (
echo You have typed [%password%]
) else (
echo You have typed nothing
)
rem End of the process
endlocal
exit /b
rem Subroutine to get the password
:getPassword returnVar
setlocal enableextensions disabledelayedexpansion
set "_password="
rem We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
rem Prompt the user
set /p "=password ?:" <nul
:keyLoop
rem retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
rem handle the keypress
rem if No keypress (enter), then exit
rem if backspace, remove character from password and console
rem else add character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
)
) else (
set "_password=%_password%%key%"
set /p "=*"<nul
)
goto :keyLoop
)
echo(
rem return password to caller
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%

batch file to replace the input with * without external file [duplicate]

This question already has answers here:
batch file to mask input with * without an external file
(2 answers)
Closed 8 years ago.
i need batch file to mask the input with * without external file and i need the code to ((((be fast in write letters))))) and does not Calculates the distance with *
((((if there is code extract the external file from the batch file which make the external file make password with * and fast to write password add comment to see the code ))))
FOR EXAMPLE:
#echo off
set char=*
set /p variable=Enter your password:
set char=%variable%
echo %char%
pause
This is a "cleanup" of the code you get in your previous question. If this is not fast enough for you, then you should consider not doing it in batch and use some aditional tool.
#echo off
setlocal enableextensions disabledelayedexpansion
rem Call the subroutine to get the password
call :getPassword password
rem Echo what the function returns
if defined password (
echo You have typed [%password%]
) else (
echo You have typed nothing
)
rem End of the process
endlocal
exit /b
rem Subroutine to get the password
:getPassword returnVar
setlocal enableextensions disabledelayedexpansion
set "_password="
rem We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"
rem Prompt the user
set /p "=password ?:" <nul
:keyLoop
rem retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"
rem handle the keypress
rem if No keypress (enter), then exit
rem if backspace, remove character from password and console
rem else add character to password and go ask for next one
if defined key (
if "%key%"=="%BS%" (
if defined _password (
set "_password=%_password:~0,-1%"
setlocal enabledelayedexpansion & set /p "=!BS! !BS!"<nul & endlocal
)
) else (
set "_password=%_password%%key%"
set /p "=*"<nul
)
goto :keyLoop
)
echo(
rem return password to caller
if defined _password ( set "exitCode=0" ) else ( set "exitCode=1" )
endlocal & set "%~1=%_password%" & exit /b %exitCode%

Batch script not working

I've recently written a password script in batch. Basically it takes the password from a different batch script, finds out how many characters it has, takes the password from the user and compares them. However, nothing is ever simple with me and I've gotten into some nested FOR loops and such. It's hard to explain, so here's the code:
#echo off
setlocal enabledelayedexpansion
call pass.bat & rem Sets the variable "pass" as "default".
set map=abcdefghijklmnopqrstuvwxyz
set len=0
for /l %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!pass:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
for /l %%C in (1,1,100) do (
set letter%%C=!pass:~%%C,1!
)
:pass
for /l %%D in (0,1,%len%) do (
cls
choice /c "abcdefghijklmnopqrstuvwxyz" /n /m "Password: !ast!"
set /a charerr=!errorlevel!-1
for /l %%E in (0,1,25) do (
set password=!password!%map:~!charerr!,1% & rem This is the problem line.
)
set ast=!ast!*
)
if "%pass%" neq "%password%" goto fail
cls
echo Correct Password
echo pass: %pass%
echo password: %password%
>nul pause
exit
:fail
set /a tries-=1
if %tries% geq 1 goto pass
Now, the script doesn't crash or anything like that, however it does not set password as the password you entered.
If you run it you'll understand.
Note: pass.bat purely contains the line set pass=default
You could try
call set "password=!password!%%map:~!charerr!,1%%"
Your variant can't work, as percent expansions are expanded when a block is parsed, so the %map:~!charerr!,1% will be expanded, but fails as !charerr! isn't expanded at that time.
The CALL can start a second parse time when evaluating the line and the double percent will then expand correct.

Resources