HOME
Stephen Knight
10/20/2009 10:36 AM
Password entry obscured from batch file

Type:

VBScript, Batch/Command file

Category:

Password
These scripts show some ways of prompting for a hidden password within a batch file so that it can be returrned in a variable for authenticating with other commands, e.g. as part of creating an automated FTP script requiring the user to enter their password rather than incorporate it in the script.

This script is a variation on the one below which I have been using for a while to obscure output using built in commands to change console colour etc. This goes one further and sets the foreground and background colours to completely hide the text which is not possible using the built in command. This makes the script XP only though without pre-creating a .REG file to import using REGEDIT as Window 2000 and before did not have the REG.EXE command. There is a version in the code section below which creates a .REG file on the fly.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_24713227.html?cid=1135#a25277510

There is also a matching non-obscured username prompting box here:

Hide details for CodeCode
1. Version for Windows 2000/XP/2003 etc. using a REGEDIT command

@echo off
setlocal
echo Running batch file
echo You will be asked for a password in popup windows after you press a key.
echo.
call :getpassword
echo You entered %pwd%
pause
exit /b

:getpassword
set InputTitle=Enter Password:
set InputResult=%Temp%\Input.tmp

REM add registry entry
set reg="%temp%\regpw.reg"
(echo Windows Registry Editor Version 5.00
echo [HKEY_CURRENT_USER\Console\Enter Password:]
echo "CursorSize"=dword:00000064
echo "FaceName"="Terminal"
echo "FontFamily"=dword:00000030
echo "FontSize"=dword:0010000c
echo "FontWeight"=dword:00000190
echo "ScreenBufferSize"=dword:0003001e
echo "ScreenColors"=dword:00000088
echo "WindowPosition"=dword:017c01e0
echo "WindowSize"=dword:0003001e) > %reg%
regedit -s %reg%

start "%InputTitle%" /wait "%ComSpec%" /v:on /c "echo.&set /p Input= &echo !Input!>"%InputResult%""
for /f "delims=" %%a in ('type "%InputResult%"') do set pwd=%%a
del "%InputResult%"

REM Remove registry entry
set reg="%temp%\regpw.reg"
(echo Windows Registry Editor Version 5.00
echo [-HKEY_CURRENT_USER\Console\Enter Password:])>%reg%
regedit -s %reg%
exit /b


2. Version for Windows XP / Server 2003 upwards with the REG.EXE command

@echo off
setlocal
echo Running batch file
echo You will be asked for a password in popup windows after you press a key.
echo.
call :getpassword
echo You entered %pwd%
pause
exit /b

:getpassword
set InputTitle=Enter Password:
set InputResult=%Temp%\Input.tmp
reg add "HKCU\Console\%InputTitle%" /v "CursorSize" /t REG_DWORD /d "100" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "FaceName" /t REG_SZ /d "Terminal" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "FontFamily" /t REG_DWORD /d "48" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "FontSize" /t REG_DWORD /d "1048588" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "FontWeight" /t REG_DWORD /d "400" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "ScreenBufferSize" /t REG_DWORD /d "196638" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "ScreenColors" /t REG_DWORD /d "136" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "WindowPosition" /t REG_DWORD /d "24904160" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "WindowSize" /t REG_DWORD /d "196638" /f >NUL 2>&1
start "%InputTitle%" /wait "%ComSpec%" /v:on /c "echo.&set /p Input= &echo !Input!>"%InputResult%""
for /f "delims=" %%a in ('type "%InputResult%"') do set pwd=%%a
del "%InputResult%"
reg delete "HKCU\Console\%InputTitle%" /f >NUL 2>&1
exit /b

3. Method without registry change using a difficult to read pale colour combination

@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%
echo exit >>%tempbat%

start /wait "" %tempbat%

set /p Password=<"%temp%\pass.txt"
echo The password is %password%

4. Matching username prompting box returns into the variable %user%

:getusername
set InputTitle=Enter Username:
set InputResult=%Temp%\Input.tmp
reg add "HKCU\Console\%InputTitle%" /v "ScreenBufferSize" /t REG_DWORD /d "196638" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "ScreenColors" /t REG_DWORD /d "128" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "WindowPosition" /t REG_DWORD /d "24904160" /f >NUL 2>&1
reg add "HKCU\Console\%InputTitle%" /v "WindowSize" /t REG_DWORD /d "196638" /f >NUL 2>&1
set input=NONE
start "%InputTitle%" /wait "%ComSpec%" /v:on /c "echo.&set /p Input= &echo !Input!>"%InputResult%""
for /f "delims=" %%a in ('type "%InputResult%"') do set user=%%a
del "%InputResult%"
reg delete "HKCU\Console\%InputTitle%" /f >NUL 2>&1


Hide details for ExplanationExplanation
Show details for ExamplesExamples
Hide details for AttachmentsAttachments